如何与推理者一起让猫头鹰的个人班级
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
OWLOntology ontology = manager.loadOntologyFromOntologyDocument(new StringDocumentSource(KOALA));
IRI ontologyIRI = IRI.create("http://www.semanticweb.org/xxxxx/ontologies/2017/10/ontology");
OWLDataFactory factory = manager.getOWLDataFactory();
OWLIndividual john = factory.getOWLNamedIndividual(IRI.create(ontologyIRI + "#John"));
OWLDataPropertyExpression hasConnexion= factory.getOWLDataProperty(IRI.create(ontologyIRI + "#hasConnexion"));
OWLDataPropertyAssertionAxiom axiom = factory.getOWLDataPropertyAssertionAxiom(hasConnexion, john, 3);
AddAxiom addAxiom = new AddAxiom(ontology, axiom);
manager.applyChange(addAxiom);
manager.saveOntology(ontology, new StreamDocumentTarget(System.out));
//reasoner
OWLReasonerFactory reasonerFactory = new StructuralReasonerFactory();
OWLReasoner reasoner = reasonerFactory.createReasoner(ontology);
OWLClass myClass= fac.getOWLClass(IRI.create("http://www.semanticweb.org/xxxxx/ontologies/2017/10/ontology#hasConnexion"));
NodeSet<OWLNamedIndividual> individuals = reasoner.getInstances(myClass,
false);
for (Node<OWLNamedIndividual> i : individuals)
{
System.out.println(i.getClass());
}
我希望结果是每个人的班级但是推理者没有结果。在Protege中,它的工作效果很好,但是当我使用本体学并尝试与OWL API合作时,我没有任何结果
是的,这是行不通的。i.getClass()
将为您提供Java类,而不是个人的主张类型。要获得所需的主张类型,您需要调用ontology.axioms(i).collect(Collectors.toSet())
。这只会返回已添加到本体的断言。
要获得推断的类型,您需要致电reasoner.types(i).collect(Collectors.toSet())
。
让我感到困扰的是,您说您有 no 结果。我希望您应该得到很多错误的结果(即Java课程,而不是OWL课程)。好的,因此您正在代码段中创建一个本体。您没有得到任何结果的原因是您尚未为john
添加类主张公理。您需要添加一个factory.getOWLClassAssertionAxiom(owlClassExpression, john)
,其中owlClassExpression
代表您本体中的类,例如Person
。
基于猫头鹰API示例,这是两个示例:
获得猫头鹰风格的子类:
OWLOntologyManager owlManager OWLManager.createOWLOntologyManager();
IRI iri = IRI.create("http://www.semanticweb.org/music-ontology#Style");
OWLClass style = owlManager.getOWLDataFactory().getOWLClass(iri);
OWLReasonerFactory reasonerFactory = new StructuralReasonerFactory();
OWLReasoner reasoner = reasonerFactory.createReasoner(ontologia);
NodeSet<OWLClass> subClasses = reasoner.getSubClasses(style, true);
Set<OWLClass> clses = subClasses.getFlattened();
Log.d(TAG, "Subclasses of Style: ");
for (OWLClass cls : clses) {
String s = cls.toString();
Log.d(TAG, s.substring(s.indexOf("#") + 1, s.length() -1));
}
获得猫头鹰岩石的个人:
OWLOntologyManager owlManager OWLManager.createOWLOntologyManager();
IRI iri = IRI.create("http://www.semanticweb.org/music-ontology#Rock");
OWLClass rock = owlManager.getOWLDataFactory().getOWLClass(iri);
OWLReasonerFactory reasonerFactory = new StructuralReasonerFactory();
OWLReasoner reasoner = reasonerFactory.createReasoner(ontologia);
NodeSet<OWLNamedIndividual> individualsNodeSet = reasoner.getInstances(rock, true);
Set<OWLNamedIndividual> individuals = individualsNodeSet.getFlattened();
Log.d(TAG, "Instances of Rock: ");
for (OWLNamedIndividual ind : individuals) {
String s = ind.toString();
Log.d(TAG, s.substring(s.indexOf("#") + 1, s.length() -1));
}