javaowl-apihermit

OWLApi: Failed to create a hermit reasoner instance


Good morning, I'm trying to write a java class that could manage an ontology, in particular I want to find all the properties (name, subclasses, superclasses, ecc..) of a single OWLClass, but i don't manage to instance Hermit and I can't understand why. I'm using Eclipse with Maven project, dependencies written in the pom file are copied from the hermit examples, same is for OWLapi dependencies.

The error in the java console is:

Exception in thread "main" java.lang.NullPointerException
    at org.semanticweb.HermiT.Reasoner.<init>(Reasoner.java:210)
    at org.semanticweb.HermiT.Reasoner.<init>(Reasoner.java:187)
    at OntologyManager.retrieve_property_class(OntologyManager.java:105)

In particular, the line that fail to execute is:

Reasoner hermit = new Reasoner(null, ontologia);

This is the code from the method that is not working, I already checked the hermit documentation and various example, but didn't help.

public void retrieve_property_class(OWLOntology ontology) {
        //creo il reasoner per svolgere recupero informazioni
        Reasoner hermit = new Reasoner(null, ontologia);

        //richiesta del nome della classe di cui si vogliono le proprietà
        String classe;
        System.out.print("Inserire il nome della classe di cui si vogliono le proprietà: ");
        classe = scannerNome.nextLine();

        //check per vedere se la classe esiste
        Set<OWLClass> classi = ontology.getClassesInSignature();
        for (OWLClass e : classi) { //scorro le classe OWL del set comparandone l'IRI con il nome della classe desiderata
            if(e.getIRI().getFragment().equals(classe)) { //se una classe soddisfa l'uguaglianza ne vengono stampate le proprietà
                System.out.println("Le informazioni della classe sono le seguenti: ");
                //nome classe
                System.out.println("Nome classe: \n"
                        + "\t"+ classe);
                //display superclassi
                System.out.println("Superclassi:");
                for(Node<OWLClass> superclasse: hermit.getSuperClasses(e)) {
                    System.out.println("\t"+ superclasse.getRepresentativeElement().getIRI().getFragment());
                }
                //display sottoclassi
                System.out.println("Sottoclassi:");
                for(Node<OWLClass> sottoclasse : hermit.getSubClasses(e)) {
                    System.out.println("\t"+ sottoclasse.getRepresentativeElement().getIRI().getFragment());
                }
                //display classi disgiunte
                System.out.println("Classi disgiunte:");
                for(Node<OWLClass> disgiunta : hermit.getDisjointClasses(e)) {
                    System.out.println("\t"+ disgiunta.getRepresentativeElement().getIRI().getFragment());
                }
                //display istanze della classe
                System.out.println("Istanze:");
                for(Node<OWLNamedIndividual> individuo : hermit.getInstances(e)) {
                    System.out.println("\t"+ individuo);
                }
            }
        }
        hermit.dispose();
    }

Solution

  • You have to use a ReasonerFactory, i.e. for HermiT you have to import the following:

    import org.semanticweb.owlapi.reasoner.OWLReasoner;
    import org.semanticweb.owlapi.reasoner.OWLReasonerFactory;
    import org.semanticweb.HermiT.ReasonerFactory;
    

    The the following code will create a HermiT reasoner.

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

    Your pom.xml needs to include:

    <dependency>
        <groupId>net.sourceforge.owlapi</groupId>
        <artifactId>owlapi-distribution</artifactId>
        <version>5.1.8</version>
    </dependency>
    
    <dependency>
        <groupId>net.sourceforge.owlapi</groupId>
        <artifactId>org.semanticweb.hermit</artifactId>
        <version>1.4.3.517</version>
    </dependency>