使用OWLAPi和JFact推理器获取特定类的所有个体 [英] getting all individuals of a specific class using OWLAPi and JFact reasoner
问题描述
有没有办法让使用推理器的特定班级的所有人?
推理因为我希望获得该类的所有推断和已确认的个体。
我正在使用JFact推理器,我正在尝试循环和if语句。而且我想找到班级的人,例如人。但我无法看到这些人。对于下面的代码有什么想法,或者是否有任何方法用于此目的?
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;
}
谢谢
推荐答案
调用 reasoner.getInstances(c,true);
只会给你/ c的/ direct /实例;如果你所追求的个体是c的子类的实例,它们将被跳过。切换到 reasoner.getInstances(c,false);
以包含子类的实例。
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.
您还在调用<第一次迭代后code> break; 。如果 person
不是签名中的第一个类,您将永远不会查找 person
的实例。
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());
}
}
}
编辑:注释中的注释,如果您希望看到SWRL推断的个体,您需要使用支持SWRL的推理器,如Pellet或HermiT。 JFact不支持SWRL规则。
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.
这篇关于使用OWLAPi和JFact推理器获取特定类的所有个体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!