Search in sources :

Example 11 with OntologySpace

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

the class ScopeResource method getCoreSpaceGraph.

@GET
@Path("/core")
@Produces(value = { APPLICATION_JSON, N3, N_TRIPLE, RDF_JSON })
public Response getCoreSpaceGraph(@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/");
    ImmutableGraph o = space.export(ImmutableGraph.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) 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 12 with OntologySpace

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

the class ScopeResource method managedOntologyGetOWL.

/**
     * 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 = { RDF_XML, TURTLE, X_TURTLE, MANCHESTER_OWL, FUNCTIONAL_OWL, OWL_XML, TEXT_PLAIN })
public Response managedOntologyGetOWL(@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/");
        OWLOntology o = null;
        OWLOntologyID id = OntologyUtils.decode(ontologyId);
        OntologySpace spc = scope.getCustomSpace();
        if (spc != null && spc.hasOntology(id)) {
            o = spc.getOntology(id, OWLOntology.class, merge, prefix);
        } else {
            spc = scope.getCoreSpace();
            if (spc != null && spc.hasOntology(id))
                o = spc.getOntology(id, OWLOntology.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) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) OntologySpace(org.apache.stanbol.ontologymanager.servicesapi.scope.OntologySpace) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 13 with OntologySpace

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

the class ScopeResource method getCustomSpaceGraph.

@GET
@Path("/custom")
@Produces(value = { APPLICATION_JSON, N3, N_TRIPLE, RDF_JSON })
public Response getCustomSpaceGraph(@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/");
    ImmutableGraph o = space.export(ImmutableGraph.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) 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 14 with OntologySpace

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

the class ScopeResource method managedOntologyUnload.

/**
     * Unloads an ontology from an ontology scope.
     * 
     * @param scopeId
     * @param ontologyid
     * @param uriInfo
     * @param headers
     */
@DELETE
@Path("/{ontologyId:.+}")
public Response managedOntologyUnload(@PathParam("scopeid") String scopeid, @PathParam("ontologyId") String ontologyId, @PathParam("scopeid") String scopeId, @Context UriInfo uriInfo, @Context HttpHeaders headers) {
    ResponseBuilder rb;
    scope = onm.getScope(scopeid);
    if (ontologyId != null && !ontologyId.trim().isEmpty()) {
        OWLOntologyID id = OntologyUtils.decode(ontologyId);
        OntologySpace cs = scope.getCustomSpace();
        if (// ontology not managed
        !cs.hasOntology(id))
            // ontology not managed
            rb = Response.notModified();
        else
            try {
                onm.setScopeActive(scopeId, false);
                cs.removeOntology(id);
                rb = Response.ok();
            } catch (IrremovableOntologyException e) {
                throw new WebApplicationException(e, FORBIDDEN);
            } catch (UnmodifiableOntologyCollectorException e) {
                throw new WebApplicationException(e, FORBIDDEN);
            } catch (OntologyCollectorModificationException e) {
                throw new WebApplicationException(e, INTERNAL_SERVER_ERROR);
            } finally {
                onm.setScopeActive(scopeId, true);
            }
    } else
        // null/blank ontology ID
        rb = Response.status(BAD_REQUEST);
    // addCORSOrigin(servletContext, rb, headers);
    return rb.build();
}
Also used : IrremovableOntologyException(org.apache.stanbol.ontologymanager.servicesapi.collector.IrremovableOntologyException) WebApplicationException(javax.ws.rs.WebApplicationException) OntologyCollectorModificationException(org.apache.stanbol.ontologymanager.servicesapi.collector.OntologyCollectorModificationException) UnmodifiableOntologyCollectorException(org.apache.stanbol.ontologymanager.servicesapi.collector.UnmodifiableOntologyCollectorException) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) OntologySpace(org.apache.stanbol.ontologymanager.servicesapi.scope.OntologySpace) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE)

Example 15 with OntologySpace

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

the class RefactorEnhancementEngine method initEngine.

/**
     * Method for adding ontologies to the scope core ontology.
     * <ol>
     * <li>Get all the ontologies from the property.</li>
     * <li>Create a base scope.</li>
     * <li>Retrieve the ontology space from the scope.</li>
     * <li>Add the ontologies to the scope via ontology space.</li>
     * </ol>
     */
