Using SPARQL for limited reasoning of RDFS and OWL

I am currently using rdflib to create and manage RDF graphs in Python. However, RDFlib does not use any RDFS or OWL reasoning. This leads to the following results:

  • 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 would like to get A (according to RDFS semantics).

  • The same thing happens with owl:equivalentClass . if I have

     A rdf:type MyType . MyType owl:equivalentClass SiblingType . 

    and I ask

     select ?x where {?x rdf:type SiblingType} 

    I want to get A , but I get nothing.

Is there any way to get these results?

+7
python rdf owl sparql rdflib
source share
2 answers

Although this is a problem with a library request and, as such, is off topic for StackOverflow, I would like to point out that in many cases you can answer both of these requests using more spectacular more complex SPARQL queries. For both of these cases, you can use the following query to get the desired results, where <class-of-interest> is :SuperClass or :SiblingClass :

 select ?x where { ?x rdf:type/(rdfs:subClassOf|owl:equivalentClass)* <class-of-interest> . } 

This finds ?x , which has a path to the beginning with rdf:type and is followed by zero or more from rdfs:subClassOf or owl:equivalentClass and ends up in :SuperType .

For example, consider the following data in Turtle / N3. (Aside, if you are asking questions about running data queries, provide data that we can work with. In your question, you provided something like RDF data, but nothing that we could copy and paste and write the query against. )

 @prefix owl: <http://www.w3.org/2002/07/owl#> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . @prefix : <http://stackoverflow.com/q/20474862/1281433/> :i1 a :C . :C rdfs:subClassOf :D . :D rdfs:subClassOf :E . :i2 a :F . :F rdfs:subClassOf :G1 . :G1 owl:equivalentClass :G2 . :G2 rdfs:subClassOf :H . 

You can run a query like the one above to select individuals and their types (note that a is an abbreviation in SPARQL and Turtle / N3 for rdf:type ):

 prefix owl: <http://www.w3.org/2002/07/owl#> prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> prefix : <http://stackoverflow.com/q/20474862/1281433/> select ?i ?type where { ?ia/(rdfs:subClassOf|owl:equivalentClass)* ?type } 
 -------------- | i | type | ============== | :i2 | :F | | :i2 | :G1 | | :i2 | :G2 | | :i2 | :H | | :i1 | :C | | :i1 | :D | | :i1 | :E | -------------- 
+6
source share

Edit : This answer was sent in response to the original, another question.

I have to get A, but I get nothing!

No, you shouldn't. RDF itself does not contain anything about ontologies, its just a dumb schedule, and this is what RDFLib does.

Going beyond this is called data reasoning. This is an extra layer. Bare RDFLib does not make any reasoning because it is complex and, as a rule, very expensive. There are third-party reasoning solutions, but before you use them, you must understand what they are doing and what impact they will have on performance.

The naive approach to RDFS and OWL 2 reasoning over the RDFLib graph is the implementation of Ivan Hermans OWL 2 RL . It is very easy to use, but you almost certainly do not want it, unless you are making a toy application, because its mute algorithm takes a lot of time on a graph of a realistic size.

FuXi is a more powerful library that implements the smarter Rete -UL. But I'm not sure if it is supported or can be used with current versions of RDFLib.

There are also many non-Python-based reasoning solutions such as Pellet , but integrating them with RDFLib - or any other RDF library - can be a daunting task.

You should also consider what type of output your application requires. Do you need to conclude that membership is a subclass? If so, maybe all you need? - then perhaps you could do it manually, iterating over three X rdfs:subClassOf Y using RDFLib and inserting new A rdf:type Y triples.

In any case, remember that the discussion of the semantic network is a complex topic that is highly dependent on the application.

+4
source share

All Articles