Search code examples
oopinheritancesparqlrdfrdfs

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 Graph

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


Solution

  • 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