简体   繁体   中英

Explanations for Inconsistency in OWL 2 ontology in Java

I am trying to find out whether an ontology is inconsistent or not, and if it is inconsistent then to print which are the classes/ axioms which cause the inconsistency. I am getting the correct result regarding the inconsistency check, however I am stuck while printing the list of axioms behind the inconsistency. I tried an approach mentioned in a question of stackoverflow, but it is not working.

I checked and the problem is that no list of explanations are being stored in explanations variable. Can you point out where I am going wrong.

    package com.tcs.HermiT;

import java.io.File;
import java.util.Set;





import org.semanticweb.HermiT.Configuration;
import org.semanticweb.HermiT.Reasoner;
import org.semanticweb.HermiT.Reasoner.ReasonerFactory;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLAxiom;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLNamedIndividual;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.semanticweb.owlapi.reasoner.InconsistentOntologyException;
import org.semanticweb.owlapi.reasoner.OWLReasoner;
import org.semanticweb.owlapi.reasoner.OWLReasonerFactory;

import com.clarkparsia.owlapi.explanation.BlackBoxExplanation;
import com.clarkparsia.owlapi.explanation.ExplanationGenerator;
import com.clarkparsia.owlapi.explanation.HSTExplanationGenerator;

public class Demo {

    public void reason() throws OWLOntologyCreationException {
        // First, we create an OWLOntologyManager object. The manager will load and save ontologies.
        OWLOntologyManager m = OWLManager.createOWLOntologyManager();
        OWLDataFactory dataFactory=m.getOWLDataFactory();

        File inputOntologyFile = new File("C:\\Users\\1047785\\Desktop\\HermiT\\Input10.owl");
        OWLOntology o=m.loadOntologyFromOntologyDocument(inputOntologyFile);// Now, we instantiate HermiT by creating an instance of the Reasoner class in the package org.semanticweb.HermiT.



        ReasonerFactory factory = new ReasonerFactory();
        Configuration configuration=new Configuration();
        configuration.throwInconsistentOntologyException = false;
        OWLReasoner reasoner=factory.createReasoner(o, configuration);

        System.out.println("Consistency : "+reasoner.isConsistent());
        System.out.println("Computing explanations for the inconsistency...");
        factory=new Reasoner.ReasonerFactory() {
            protected OWLReasoner createHermiTOWLReasoner(org.semanticweb.HermiT.Configuration configuration,OWLOntology o) {
                // don't throw an exception since otherwise we cannot compte explanations 
                configuration.throwInconsistentOntologyException=false;
                return new Reasoner(configuration,o);
            }  
        };


        BlackBoxExplanation exp=new BlackBoxExplanation(o, factory, reasoner);
        HSTExplanationGenerator multExplanator=new HSTExplanationGenerator(exp);
        // Now we can get explanations for the inconsistency 
        Set<Set<OWLAxiom>> explanations=multExplanator.getExplanations(dataFactory.getOWLThing());

        // Let us print them. Each explanation is one possible set of axioms that cause the 
        // unsatisfiability. 
        for (Set<OWLAxiom> explanation : explanations) {
            System.out.println("------------------");
            System.out.println("Axioms causing the inconsistency: ");
            for (OWLAxiom causingAxiom : explanation) {
                System.out.println(causingAxiom);
            }
            System.out.println("------------------");
        }


    }
}

The output is coming as

consistency : true/false(correct result)

Computing explanations for inconsistency

The explanation classes in owl api are ancient and not very efficient. There are replacements in the owlexplanation project maintained by Matthew Horridge on GitHub. You can try adapting the code to use them. I don't have an example handy unfortunately.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM