Skip to content

Commit

Permalink
Infer more properties
Browse files Browse the repository at this point in the history
  • Loading branch information
vandenoever committed Nov 5, 2018
1 parent d8df5de commit 5c1e2ee
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions examples/generate_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
///
extern crate rome;
use rome::graph::{
Graph, GraphWriter, IRIPtr, LiteralPtr, ResourceTranslator, Triple, WriterBlankNodeOrIRI,
WriterResource,
BlankNodeOrIRI, Graph, GraphWriter, IRIPtr, LiteralPtr, ResourceTranslator, Triple,
WriterBlankNodeOrIRI, WriterResource,
};
use rome::graphs::tel;
use rome::io::TurtleParser;
Expand Down Expand Up @@ -156,6 +156,8 @@ where
}

const RDFS_CLASS: &'static str = "https://www.w3.org/2000/01/rdf-schema#Class";
const RDFS_DOMAIN: &'static str = "https://www.w3.org/2000/01/rdf-schema#domain";
const RDFS_RANGE: &'static str = "https://www.w3.org/2000/01/rdf-schema#range";
const RDFS_SUB_CLASS_OF: &'static str = "https://www.w3.org/2000/01/rdf-schema#subClassOf";
const RDF_PROPERTY: &'static str = "https://www.w3.org/1999/02/22-rdf-syntax-ns#Property";
const RDF_TYPE: &'static str = "https://www.w3.org/1999/02/22-rdf-syntax-ns#type";
Expand Down Expand Up @@ -225,16 +227,34 @@ where
}

/// Infer properties
fn infer_properties<'g, G, W>(graph: &'g G, w: &mut W)
///
/// CONSTRUCT {
/// ?p a rdf:Property
/// } WHERE {
/// { ?s ?p ?o }
/// UNION
/// { ?p rdfs:domain ?d }
/// UNION
/// { ?p rdfs:range ?d }
/// }
fn infer_properties<'g, G, W, T>(graph: &'g G, w: &mut W, translator: &mut T)
where
G: Graph<'g>,
W: GraphWriter<'g>,
T: ResourceTranslator<'g, Graph = G, GraphWriter = W> + 'g,
{
let rdf_type = w.create_iri(&RDF_TYPE);
let rdf_property = WriterResource::IRI(w.create_iri(&RDF_PROPERTY));
for triple in graph.iter() {
let predicate = WriterBlankNodeOrIRI::IRI(w.create_iri(&triple.predicate()));
w.add(&predicate, &rdf_type, &rdf_property);
if let BlankNodeOrIRI::IRI(iri) = triple.subject() {
let iri = iri.as_str();
if iri == RDFS_DOMAIN || iri == RDFS_RANGE {
let subject = translator.translate_blank_node_or_iri(w, &triple.subject());
w.add(&subject, &rdf_type, &rdf_property);
}
}
}
}

Expand Down Expand Up @@ -286,7 +306,7 @@ fn infer<'g>(graph: &'g MyGraph) -> rome::Result<MyGraph> {
let oa = ontology::adapter(graph);
let mut translator = Translator::new();
infer_class_from_sub_class_of(graph, &mut w, &mut translator);
infer_properties(graph, &mut w);
infer_properties(graph, &mut w, &mut translator);
copy_triples(graph, &mut w, &mut translator);
make_sub_class_of_entailment_concrete(&mut w, &mut translator, &oa);
Ok(w.collect().sort_blank_nodes())
Expand Down

0 comments on commit 5c1e2ee

Please sign in to comment.