2013-06-28 88 views
3

我無法找到一些體面的簡單代碼示例,使用SWRL和Jena與 Pellet或至少使用SWRL?我已經在Pellet文檔中研究了一些示例,但沒有關於使用SWRL的示例。網絡上的大多數例子都不完整,令人困惑。在Jena和Pellet中使用SWRL

我發現的唯一解決方案是使用Jess規則引擎,但它不是免費的並且處於商業許可下。我發現Pellet支持SWRL規則,但找不到運行示例。

我發現的唯一的例子是這樣的,但我不明白:

OWLOntologyManager m = create(); 
OWLOntology o = m.createOntology(example_iri); 
// Get hold of references to class A and class B. 
OWLClass clsA = df.getOWLClass(IRI.create(example_iri + "#A")); 
OWLClass clsB = df.getOWLClass(IRI.create(example_iri + "#B" )); 
SWRLVariable var = df.getSWRLVariable(IRI.create(example_iri + "#x")); 
SWRLClassAtom body = df.getSWRLClassAtom(clsA, var); 
SWRLClassAtom head = df.getSWRLClassAtom(clsB, var); 
SWRLRule rule = df.getSWRLRule(Collections.singleton(body), 
Collections.singleton(head)); 
m.applyChange(new AddAxiom(o, rule)); 

回答

5

顆粒規則和耶拿規則有很大的不同™

簡短的回答是佩萊支持SWRL規則。如果你有一個包含SWRL規則的本體,並要求Pellet推理它,它將考慮到它們。

Jena擁有自己的規則語言,在文檔頁面Reasoners and rule engines: Jena inference support中進行了描述。它支持前向和後向鏈接規則。

但是,儘管Pellet和Jena都支持規則概念,但SWRL規則和Jena規則的預期域名卻有很大不同。 SWRL規則是OWL級別的構造; SWRL規則中的一元謂詞是類表達式,二元謂詞是對象和數據屬性。此外,SWRL規則僅適用於指定的個人;它們不適用於僅推斷其存在的個體。另一方面,耶拿規則是RDF級別的,旨在用於RDF圖表。雖然RDF和OWL經常一起使用(例如,OWL數據在RDF中序列化),但兩者在概念上是不同的。可以實現不使用RDF的OWL推理器,並且可以構建不使用RDF圖的SWRL引擎。

Jena or OWL API?

基於OWLOntologyManager的存在顯示的代碼基於OWL API,而不是基於Jena的API。 OWL API將有更直接的功能來處理OWL和SWRL規則,而耶拿不會。 (Jena的OntModels與OWL1協同工作,但對OWL2的支持並不完整(並且仍然「對貢獻者開放」)。

與使用OWL API或嘗試使用Jena的API相比,您可能更容易使用Protégé等編輯器創建規則Martin Kuba編寫了一個非常好的OWL2 and SWRL Tutorial,可以幫助你在這裏

+0

我已經用Protege 4.3測試了一些SWRL規則嗎?它會和Pellet一起工作嗎?其次,什麼是SWRL的替代方案 –

+0

@AliAhmadProtégé4.3就Pellet而言,據我所知。有基於SPARQL的[SPIN](http://spinrdf.org/)[rules](http://www.w3.org/Submission/2011/SUBM-spin-overview-20110222/)。 (我沒有用過這些,也不能真正對它們發表評論。) –

+0

@alex手頭上,我不確定protege 4是否支持SWRL,但我認爲它。但是,我不認爲默認顯示適當的選項卡。它需要打開(像「Show View」之類的菜單),它沒有一個明顯的名字,它只是被稱爲「規則」。 –

3

SWRL規則與Pellet API協同工作我使用Protégé創建了我的本體和SWRL規則,並且我能夠創建OWL個人動態地使用Java代碼這個整體本體在下面的代碼中用作aggregatedOwl這個代碼加載本體(如果有+ SWRL規則的話,基本OWL +個體)並且運行Pellet推理器並且將推斷結果保存在一個字符串中

import org.semanticweb.owlapi.apibinding.OWLManager; 
import org.semanticweb.owlapi.io.StringDocumentTarget; 
import org.semanticweb.owlapi.model.OWLAxiom; 
import org.semanticweb.owlapi.model.OWLOntology; 
import org.semanticweb.owlapi.model.OWLOntologyCreationException; 
import org.semanticweb.owlapi.model.OWLOntologyManager; 
import org.semanticweb.owlapi.model.OWLOntologyStorageException; 
import org.semanticweb.owlapi.util.InferredAxiomGenerator; 
import org.semanticweb.owlapi.util.InferredOntologyGenerator; 
import org.semanticweb.owlapi.util.InferredPropertyAssertionGenerator; 
import com.clarkparsia.pellet.owlapiv3.PelletReasoner; 
import com.clarkparsia.pellet.owlapiv3.PelletReasonerFactory; 

try { 
    manager = OWLManager.createOWLOntologyManager(); 

    InputStream owlInputStream = new ByteArrayInputStream(aggregatedOwl.getBytes("UTF-8")); 
    inferredOntology = manager.loadOntologyFromOntologyDocument(owlInputStream); 

    PelletReasoner reasoner = PelletReasonerFactory.getInstance().createReasoner(inferredOntology); 
    reasoner.getKB().realize(); 

    List<InferredAxiomGenerator<? extends OWLAxiom>> axiomGenerators = new ArrayList<InferredAxiomGenerator<? extends OWLAxiom>>(); 
    axiomGenerators.add(new InferredPropertyAssertionGenerator()); 

    InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner,axiomGenerators); 
    iog.fillOntology(manager, inferredOntology); 

    // Save the new ontology 
    OutputStream owlOutputStream = new ByteArrayOutputStream(); 
    manager.saveOntology(inferredOntology, owlOutputStream); 
    inferredData = owlOutputStream.toString(); 
} 
catch (Exception e) { 
    throw new Exception("Exception occurred in applying reasoner"); 
} 

希望這對你有幫助。

+2

我發現你的代碼混淆了!你在混合Jena和OwlApi的時候在你的進口中,而你沒有使用任何的Jena進口。 – PCoder

+0

感謝您指出,我沒有從我的代碼片段中刪除不必要的進口我編輯了我的答案,希望現在好了。 – Swamy