2016-09-07 35 views
1

我在Protege 4.3.0中創建了一個存儲在OWL文件中的本體。此本體的一些數據屬性已在它們的範圍被定義爲在以下表達式中:如何驗證OWLDataRange對象是否包含指定值?

({"absent"} or {"value1" , "value2" , "value3"}) 

我將搜索其可以在它們的範圍內的規定值的數據的屬性,所以我寫下面的代碼示例,但我不知道如何查詢OWLDataRange對象以查看它是否包含指定值(例如字符串"value1")。

final OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); 
final OWLOntology ontology = manager.loadOntologyFromOntologyDocument(file); 
final OWLReasonerFactory rf = new StructuralReasonerFactory(); 
final OWLReasoner reasoner = rf.createReasoner(ontology); 

// ... 

// iterate over all data properties 
for (OWLDataProperty topDataProperty : reasoner.getTopDataPropertyNode()) { 
    for(OWLDataProperty property: reasoner.getSubDataProperties(topDataProperty, false).getFlattened()) { 

     // iterate over all data ranges for the current data property 
     for (OWLDataRange dataRange : property.getRanges(ontology)) { 

      // I would check if the current data property contains a specified value in their ranges. 
      // ... 

     } 
    } 
} 
+1

正如你可以從文檔(http://owlapi.sourceforge.net/javadoc/org/semanticweb/owlapi/model/OWLDataRange.html)看到的那樣,數據範圍有多種類型 - 在你的情況下它是一個' DATA_UNION_OF'包含兩個'DATA_ONE_OF'。因此,您必須通過一些IF-ELSE(或SWITCH-CASE)分支來處理這些情況,或者實施相應的訪問者(http://owlapi.sourceforge.net/javadoc/org/semanticweb/owlapi/model /OWLDataRangeVisitor.html) – AKSW

+0

我在哪裏可以閱讀實現OWLDataRangeVisitor接口的示例? – enzom83

+0

這是很容易實現的界面。對於你的情況,我想這足以實現布爾方法,即交集,聯合,補充,然後,最後,對於包含值的構造的部分之一。 – AKSW

回答

0

我對這類問題的解決方案是使用推理器(This Pellet fork)。

這個想法是創建一個類,它表示'具有數據屬性的範圍要檢查的個人',另一個類表示'擁有屬性/文字的個人'。然後使用推理器檢查兩個類的交集是否非空。

由於有一些技巧來得到它正常工作,這裏是我完整的解決方案:

import java.util.function.BiFunction; 
import org.semanticweb.owlapi.model.*; 
import openllet.owlapi.*; 

public class RangeInclusionTest 
{ 
    public static void main(final String[] args) 
    { 
     try (final OWLManagerGroup group = new OWLManagerGroup()) 
     { 
      final OWLOntologyID ontId = OWLHelper.getVersion(IRI.create("http://test.org#entail-class-restrict-to-some-range"), 1.0); 
      final OWLHelper owl = new OWLGenericTools(group, ontId, true); 

      // This declaration is vital since this reasoner have problems with pure anonymous reasoning. 
      // You can remove this property after yours tests, (or better use one of your already existing properties). 
      final OWLDataProperty prop = OWL.DataProperty("http://test.org#dummyProp"); 
      owl.addAxiom(OWL.declaration(prop)); 

      final OWLLiteral un = OWL.constant(1); 
      final OWLLiteral deux = OWL.constant(2); 
      final OWLLiteral trois = OWL.constant(3); 
      final OWLLiteral quatre = OWL.constant(4); 
      final OWLDataRange dataRange = OWL.dataOr(OWL.oneOf(un), OWL.oneOf(deux), OWL.oneOf(trois)); 

      final BiFunction<OWLDataRange, OWLLiteral, Boolean> isIncludeInRange = // 
        (range, literal) -> owl.getReasoner().isSatisfiable(// 
          OWL.and(// You must be of all the following class 
            OWL.some(prop, OWL.oneOf(literal)), // The class of the 'literal' 
            OWL.some(prop, range), // The class of the range. 
            OWL.max(prop, 1))// But you can have the property only once. 
        ); 

      System.out.println("[A] " + (isIncludeInRange.apply(dataRange, un))); 
      System.out.println("[B] " + (isIncludeInRange.apply(dataRange, deux))); 
      System.out.println("[C] " + (isIncludeInRange.apply(dataRange, trois))); 
      System.out.println("[D] " + (isIncludeInRange.apply(dataRange, quatre))); 

     } catch (final Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 
} 

首先,你必須定義你的範圍。然後使用推理器的'isSatisfiable'方法。一個訣竅是通過在交集類上添加限制「OWL.max(prop,1)」來強制只使用該屬性的一個實例。

的輸出必須是

[A] true 
[B] true 
[C] true 
[D] false 

由於文字 '的Quatre' 不包括 'dataRange的' 答案 '[d]' 是假的。 正如你可以看到這個解決方案easilly允許在另一個範圍內測試一個範圍的內含物。

一個解決方案不需要在本體上改變本來可以創建一個特殊的swrl規則,並檢查本體(甚至是空的)是否需要規則,但目前沒有dl-reasoner支持swrl。

相關問題