Search in sources :

Example 1 with OntologySpace

use of org.apache.stanbol.ontologymanager.servicesapi.scope.OntologySpace in project stanbol by apache.

the class ScopeResource method getCoreSpaceOWL.

@GET
@Path("/core")
@Produces(value = { RDF_XML, TURTLE, X_TURTLE, MANCHESTER_OWL, FUNCTIONAL_OWL, OWL_XML, TEXT_PLAIN })
public Response getCoreSpaceOWL(@PathParam("scopeid") String scopeid, @DefaultValue("false") @QueryParam("merge") boolean merge, @Context UriInfo uriInfo, @Context HttpHeaders headers) {
    scope = onm.getScope(scopeid);
    OntologySpace space = scope.getCoreSpace();
    IRI prefix = IRI.create(getPublicBaseUri() + "ontonet/ontology/");
    OWLOntology o = space.export(OWLOntology.class, merge, prefix);
    ResponseBuilder rb = Response.ok(o);
    // addCORSOrigin(servletContext, rb, headers);
    return rb.build();
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) OntologySpace(org.apache.stanbol.ontologymanager.servicesapi.scope.OntologySpace) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 2 with OntologySpace

use of org.apache.stanbol.ontologymanager.servicesapi.scope.OntologySpace in project stanbol by apache.

the class ScopeResource method getCustomSpaceOWL.

@GET
@Path("/custom")
@Produces(value = { RDF_XML, TURTLE, X_TURTLE, MANCHESTER_OWL, FUNCTIONAL_OWL, OWL_XML, TEXT_PLAIN })
public Response getCustomSpaceOWL(@PathParam("scopeid") String scopeid, @DefaultValue("false") @QueryParam("merge") boolean merge, @Context UriInfo uriInfo, @Context HttpHeaders headers) {
    scope = onm.getScope(scopeid);
    OntologySpace space = scope.getCustomSpace();
    IRI prefix = IRI.create(getPublicBaseUri() + "ontonet/ontology/");
    OWLOntology o = space.export(OWLOntology.class, merge, prefix);
    ResponseBuilder rb = Response.ok(o);
    // addCORSOrigin(servletContext, rb, headers);
    return rb.build();
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) OntologySpace(org.apache.stanbol.ontologymanager.servicesapi.scope.OntologySpace) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 3 with OntologySpace

use of org.apache.stanbol.ontologymanager.servicesapi.scope.OntologySpace in project stanbol by apache.

the class ScopeResource method managedOntologyGetGraph.

/**
     * Gets the ontology with the given identifier in its version managed by the session.
     * 
     * @param sessionId
     *            the session identifier.
     * @param ontologyId
     *            the ontology identifier.
     * @param uriInfo
     * @param headers
     * @return the requested managed ontology, or {@link Status#NOT_FOUND} if either the sessionn does not
     *         exist, or the if the ontology either does not exist or is not managed.
     */
@GET
@Path("/{ontologyId:.+}")
@Produces(value = { APPLICATION_JSON, N3, N_TRIPLE, RDF_JSON })
public Response managedOntologyGetGraph(@PathParam("scopeid") String scopeid, @PathParam("ontologyId") String ontologyId, @DefaultValue("false") @QueryParam("merge") boolean merge, @Context UriInfo uriInfo, @Context HttpHeaders headers) {
    log.debug("Absolute URL Path {}", uriInfo.getRequestUri());
    log.debug("Ontology ID {}", ontologyId);
    ResponseBuilder rb;
    scope = onm.getScope(scopeid);
    if (scope == null)
        rb = Response.status(NOT_FOUND);
    else {
        IRI prefix = IRI.create(getPublicBaseUri() + "ontonet/ontology/");
        ImmutableGraph o = null;
        OWLOntologyID id = OntologyUtils.decode(ontologyId);
        OntologySpace spc = scope.getCustomSpace();
        if (spc != null && spc.hasOntology(id)) {
            o = spc.getOntology(id, ImmutableGraph.class, merge, prefix);
        } else {
            spc = scope.getCoreSpace();
            if (spc != null && spc.hasOntology(id))
                o = spc.getOntology(id, ImmutableGraph.class, merge, prefix);
        }
        if (o == null)
            rb = Response.status(NOT_FOUND);
        else
            rb = Response.ok(o);
    }
    // addCORSOrigin(servletContext, rb, headers);
    return rb.build();
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) OntologySpace(org.apache.stanbol.ontologymanager.servicesapi.scope.OntologySpace) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) ImmutableGraph(org.apache.clerezza.commons.rdf.ImmutableGraph) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 4 with OntologySpace

use of org.apache.stanbol.ontologymanager.servicesapi.scope.OntologySpace in project stanbol by apache.

the class ScopeImpl method exportToOWLOntology.

/**
     * Get an OWL API {@link OWLOntology} representation of the scope.
     * 
     * @param merge
     *            if true the core and custom spaces will be recursively merged with the scope ontology,
     *            otherwise owl:imports statements will be added.
     * @return the OWL representation of the scope.
     */
protected OWLOntology exportToOWLOntology(boolean merge, org.semanticweb.owlapi.model.IRI universalPrefix) {
    // if (merge) throw new UnsupportedOperationException(
    // "Ontology merging only implemented for managed ontologies, not for collectors. "
    // + "Please set merge parameter to false.");
    // Create an ontology manager on the fly. We don't really need a permanent one.
    OWLOntologyManager mgr = OWLManager.createOWLOntologyManager();
    OWLDataFactory df = mgr.getOWLDataFactory();
    OWLOntology ont = null;
    try {
        if (merge) {
            final Set<OWLOntology> set = new HashSet<OWLOntology>();
            log.debug("Merging custom space of {}.", getID());
            set.add(this.getCustomSpace().export(OWLOntology.class, merge));
            log.debug("Merging core space of {}.", getID());
            set.add(this.getCoreSpace().export(OWLOntology.class, merge));
            OWLOntologySetProvider provider = new OWLOntologySetProvider() {

                @Override
                public Set<OWLOntology> getOntologies() {
                    return set;
                }
            };
            OWLOntologyMerger merger = new OWLOntologyMerger(provider);
            try {
                ont = merger.createMergedOntology(OWLManager.createOWLOntologyManager(), org.semanticweb.owlapi.model.IRI.create(getDefaultNamespace() + getID()));
            } catch (OWLOntologyCreationException e) {
                log.error("Failed to merge imports for ontology.", e);
                ont = null;
            }
        } else {
            // The root ontology ID is in the form [namespace][scopeId]
            ont = mgr.createOntology(org.semanticweb.owlapi.model.IRI.create(universalPrefix + getID()));
            List<OWLOntologyChange> additions = new LinkedList<OWLOntologyChange>();
            // Add the import statement for the custom space, if existing and not empty
            OntologySpace spc = getCustomSpace();
            if (spc != null && spc.listManagedOntologies().size() > 0) {
                org.semanticweb.owlapi.model.IRI spaceIri = org.semanticweb.owlapi.model.IRI.create(universalPrefix + spc.getID());
                additions.add(new AddImport(ont, df.getOWLImportsDeclaration(spaceIri)));
            }
            // Add the import statement for the core space, if existing and not empty
            spc = getCoreSpace();
            if (spc != null && spc.listManagedOntologies().size() > 0) {
                org.semanticweb.owlapi.model.IRI spaceIri = org.semanticweb.owlapi.model.IRI.create(universalPrefix + spc.getID());
                additions.add(new AddImport(ont, df.getOWLImportsDeclaration(spaceIri)));
            }
            mgr.applyChanges(additions);
        }
    } catch (OWLOntologyCreationException e) {
        log.error("Failed to generate an OWL form of scope " + getID(), e);
        ont = null;
    }
    return ont;
}
Also used : OWLOntologyMerger(org.semanticweb.owlapi.util.OWLOntologyMerger) AddImport(org.semanticweb.owlapi.model.AddImport) LinkedList(java.util.LinkedList) OWLOntologySetProvider(org.semanticweb.owlapi.model.OWLOntologySetProvider) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntologyChange(org.semanticweb.owlapi.model.OWLOntologyChange) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OntologySpace(org.apache.stanbol.ontologymanager.servicesapi.scope.OntologySpace) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory) HashSet(java.util.HashSet)

Example 5 with OntologySpace

use of org.apache.stanbol.ontologymanager.servicesapi.scope.OntologySpace in project stanbol by apache.

the class ScopeManagerImpl method rebuildScopes.

