简体   繁体   English

使用SWRL与Jena和Pellet

[英]Using SWRL with Jena and Pellet

I was unable to find some decent simple code examples of using SWRL and Jena with Pellet, or at least using SWRL? 我无法找到一些像SWRL和Jena一样使用Pellet,或者至少使用SWRL的简单代码示例? I have studied some examples in Pellet documentation, but there is no example about using SWRL. 我在Pellet文档中研究过一些例子,但没有关于使用SWRL的例子。 Most examples on the web are incomplete and confusing. 网络上的大多数示例都是不完整和令人困惑的。

The only solution I found was with the Jess Rule Engine but it is not free and is under commercial license. 我找到的唯一解决方案是使用Jess Rule Engine,但它不是免费的,并且是在商业许可下。 I found that Pellet support SWRL rules but could not find running example. 我发现Pellet支持SWRL规则,但找不到运行示例。

The only example I found is this, but I do not understand it: 我找到的唯一例子就是这个,但我不明白:

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 Rules and Jena Rules are Very Different™ 颗粒规则和耶拿规则是非常不同的

The short answer is that Pellet supports SWRL rules. 简短的回答是Pellet支持SWRL规则。 If you have an ontology that contains SWRL rules and ask Pellet to reason over it, it will take them into consideration. 如果您的本体包含SWRL规则并要求Pellet对其进行推理,则需要考虑它们。

Jena has its own rules language, which is described in the documentation page, Reasoners and rule engines: Jena inference support . Jena有自己的规则语言,在文档页面, Reasoners和规则引擎中描述:Jena推理支持 It supports both forward and backward chaining rules. 它支持前向和后向链接规则。

However, though both Pellet and Jena support a notion of rules, the intended domains of SWRL rules and Jena rules are very different. 然而,尽管Pellet和Jena都支持规则概念,但SWRL规则和Jena规则的预期领域却截然不同。 SWRL rules are OWL-level constructs; SWRL规则是OWL级别的构造; the unary predicates in a SWRL rule are class expressions, and the binary predicates are object and data properties. SWRL规则中的一元谓词是类表达式,二元谓词是对象和数据属性。 Additionally, SWRL rules only match on named individuals; 此外,SWRL规则仅与指定的个人匹配; they don't match for individuals whose existence is only inferred. 它们与仅推断其存在的个体不匹配。 Jena rules, on the other hand, are RDF-level, and designed to work on RDF-graphs. 另一方面,Jena规则是RDF级的,旨在用于RDF图。 While RDF and OWL are often used together, (eg, OWL data is serialized in RDF), the two are conceptually distinct. 虽然RDF和OWL经常一起使用,(例如,OWL数据在RDF中被序列化),但这两者在概念上是不同的。 An OWL reasoner could be implemented that makes no use of RDF, and a SWRL engine could be built that make no use of RDF graphs. 可以实现不使用RDF的OWL推理器,并且可以构建不使用RDF图的SWRL引擎。

Jena or OWL API? Jena或OWL API?

The code that you've shown, based on the presence of an OWLOntologyManager , is based on the OWL API, not on Jena's API. 基于OWLOntologyManager的存在,您显示的代码基于OWL API,而不是Jena的API。 The OWL API will have more direct functionality for working with OWL and SWRL rules, while Jena will not. OWL API将具有更多用于处理OWL和SWRL规则的直接功能,而Jena则不会。 (Jena's OntModels work well with OWL1, but support for OWL2 is not complete (and still “open for contributors”). (Jena的OntModels与OWL1配合得很好,但对OWL2的支持并不完整(并且仍然“对贡献者开放”)。

Rather than using the OWL API or trying to use Jena's API, you'll probably find it easier to create rules using an editor such as Protégé. 您可能会发现使用Protégé等编辑器创建规则更容易,而不是使用OWL API或尝试使用Jena的API。 Martin Kuba has written a very nice OWL2 and SWRL Tutorial that can help you here. Martin Kuba撰写了一篇非常好的OWL2和SWRL教程 ,可以为您提供帮助。

SWRL rules work with Pellet API. SWRL规则适用于Pellet API。 I created my ontology and SWRL rules using Protégé and I was able to create OWL individuals dynamically using Java code. 我使用Protégé创建了我的本体和SWRL规则,并且我能够使用Java代码动态创建OWL个体。 This whole ontology is used as aggregatedOwl in the following code. 整个本体在以下代码中用作aggregatedOwl This code loads ontology (base OWL + individuals if any + SWRL rules) and runs the Pellet reasoner on it and saves the inferred result in a string. 此代码加载本体(基本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");
}

Hope this is helpful to you. 希望这对你有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM