James Hudson
James Hudson

Reputation: 904

Obtaining the full property path from SPARQL query

I have the following sample data:

@prefix hr: <http://ex.com/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix sch: <http://schema.org/> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

hr:AAAA a rdfs:Class .
hr:BBBB a rdfs:Class .

hr:ClassA a rdfs:Class ;
    rdfs:subClassOf hr:AAAA ;
    rdfs:subClassOf hr:BBBB .

hr:ClassB a rdfs:Class ;
    rdfs:subClassOf hr:ClassA .    

hr:ClassC a rdfs:Class ;
    rdfs:subClassOf hr:ClassB .

hr:ClassD a rdfs:Class ;
    rdfs:subClassOf hr:AAAA . 

hr:ClassE a rdfs:Class ;
    rdfs:subClassOf hr:ClassD .   

What I would like to get as a series of strings are the full rdfs:subClassOf property paths leading from one node to another. In this case, the set of strings I would like to get back would look like or be similar to:

hr:ClassC -> hr:ClassB -> hr:ClassA -> hr:AAAA
hr:ClassC -> hr:ClassB -> hr:ClassA -> hr:BBBB
hr:ClassB -> hr:ClassA -> hr:AAAA
hr:ClassB -> hr:ClassA -> hr:BBBB
hr:ClassA -> hr:AAAA
hr:ClassA -> hr:BBBB
hr:ClassD -> hr:AAAA
hr:ClassE -> hr:ClassD -> hr:AAAA

Is this possible with SPARQL? If so, what would the query look like?

The following query seems like it is close:

SELECT ( GROUP_CONCAT( ?subclass; SEPARATOR = " -> " ) AS ?hierarchy )
WHERE {        
        ?class rdfs:subClassOf+ ?subclass .                                                       
}
GROUP BY ?class

but the results are:

http://ex.com/AAAA
http://ex.com/AAAA -> http://ex.com/BBBB
http://ex.com/ClassA -> http://ex.com/AAAA -> http://ex.com/BBBB
http://ex.com/ClassB -> http://ex.com/ClassA -> http://ex.com/AAAA -> http://ex.com/BBBB
http://ex.com/ClassD -> http://ex.com/AAAA

I cannot explain where:

http://ex.com/AAAA -> http://ex.com/BBBB

is coming from or why hr:ClassE is missing from the results.

Upvotes: 1

Views: 307

Answers (1)

James Hudson
James Hudson

Reputation: 904

This is not possible with SPARQL.

Upvotes: 1

Related Questions