How to query for all direct subclasses in SPARQL?

前端 未结 1 1458
醉梦人生
醉梦人生 2021-01-27 12:37

I have A, B and C as classes related by the transitive property isSubClassOf. So A isSuclassOF B and B isS

相关标签:
1条回答
  • 2021-01-27 13:07

    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.

    0 讨论(0)
提交回复
热议问题