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

安稳与你 提交于 2019-12-21 20:03:15

问题


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


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/28072721/getting-all-individuals-of-a-specific-class-using-owlapi-and-jfact-reasoner

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!