Search in sources :

Example 6 with OWLOntologyChange

use of org.semanticweb.owlapi.model.OWLOntologyChange in project stanbol by apache.

the class RootResource method getOWLOntology.

private OWLOntology getOWLOntology(String ontologyId, boolean merge, URI requestUri) {
    long before = System.currentTimeMillis();
    IRI iri = URIUtils.sanitize(IRI.create(ontologyId));
    log.debug("Will try to retrieve ontology {} from provider.", iri);
    // TODO be selective: if the ontology is small enough, use OWLOntology otherwise export to ImmutableGraph.
    OWLOntology o = null;
    try {
        // XXX Guarantee that there MUST always be an entry for any decoded ontology ID submitted.
        OWLOntologyID id = OntologyUtils.decode(ontologyId);
        o = ontologyProvider.getStoredOntology(id, OWLOntology.class, merge);
    } catch (Exception ex) {
        log.warn("Retrieval of ontology with ID " + iri + " failed.", ex);
    }
    if (o == null) {
        log.debug("Ontology {} missing from provider. Trying libraries...", iri);
        // 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 {
                o = registryManager.getLibrary(smallest).getOntology(iri, OWLOntology.class);
            } catch (RegistryContentException e) {
                log.warn("The content of library " + smallest + " could not be accessed.", e);
            }
        }
    }
    if (o == null) {
        log.debug("Ontology {} not found in any ontology provider or library.", iri);
        return null;
    }
    log.debug("Retrieved ontology {} .", iri);
    // Rewrite import statements - no ontology collector to do it for us here.
    URI base = URI.create(getPublicBaseUri() + "ontonet/");
    List<OWLOntologyChange> changes = new ArrayList<OWLOntologyChange>();
    OWLDataFactory df = o.getOWLOntologyManager().getOWLDataFactory();
    // TODO manage import rewrites better once the container ID is fully configurable.
    for (OWLImportsDeclaration oldImp : o.getImportsDeclarations()) {
        changes.add(new RemoveImport(o, oldImp));
        String s = oldImp.getIRI().toString();
        if (s.contains("::")) {
            s = s.substring(s.indexOf("::") + 2, s.length());
        }
        IRI target = IRI.create(base + s);
        changes.add(new AddImport(o, df.getOWLImportsDeclaration(target)));
    }
    // Versioning.
    OWLOntologyID id = o.getOntologyID();
    if (!id.isAnonymous() && id.getVersionIRI() == null) {
        IRI viri = IRI.create(requestUri);
        log.debug("Setting version IRI for export : {}", viri);
        changes.add(new SetOntologyID(o, new OWLOntologyID(id.getOntologyIRI(), viri)));
    }
    o.getOWLOntologyManager().applyChanges(changes);
    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) ArrayList(java.util.ArrayList) OWLImportsDeclaration(org.semanticweb.owlapi.model.OWLImportsDeclaration) URI(java.net.URI) AddImport(org.semanticweb.owlapi.model.AddImport) 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) RemoveImport(org.semanticweb.owlapi.model.RemoveImport) SetOntologyID(org.semanticweb.owlapi.model.SetOntologyID) OWLOntologyChange(org.semanticweb.owlapi.model.OWLOntologyChange) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) RegistryContentException(org.apache.stanbol.ontologymanager.registry.api.RegistryContentException) Library(org.apache.stanbol.ontologymanager.registry.api.model.Library) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory)

Example 7 with OWLOntologyChange

use of org.semanticweb.owlapi.model.OWLOntologyChange 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 &quot;subtree&quot; 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 8 with OWLOntologyChange

use of org.semanticweb.owlapi.model.OWLOntologyChange in project stanbol by apache.

the class ScopeSetRenderer method getScopes.

