Search code examples
javaowl-api

getting all individuals of a specific class using OWLAPi and JFact reasoner


Is there any way to get all individuals of a specific class using reasoner? Reasoner because i want to get all the inferred and assereted individuals of that class. I am using JFact reasoner, and i am trying for loops and if statement. And i want to find the individuals of class e.g "person". But i am unable to see the individuals. Any idea about below code or is there any method for this purpose?

for (OWLClass c : myPizza.getClassesInSignature()) {
        NodeSet<OWLNamedIndividual> instances = reasoner.getInstances(c, true);
        System.out.println(c.getIRI().getFragment());
        if (c.getIRI().getFragment().equals("Person")){

            for (OWLNamedIndividual i : instances.getFlattened()) {
                System.out.println(i.getIRI().getFragment()); 

        }
    }
        else {
            continue;
        }
        break;

    }

Thanks


Solution

  • Calling reasoner.getInstances(c, true); will only give you the /direct/ instances of c; if the individuals you are after are instances of subclasses of c, they will be skipped. Switch to reasoner.getInstances(c, false); to include instances of subclasses.

    You are also calling break; after the first iteration. If person is not the first class in the signature, you'll never look for instances of person.

    You could slightly change your code to do less reasoning work:

    for (OWLClass c : myPizza.getClassesInSignature()) {
        if (c.getIRI().getFragment().equals("Person")){
            NodeSet<OWLNamedIndividual> instances = reasoner.getInstances(c, false);
            System.out.println(c.getIRI().getFragment());
            for (OWLNamedIndividual i : instances.getFlattened()) {
                System.out.println(i.getIRI().getFragment()); 
            }
        }
    }
    

    Edit: Note from comments, if you expect to see SWRL inferred individuals you need to use a reasoner that supports SWRL, like Pellet or HermiT. JFact does not support SWRL rules.