1

I am using the OWL-API to load and owl ontology with SWRL rules.

I loaded an ontology with the following code:

IRI iri = IRI.create(BASE_URL);
OntologyManager manager = OntManagers.createManager();
// Load an ontology
Ontology ontologyWithRules = manager.loadOntologyFromOntologyDocument(iri);

Then I instantiate a Hermit reasoner:

import org.semanticweb.HermiT.ReasonerFactory;

OWLReasonerFactory reasonerFactory = new ReasonerFactory();
OWLReasoner reasoner = reasonerFactory.createReasoner(ontologyWithRules);

Finally, I'd like to query this model:

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

However the reasoner wasn't used. Is there a way to use the SPARQL query over the graph with the reasoner turned on?

smitop
  • 4,770
  • 2
  • 20
  • 53
Marcelo Machado
  • 1,179
  • 2
  • 13
  • 33
  • 2
    This is not OWL-API related but ONT-API - OntAPI implements OWLAPI but these classes and methods are extensions outside OWLAPI. – Ignazio Apr 26 '21 at 21:31
  • what should happen here? I mean, for inference you have to use the reasoner, not the base ontology. Sure, you loaded the ontology into the reasoner, but the assignment is not vice versa, i.e. the ontology doesn't make use of the reasoner, thus, `ontologyWithRules.asGraphModel()` is still the plain ontology. In Jena this would take usually an `InfModel` as argument. Not sure how Ont-API does manage to provide a `Model` implementation backed by the inference. – UninformedUser Apr 27 '21 at 10:39
  • 2
    @Ignazio, I think this question is explicitly related to OWLAPI+Hermit, but to ONT-API only implicitly. It seems @marcelo-machado expects some changes in the ontology after calling the method `createReasoner`, but naturally does not receive them. This is an inappropriate usage of Hermit. @UniformedUser as for the inferencing, ONT-API has no its own dedicated support of inferencing, but, obviously, this functionality must work in both ways - via external `OWLReasoner` and via `InfModel`. The last way is bit sophisticated, by default there is `GraphMem`, not `InfGraph`. – ssz Apr 27 '21 at 13:55
  • @UninformedUser You are right, there is a missing step, and that step is what I don't know. However, I think I have just solved the problem using the class `InferredOntologyGenerator`. I will test and if it works I answer this question. – Marcelo Machado Apr 27 '21 at 14:03

1 Answers1

0

Running inferences over the model was the missing step. Therefore, I needed to use the InferredOntologyGenerator class.

One line of code speaks more than a thousand words:

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

user0221441
  • 368
  • 4
  • 11
Marcelo Machado
  • 1,179
  • 2
  • 13
  • 33