简体   繁体   English

计算OWL本体中子类的深度

[英]Calculate the depth of subclass in the OWL ontology

I'm looking for a SPARQL query that could return the position of specified subclass in the OWL hierarchy. 我正在寻找一个SPARQL查询,它可以返回OWL层次结构中指定子类的位置。 I have studied several examples but the best result I could ever reach is the computation the relative paths between the specified superclass and its subclasses ( thanks to Joshua Taylor ). 我已经研究了几个例子,但我能达到的最好结果是计算指定超类与其子类之间的相对路径( 感谢Joshua Taylor )。 Instead of that I need to calculate the "absolute" depth for a given subclass. 而不是我需要计算给定子类的“绝对”深度。

My ontology contains several top-level classes and every of them is followed with a separate tree of subclasses. 我的本体包含几个顶级类,每个类都跟着一个单独的子类树。 Here is part of my OWL (converted to TTL with a rdfcat utility): 这是我的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 .

In this case for a given depression class I expect to get 3 , pit -> 2 , on_the_road -> 1 , traffc_accident (a top-level class) -> 0. 在这种情况下,对于给定的depression等级,我期望得到3pit - > 2on_the_road - > 1traffc_accident (顶级等级) - > 0。

The same approach works here for finding the depth of a class in a hierarchy (assuming, of course, that each class has a unique path to a root). 这里使用相同的方法来查找层次结构中类的深度(当然,假设每个类具有到根的唯一路径)。 The trick is just that you first need to find the roots of the hierarchy. 诀窍只是你首先需要找到层次结构的根源。 You can do that with the following query to get the following results. 您可以使用以下查询执行此操作以获得以下结果。

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
-----------------------------
| class             | depth |
=============================
| :traffic_accident | 0     |
| :on_the_road      | 1     |
| :pit              | 2     |
| :depression       | 3     |
-----------------------------

Note that things may be a bit more complicated if you've got a reasoner. 请注意,如果你有一个推理器,事情可能会有点复杂。 If you've got a reasoner, then every class, including your roots, is a subclass of owl:Thing, so there will actually only be one root, and all the depths will be off by one (from what you've mentioned in the question). 如果你有一个推理器,那么每个类,包括你的根,都是owl的子类:Thing,所以实际上只有一个根,所有的深度都会被一个(从你提到的中提到)问题)。 You can avoid that by adjusting the filters in the query that finds values for ?root. 您可以通过调整查询中找到?root的值的过滤器来避免这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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