Search in sources :

Example 6 with RegistryContentException

use of org.apache.stanbol.ontologymanager.registry.api.RegistryContentException in project stanbol by apache.

the class RootResource method getGraph.

private Graph getGraph(String ontologyId, boolean merged, URI requestUri) {
    long before = System.currentTimeMillis();
    OWLOntologyID key = OntologyUtils.decode(ontologyId);
    log.debug("Will try to retrieve ontology {} from provider.", key);
    /*
         * Export directly to Graph since the OWLOntologyWriter uses (de-)serializing converters for the
         * other formats.
         * 
         * Use oTemp for the "real" graph and o for the graph that will be exported. This is due to the fact
         * that in o we want to change import statements, but we do not want these changes to be stored
         * permanently.
         */
    Graph o = null, oTemp = null;
    try {
        oTemp = ontologyProvider.getStoredOntology(key, Graph.class, merged);
    } catch (Exception ex) {
        log.warn("Retrieval of ontology with ID " + key + " failed.", ex);
    }
    if (oTemp == null) {
        log.debug("Ontology {} missing from provider. Trying libraries...", key);
        // TODO remove once registry supports OWLOntologyID as public key.
        IRI iri = URIUtils.sanitize(IRI.create(ontologyId));
        // See if we can touch a library. TODO: replace with event model on the ontology provider.
        int minSize = -1;
        IRI smallest = null;
        for (Library lib : registryManager.getLibraries(iri)) {
            int size = lib.getChildren().length;
            if (minSize < 1 || size < minSize) {
                smallest = lib.getIRI();
                minSize = size;
            }
        }
        if (smallest != null) {
            log.debug("Selected library for ontology {} is {} .", iri, smallest);
            try {
                oTemp = registryManager.getLibrary(smallest).getOntology(iri, Graph.class);
            } catch (RegistryContentException e) {
                log.warn("The content of library " + smallest + " could not be accessed.", e);
            }
        }
    }
    // resource-intensive IndexedGraph, since both o and oTemp will be GC'ed after serialization.
    if (oTemp != null) {
        o = new SimpleGraph(oTemp);
    }
    if (o == null) {
        log.debug("Ontology {} not found in any ontology provider or library.", ontologyId);
        return null;
    }
    log.debug("Retrieved ontology {} .", ontologyId);
    // Rewrite imports
    String uri = uriInfo.getRequestUri().toString();
    URI base = URI.create(uri.substring(0, uri.lastIndexOf(ontologyId) - 1));
    // Rewrite import statements
    /*
         * TODO manage import rewrites better once the container ID is fully configurable (i.e. instead of
         * going upOne() add "session" or "ontology" if needed).
         */
    Iterator<Triple> imports = o.filter(null, OWL.imports, null);
    Set<Triple> oldImports = new HashSet<Triple>();
    while (imports.hasNext()) {
        oldImports.add(imports.next());
    }
    for (Triple t : oldImports) {
        // construct new statement
        String s = ((org.apache.clerezza.commons.rdf.IRI) t.getObject()).getUnicodeString();
        if (s.contains("::")) {
            s = s.substring(s.indexOf("::") + 2, s.length());
        }
        org.apache.clerezza.commons.rdf.IRI target = new org.apache.clerezza.commons.rdf.IRI(base + "/" + s);
        o.add(new TripleImpl(t.getSubject(), OWL.imports, target));
        // remove old statement
        o.remove(t);
    }
    // Versioning.
    OWLOntologyID id = OWLUtils.extractOntologyID(o);
    if (id != null && !id.isAnonymous() && id.getVersionIRI() == null) {
        org.apache.clerezza.commons.rdf.IRI viri = new org.apache.clerezza.commons.rdf.IRI(requestUri.toString());
        log.debug("Setting version IRI for export : {}", viri);
        o.add(new TripleImpl(new org.apache.clerezza.commons.rdf.IRI(id.getOntologyIRI().toString()), new org.apache.clerezza.commons.rdf.IRI(OWL2Constants.OWL_VERSION_IRI), viri));
    }
    log.debug("Exported as Clerezza ImmutableGraph in {} ms. Handing over to writer.", System.currentTimeMillis() - before);
    return o;
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) URI(java.net.URI) UnsupportedFormatException(org.apache.clerezza.rdf.core.serializedform.UnsupportedFormatException) WebApplicationException(javax.ws.rs.WebApplicationException) ConcurrentModificationException(java.util.ConcurrentModificationException) IOException(java.io.IOException) OntologyLoadingException(org.apache.stanbol.ontologymanager.servicesapi.ontology.OntologyLoadingException) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OntologyHandleException(org.apache.stanbol.ontologymanager.servicesapi.ontology.OntologyHandleException) RegistryContentException(org.apache.stanbol.ontologymanager.registry.api.RegistryContentException) OrphanOntologyKeyException(org.apache.stanbol.ontologymanager.servicesapi.ontology.OrphanOntologyKeyException) Triple(org.apache.clerezza.commons.rdf.Triple) ImmutableGraph(org.apache.clerezza.commons.rdf.ImmutableGraph) Graph(org.apache.clerezza.commons.rdf.Graph) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) RegistryContentException(org.apache.stanbol.ontologymanager.registry.api.RegistryContentException) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) Library(org.apache.stanbol.ontologymanager.registry.api.model.Library) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl) HashSet(java.util.HashSet)

Aggregations

RegistryContentException (org.apache.stanbol.ontologymanager.registry.api.RegistryContentException)6 IRI (org.semanticweb.owlapi.model.IRI)5 OWLOntology (org.semanticweb.owlapi.model.OWLOntology)5 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)5 HashSet (java.util.HashSet)4 OWLDataFactory (org.semanticweb.owlapi.model.OWLDataFactory)4 OWLIndividual (org.semanticweb.owlapi.model.OWLIndividual)4 Library (org.apache.stanbol.ontologymanager.registry.api.model.Library)3 RegistryItem (org.apache.stanbol.ontologymanager.registry.api.model.RegistryItem)3 OWLAnnotationAssertionAxiom (org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom)3 OWLOntologyAlreadyExistsException (org.semanticweb.owlapi.model.OWLOntologyAlreadyExistsException)3 IOException (java.io.IOException)2 URI (java.net.URI)2 ConcurrentModificationException (java.util.ConcurrentModificationException)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 UnsupportedFormatException (org.apache.clerezza.rdf.core.serializedform.UnsupportedFormatException)2 Registry (org.apache.stanbol.ontologymanager.registry.api.model.Registry)2 OntologyHandleException (org.apache.stanbol.ontologymanager.servicesapi.ontology.OntologyHandleException)2 OntologyLoadingException (org.apache.stanbol.ontologymanager.servicesapi.ontology.OntologyLoadingException)2 OrphanOntologyKeyException (org.apache.stanbol.ontologymanager.servicesapi.ontology.OrphanOntologyKeyException)2