private void initEngine(RefactorEnhancementEngineConf engineConfiguration) {
    // IRI dulcifierScopeIRI = org.semanticweb.owlapi.model.IRI.create((String) context.getProperties().get(SCOPE));
    String scopeId = engineConfiguration.getScope();
    // Create or get the scope with the configured ID
    try {
        scope = onManager.createOntologyScope(scopeId);
    // No need to deactivate a newly created scope.
    } catch (DuplicateIDException e) {
        scope = onManager.getScope(scopeId);
        onManager.setScopeActive(scopeId, false);
    }
    // All resolvable ontologies stated in the configuration are loaded into the core space.
    OntologySpace ontologySpace = scope.getCoreSpace();
    ontologySpace.tearDown();
    String[] coreScopeOntologySet = engineConfiguration.getScopeCoreOntologies();
    List<String> success = new ArrayList<String>(), failed = new ArrayList<String>();
    try {
        log.info("Will now load requested ontology into the core space of scope '{}'.", scopeId);
        OWLOntologyManager sharedManager = OWLManager.createOWLOntologyManager();
        org.semanticweb.owlapi.model.IRI physicalIRI = null;
        for (int o = 0; o < coreScopeOntologySet.length; o++) {
            String url = coreScopeOntologySet[o];
            try {
                physicalIRI = org.semanticweb.owlapi.model.IRI.create(url);
            } catch (Exception e) {
                failed.add(url);
            }
            try {
                // TODO replace with a Clerezza equivalent
                ontologySpace.addOntology(new RootOntologySource(physicalIRI, sharedManager));
                success.add(url);
            } catch (OWLOntologyCreationException e) {
                log.error("Failed to load ontology from physical location " + physicalIRI + " Continuing with next...", e);
                failed.add(url);
            }
        }
    } catch (UnmodifiableOntologyCollectorException ex) {
        log.error("Ontology space {} was found locked for modification. Cannot populate.", ontologySpace);
    }
    for (String s : success) log.info(" >> {} : SUCCESS", s);
    for (String s : failed) log.info(" >> {} : FAILED", s);
    ontologySpace.setUp();
    // if (!onManager.containsScope(scopeId)) onManager.registerScope(scope);
    onManager.setScopeActive(scopeId, true);
    /*
         * The first thing to do is to create a recipe in the rule store that can be used by the engine to
         * refactor the enhancement graphs.
         */
    String recipeId = engineConfiguration.getRecipeId();
    Recipe recipe = null;
    try {
        recipe = ruleStore.createRecipe(new IRI(recipeId), null);
    } catch (AlreadyExistingRecipeException e1) {
        log.error("A recipe with ID {} already exists in the store.", recipeId);
    }
    if (recipe != null) {
        log.debug("Initialised blank recipe with ID {}", recipeId);
        /*
             * The set of rule to put in the recipe can be provided by the user. A default set of rules is
             * provided in /META-INF/default/seo_rules.sem. Use the property engine.refactor in the felix
             * console to pass to the engine your set of rules.
             */
        String recipeLocation = engineConfiguration.getRecipeLocation();
        InputStream recipeStream = null;
        String recipeString = null;
        if (recipeLocation != null && !recipeLocation.isEmpty()) {
            Dereferencer dereferencer = new DereferencerImpl();
            try {
                recipeStream = dereferencer.resolve(recipeLocation);
                log.debug("Loaded recipe from external source {}", recipeLocation);
            } catch (FileNotFoundException e) {
                log.error("Recipe Stream is null.", e);
            }
        } else {
            // TODO remove this part (or manage it better in the @Activate method).
            String loc = "/META-INF/default/seo_rules.sem";
            recipeStream = getClass().getResourceAsStream(loc);
            log.debug("Loaded default recipe in {}.", loc);
        }
        if (recipeStream != null) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(recipeStream));
            recipeString = "";
            String line = null;
            try {
                while ((line = reader.readLine()) != null) recipeString += line;
            } catch (IOException e) {
                log.error("Failed to load Refactor Engine recipe from stream. Aborting read. ", e);
                recipeString = null;
            }
        }
        log.debug("Recipe content follows :\n{}", recipeString);
        if (recipeString != null) {
            ruleStore.addRulesToRecipe(recipe, recipeString, null);
            log.debug("Added rules to recipe {}", recipeId);
        }
    }
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) Recipe(org.apache.stanbol.rules.base.api.Recipe) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) UnmodifiableOntologyCollectorException(org.apache.stanbol.ontologymanager.servicesapi.collector.UnmodifiableOntologyCollectorException) DuplicateIDException(org.apache.stanbol.ontologymanager.servicesapi.collector.DuplicateIDException) RootOntologySource(org.apache.stanbol.ontologymanager.sources.owlapi.RootOntologySource) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) AlreadyExistingRecipeException(org.apache.stanbol.rules.base.api.AlreadyExistingRecipeException) Dereferencer(org.apache.stanbol.enhancer.engines.refactor.dereferencer.Dereferencer) DereferencerImpl(org.apache.stanbol.enhancer.engines.refactor.dereferencer.DereferencerImpl) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) IOException(java.io.IOException) EngineException(org.apache.stanbol.enhancer.servicesapi.EngineException) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) RecipeConstructionException(org.apache.stanbol.rules.base.api.RecipeConstructionException) ConfigurationException(org.osgi.service.cm.ConfigurationException) RefactoringException(org.apache.stanbol.rules.refactor.api.RefactoringException) FileNotFoundException(java.io.FileNotFoundException) RecipeEliminationException(org.apache.stanbol.rules.base.api.RecipeEliminationException) NoSuchRecipeException(org.apache.stanbol.rules.base.api.NoSuchRecipeException) SessionLimitException(org.apache.stanbol.ontologymanager.servicesapi.session.SessionLimitException) UnmodifiableOntologyCollectorException(org.apache.stanbol.ontologymanager.servicesapi.collector.UnmodifiableOntologyCollectorException) AlreadyExistingRecipeException(org.apache.stanbol.rules.base.api.AlreadyExistingRecipeException) DuplicateIDException(org.apache.stanbol.ontologymanager.servicesapi.collector.DuplicateIDException) IOException(java.io.IOException) OntologySpace(org.apache.stanbol.ontologymanager.servicesapi.scope.OntologySpace) BufferedReader(java.io.BufferedReader)

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