简体   繁体   中英

OWLOntologyManager trying to load multiple OWLDocumentSources

I am trying to load multiple ttl files with the OWLOntologyManager by documentSource, and then reasoning over the imports closure with the main Ontology. This was done to try and mirror Protege's method of having a base Ontology that imports ontologies on its own.

How I attempt this, works in the following manner. I connect to the web repo and download the ttl file at its location, these files are then read and the documentIRI is found whithin. The documentIRI as well as the inputstream are both added to the OWLDOcumentSource and given to the manager to load.

The idea is to load over all the ontologies whithout following imports and then to reason over the base ontology and reasoning over the import closure.

The first problem is that even with:



manager.getOntologyLoaderConfiguration().setMissingImportHandlingStrategy(MissingImportHandlingStrategy.SILENT);

the imports are still being loaded and the following error message is received:

An error occurred creating the Configuration(s): org.semanticweb.owlapi.model.OWLOntologyFactoryNotFoundException: Could not find an appropriate factory to load ontology from ontology document: <sm:sentx.sol/config/site.ttl>"

How do I solve this problem? Is it a missing Factory that I need to implement?

@UninformedUser has pointed out that the configuration object needs to be assigned back to the manager, eg,

OWLOntologyLoaderConfiguration config = manager.getOntologyLoaderConfiguration();
config = config.setMissingImportHandlingStrategy(MissingImportHandlingStrategy.SILENT);
manager.setOntologyLoaderConfiguration(config);

However, for your use case, the missing import strategy is not relevant. If your ontology imports can be resolved as URLs, setting the missing import strategy will not have any effects - the imports are not missing. And if they cannot be resolved as URLs, all that will happen is that no exceptions will be thrown, but the imports will not be loaded, and therefore you won't be able to replicate Protege's behaviour.

What I would suggest is to put all ontology files you wish to load in the same folder and use an AutoIRIMapper instance pointed at the folder; what this does is to provide a mapping strategy that prefers local files over remote URLs.

This might not be sufficient for your needs - for a more granular strategy (for example, if you want just one of the imports to be resolved locally) you can use SimpleIRIMapper instead.

See this file for examples of how to use these classes: https://github.com/owlcs/owlapi/blob/version5/contract/src/test/java/uk/ac/manchester/owl/owlapi/tutorialowled2011/TutorialSnippetsTestCase.java

@Test
void testIRIMapper() throws OWLOntologyCreationException, OWLOntologyStorageException {
    OWLOntologyManager m = OWLManager.createOWLOntologyManager();
    // map the ontology IRI to a physical IRI (files for example)
    // Create the document IRI for our ontology
    File output = new File(temporaryFolder, "saved_pizza.owl");
    // Output will be deleted on exit; to keep temporary file replace
    // previous line with the following
    // File output = File.createTempFile("saved_pizza", ".owl");
    IRI documentIRI = IRI.create(output);
    // Set up a mapping, which maps the ontology to the document IRI
    SimpleIRIMapper mapper = new SimpleIRIMapper(EXAMPLE_SAVE_IRI, documentIRI);
    m.getIRIMappers().add(mapper);
    // set up a mapper to read local copies of ontologies
    File localFolder = new File("materializedOntologies");
    // the manager will look up an ontology IRI by checking
    // localFolder first for a local copy, checking its subfolders as well
    m.getIRIMappers().add(new AutoIRIMapper(localFolder, true));
    // Create the ontology - we use the ontology IRI (not the physical URI)
    OWLOntology o = m.createOntology(EXAMPLE_SAVE_IRI);
    // save the ontology to its physical location - documentIRI
    o.saveOntology();
}

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