【问题标题】:Using Hermit with ONT-API and SPARQL Query使用 Hermit 和 ONT-API 和 SPARQL 查询
【发布时间】:2021-04-26 19:41:04
【问题描述】:

我正在使用 OWL-API 通过 SWRL 规则加载和 owl 本体。

我用以下代码加载了一个本体:

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());
    }
}

但是没有使用推理器。有没有办法在打开推理器的情况下对图使用 SPARQL 查询?

【问题讨论】:

  • 这与 OWL-API 无关,而是 ONT-API - OntAPI 实现 OWLAPI,但这些类和方法是 OWLAPI 之外的扩展。
  • 这里应该发生什么?我的意思是,对于推理,您必须使用推理器,而不是基础本体。当然,您将本体加载到推理器中,但分配并非相反,即本体不使用推理器,因此,ontologyWithRules.asGraphModel() 仍然是普通本体。在耶拿,这通常需要InfModel 作为参数。不确定 Ont-API 如何设法提供由推理支持的 Model 实现。
  • @Ignazio,我认为这个问题与 OWLAPI+Hermit 明确相关,但仅与 ONT-API 隐式相关。似乎@marcelo-machado 在调用createReasoner 方法后期望本体发生一些变化,但自然不会收到它们。这是对隐士的不当用法。 @UniformedUser 至于推理,ONT-API 没有自己的推理支持,但显然,此功能必须以两种方式工作 - 通过外部 OWLReasoner 和通过 InfModel。最后一种方式有点复杂,默认是GraphMem,而不是InfGraph
  • @UninformedUser 你说得对,少了一个步骤,而那个步骤是我不知道的。但是,我想我刚刚使用 InferredOntologyGenerator 类解决了这个问题。我会测试,如果它有效,我会回答这个问题。

标签: sparql jena owl-api hermit


【解决方案1】:

对模型进行推理是缺少的步骤。因此,我需要使用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());
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-03
    • 1970-01-01
    • 2013-04-19
    • 1970-01-01
    相关资源
    最近更新 更多