propertiespathsparqlrdfreification

SPARQL property path in reified rdf triples


Is it possible, to use property path with reified rdf triples?

I like to get all superclasses of a specified class (stored in ?class).

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT ?class ?superclass ?supersuperclass ?supersupersuperclass
WHERE{
  {
    ?s rdf:subject ?class .
    ?s rdf:predicate rdfs:subClassOf .
    ?s rdf:object ?superclass .
    
    OPTIONAL {
    ?s1 rdf:subject ?superclass .
    ?s1 rdf:predicate rdfs:subClassOf .
    ?s1 rdf:object ?supersuperclass .
    }
    
    OPTIONAL {
    ?s2 rdf:subject ?supersuperclass .
    ?s2 rdf:predicate rdfs:subClassOf .
    ?s2 rdf:object ?supersupersuperclass .
    }
    
  }

}

Solution

  • You can use intermediate queries, first to dereify the statements:

    CONSTRUCT {
      ?s a rdfs:Class .
      ?o a rdfs:Class .
      ?s rdfs:subClassOf ?o .
    }
    WHERE {
      ?t a rdf:Statement .
      ?t rdf:object ?o .
      ?t rdf:predicate rdfs:subClassOf .
      ?t rdf:subject ?s .
    }
    

    Then to obtain all of the subclass relations:

    SELECT ?s ?o
    WHERE {
      ?s a rdfs:Class .
      ?s rdfs:subClassOf* ?o .
    }
    

    If you know that classes are not interlinked by other predicates, you may use paths:

    SELECT ?s ?o
    WHERE {
      ?s a rdfs:Class .
      ?o a rdfs:Class .
      ?s (^rdf:subject/rdf:object)* ?o .
    }