How to get the class of individual in OWL API

2.7k Views Asked by At

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

2

There are 2 best solutions below

11
On

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 call ontology.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 a factory.getOWLClassAssertionAxiom(owlClassExpression, john) where owlClassExpression represents a class in your ontology like Person.

0
On

Based on OWL Api Examples, here is two examples:

Get SubClasses of OWLClass Style:

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));
}

Get Individuals of OWLClass Rock:

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));
}