【问题标题】:Inferring using Jena使用 Jena 进行推断
【发布时间】:2011-03-02 17:50:00
【问题描述】:
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 是素食披萨。但是,我无法以编程方式推断)

【问题讨论】:

  • 如果您包含一个指向 Pizza.owl 文件的指针(我想它在某处是公开的)并且您还提供了用于设置 reasoner 变量的代码,将会很有帮助。跨度>
  • 非常感谢 cygri 的关注。我解决了我的问题并在下面提供了一个示例。

标签: owl jena inference


【解决方案1】:

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

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

最后,我将 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

【讨论】:

  • 谢谢三凯!非常有帮助!如果您也可以包含您的 OWL 和 Turtle 文件,那就太好了:-)
  • 有谁知道如何使用从文件加载的自己的规则来扩展它?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-28
  • 2012-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多