我找不到一些像样的简单代码示例来使用SWRL和Jena与Pellet,或者至少使用SWRL?我研究过Pellet文档中的一些示例,但没有关于使用SWRL的示例。网络上的大多数例子都是不完整和令人困惑的。
我找到的唯一解决方案是Jess Rule引擎,但它不是免费的,而且是商业许可的。我发现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));发布于 2013-06-28 20:31:48
Pellet规则和Jena规则是非常不同的™
简短的答案是,Pellet支持SWRL规则。如果你有一个包含SWRL规则的本体,并要求Pellet对其进行推理,它会将它们考虑在内。
Jena有自己的规则语言,文档页面Reasoners and rule engines: Jena inference support中对此进行了描述。它同时支持正向和反向链接规则。
然而,尽管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的支持还不完整(仍然“对贡献者开放”)。
与使用OWL API或尝试使用Jena的API相比,您可能会发现使用Protégé等编辑器创建规则更容易。Martin Kuba写了一个非常好的OWL2 and SWRL Tutorial,可以在这里对你有所帮助。
发布于 2013-07-03 13:36:08
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");
}希望这对你有帮助。
https://stackoverflow.com/questions/17357836
复制相似问题