简体   繁体   English

使用OWLAPi和JFact推理器获取特定类的所有个体

[英]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. 我正在使用JFact推理器,我正在尝试循环和if语句。 And i want to find the individuals of class eg "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); 调用reasoner.getInstances(c, true); will only give you the /direct/ instances of c; 只会给你/直接/ c的实例; if the individuals you are after are instances of subclasses of c, they will be skipped. 如果你所追求的个体是c的子类的实例,它们将被跳过。 Switch to reasoner.getInstances(c, false); 切换到reasoner.getInstances(c, false); to include instances of subclasses. 包含子类的实例。

You are also calling break; 你也叫break; after the first iteration. 在第一次迭代之后。 If person is not the first class in the signature, you'll never look for instances of person . 如果person不在签名一流的,你永远也不会找实例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. 编辑:请注意评论,如果您希望看到SWRL推断的个人,您需要使用支持SWRL的推理器,如Pellet或HermiT。 JFact does not support SWRL rules. JFact不支持SWRL规则。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM