我正在使用 Protege OWL API 4.3 (OWL API 3.4.2)。 它安装了 HermiT Reasoner 插件。
我的问题是,无论我对推理器有什么查询,都没有输出,它不提供任何实例。
这就是我的本体的样子:
<Ontology xmlns="http://www.w3.org/2002/07/owl#"
xml:base="http://www.semanticweb.org/sabse/ontologies/2013/11/untitled-ontology-17"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:xml="http://www.w3.org/XML/1998/namespace"
ontologyIRI="http://www.semanticweb.org/sabse/ontologies/2013/11/untitled-ontology-17">
<Prefix name="" IRI="http://www.w3.org/2002/07/owl#"/>
<Prefix name="owl" IRI="http://www.w3.org/2002/07/owl#"/>
<Prefix name="rdf" IRI="http://www.w3.org/1999/02/22-rdf-syntax-ns#"/>
<Prefix name="xsd" IRI="http://www.w3.org/2001/XMLSchema#"/>
<Prefix name="rdfs" IRI="http://www.w3.org/2000/01/rdf-schema#"/>
<Declaration>
<Class IRI="#Person"/>
</Declaration>
<Declaration>
<Class IRI="#PersonWithPosition1"/>
</Declaration>
<Declaration>
<DataProperty IRI="#position"/>
</Declaration>
<Declaration>
<NamedIndividual IRI="#mary"/>
</Declaration>
<Declaration>
<NamedIndividual IRI="#peter"/>
</Declaration>
<EquivalentClasses>
<Class IRI="#PersonWithPosition1"/>
<ObjectIntersectionOf>
<Class IRI="#Person"/>
<DataHasValue>
<DataProperty IRI="#position"/>
<Literal datatypeIRI="http://www.w3.org/2001/XMLSchema#integer">1</Literal>
</DataHasValue>
</ObjectIntersectionOf>
</EquivalentClasses>
<SubClassOf>
<Class IRI="#PersonWithPosition1"/>
<Class IRI="#Person"/>
</SubClassOf>
<ClassAssertion>
<Class IRI="#Person"/>
<NamedIndividual IRI="#mary"/>
</ClassAssertion>
<ClassAssertion>
<Class IRI="#Person"/>
<NamedIndividual IRI="#peter"/>
</ClassAssertion>
<DataPropertyAssertion>
<DataProperty IRI="#position"/>
<NamedIndividual IRI="#mary"/>
<Literal datatypeIRI="http://www.w3.org/2001/XMLSchema#int">1</Literal>
</DataPropertyAssertion>
<DataPropertyAssertion>
<DataProperty IRI="#position"/>
<NamedIndividual IRI="#peter"/>
<Literal datatypeIRI="http://www.w3.org/2001/XMLSchema#int">2</Literal>
</DataPropertyAssertion>
<DataPropertyDomain>
<DataProperty IRI="#position"/>
<Class IRI="#Person"/>
</DataPropertyDomain>
<DataPropertyRange>
<DataProperty IRI="#position"/>
<Datatype abbreviatedIRI="xsd:int"/>
</DataPropertyRange>
这是我的代码:
public class OWLAPIDemoApplication {
public static void main(String[] args) {
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
try {
OWLOntology ontology;
File file = new File("ontology.owl");
ontology = manager.loadOntologyFromOntologyDocument(file);
System.out.println("Loaded ontology: " + ontology);
// Create an HermiT reasoner.
Reasoner reasoner = new Reasoner(ontology);
OWLDataFactory factory = manager.getOWLDataFactory();
PrefixManager pm = new DefaultPrefixManager("#");
// Get reference to the class PersonWithinPosition1
OWLClass person = factory.getOWLClass(":Person", pm);
OWLDataProperty position = factory.getOWLDataProperty(":position",
pm);
OWLClassExpression query = factory.getOWLObjectIntersectionOf(
person,
factory.getOWLDataHasValue(position,
factory.getOWLLiteral(1)));
// Create a fresh name for the query.
OWLClass newName = factory.getOWLClass(IRI.create("temp001"));
// Make the query equivalent to the fresh class
OWLAxiom definition = factory.getOWLEquivalentClassesAxiom(newName,
query);
manager.addAxiom(ontology, definition);
manager.saveOntology(ontology, new SystemOutDocumentTarget());
reasoner.flush();
NodeSet<OWLNamedIndividual> w = reasoner
.getInstances(newName, true);
Set<OWLNamedIndividual> e;
for (Node<OWLNamedIndividual> n : w) {
for (OWLNamedIndividual i : n.getEntities()) {
System.out.println(i.getIRI().toString());
}
}
// After you are done with the query, you should remove the
// definition
manager.removeAxiom(ontology, definition);
reasoner.dispose();
} catch (OWLOntologyCreationException | OWLOntologyStorageException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} 2 该查询查找位置为“1”的人员。 System.out.println 应该显示单个“Mary”,但没有结果。有人可以告诉我缺少什么吗? 我想我所做的一切都是根据http://code.google.com/p/elk-reasoner/wiki/QueryingComplexClasses 。代码几乎是1:1。
干杯,
最佳答案
您正在手动创建推理器,而不是通过其工厂。这可能会使其不监听本体的更新,因此对reasoner.flush()的调用可能不起作用。
尝试使用 Reasoner.ReasonerFactory
创建实例,看看这是否可以解决您的问题。
另一种可能性是代码中的 IRI 与本体中的 IRI 不匹配。通过打印您的 IRI 和 ontology.getSignature()
中的 IRI 来确保它们这样做。
编辑:第二种可能性已得到确认。我在测试中复制了您的本体片段和代码。本体中的类 IRI 是:
<http://www.semanticweb.org/sabse/ontologies/2013/11/untitled-ontology-17#Person>
<http://www.semanticweb.org/sabse/ontologies/2013/11/untitled-ontology-17#PersonWithPosition1>
代码中的 Person 类具有 IRI:
<#Person>
不匹配是由前缀管理器将 "#"
作为默认命名空间引起的。这不是本体的默认命名空间。
关于java - HermiT Resoner 与 Protege OWL API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20590185/