Add an import directive to an ontology - owlcs/owlapi GitHub Wiki

An import directive is an annotation on an OWLOntology, using owl:imports as annotation property.

Import directives, or OWLImportsDeclaration, can be added and removed through the AddImport/RemoveImport change classes.

Imports declarations are only resolved automatically during ontology loading; to load an import after the main ontology has been loaded, the imported ontology must be loaded in the manager manually.

OWLOntologyManager m=...
OWLOntology o=... (m is the manager for o)
OWLImportsDeclaration importDeclaration=m.getOWLDataFactory().getOWLImportsDeclaration(IRI.create("http://domain.for.import.ontology/importedontology"));
m.applyChange(new AddImport(o, importDeclaration));
// Save the ontology at this point

This is all that's needed to add the import to the ontology; if you wish to start using the import right away, without reloading the ontology, you need to load the import manually in the manager (import declarations are only resolved at loading time, to avoid having to download the imported ontology only to save the change to the ontology).

Manual loading can be done by calling:

m.loadOntology(IRI.create("http://domain.for.import.ontology/importedontology"));

If the imported ontology is not available online, but only locally, this command must be preceded by the setting of an adequate IRI mapper, which will redirect the remote IRI to a local, file IRI.