簡體   English   中英

使用SWRL與Jena和Pellet

[英]Using SWRL with Jena and Pellet

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

我找到的唯一解決方案是使用Jess Rule Engine,但它不是免費的,並且是在商業許可下。 我發現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));

顆粒規則和耶拿規則是非常不同的

簡短的回答是Pellet支持SWRL規則。 如果您的本體包含SWRL規則並要求Pellet對其進行推理,則需要考慮它們。

Jena有自己的規則語言,在文檔頁面, Reasoners和規則引擎中描述:Jena推理支持 它支持前向和后向鏈接規則。

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

Jena或OWL API?

基於OWLOntologyManager的存在,您顯示的代碼基於OWL API,而不是Jena的API。 OWL API將具有更多用於處理OWL和SWRL規則的直接功能,而Jena則不會。 (Jena的OntModels與OWL1配合得很好,但對OWL2的支持並不完整(並且仍然“對貢獻者開放”)。

您可能會發現使用Protégé等編輯器創建規則更容易,而不是使用OWL API或嘗試使用Jena的API。 Martin Kuba撰寫了一篇非常好的OWL2和SWRL教程 ,可以為您提供幫助。

SWRL規則適用於Pellet API。 我使用Protégé創建了我的本體和SWRL規則,並且我能夠使用Java代碼動態創建OWL個體。 整個本體在以下代碼中用作aggregatedOwl 此代碼加載本體(基本OWL +個人,如果有任何+ SWRL規則)並在其上運行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");
}

希望這對你有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM