按顺序获取等效 类
Get equivalent classes in order
我需要以与 .owl 文件中相同的顺序获取 OWL Class 的等效 classes。
我用这个代码
for(OWLClassExpression cls: clazz.getEquivalentClasses(ontology) ) {
Set <OWLClass> classes_of_the_Expression =cls.getClassesInSignature();
}
但是这段代码是随机获取的。
请在下面找到我处理的案例示例。这里,dog_owner class 是等价的 Class 和人与狗的交集 classes。通过执行我的 java 代码,我得到了第一条狗 class,然后是人 class;我需要得到倒数,这意味着人 class 然后是狗 class。因为我恰好需要等效 class 的第一个 class。
<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>
尝试 org.semanticweb.owlapi.model.OWLNaryBooleanClassExpression#getOperandsAsList 方法或其流伴侣。
请注意:OWL 是 RDF。 RDF 不支持按设计和定义排序。尽管您使用 "signature" 方法,但由于这些一般原因,它不应该 return 排序数据(这似乎就是设置它的原因)。
但是 owl:intersectionOf 的右边部分是一个 rdf:List,它总是有序的,所以应该有一些东西可以从 n 元 class 表达式的右边部分检索有序信息.
使用代码示例:
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
为了仅考虑等效 类 公理中包含的交集内的命名 类,您可以使用访问者:
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);
}
}
我需要以与 .owl 文件中相同的顺序获取 OWL Class 的等效 classes。
我用这个代码
for(OWLClassExpression cls: clazz.getEquivalentClasses(ontology) ) {
Set <OWLClass> classes_of_the_Expression =cls.getClassesInSignature();
}
但是这段代码是随机获取的。
请在下面找到我处理的案例示例。这里,dog_owner class 是等价的 Class 和人与狗的交集 classes。通过执行我的 java 代码,我得到了第一条狗 class,然后是人 class;我需要得到倒数,这意味着人 class 然后是狗 class。因为我恰好需要等效 class 的第一个 class。
<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>
尝试 org.semanticweb.owlapi.model.OWLNaryBooleanClassExpression#getOperandsAsList 方法或其流伴侣。
请注意:OWL 是 RDF。 RDF 不支持按设计和定义排序。尽管您使用 "signature" 方法,但由于这些一般原因,它不应该 return 排序数据(这似乎就是设置它的原因)。
但是 owl:intersectionOf 的右边部分是一个 rdf:List,它总是有序的,所以应该有一些东西可以从 n 元 class 表达式的右边部分检索有序信息.
使用代码示例:
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
为了仅考虑等效 类 公理中包含的交集内的命名 类,您可以使用访问者:
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);
}
}