2010-06-11 77 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