我需要以与.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 :(得分:1)
尝试org.semanticweb.owlapi.model.OWLNaryBooleanClassExpression# getOperandsAsList 方法或其流随播广告。
请注意:OWL是RDF。 RDF不支持按设计和定义排序。虽然您使用“签名”方法,但由于这些一般原因,它不应返回有序数据(这似乎就是Set的原因)。
但是猫头鹰的右边部分: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
答案 1 :(得分: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);
}
}