使用 Jena 进行推断

发布于 2024-09-05 03:12:28 字数 594 浏览 10 评论 0原文

InfModel infmodel = ModelFactory.createInfModel(reasoner, m);
Resource vegetarian = infmodel.getResource(source + "Vegetarian");
Resource margherita = infmodel.getResource(source + "Example-Margherita");
if (infmodel.contains(margherita, RDF., vegetarian)) {
        System.out.println("Margherita is a memberOf Vegetarian pizza");
    }

上面给出的例子是由正式的 Pizza.owl 构成的。在这只猫头鹰中,Example-Margherita 是 Margherita 类的个体。所以,它已经写在owl文件中了。然而,问题是推理者应该推断 margherita-example 也应该是素食披萨。 谁能举一个例子来说明如何像 Protege 中那样找到一个人可能的推断类?(Protege 正确地推断出 Example-Margherita 是素食披萨。但是,我无法以编程方式推断)

InfModel infmodel = ModelFactory.createInfModel(reasoner, m);
Resource vegetarian = infmodel.getResource(source + "Vegetarian");
Resource margherita = infmodel.getResource(source + "Example-Margherita");
if (infmodel.contains(margherita, RDF., vegetarian)) {
        System.out.println("Margherita is a memberOf Vegetarian pizza");
    }

The example given above is formed by formal pizza.owl. In this owl, Example-Margherita is an individual of Margherita class. So, it is already written in owl file. However, the problem is that the reasoner should infer that margherita-example should be also an vegetarian pizza.
Could anyone please give an example that shows how to find an individual's possible inferred classes like in Protege ?(Protege correctly infers that Example-Margherita is a Vegetarian Pizza. However, I can't infer programmatically)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧时浪漫 2024-09-12 03:12:28

我解决了我的问题。我认为我的本体有问题。因此,我创建了另一个本体来推断个体。我创建的本体包含 Person 和 Person 的子类:MalePerson、FemalePerson 和 MarriedPerson。并且,有两个对象属性(hasSpouse、hasSibling)和一个数据类型属性(hasAge)。
而且,我创建了 3 个个体。
约翰 - 男性 - hasAge(20) - hasSibling(Jane)
Jane - 女性 - hasSibling(John) - hasSpouse(Bob)
Bob - MalePerson - hasSpouse(Jane)

而且,我对 MalePerson 和 FemalePerson 类设置了两个限制。
对于男性:
拥有配偶 最多 1
有配偶仅限男性
对于女性:
拥有配偶 最多 1
hasSpouse only FemalePerson

最后,我将 MarriedPerson 定义为一个定义的类。在推理之前,MarriedPerson 没有个体。但是,该模型应该推断 Jane 和 Bob 已婚。因此,最后 MarriedPerson 类应该有 2 个人。

当我使用 Jena 在 Java 中运行此代码时,我推断出了 2 个个体。

OntModel ontModel = ModelFactory.createOntologyModel();
    InputStream in = FileManager.get().open(inputFileName);
    if (in == null) {
        throw new IllegalArgumentException( "File: " + inputFileName + " not found");
    }
    ontModel.read(in, "");


    Reasoner reasoner = ReasonerRegistry.getOWLReasoner();
    reasoner = reasoner.bindSchema(ontModel);
    // Obtain standard OWL-DL spec and attach the Pellet reasoner
    OntModelSpec ontModelSpec = OntModelSpec.OWL_DL_MEM;
    ontModelSpec.setReasoner(reasoner);
    // Create ontology model with reasoner support
    OntModel model = ModelFactory.createOntologyModel(ontModelSpec, ontModel);

    // MarriedPerson has no asserted instances
    // However, if an inference engine is used, two of the three
    // individuals in the example presented here will be
    // recognized as MarriedPersons
            //ns is the uri
    OntClass marPerson = model.getOntClass(ns + "OWLClass_00000003866036241880"); // this is the uri for MarriedPerson class
    ExtendedIterator married = marPerson.listInstances();
    while(married.hasNext()) {
        OntResource mp = (OntResource)married.next();
        System.out.println(mp.getURI());
    } // this code returns 2 individuals with the help of reasoner

I solved my question. I think there was a problem with my ontology. Therefore, I created another ontology to infer individuals. The ontology that I created contains Person and subclasses of Person : MalePerson, FemalePerson and MarriedPerson. And, there are two object properties(hasSpouse, hasSibling) and one data type property(hasAge).
And, I created 3 individuals.
John - MalePerson - hasAge(20) - hasSibling(Jane)
Jane - FemalePerson - hasSibling(John) - hasSpouse(Bob)
Bob - MalePerson - hasSpouse(Jane)

And, I put two restrictions for MalePerson and FemalePerson classes.
For MalePerson :
hasSpouse max 1
hasSpouse only MalePerson
For FemalePerson :
hasSpouse max 1
hasSpouse only FemalePerson

Lastly, I made MarriedPerson to be a defined class. Before reasoning, MarriedPerson has no individual. However, the model should infer that Jane and Bob are married. Therefore, at the end, MarriedPerson class should have 2 individuals.

When I ran this code in Java using Jena, I got 2 inferred individuals.

OntModel ontModel = ModelFactory.createOntologyModel();
    InputStream in = FileManager.get().open(inputFileName);
    if (in == null) {
        throw new IllegalArgumentException( "File: " + inputFileName + " not found");
    }
    ontModel.read(in, "");


    Reasoner reasoner = ReasonerRegistry.getOWLReasoner();
    reasoner = reasoner.bindSchema(ontModel);
    // Obtain standard OWL-DL spec and attach the Pellet reasoner
    OntModelSpec ontModelSpec = OntModelSpec.OWL_DL_MEM;
    ontModelSpec.setReasoner(reasoner);
    // Create ontology model with reasoner support
    OntModel model = ModelFactory.createOntologyModel(ontModelSpec, ontModel);

    // MarriedPerson has no asserted instances
    // However, if an inference engine is used, two of the three
    // individuals in the example presented here will be
    // recognized as MarriedPersons
            //ns is the uri
    OntClass marPerson = model.getOntClass(ns + "OWLClass_00000003866036241880"); // this is the uri for MarriedPerson class
    ExtendedIterator married = marPerson.listInstances();
    while(married.hasNext()) {
        OntResource mp = (OntResource)married.next();
        System.out.println(mp.getURI());
    } // this code returns 2 individuals with the help of reasoner
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文