I have A
, B
and C
as classes related by the transitive property isSubClassOf
.
So A isSuclassOF B
and B isS
Within the standard SPARQL language, you can do this by querying for those subclasses where no other subclass exists "in between", like so:
SELECT ?directSub ?super
WHERE { ?directSub rdfs:subClassOf ?super .
FILTER NOT EXISTS { ?otherSub rdfs:subClassOf ?super.
?directSub rdfs:subClassOf ?otherSub .
FILTER (?otherSub != ?directSub)
}
}
If you want to count the number of subclasses, you will need to adapt the above query using the COUNT
and GROUP BY
operators.
Many SPARQL engines offer some shortcuts for querying direct subclasses, however. For example in Sesame, when querying programmatically, you can disable inferencing for the duration of the query by setting a boolean property on the Query
object to false
. It also offers an additional reasoner which can be configured on top of a datastore and which allows you to query using a "virtual" property, sesame:directSubClassOf
(as well as sesame:directType
and sesame:directSubPropertyOf
).
Other SPARQL engines have similar mechanisms.