计算OWL本体中子类的深度

Vit*_*aev 3 rdf owl sparql

我正在寻找一个 SPARQL 查询,它可以返回 OWL 层次结构中指定子类的位置。我研究了几个例子,但我能达到的最好结果是计算指定超类与其子类之间的相对路径(感谢 Joshua Taylor)。相反,我需要计算给定子类的“绝对”深度。

我的本体包含几个顶级类,每个类后面都有一个单独的子类树。这是我的 OWL 的一部分(使用 rdfcat 实用程序转换为 TTL):

@prefix :      <http://www.semanticweb.org/administrator/ontologies/2014/7/untitled-ontology-9#> .
@prefix owl:   <http://www.w3.org/2002/07/owl#> .
@prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xsd:   <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs:  <http://www.w3.org/2000/01/rdf-schema#> .

:depression  a              owl:Class ;
    rdfs:subClassOf  :pit .

:pit    a                owl:Class ;
    rdfs:subClassOf  :on_the_road .

:on_the_road  a            owl:Class ;
    rdfs:subClassOf  :traffic_accident .

:traffic_accident  a  owl:Class .
Run Code Online (Sandbox Code Playgroud)

在这种情况下,对于给定的depression类,我期望得到3pit-> 2on_the_road-> 1traffc_accident(顶级类) -> 0 。

Jos*_*lor 5

同样的方法在这里可以用于查找层次结构中类的深度(当然,假设每个类都有到根的唯一路径)。诀窍在于您首先需要找到层次结构的根。您可以使用以下查询来执行此操作以获得以下结果。

prefix : <http://www.semanticweb.org/administrator/ontologies/2014/7/untitled-ontology-9#>
prefix owl: <http://www.w3.org/2002/07/owl#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>

select ?class (count(?mid)-1 as ?depth) {
  #-- Select root classes (classes that have no
  #-- superclasses other than themselves).
  {
    select ?root {
      ?root a owl:Class
      filter not exists {
        ?root rdfs:subClassOf ?superroot 
        filter ( ?root != ?superroot )
      }
    }
  }

  ?class rdfs:subClassOf* ?mid .
  ?mid rdfs:subClassOf* ?root .
}
group by ?class
order by ?depth
Run Code Online (Sandbox Code Playgroud)
-----------------------------
| class             | depth |
=============================
| :traffic_accident | 0     |
| :on_the_road      | 1     |
| :pit              | 2     |
| :depression       | 3     |
-----------------------------
Run Code Online (Sandbox Code Playgroud)

请注意,如果您有推理机,事情可能会更复杂一些。如果你有一个推理机,那么每个类,包括你的根,都是 owl:Thing 的子类,所以实际上只有一个根,并且所有深度都会减少一(从你在的问题)。您可以通过调整查找 ?root 值的查询中的过滤器来避免这种情况。