简体   繁体   中英

SPARQL query to get all the “leaf” classes / lowest level classes

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!

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM