2010-06-11 65 views
6
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类的个体。所以,它已经写在猫头鹰文件中。然而,问题是推理者应该推断margherita-example应该也是素食比萨饼。 (Protege正确地推断示例 - 玛格丽塔是一个素食比萨饼,但我不能通过编程推断)使用Jena推测

+0

如果你包含一个指向pizza.owl文件的指针(假设它在某处公开),并且你还提供了用于设置'reasoner'变量的代码将会很有帮助。 – cygri 2010-06-11 17:57:12

+0

非常感谢你的兴趣cygri。我解决了我的问题,并在下面提供了一个示例。 – Mikae 2010-06-12 12:42:37

回答

9

我解决了我的问题题。我认为我的本体论存在问题。因此,我创建了另一个本体来推断个体。我创建的本体包含Person和Person的子类:MalePerson,FemalePerson和MarriedPerson。并且,有两个对象属性(hasSpouse,hasSibling)和一个数据类型属性(hasAge)。 而且,我创建了3个人。 约翰 - MalePerson - hasAge(20) - hasSibling(简) 简 - FemalePerson - hasSibling(约翰) - hasSpouse(BOB) 鲍勃 - MalePerson - hasSpouse(简)

而且,我放了两个限制为MalePerson和FemalePerson类。 对于MalePerson: hasSpouse最大1 hasSpouse仅MalePerson 对于FemalePerson: hasSpouse最大1 hasSpouse仅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 
+0

谢谢Mikae!很有帮助!如果你可以包含你的OWL和Turtle文件,那将是非常好的:-) – 2013-09-06 16:15:37

+0

有没有人知道如何用自己的规则来加载文件? – Macilias 2014-07-23 12:42:23