简体   繁体   中英

SPARQL all predicate-object pairs of subject and all its superclasses

Imagine you do something crazy and store your object-oriented model as an RDF graph.

RDF图

shows a simplified example of the inheritance hierarchy and the associated attributes.

In practice, you get such graph structure if you translate some UML class diagram into RDFS.

The question is: what SPARQL query can deliver all the predicate-object pairs necessary to instantiate a particular resource of "Class C". In other words: how do you get all the predicate-object pairs along the whole inheritance chain (only single inheritance).

Given this diagram, the predicate-object pairs of all members of the class :ClassC is simply:

SELECT ?inst ?p ?o
WHERE {
   ?inst a :ClassC .
   Inst ?p ?o .

Keep in mind that there is not property inheritance in RDF/RDFS. If you want to find all of property/values pairs for ClassA with entailments for subclasses then useL

SELECT ?inst ?p ?o
WHERE {
   ?cls rdfs:subClassOf* :ClassA .
   ?inst a ?cls .
   ?inst ?p ?o
}

In this respect, RDFS works a bit backwards of one's expectations of OO inheritance.

With the info from @scotthenninger the following query did the job:

SELECT ?p ?o
WHERE {
   :ClassC rdfs:subClassOf* ?anySuperClass .
   ?anySuperClass ?p ?o .   
}

edit: Similar query gets all the self-defined properties and their range along the inheritance chain:

SELECT ?prop ?obj
    WHERE {
       :ClassC rdfs:subClassOf* ?anySuperClass .
       ?prop rdfs:domain ?anySuperClass .   
       ?prop rdfs:range ?obj .
}

End results combined:

foo:ID         xsd:string
foo:name       xsd:string
rdfs:comment   xsd:string
foo:similarTo  :ClassD

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM