What I'm currently using rdflib for creating and managing RDF graphs in Python. RDFlib doesn't do any RDFS or OWL reasoning, though. This leads to results like the following:
If I have
A rdf:type MyType . MyType rdfs:subClassOf SuperType .
and I ask
select ?x where {?x rdf:type SuperType}
then I get nothing, but I'd like to get
A
(by RDFS semantics).The same thing happens with
owl:equivalentClass
. If I haveA rdf:type MyType . MyType owl:equivalentClass SiblingType .
and I ask
select ?x where {?x rdf:type SiblingType}
I'd like to get
A
, but I get nothing.
Is there a way to get these results?
Edit: this answer was posted in response to the original, different question.
No, you shouldn’t. RDF itself does not include anything about ontologies, it’s just a dumb graph, and that is what RDFLib does.
Going beyond this is called reasoning over the data. It’s an extra layer. Bare RDFLib does not do reasoning because it is complex and, in general, very computationally expensive. There are third-party solutions for reasoning, but before you employ them, you ought to understand what they do and what performance impact they will have.
A naive approach to RDFS and OWL 2 reasoning over an RDFLib graph is Ivan Herman’s OWL 2 RL implementation. It’s very easy to use, but you almost certainly don’t want it unless you’re doing a toy application, because it’s a dumb algorithm that takes very long on a graph of a realistic size.
FuXi is a more powerful library, implementing the smarter Rete-UL algorithm. But I’m not sure if it is maintained or if it’s usable with the current versions of RDFLib.
There is also a wealth of non-Python-based solutions for reasoning, such as Pellet, but integrating them with RDFLib—or any other RDF library—may be a chore.
You should also consider what kind of inference your application actually requires. Do you need to infer that subclass membership? If you do, maybe it’s all you need? — then maybe you could do it manually by iterating over the
X rdfs:subClassOf Y
triples with RDFLib and inserting the newA rdf:type Y
triples.In any case, remember that Semantic Web reasoning is a complex topic that depends heavily on the application.