Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
329 views
in Technique[技术] by (71.8m points)

python - Using SPARQL for limited RDFS and OWL reasoning

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:

  1. 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).

  2. 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'd like to get A, but I get nothing.

Is there a way to get these results?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

This finds ?xs that have a path to starting with rdf:type and followed by zero or more of rdfs:subClassOf or owl:equivalentClass and eventually gets to :SuperType.

For instance, consider the following data in Turtle/N3. (As an aside, if you're asking questions about running queries against data, provide data that we can work with. You provided something sort of like RDF data in your question, but nothing that we could copy and paste and write a 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 shorthand 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 {
  ?i a/(rdfs:subClassOf|owl:equivalentClass)* ?type
}
--------------
| i   | type |
==============
| :i2 | :F   |
| :i2 | :G1  |
| :i2 | :G2  |
| :i2 | :H   |
| :i1 | :C   |
| :i1 | :D   |
| :i1 | :E   |
--------------

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...