Warm tip: This article is reproduced from serverfault.com, please click

SPARQL query to get all the "leaf" classes / lowest level classes

发布于 2021-01-26 16:24:03

I feel like I'm missing something rather obvious. I would like to query my graph so that I get all the "leafs", so any class that does not have a subclass. Basically, I need the inverse of rdfs:hasSubClass (I tried rdfs:superClassOf but it doesn't not exist ahaha).

For example, when I query for the lowest level of the classes by using WHERE {?level1 subClassOf ?level2 ?level2 subClassOf ?level3 ?level3 subClassOf ?level4 etc.}

I eventually end up with kind of what I want: some of the leafs. But if I then join this result to the leafs of one level higher, I also receive the parent classes of the leafs of the query above, which I do not want. I only want children classes, no parents, if that makes sense.

The goal is to be able to output a list of classes that can be instantiated.

Looking for any tips in the right direction! Thank you!

Questioner
Robin
Viewed
0
Robin 2021-01-28 16:07:25

An RDF triple looks like rdfs:subClassOf .` - so in your SPARQL query you would use a triple pattern ?sub rdfs:subClassOf ?sup . and either select ?sub or ?sup

to get all leaf nodes just get all classes that do not have a subclass. Absence of something is done via FILTER NOT EXISTS clause. Like select distinct ?cls {?cls rdfs:subClassOf ?sup . FILTER NOT EXISTS{?sub rdfs:subClassOf ?cls FILTER(?sub != ?cls && ?sub != owl:Nothing ) }}

Credits to UninformedUser for this solution!