Function rdf_canon::api::relabel_graph

source ·
pub fn relabel_graph(
    input_graph: &Graph,
    issued_identifiers_map: &HashMap<String, String>
) -> Result<Graph, CanonicalizationError>
Expand description

Re-label blank node identifiers in the input graph according to the issued identifiers map. Note that the output Graph does not retain the order of triples, unlike Vec<Triple>.

§Examples

use oxrdf::Graph;
use oxttl::NTriplesParser;
use rdf_canon::relabel_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 .
_:c14n2 <http://example.org/vocab#next> _:c14n1 .
_:c14n2 <http://example.org/vocab#prev> _:c14n0 .
_:c14n1 <http://example.org/vocab#next> _:c14n0 .
_:c14n1 <http://example.org/vocab#prev> _:c14n2 .
"#;

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 expected_triples = NTriplesParser::new()
    .parse_read(Cursor::new(expected))
    .map(|x| x.unwrap());
let expected_graph = Graph::from_iter(expected_triples);

assert_eq!(labeled_graph, expected_graph);