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

北战南征 提交于 2019-12-04 16:12:51

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.

achini

Try out this method. You can get all individuals for a particular class using below method.

 private static void printIndividualsByclass(OWLOntology ontology, String owlClass){
    OWLReasonerFactory reasonerFactory = new PelletReasonerFactory();
    OWLReasoner reasoner = reasonerFactory.createNonBufferingReasoner(ontology);
    for (OWLClass c : ontology.getClassesInSignature()) {
        if (c.getIRI().getShortForm().equals(owlClass)){
            NodeSet<OWLNamedIndividual> instances = reasoner.getInstances(c, false);
            System.out.println("Class : "+ c.getIRI().getShortForm());
            for (OWLNamedIndividual i : instances.getFlattened()) {
                System.out.println(i.getIRI().getShortForm()); 
            }
        }
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!