Search in sources :

Example 1 with UnknownOWLOntologyException

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;
}
Also used : MissingImportEvent(org.semanticweb.owlapi.model.MissingImportEvent) UnknownOWLOntologyException(org.semanticweb.owlapi.model.UnknownOWLOntologyException) URI(java.net.URI) OWLOntologyAlreadyExistsException(org.semanticweb.owlapi.model.OWLOntologyAlreadyExistsException) MissingImportListener(org.semanticweb.owlapi.model.MissingImportListener) OWLOntologyLoaderListener(org.semanticweb.owlapi.model.OWLOntologyLoaderListener) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) File(java.io.File) OWLOntologyStorageException(org.semanticweb.owlapi.model.OWLOntologyStorageException)

Example 2 with UnknownOWLOntologyException

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;
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) AddAxiom(org.semanticweb.owlapi.model.AddAxiom) OWLImportsDeclaration(org.semanticweb.owlapi.model.OWLImportsDeclaration) UnknownOWLOntologyException(org.semanticweb.owlapi.model.UnknownOWLOntologyException) AddImport(org.semanticweb.owlapi.model.AddImport) LinkedList(java.util.LinkedList) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) UnmodifiableOntologyCollectorException(org.apache.stanbol.ontologymanager.servicesapi.collector.UnmodifiableOntologyCollectorException) OWLOntologyStorageException(org.semanticweb.owlapi.model.OWLOntologyStorageException) UnknownOWLOntologyException(org.semanticweb.owlapi.model.UnknownOWLOntologyException) OWLOntologyChange(org.semanticweb.owlapi.model.OWLOntologyChange) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLAxiom(org.semanticweb.owlapi.model.OWLAxiom) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory)

Example 3 with UnknownOWLOntologyException

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);
        }
    }
}
Also used : OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) FileDocumentSource(org.semanticweb.owlapi.io.FileDocumentSource) UnknownOWLOntologyException(org.semanticweb.owlapi.model.UnknownOWLOntologyException) File(java.io.File) OWLOntologyStorageException(org.semanticweb.owlapi.model.OWLOntologyStorageException)

Aggregations

OWLOntologyStorageException (org.semanticweb.owlapi.model.OWLOntologyStorageException)3 UnknownOWLOntologyException (org.semanticweb.owlapi.model.UnknownOWLOntologyException)3 File (java.io.File)2 OWLOntology (org.semanticweb.owlapi.model.OWLOntology)2 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)2 URI (java.net.URI)1 LinkedList (java.util.LinkedList)1 UnmodifiableOntologyCollectorException (org.apache.stanbol.ontologymanager.servicesapi.collector.UnmodifiableOntologyCollectorException)1 FileDocumentSource (org.semanticweb.owlapi.io.FileDocumentSource)1 AddAxiom (org.semanticweb.owlapi.model.AddAxiom)1 AddImport (org.semanticweb.owlapi.model.AddImport)1 IRI (org.semanticweb.owlapi.model.IRI)1 MissingImportEvent (org.semanticweb.owlapi.model.MissingImportEvent)1 MissingImportListener (org.semanticweb.owlapi.model.MissingImportListener)1 OWLAxiom (org.semanticweb.owlapi.model.OWLAxiom)1 OWLDataFactory (org.semanticweb.owlapi.model.OWLDataFactory)1 OWLImportsDeclaration (org.semanticweb.owlapi.model.OWLImportsDeclaration)1 OWLOntologyAlreadyExistsException (org.semanticweb.owlapi.model.OWLOntologyAlreadyExistsException)1 OWLOntologyChange (org.semanticweb.owlapi.model.OWLOntologyChange)1 OWLOntologyLoaderListener (org.semanticweb.owlapi.model.OWLOntologyLoaderListener)1