owlcs / owlapi

OWL API main repository
821 stars 315 forks source link

OWL API not handing cyclic imports #871

Open noorbakerally opened 5 years ago

noorbakerally commented 5 years ago

I am trying to load an ontology (file is here) with OWL API(5.1.10). The ontology has imports and the imported ontology imports the original ontology. Consequently this is generating errors when loading the original ontology. The codes for loading the original ontology and errors are below. I believe that the error is mostly because of trying to load twice the same ontology due to the cyclic imports. Can OWL API support for this ?

Codes for loading original ontology

public static OWLOntology getOWLOntology(String ontologyIRI){
        OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
        OWLOntology ontology = null;
        try {

            OWLOntologyLoaderConfiguration config = new OWLOntologyLoaderConfiguration();
            config = config.setMissingImportHandlingStrategy(MissingImportHandlingStrategy.SILENT);
            manager.setOntologyLoaderConfiguration(config);
            ontology = manager.loadOntology(
                    IRI.create(ontologyIRI));

        } catch (OWLOntologyCreationException e) {
            e.printStackTrace();
        }
        return ontology;
    }

Error


org.semanticweb.owlapi.model.OWLOntologyAlreadyExistsException: Ontology already exists. OntologyID(OntologyIRI(<http://www.opengis.net/ont/geosparql>) VersionIRI(<null>))
    at uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.load(OWLOntologyManagerImpl.java:1122)
    at uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.loadOntology(OWLOntologyManagerImpl.java:1057)
    at uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.loadOntology(OWLOntologyManagerImpl.java:957)
    at uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.loadOntology(OWLOntologyManagerImpl.java:913)
    at ontology.OntologyUtils.getStaticOWLOntology(OntologyUtils.java:482)
    at ontology.Ontology.<init>(Ontology.java:51)
    at ontology.OntologyUtils.saveOntologyToDatabase(OntologyUtils.java:622)
    at examples.ThingGetOntologies.main(ThingGetOntologies.java:29)
Caused by: org.semanticweb.owlapi.model.OWLOntologyRenameException: Could not rename ontology. An ontology with this ID already exists: OntologyID(OntologyIRI(<http://www.opengis.net/ont/geosparql>) VersionIRI(<null>))
    at uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.checkForOntologyIDChange(OWLOntologyManagerImpl.java:744)
    at uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.enactChangeApplication(OWLOntologyManagerImpl.java:597)
    at uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.actuallyApply(OWLOntologyManagerImpl.java:645)
    at uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.applyChangesAndGetDetails(OWLOntologyManagerImpl.java:614)
    at org.semanticweb.owlapi.model.HasApplyChanges.applyChanges(HasApplyChanges.java:37)
    at uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.applyChange(OWLOntologyManagerImpl.java:693)
    at org.semanticweb.owlapi.model.OWLOntology.applyChange(OWLOntology.java:92)
    at uk.ac.manchester.cs.owl.owlapi.concurrent.ConcurrentOWLOntologyImpl.lambda$applyChange$174(ConcurrentOWLOntologyImpl.java:1516)
    at uk.ac.manchester.cs.owl.owlapi.concurrent.ConcurrentOWLOntologyImpl.withWriteLock(ConcurrentOWLOntologyImpl.java:161)
    at uk.ac.manchester.cs.owl.owlapi.concurrent.ConcurrentOWLOntologyImpl.applyChange(ConcurrentOWLOntologyImpl.java:1516)
    at org.semanticweb.owlapi.rdf.rdfxml.parser.OWLRDFConsumer.chooseAndSetOntologyIRI(OWLRDFConsumer.java:1490)
    at org.semanticweb.owlapi.rdf.rdfxml.parser.OWLRDFConsumer.endModel(OWLRDFConsumer.java:1406)
    at org.semanticweb.owlapi.rdf.rdfxml.parser.RDFParser.parse(RDFParser.java:159)
    at org.semanticweb.owlapi.rdf.rdfxml.parser.RDFXMLParser.parse(RDFXMLParser.java:60)
    at uk.ac.manchester.cs.owl.owlapi.OWLOntologyFactoryImpl.loadOWLOntology(OWLOntologyFactoryImpl.java:194)
    at uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.load(OWLOntologyManagerImpl.java:1107)
    ... 7 more
Error:/home/noor/Documents/repositories/gitlab/ThingInOntologies/ontologies/35.owl
java.lang.NullPointerException
    at org.semanticweb.HermiT.Reasoner.<init>(Reasoner.java:196)
    at org.semanticweb.HermiT.Reasoner.<init>(Reasoner.java:179)
    at org.semanticweb.HermiT.ReasonerFactory.createHermiTOWLReasoner(ReasonerFactory.java:51)
    at org.semanticweb.HermiT.ReasonerFactory.createReasoner(ReasonerFactory.java:19)
    at org.semanticweb.HermiT.ReasonerFactory.createReasoner(ReasonerFactory.java:15)
    at ontology.Ontology.getReasoner(Ontology.java:265)
    at ontology.Ontology.<init>(Ontology.java:59)
    at ontology.OntologyUtils.saveOntologyToDatabase(OntologyUtils.java:622)
    at examples.ThingGetOntologies.main(ThingGetOntologies.java:29)

Process finished with exit code 0
ignazio1977 commented 5 years ago

This is likely two errors: one is a known bug with cycling imports, and the other is missing declarations in one of the ontologies.

As a workaround, try loading the ontology that is both imported by your root ontology and imports the root ontology - the imports closure for the reasoner will be the same but the loading sequence might work better.

noorbakerally commented 5 years ago

For the second error, what declaration could be missing ?

ignazio1977 commented 5 years ago

Can't tell, sorry.

noorbakerally commented 5 years ago

I'm seeing that an addOntologyLoaderListener can be added to the manager. Using this, the IRI of ontology that is about to be imported can be know. I was thinking, if there anyway to stop loading that important if it was already loaded.

manager.addOntologyLoaderListener(new OWLOntologyLoaderListener() {
            @Override
            public void startedLoadingOntology(LoadingStartedEvent loadingStartedEvent) {
                String iri = loadingStartedEvent.getOntologyID().getOntologyIRI().get().getIRIString();
                //STOP THE LOADING HERE if IRI is the original ontology or already imported
            }

            @Override
            public void finishedLoadingOntology(LoadingFinishedEvent loadingFinishedEvent) {

            }
        });
vChavezB commented 9 months ago

I am not sure if this is still an issue. I tried opening the ontology provided from OP and it loads correctly in protege 5.5 and 5.6