public static OWLOntology getScopes(Set<Scope> scopes) {
    OWLOntologyManager mgr = OWLManager.createOWLOntologyManager();
    OWLOntology ont = null;
    try {
        ont = mgr.createOntology();
    } catch (OWLOntologyCreationException e) {
        LoggerFactory.getLogger(ScopeSetRenderer.class).error("KReS :: could not create empty ontology for rendering scopes.", e);
        return null;
    }
    List<OWLOntologyChange> additions = new LinkedList<OWLOntologyChange>();
    // The ODP metadata vocabulary is always imported.
    // TODO : also import the ONM meta when it goes online.
    additions.add(new AddImport(ont, __factory.getOWLImportsDeclaration(IRI.create("http://www.ontologydesignpatterns.org/schemas/meta.owl"))));
    for (Scope scope : scopes) {
        OWLNamedIndividual iScope = __factory.getOWLNamedIndividual(IRI.create(scope.getDefaultNamespace() + scope.getID()));
        OWLAxiom ax = __factory.getOWLClassAssertionAxiom(cScope, iScope);
        additions.add(new AddAxiom(ont, ax));
    }
    mgr.applyChanges(additions);
    return ont;
}
Also used : AddAxiom(org.semanticweb.owlapi.model.AddAxiom) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) Scope(org.apache.stanbol.ontologymanager.servicesapi.scope.Scope) OWLOntologyChange(org.semanticweb.owlapi.model.OWLOntologyChange) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLNamedIndividual(org.semanticweb.owlapi.model.OWLNamedIndividual) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLAxiom(org.semanticweb.owlapi.model.OWLAxiom) AddImport(org.semanticweb.owlapi.model.AddImport) LinkedList(java.util.LinkedList)

Example 9 with OWLOntologyChange

use of org.semanticweb.owlapi.model.OWLOntologyChange in project stanbol by apache.

the class SessionImpl method attachScopeImportsOwlApi.

private void attachScopeImportsOwlApi(OWLOntology target, org.semanticweb.owlapi.model.IRI prefix) {
    if (!attachedScopes.isEmpty()) {
        String scopePrefix = prefix.toString();
        scopePrefix = scopePrefix.substring(0, scopePrefix.lastIndexOf("/" + shortName + "/")) + "/ontology/";
        List<OWLOntologyChange> changes = new LinkedList<OWLOntologyChange>();
        OWLOntologyManager ontologyManager = target.getOWLOntologyManager();
        OWLDataFactory df = ontologyManager.getOWLDataFactory();
        // Add import declarations for attached scopes.
        for (String scopeID : attachedScopes) {
            org.semanticweb.owlapi.model.IRI physIRI = org.semanticweb.owlapi.model.IRI.create(scopePrefix + scopeID);
            changes.add(new AddImport(target, df.getOWLImportsDeclaration(physIRI)));
        }
        // Commit
        ontologyManager.applyChanges(changes);
    }
}
Also used : OWLOntologyChange(org.semanticweb.owlapi.model.OWLOntologyChange) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory) AddImport(org.semanticweb.owlapi.model.AddImport) LinkedList(java.util.LinkedList)

Aggregations

OWLOntologyChange (org.semanticweb.owlapi.model.OWLOntologyChange)9 AddImport (org.semanticweb.owlapi.model.AddImport)8 OWLDataFactory (org.semanticweb.owlapi.model.OWLDataFactory)8 OWLOntology (org.semanticweb.owlapi.model.OWLOntology)8 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)7 OWLOntologyManager (org.semanticweb.owlapi.model.OWLOntologyManager)6 LinkedList (java.util.LinkedList)5 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)4 AddAxiom (org.semanticweb.owlapi.model.AddAxiom)3 IRI (org.semanticweb.owlapi.model.IRI)3 OWLImportsDeclaration (org.semanticweb.owlapi.model.OWLImportsDeclaration)3 OWLOntologyID (org.semanticweb.owlapi.model.OWLOntologyID)3 OWLOntologySetProvider (org.semanticweb.owlapi.model.OWLOntologySetProvider)3 OWLOntologyMerger (org.semanticweb.owlapi.util.OWLOntologyMerger)3 WebApplicationException (javax.ws.rs.WebApplicationException)2 OWLAxiom (org.semanticweb.owlapi.model.OWLAxiom)2 OWLNamedIndividual (org.semanticweb.owlapi.model.OWLNamedIndividual)2 RemoveImport (org.semanticweb.owlapi.model.RemoveImport)2 IOException (java.io.IOException)1