繁体   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