Function rdf_canon::api::sort_graph
source · pub fn sort_graph(graph: &Graph) -> Vec<Triple>
Expand description
Sort each triple from the canonicalized graph into code point order.
§Examples
use oxrdf::{Graph, Triple};
use oxttl::NTriplesParser;
use rdf_canon::{relabel_graph, sort_graph};
use std::collections::HashMap;
use std::io::Cursor;
let input = r#"
_:e0 <http://example.org/vocab#next> _:e1 .
_:e0 <http://example.org/vocab#prev> _:e2 .
_:e1 <http://example.org/vocab#next> _:e2 .
_:e1 <http://example.org/vocab#prev> _:e0 .
_:e2 <http://example.org/vocab#next> _:e0 .
_:e2 <http://example.org/vocab#prev> _:e1 .
"#;
let issued_identifiers_map = HashMap::from([
("e0".to_string(), "c14n0".to_string()),
("e1".to_string(), "c14n2".to_string()),
("e2".to_string(), "c14n1".to_string()),
]);
let expected = r#"
_:c14n0 <http://example.org/vocab#next> _:c14n2 .
_:c14n0 <http://example.org/vocab#prev> _:c14n1 .
_:c14n1 <http://example.org/vocab#next> _:c14n0 .
_:c14n1 <http://example.org/vocab#prev> _:c14n2 .
_:c14n2 <http://example.org/vocab#next> _:c14n1 .
_:c14n2 <http://example.org/vocab#prev> _:c14n0 .
"#;
let input_triples = NTriplesParser::new()
.parse_read(Cursor::new(input))
.map(|x| x.unwrap());
let input_graph = Graph::from_iter(input_triples);
let labeled_graph = relabel_graph(&input_graph, &issued_identifiers_map).unwrap();
let canonicalized_triples = sort_graph(&labeled_graph);
let expected_triples: Vec<Triple> = NTriplesParser::new()
.parse_read(Cursor::new(expected))
.map(|x| x.unwrap())
.collect();
assert_eq!(canonicalized_triples, expected_triples);