owlcs / ont-api

ONT-API (OWL-API over Apache Jena)
44 stars 5 forks source link

Is it possible to run Hermit reasoner when running a SPARQL query? #32

Closed marcelomachado closed 3 years ago

marcelomachado commented 3 years ago

I loaded an ontology with SWRL rules:

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

Then I instantiate a Hermit reasoner:

import org.semanticweb.HermiT.ReasonerFactory;

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

Finally, I query:

try (
    QueryExecution qexec = QueryExecutionFactory.create(QueryFactory
        .create("SELECT ?s ?p ?o WHERE { ?s ?p ?o }"), mergedOntology.asGraphModel())
) {
    ResultSet res = qexec.execSelect();
    while (res.hasNext()) {
        System.out.println(res.next());
    }
}

However the reasoner wasn't used. Is there a way to do what I want?

sszuev commented 3 years ago

Yes, that must be possible. But your example is missing the most important part: constructing and inferring mergedOntology.

(the example of inferring: new InferredOntologyGenerator(reasoner).fillOntology(df, ont)).

In general speaking, ONT-API+Hermit must behave same as OWLAPI+Hermit (since Hermit works over OWLAPI interfaces and know nothing about RDF-support in ONTAPI). Here, in the example, the only thing which should make them differnt is asGraphModel functionality. So the question is do you see difference between those behavior (ONT-API+Hermit vs OWLAPI+Hermit)? To check, you can traverse over axioms using OWLAPI interface. If mergedOntology was not changed during the process of reasoning, then, perhaps, the problem is in configuration, and that should be repeatable for default OWLAPI-impl too, then you could ask OWLAPI or Hermit authors (they are more competent in such questions).

marcelomachado commented 3 years ago

@sszuev Thank you very much for your answer, that was exactly what was missing.

The final code:

/** 
* 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 mergedOntology = manager.loadOntologyFromOntologyDocument(iri);

// Then I instantiate a Hermit reasoner:
OWLReasonerFactory reasonerFactory = new ReasonerFactory();
OWLReasoner reasoner = reasonerFactory.createReasoner(mergedOntology);

OWLDataFactory df = manager.getOWLDataFactory();
InferredOntologyGenerator inference = new  InferredOntologyGenerator(reasoner);

// Infer
inference.fillOntology(df, mergedOntology);

// Query
try (
    QueryExecution qexec = QueryExecutionFactory.create(QueryFactory
        .create("SELECT ?s ?p ?o WHERE {  ?s ?p ?o } "), mergedOntology.asGraphModel())
) {
    ResultSet res = qexec.execSelect();
    while (res.hasNext()) {
        System.out.println(res.next());
    }
}