2017-08-13 38 views
0

我需要按照与.owl文件相同的顺序获得OWL类的等价类。按顺序获取等价类

我使用此代码

for(OWLClassExpression cls: clazz.getEquivalentClasses(ontology)) { 

Set <OWLClass> classes_of_the_Expression =cls.getClassesInSignature(); 
} 

但这个代码让他们随机。

请看下面的例子,我对待。在这里,dog_owner类是人类和狗类的等效类和交集。通过执行我的java代码,我得到第一个狗课,然后是人类;并且我需要得到反面这意味着人类然后狗类。因为我需要精确的等同类的第一类。

<owl:Class rdf:about="http://owl.man.ac.uk/2005/07/sssw/peopleeemodifiée#dog_owner"> 
<owl:equivalentClass> 
    <owl:Class> 
    <owl:intersectionOf rdf:parseType="Collection"> 
     <owl:Class rdf:about="http://owl.man.ac.uk/2005/07/sssw/peopleeemodifiée#person"/> 
     <owl:Restriction> 
     <owl:onProperty> 
      <owl:ObjectProperty rdf:about="http://owl.man.ac.uk/2005/07/sssw/peopleeemodifiée#has_pet"/> 
     </owl:onProperty> 
     <owl:someValuesFrom rdf:resource="http://owl.man.ac.uk/2005/07/sssw/peopleeemodifiée#dog"/> 
     </owl:Restriction> 
    </owl:intersectionOf> 
    </owl:Class> 
</owl:equivalentClass> 
<rdfs:label rdf:datatype="http://www.w3.org/2001/XMLSchema#string" 
>dog owner</rdfs:label> 
<rdfs:comment rdf:datatype="http://www.w3.org/2001/XMLSchema#string" 
></rdfs:comment> 

+0

我猜你正在使用的OWLAPI版本是3.5。 OWLClass对象具有可比性,因此您可以将它们添加到列表并可靠地对列表进行排序,但是不支持尊重输入OWL文件的顺序。 OWL没有为等价类指定强制性订单。 为什么你需要提到第一个元素? – Ignazio

+0

