use of org.semanticweb.owlapi.model.UnknownOWLOntologyException in project stanbol by apache.
the class ODPRegistryCacheManager method retrieveRemoteResource.
/**
* Gets the remote ontology and saves it locally
*
* @param uri
* @return
* @throws OWLOntologyCreationException
* @throws UnknownOWLOntologyException
* @throws OWLOntologyStorageException
*/
private static synchronized OWLOntology retrieveRemoteResource(URI uri) throws OWLOntologyCreationException, UnknownOWLOntologyException, OWLOntologyStorageException {
manager.setSilentMissingImportsHandling(true);
manager.addMissingImportListener(new MissingImportListener() {
public void importMissing(MissingImportEvent arg0) {
if (!getUnresolvedURIs().contains(arg0.getImportedOntologyURI()))
getUnresolvedURIs().add(arg0.getImportedOntologyURI().toURI());
}
});
manager.addOntologyLoaderListener(new OWLOntologyLoaderListener() {
@Override
public void startedLoadingOntology(LoadingStartedEvent event) {
// Nothing to do
}
@Override
public void finishedLoadingOntology(LoadingFinishedEvent event) {
URI onturi = event.getDocumentIRI().toURI();
if (event.getException() != null) {
getUnresolvedURIs().add(onturi);
LoggerFactory.getLogger(ODPRegistryCacheManager.class).warn("Failed to resolve ontology at " + onturi + " . Skipping.", event.getException());
return;
}
try {
if (!uris.containsKey(onturi)) {
cacheOntology(onturi, newFile(), manager.getOntology(event.getOntologyID()));
}
} catch (UnknownOWLOntologyException e) {
LoggerFactory.getLogger(ODPRegistryCacheManager.class).warn("Failed to cache ontology at " + onturi + " . Skipping.", e);
getUnresolvedURIs().add(onturi);
} catch (OWLOntologyStorageException e) {
LoggerFactory.getLogger(ODPRegistryCacheManager.class).warn("Failed to cache ontology at " + onturi + " . Skipping.", e);
getUnresolvedURIs().add(onturi);
}
}
});
OWLOntology ont;
try {
ont = manager.loadOntologyFromOntologyDocument(IRI.create(uri));
} catch (OWLOntologyAlreadyExistsException e) {
ont = manager.getOntology(e.getOntologyID().getOntologyIRI());
}
File file = newFile();
cacheOntology(uri, file, ont);
return ont;
}
use of org.semanticweb.owlapi.model.UnknownOWLOntologyException in project stanbol by apache.
the class OntologyImportUtils method buildImportTree.
/**
* Non-recursively adds import statements to the root ontology so that it is directly linked to all the
* ontologies in the subtrees set.
*
* @param parent
* the ontology to which import subtrees should be appended. If null, a runtime exception will
* be thrown.
* @param subtrees
* the set of target ontologies for import statements. These can in turn be importing other
* ontologies, hence the "subtree" notation. A single statement will be added for
* each member of this set.
* @param mgr
* the OWL ontology manager to use for constructing the import tree. If null, an internal one
* will be used instead, otherwise an existing ontology manager can be used e.g. for extracting
* import statements from its IRI mappers or known ontologies. Note that the supplied manager
* will <i>never</i> try to load any ontologies, even when they are unknown.
* @return the same input ontology as defined in <code>root</code>, but with the added import statements.
*/
public static OWLOntology buildImportTree(OWLOntology parent, Set<OWLOntology> subtrees, OWLOntologyManager mgr) {
if (parent == null)
throw new NullPointerException("Cannot append import trees to a nonexistent ontology.");
// If no manager was supplied, use a temporary one.
if (mgr == null)
mgr = OWLManager.createOWLOntologyManager();
OWLDataFactory owlFactory = mgr.getOWLDataFactory();
List<OWLOntologyChange> changes = new LinkedList<OWLOntologyChange>();
for (OWLOntology o : subtrees) {
IRI importIri = null;
try {
/*
* First query the manager, as it could know the physical location of anonymous ontologies, if
* previously loaded or IRI-mapped.
*/
importIri = mgr.getOntologyDocumentIRI(o);
} catch (UnknownOWLOntologyException ex) {
/*
* Otherwise, ask the ontology itself (the location of an anonymous ontology may have been
* known at creation/loading time, even if another manager built it.)
*/
importIri = o.getOntologyID().getDefaultDocumentIRI();
} catch (Exception ex) {
logger.error("Exception caught during tree building. Skipping import of ontology " + o.getOntologyID(), ex);
} finally {
/*
* It is still possible that an imported ontology is anonymous but has no physical document
* IRI (for example, because it was only generated in-memory but not stored). In this case it
* is necessary (and generally safe) to copy all its axioms and import statements to the
* parent ontology, or else it is lost.
*/
if (o.isAnonymous() && importIri == null) {
logger.warn("Anonymous import target " + o.getOntologyID() + " not mapped to physical IRI. Will add extracted axioms to parent ontology.");
for (OWLImportsDeclaration im : o.getImportsDeclarations()) changes.add(new AddImport(parent, im));
for (OWLAxiom im : o.getAxioms()) changes.add(new AddAxiom(parent, im));
} else if (importIri != null) {
// An anonymous ontology can still be imported if it has a
// valid document IRI.
changes.add(new AddImport(parent, owlFactory.getOWLImportsDeclaration(importIri)));
}
}
}
// apply the changes one by one, just in case.
for (OWLOntologyChange im : changes) try {
mgr.applyChange(im);
} catch (Exception ex) {
logger.error("KReS :: Exception caught during tree building. Skipping import", ex);
continue;
}
return parent;
}
use of org.semanticweb.owlapi.model.UnknownOWLOntologyException in project stanbol by apache.
the class ODPRegistryCacheManager method getOntologyInputSource.
public static synchronized OWLOntologyDocumentSource getOntologyInputSource(URI uri) throws ODPRegistryCacheException, URIUnresolvableException {
if (getUnresolvedURIs().contains(uri))
throw new URIUnresolvableException();
if (uris.containsKey(uri)) {
File f = uris.get(uri);
FileDocumentSource fds = new FileDocumentSource(f);
return fds;
} else {
try {
retrieveRemoteResource(uri);
return getOntologyInputSource(uri);
} catch (UnknownOWLOntologyException e) {
throw new ODPRegistryCacheException(e);
} catch (OWLOntologyCreationException e) {
throw new ODPRegistryCacheException(e);
} catch (OWLOntologyStorageException e) {
throw new ODPRegistryCacheException(e);
}
}
}
Aggregations