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
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.
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