How to get the class of individual in owl with the reasoner
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());
}
I expect the result to be the class of every individual but the reasoner gives no result. In protege it's work well but when i take my ontology and try to make it with owl api, i don't got any result
Yes, that will not work.
i.getClass()
will give you the Java class rather than the asserted types of the individual. To get the asserted types of the individuals you will need to callontology.axioms(i).collect(Collectors.toSet())
. This will only return assertions that have been added to the ontology.To get inferred types you will need to call
reasoner.types(i).collect(Collectors.toSet())
.What bothers me is that you say you got no results. I would expect that you should get lots of wrong results (i.e. Java classes rather than OWL classes). Ok, so you are creating an ontology in the code snippet. The reason why you are not getting any results is that you have not added a class assertion axiom for
john
. You need to add afactory.getOWLClassAssertionAxiom(owlClassExpression, john)
whereowlClassExpression
represents a class in your ontology likePerson
.