1

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.

James Hudson
  • 844
  • 6
  • 19
  • 2
    what you want is **not** possible with SPARQL. The closest you can get is the answer from Joshua here: https://stackoverflow.com/questions/18024413/finding-all-steps-in-property-path - but this can't be converted to your `group_concat` function, simply because the result of `group_concat` is not ordered in a way you expect – UninformedUser Jun 08 '20 at 18:16
  • I guess one possible option would be to have an external loop which gradually increased ```?class rdfs:subClassOf ?subclass``` to ```?class rdfs:subClassOf/rdfs:subClassOf ?subclass``` to ```?class rdfs:subClassOf/rdfs:subClassOf/rdfs:subClassOf ?subclass```, etc. until no results were returned. – James Hudson Jun 08 '20 at 18:37
  • https://www.stardog.com/docs/#_path_queries_2. As well as I rememer, `PATH` queries work in Studio only. – Stanislav Kralin Jun 08 '20 at 19:06

1 Answers1

1

This is not possible with SPARQL.

James Hudson
  • 844
  • 6
  • 19