private void rebuildScopes() {
    OntologyNetworkConfiguration struct = ontologyProvider.getOntologyNetworkConfiguration();
    for (String scopeId : struct.getScopeIDs()) {
        long before = System.currentTimeMillis();
        log.debug("Rebuilding scope with ID \"{}\".", scopeId);
        Collection<OWLOntologyID> coreOnts = struct.getCoreOntologyKeysForScope(scopeId);
        OntologyInputSource<?>[] srcs = new OntologyInputSource<?>[coreOnts.size()];
        int i = 0;
        for (OWLOntologyID coreOnt : coreOnts) {
            log.debug("Core ontology key : {}", coreOnts);
            srcs[i++] = new StoredOntologySource(coreOnt);
        }
        Scope scope;
        try {
            scope = createOntologyScope(scopeId, srcs);
        } catch (DuplicateIDException e) {
            String dupe = e.getDuplicateID();
            log.warn("Scope \"{}\" already exists and will be reused.", dupe);
            scope = getScope(dupe);
        }
        OntologySpace custom = scope.getCustomSpace();
        // Register even if some ontologies were to fail to be restored afterwards.
        scopeMap.put(scopeId, scope);
        for (OWLOntologyID key : struct.getCustomOntologyKeysForScope(scopeId)) try {
            log.debug("Custom ontology key : {}", key);
            custom.addOntology(new StoredOntologySource(key));
        } catch (MissingOntologyException ex) {
            log.error("Could not find an ontology with public key {} to be managed by scope \"{}\". Proceeding to next ontology.", key, scopeId);
            continue;
        } catch (Exception ex) {
            log.error("Exception caught while trying to add ontology with public key " + key + " to rebuilt scope \"" + scopeId + "\". proceeding to next ontology", ex);
            continue;
        }
        log.info("Scope \"{}\" rebuilt in {} ms.", scopeId, System.currentTimeMillis() - before);
    }
}
Also used : MissingOntologyException(org.apache.stanbol.ontologymanager.servicesapi.collector.MissingOntologyException) StoredOntologySource(org.apache.stanbol.ontologymanager.servicesapi.io.StoredOntologySource) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) NoSuchScopeException(org.apache.stanbol.ontologymanager.servicesapi.scope.NoSuchScopeException) MissingOntologyException(org.apache.stanbol.ontologymanager.servicesapi.collector.MissingOntologyException) UnmodifiableOntologyCollectorException(org.apache.stanbol.ontologymanager.servicesapi.collector.UnmodifiableOntologyCollectorException) DuplicateIDException(org.apache.stanbol.ontologymanager.servicesapi.collector.DuplicateIDException) IOException(java.io.IOException) OntologyNetworkConfiguration(org.apache.stanbol.ontologymanager.ontonet.api.OntologyNetworkConfiguration) Scope(org.apache.stanbol.ontologymanager.servicesapi.scope.Scope) DuplicateIDException(org.apache.stanbol.ontologymanager.servicesapi.collector.DuplicateIDException) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) OntologySpace(org.apache.stanbol.ontologymanager.servicesapi.scope.OntologySpace) OntologyInputSource(org.apache.stanbol.ontologymanager.servicesapi.io.OntologyInputSource)

Aggregations

OntologySpace (org.apache.stanbol.ontologymanager.servicesapi.scope.OntologySpace)29 Test (org.junit.Test)13 IRI (org.semanticweb.owlapi.model.IRI)9 UnmodifiableOntologyCollectorException (org.apache.stanbol.ontologymanager.servicesapi.collector.UnmodifiableOntologyCollectorException)8 OWLOntologyID (org.semanticweb.owlapi.model.OWLOntologyID)8 Path (javax.ws.rs.Path)7 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)7 GET (javax.ws.rs.GET)6 Produces (javax.ws.rs.Produces)6 OWLOntology (org.semanticweb.owlapi.model.OWLOntology)6 Scope (org.apache.stanbol.ontologymanager.servicesapi.scope.Scope)4 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)4 IOException (java.io.IOException)3 IRI (org.apache.clerezza.commons.rdf.IRI)3 ImmutableGraph (org.apache.clerezza.commons.rdf.ImmutableGraph)3 DuplicateIDException (org.apache.stanbol.ontologymanager.servicesapi.collector.DuplicateIDException)3 MissingOntologyException (org.apache.stanbol.ontologymanager.servicesapi.collector.MissingOntologyException)3 OWLOntologyManager (org.semanticweb.owlapi.model.OWLOntologyManager)3 InputStream (java.io.InputStream)2 BlankNodeOrIRI (org.apache.clerezza.commons.rdf.BlankNodeOrIRI)2