我需要它,因为我必须添加一个连接常规类的子类公理(在下面的示例中:dog_owner与第一个交叉点是person:因此,我需要自动添加(使用脚本)公理: 但这不是:这是我的目标 – Nina

+0

我首先将它们添加到列表中,我认为通过这样做,我会保持顺序,但不幸的是,函数clazz.getClassesInSignature();让它们紊乱 – Nina

回答

0

为了既兼顾命名类中包括的等价类公理的交集,您可以使用用户:

OWLEquivalentClassesAxiom ax=null; 
ax.accept(new OWLObjectVisitor() { 
    @Override 
    public void visit(OWLObjectIntersectionOf ce) { 
     ce.operands().filter(x->x.isOWLClass()).forEach(x->{ 
      // this is where x is Person, or any other 
      // named class in the intersection; 
      // anonymous classes are skipped 
     }); 
    } 
}); 

对于OWLAPI 3:

for(OWLClass clazzzz : ontology.getClassesInSignature()) { 
    for(OWLEquivalentClassesAxiom ax: ontology.getEquivalentClassesAxioms(clazzzz)) { 
     OWLObjectVisitorAdapter visitor = new OWLObjectVisitorAdapter() { 
     @Override 
     public void visit(OWLObjectIntersectionOf ce) { 
      for (OWLClassExpression e : ce.getOperands()) { 
       if (!e.isAnonymous()) { 
        // this is where x is Person, or any other 
        // named class in the intersection; 
        // anonymous classes are skipped 
       } 
      } 
     } 
     }; 
     ax.accept(visitor); 
    } 
} 
+0

我不明白我可以使用这段代码的方式,我以前从未使用过访问者。请,你能帮助我吗?我的课是否必须扩展另一个类或实现一个接口?因为当我将你的代码添加到我的java类中时,接受ax的函数是未知的 – Nina

+0

您正在使用哪个版本的owl api?我的代码是用owl api编写的5 – Ignazio

+0

我有OWL API 3 – Nina

1

尝试org.semanticweb.owlapi.model.OWLNaryBooleanClassExpression#getOperandsAsList方法或者其流伴侣。

请注意:OWL是RDF。 RDF不支持按设计和定义进行排序。虽然你使用“签名”方法,但由于这些一般原因,它不应该返回有序的数据(这似乎是它为什么被设置)。

但owl的右侧部分:intersectionOf是一个rdf:List,它总是有序的,所以应该有东西从n-ary类表达式的正确部分检索有序信息。用法

代码示例:

  String s = "<rdf:RDF\n" + 
      " xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n" + 
      " xmlns:dc=\"http://purl.org/dc/elements/1.1/\"\n" + 
      " xmlns:owl=\"http://www.w3.org/2002/07/owl#\"\n" + 
      " xmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\"\n" + 
      " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema#\">\n" + 
      " <owl:Ontology/>\n" + 
      " <owl:Class rdf:about=\"http://owl.man.ac.uk/2005/07/sssw/peopleeemodifiée#dog_owner\">\n" + 
      " <rdfs:comment></rdfs:comment>\n" + 
      " <rdfs:label>dog owner</rdfs:label>\n" + 
      " <owl:equivalentClass>\n" + 
      "  <owl:Class>\n" + 
      "  <owl:intersectionOf rdf:parseType=\"Collection\">\n" + 
      "   <owl:Class rdf:about=\"http://owl.man.ac.uk/2005/07/sssw/peopleeemodifiée#person\"/>\n" + 
      "   <owl:Restriction>\n" + 
      "   <owl:someValuesFrom rdf:resource=\"http://owl.man.ac.uk/2005/07/sssw/peopleeemodifiée#dog\"/>\n" + 
      "   <owl:onProperty>\n" + 
      "    <owl:ObjectProperty rdf:about=\"http://owl.man.ac.uk/2005/07/sssw/peopleeemodifiée#has_pet\"/>\n" + 
      "   </owl:onProperty>\n" + 
      "   </owl:Restriction>\n" + 
      "  </owl:intersectionOf>\n" + 
      "  </owl:Class>\n" + 
      " </owl:equivalentClass>\n" + 
      " </owl:Class>\n" + 
      "</rdf:RDF>"; 

    OWLOntology ontology; 
    try (InputStream in = new ByteArrayInputStream(s.getBytes(StandardCharsets.UTF_8))) { 
     ontology = OntManagers.createONT().loadOntologyFromOntologyDocument(in); 
    } 
    System.out.println("========"); 
    OWLEquivalentClassesAxiom equivalentClassesAxiom = ontology.axioms(AxiomType.EQUIVALENT_CLASSES).findFirst().orElseThrow(IllegalArgumentException::new); 
    OWLObjectIntersectionOf anon = equivalentClassesAxiom.classExpressions() 
      .filter(e -> ClassExpressionType.OBJECT_INTERSECTION_OF.equals(e.getClassExpressionType())) 
      .map(OWLObjectIntersectionOf.class::cast) 
      .findFirst().orElseThrow(IllegalArgumentException::new); 
    System.out.println(anon.getOperandsAsList().get(0)); // <-- always person 
    System.out.println(anon.getOperandsAsList().get(1)); // <-- always anon ObjectSomeValuesFrom 
    System.out.println(OWLObjectSomeValuesFrom.class.cast(anon.getOperandsAsList().get(1)).getFiller()); // <--always dog 
+0

之下添加一个答案就像注释:'getOperandsAsList'不一定返回文档的顺序。它在SortedSet中使用OWL API特定的类表达式排序(OWL API 5中的流),请参阅[code](https://github.com/owlcs/owlapi/blob/version5/impl/src/主/ JAVA /英国/ AC /曼彻斯特/ CS /猫头鹰/ owlapi/OWLNaryBooleanClassExpressionImpl。java#L39) – AKSW