如何使用Openllet和OWL api处理SWRL规则



我一直在尝试设置一个查询打印机,非常像这样:https://github.com/owlcs/owlapi/wiki/DL-Queries-with-a-real-reasoner如果我用曼彻斯特语法给它一个查询,我可以得到一个反映我在protege中得到的响应。我用Openllet换掉了Hermit推理机,它似乎再也找不到任何人了。

出于某些原因,如果可能的话,我想远离耶拿。

OntController.java

public class OntController {
//declared variables here
public OntController(String name) throws OWLOntologyCreationException, OWLOntologyStorageException, IOException{
//initialized a bunch of other variables here
manager = OWLManager.createOWLOntologyManager();
reasonFactory = new OpenlletReasonerFactory();
}
public void reason(){
reasoner = reasonFactory.createReasoner(ont);
reasoner.precomputeInferences(InferenceType.OBJECT_PROPERTY_HIERARCHY, InferenceType.OBJECT_PROPERTY_ASSERTIONS);
}
public void infer(){
reasoner.precomputeInferences();
List<InferredAxiomGenerator<? extends OWLAxiom>> gens = new ArrayList<InferredAxiomGenerator<? extends OWLAxiom>>();
gens.add(new InferredSubClassAxiomGenerator());
InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner, gens);
iog.fillOntology(manager.getOWLDataFactory(), ont);
}
public void query() throws IOException{
reasoner = reasonFactory.createReasoner(ont);
ShortFormProvider shortFormProvider = new SimpleShortFormProvider();
DLQueryPrinter dlQueryPrinter = new DLQueryPrinter(new DLQueryEngine(reasoner,
shortFormProvider), shortFormProvider);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));
while (true) {
System.out.println("Type a class expression in Manchester Syntax and press Enter (or press q to exit):");
//blah blah
}
dlQueryPrinter.askQuery(classExpression.trim());
System.out.println();
}
//more unrelated methods here
}

DLQueryEngine

import java.util.Collections;
import java.util.Set;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLClassExpression;
import org.semanticweb.owlapi.model.OWLNamedIndividual;
import org.semanticweb.owlapi.reasoner.Node;
import org.semanticweb.owlapi.reasoner.NodeSet;
//import org.semanticweb.owlapi.reasoner.OWLReasoner;
import org.semanticweb.owlapi.util.ShortFormProvider;
import openllet.owlapi.OpenlletReasoner;
class DLQueryEngine {
private final OpenlletReasoner reasoner;
private final DLQueryParser parser;
public DLQueryEngine(OpenlletReasoner reasoner, ShortFormProvider shortFormProvider) {
this.reasoner = reasoner;
parser = new DLQueryParser(reasoner.getRootOntology(), shortFormProvider);
}
public Set<OWLClass> getSuperClasses(String classExpressionString, boolean direct) {
if (classExpressionString.trim().length() == 0) {
return Collections.emptySet();
}
OWLClassExpression classExpression = parser
.parseClassExpression(classExpressionString);
NodeSet<OWLClass> superClasses = reasoner
.getSuperClasses(classExpression, direct);
return superClasses.getFlattened();
}
public Set<OWLClass> getEquivalentClasses(String classExpressionString) {
if (classExpressionString.trim().length() == 0) {
return Collections.emptySet();
}
OWLClassExpression classExpression = parser
.parseClassExpression(classExpressionString);
Node<OWLClass> equivalentClasses = reasoner.getEquivalentClasses(classExpression);
Set<OWLClass> result = null;
if (classExpression.isAnonymous()) {
result = equivalentClasses.getEntities();
} else {
result = equivalentClasses.getEntitiesMinus(classExpression.asOWLClass());
}
return result;
}
public Set<OWLClass> getSubClasses(String classExpressionString, boolean direct) {
if (classExpressionString.trim().length() == 0) {
return Collections.emptySet();
}
OWLClassExpression classExpression = parser
.parseClassExpression(classExpressionString);
NodeSet<OWLClass> subClasses = reasoner.getSubClasses(classExpression, direct);
return subClasses.getFlattened();
}
public Set<OWLNamedIndividual> getInstances(String classExpressionString,
boolean direct) {
if (classExpressionString.trim().length() == 0) {
return Collections.emptySet();
}
OWLClassExpression classExpression = parser
.parseClassExpression(classExpressionString);
NodeSet<OWLNamedIndividual> individuals = reasoner.getInstances(classExpression,
direct);
return individuals.getFlattened();
}
}

您正在使用推断的子类公理生成器。这不会为个体创建公理,所以我不会期望生成本体中的个体。要获得更多建议,我们需要查看重现问题的代码和数据片段。

问题似乎出在我链接的代码上。在查询打印机类中,他们设置

Set<OWLNamedIndividual> individuals = dlQueryEngine.getInstances(
classExpression, true);

如果你想让个人出现,布尔值应该是false,这真的让我很反感。