Search code examples
owlprotegeswrlpellet

How to process SWRL rules using Openllet and OWL api?


I've been trying to set up a query printer a lot like this: https://github.com/owlcs/owlapi/wiki/DL-Queries-with-a-real-reasoner where if I give it a query in Manchester syntax I can get a response that mirrors what I would get back in protege. I swapped out the Hermit reasoner with Openllet and it can't seem to retrieve any of the individuals anymore.

For reasons, I would like to stay away from Jena if possible.

OntController.java

public class OntController {
//declared variables here
    public OntController(String name) throws OWLOntologyCreationException, OWLOntologyStorageException, IOException{
        //initialized a bunch of other variables here
        manager = OWLManager.createOWLOntologyManager();
        reasonFactory = new OpenlletReasonerFactory();
    }
    public void reason(){
        reasoner = reasonFactory.createReasoner(ont);
        reasoner.precomputeInferences(InferenceType.OBJECT_PROPERTY_HIERARCHY, InferenceType.OBJECT_PROPERTY_ASSERTIONS);
    }

    public void infer(){
        reasoner.precomputeInferences();
        List<InferredAxiomGenerator<? extends OWLAxiom>> gens = new ArrayList<InferredAxiomGenerator<? extends OWLAxiom>>();
        gens.add(new InferredSubClassAxiomGenerator());
        InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner, gens);
        iog.fillOntology(manager.getOWLDataFactory(), ont);
    }

    public void query() throws IOException{
        reasoner = reasonFactory.createReasoner(ont);
        ShortFormProvider shortFormProvider = new SimpleShortFormProvider();
        DLQueryPrinter dlQueryPrinter = new DLQueryPrinter(new DLQueryEngine(reasoner,
            shortFormProvider), shortFormProvider);
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));
        while (true) {
            System.out.println("Type a class expression in Manchester Syntax and press Enter (or press q to exit):");
            //blah blah
        }
        dlQueryPrinter.askQuery(classExpression.trim());
        System.out.println();
    }

    //more unrelated methods here
}

DLQueryEngine

import java.util.Collections;
import java.util.Set;

import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLClassExpression;
import org.semanticweb.owlapi.model.OWLNamedIndividual;
import org.semanticweb.owlapi.reasoner.Node;
import org.semanticweb.owlapi.reasoner.NodeSet;
//import org.semanticweb.owlapi.reasoner.OWLReasoner;
import org.semanticweb.owlapi.util.ShortFormProvider;

import openllet.owlapi.OpenlletReasoner;

class DLQueryEngine {
    private final OpenlletReasoner reasoner;
    private final DLQueryParser parser;

    public DLQueryEngine(OpenlletReasoner reasoner, ShortFormProvider shortFormProvider) {
        this.reasoner = reasoner;
        parser = new DLQueryParser(reasoner.getRootOntology(), shortFormProvider);
    }

public Set<OWLClass> getSuperClasses(String classExpressionString, boolean direct) {
    if (classExpressionString.trim().length() == 0) {
        return Collections.emptySet();
    }
    OWLClassExpression classExpression = parser
            .parseClassExpression(classExpressionString);
    NodeSet<OWLClass> superClasses = reasoner
            .getSuperClasses(classExpression, direct);
    return superClasses.getFlattened();
}

public Set<OWLClass> getEquivalentClasses(String classExpressionString) {
    if (classExpressionString.trim().length() == 0) {
        return Collections.emptySet();
    }
    OWLClassExpression classExpression = parser
            .parseClassExpression(classExpressionString);
    Node<OWLClass> equivalentClasses = reasoner.getEquivalentClasses(classExpression);
    Set<OWLClass> result = null;
    if (classExpression.isAnonymous()) {
        result = equivalentClasses.getEntities();
    } else {
        result = equivalentClasses.getEntitiesMinus(classExpression.asOWLClass());
    }
    return result;
    }

public Set<OWLClass> getSubClasses(String classExpressionString, boolean direct) {
    if (classExpressionString.trim().length() == 0) {
        return Collections.emptySet();
    }
    OWLClassExpression classExpression = parser
            .parseClassExpression(classExpressionString);
    NodeSet<OWLClass> subClasses = reasoner.getSubClasses(classExpression, direct);
    return subClasses.getFlattened();
    }

public Set<OWLNamedIndividual> getInstances(String classExpressionString,
        boolean direct) {
    if (classExpressionString.trim().length() == 0) {
        return Collections.emptySet();
    }
    OWLClassExpression classExpression = parser
            .parseClassExpression(classExpressionString);
    NodeSet<OWLNamedIndividual> individuals = reasoner.getInstances(classExpression,
            direct);
    return individuals.getFlattened();
    }
}

Solution

  • It seems that the problem is with the code that I had linked. In the Query Printer class they set

    Set<OWLNamedIndividual> individuals = dlQueryEngine.getInstances(
                            classExpression, true);
    

    the boolean should be false if you want individuals to appear and that really threw me off.