Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
613 views
in Technique[技术] by (71.8m points)

owl - 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!

question from:https://stackoverflow.com/questions/65905143/sparql-query-to-get-all-the-leaf-classes-lowest-level-classes

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...