结合ONT-API和SPARQL查询使用Hermit
我正在使用OWL-API来加载OWL本体,并使用SWRL规则来创建OWL本体 我用以下代码加载了一个本体:结合ONT-API和SPARQL查询使用Hermit,sparql,jena,owl-api,hermit,Sparql,Jena,Owl Api,Hermit,我正在使用OWL-API来加载OWL本体,并使用SWRL规则来创建OWL本体 我用以下代码加载了一个本体: IRI iri = IRI.create(BASE_URL); OntologyManager manager = OntManagers.createManager(); // Load an ontology Ontology ontologyWithRules = manager.loadOntologyFromOntologyDocument(iri); 然后我举例说明一个隐士推
IRI iri = IRI.create(BASE_URL);
OntologyManager manager = OntManagers.createManager();
// Load an ontology
Ontology ontologyWithRules = manager.loadOntologyFromOntologyDocument(iri);
然后我举例说明一个隐士推理者:
import org.semanticweb.HermiT.ReasonerFactory;
OWLReasonerFactory reasonerFactory = new ReasonerFactory();
OWLReasoner reasoner = reasonerFactory.createReasoner(ontologyWithRules);
最后,我想查询这个模型:
try (
QueryExecution qexec = QueryExecutionFactory.create(QueryFactory
.create("SELECT ?s ?p ?o WHERE { ?s ?p ?o }"), ontologyWithRules.asGraphModel())
) {
ResultSet res = qexec.execSelect();
while (res.hasNext()) {
System.out.println(res.next());
}
}
然而,没有使用推理机。是否有一种方法可以在reasoner打开的情况下在图形上使用SPARQL查询?在模型上运行推断是缺少的步骤。因此,我需要使用
InferredOntologyGenerator
类
一行代码可以说上千字:
/**
* Important imports:
* import org.semanticweb.HermiT.ReasonerFactory;
* import org.semanticweb.owlapi.util.InferredOntologyGenerator;
**/
IRI iri = IRI.create(BASE_URL);
OntologyManager manager = OntManagers.createManager();
// Load an ontology
Ontology ontologyWithRules = manager.loadOntologyFromOntologyDocument(iri);
// Instantiate a Hermit reasoner:
OWLReasonerFactory reasonerFactory = new ReasonerFactory();
OWLReasoner reasoner = reasonerFactory.createReasoner(ontologyWithRules);
OWLDataFactory df = manager.getOWLDataFactory();
// Create an inference generator over Hermit reasoner
InferredOntologyGenerator inference = new InferredOntologyGenerator(reasoner);
// Infer
inference.fillOntology(df, ontologyWithRules);
// Query
try (
QueryExecution qexec = QueryExecutionFactory.create(QueryFactory
.create("SELECT ?s ?p ?o WHERE { ?s ?p ?o } "), ontologyWithRules.asGraphModel())
) {
ResultSet res = qexec.execSelect();
while (res.hasNext()) {
System.out.println(res.next());
}
}
这与OWL-API无关,但是ONT-API-OntAPI实现了OWLAPI,但是这些类和方法是OWLAPI之外的扩展。这里应该发生什么?我的意思是,对于推理,你必须使用推理机,而不是基本本体。当然,您已将本体加载到推理器中,但分配不是相反,即本体不使用推理器,因此,
ontologyWithRules.asGraphModel()
仍然是普通本体。在Jena中,这通常以InfModel
作为参数。不确定Ont-API是如何提供一个由推理支持的模型实现的。@Ignazio,我认为这个问题与OWLAPI+Hermit明确相关,但与Ont-API仅隐式相关。似乎@marcelo machado在调用方法createReasoner
后期望本体发生一些变化,但自然不会收到这些变化。这是对隐士的不当使用@UniformedUser对于推理,ONT-API没有自己的推理专用支持,但显然,此功能必须以两种方式工作-通过外部OWLReasoner
和通过InfModel
。最后一种方法有点复杂,默认情况下有GraphMem
,而不是InfGraph
@UninformedUser您是对的,缺少一步,这一步我不知道。然而,我想我已经用类推理生物学生成器
解决了这个问题。我会测试,如果它有效,我会回答这个问题。