Search in sources :

Example 1 with DuplicateIDException

use of org.apache.stanbol.ontologymanager.servicesapi.collector.DuplicateIDException 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)

Example 2 with DuplicateIDException

use of org.apache.stanbol.ontologymanager.servicesapi.collector.DuplicateIDException in project stanbol by apache.

the class ScopeResource method registerScope.

/**
     * At least one between corereg and coreont must be present. Registry iris supersede ontology iris.
     * 
     * @param scopeid
     * @param coreRegistry
     *            a. If it is a well-formed IRI it supersedes <code>coreOntology</code>.
     * @param coreOntologies
     * @param customRegistry
     *            a. If it is a well-formed IRI it supersedes <code>customOntology</code>.
     * @param customOntologies
     * @param activate
     *            if true, the new scope will be activated upon creation.
     * @param uriInfo
     * @param headers
     * @return
     */
@PUT
@Consumes(MediaType.WILDCARD)
public Response registerScope(@PathParam("scopeid") String scopeid, @QueryParam("corereg") final List<String> coreRegistries, @QueryParam("coreont") final List<String> coreOntologies, @DefaultValue("false") @QueryParam("activate") boolean activate, @Context HttpHeaders headers) {
    log.debug("Request URI {}", uriInfo.getRequestUri());
    scope = onm.getScope(scopeid);
    List<OntologyInputSource<?>> srcs = new ArrayList<OntologyInputSource<?>>(coreOntologies.size() + coreRegistries.size());
    // First thing, check registry sources.
    if (coreRegistries != null)
        for (String reg : coreRegistries) if (reg != null && !reg.isEmpty())
            try {
                // Library IDs are sanitized differently
                srcs.add(new LibrarySource(URIUtils.desanitize(IRI.create(reg)), regMgr));
            } catch (Exception e1) {
                throw new WebApplicationException(e1, BAD_REQUEST);
            // Bad or not supplied core registry, try the ontology.
            }
    // Then ontology sources
    if (coreOntologies != null)
        for (String ont : coreOntologies) if (ont != null && !ont.isEmpty())
            try {
                srcs.add(new RootOntologySource(IRI.create(ont)));
            } catch (OWLOntologyCreationException e2) {
                // If this fails too, throw a bad request.
                throw new WebApplicationException(e2, BAD_REQUEST);
            }
    // Now the creation.
    try {
        // Expand core sources
        List<OntologyInputSource<?>> expanded = new ArrayList<OntologyInputSource<?>>();
        for (OntologyInputSource<?> coreSrc : srcs) if (coreSrc != null) {
            if (coreSrc instanceof SetInputSource) {
                for (Object o : ((SetInputSource<?>) coreSrc).getOntologies()) {
                    OntologyInputSource<?> src = null;
                    if (o instanceof OWLOntology)
                        src = new RootOntologySource((OWLOntology) o);
                    else if (o instanceof Graph)
                        src = new GraphSource((Graph) o);
                    if (src != null)
                        expanded.add(src);
                }
            } else
                // Must be denoting a single ontology
                expanded.add(coreSrc);
        }
        scope = onm.createOntologyScope(scopeid, expanded.toArray(new OntologyInputSource[0]));
        // Setup and register the scope. If no custom space was set, it will
        // still be open for modification.
        scope.setUp();
        onm.setScopeActive(scopeid, activate);
    } catch (DuplicateIDException e) {
        throw new WebApplicationException(e, CONFLICT);
    } catch (Exception ex) {
        throw new WebApplicationException(ex, INTERNAL_SERVER_ERROR);
    }
    ResponseBuilder rb = Response.created(uriInfo.getAbsolutePath());
    // addCORSOrigin(servletContext, rb, headers);
    return rb.build();
}
Also used : SetInputSource(org.apache.stanbol.ontologymanager.servicesapi.io.SetInputSource) WebApplicationException(javax.ws.rs.WebApplicationException) ArrayList(java.util.ArrayList) LibrarySource(org.apache.stanbol.ontologymanager.registry.io.LibrarySource) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) UnsupportedFormatException(org.apache.clerezza.rdf.core.serializedform.UnsupportedFormatException) IrremovableOntologyException(org.apache.stanbol.ontologymanager.servicesapi.collector.IrremovableOntologyException) WebApplicationException(javax.ws.rs.WebApplicationException) UnmodifiableOntologyCollectorException(org.apache.stanbol.ontologymanager.servicesapi.collector.UnmodifiableOntologyCollectorException) DuplicateIDException(org.apache.stanbol.ontologymanager.servicesapi.collector.DuplicateIDException) OWLOntologyStorageException(org.semanticweb.owlapi.model.OWLOntologyStorageException) IOException(java.io.IOException) OntologyLoadingException(org.apache.stanbol.ontologymanager.servicesapi.ontology.OntologyLoadingException) OntologyCollectorModificationException(org.apache.stanbol.ontologymanager.servicesapi.collector.OntologyCollectorModificationException) ImmutableGraph(org.apache.clerezza.commons.rdf.ImmutableGraph) Graph(org.apache.clerezza.commons.rdf.Graph) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) DuplicateIDException(org.apache.stanbol.ontologymanager.servicesapi.collector.DuplicateIDException) RootOntologySource(org.apache.stanbol.ontologymanager.sources.owlapi.RootOntologySource) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) GraphSource(org.apache.stanbol.ontologymanager.sources.clerezza.GraphSource) OntologyInputSource(org.apache.stanbol.ontologymanager.servicesapi.io.OntologyInputSource) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT)

Example 3 with DuplicateIDException

use of org.apache.stanbol.ontologymanager.servicesapi.collector.DuplicateIDException 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)

Example 4 with DuplicateIDException

use of org.apache.stanbol.ontologymanager.servicesapi.collector.DuplicateIDException in project stanbol by apache.

the class ScopeManagerImpl method registerScope.

@Override
public synchronized void registerScope(Scope scope) throws DuplicateIDException {
    if (scope == null)
        throw new IllegalArgumentException("scope cannot be null.");
    String id = scope.getID();
    if (this.containsScope(id))
        throw new DuplicateIDException(id, "Scope registry already contains ontology scope with ID " + id);
    // if (this.containsScope(id)) {
    // if (scope != getScope(id)) {
    // log.warn("Overriding different scope with same ID {}", id);
    // super.registerScope(scope);
    // } else log.warn("Ignoring unnecessary call to already registered scope {}", id);
    // } else
    super.registerScope(scope);
}
Also used : DuplicateIDException(org.apache.stanbol.ontologymanager.servicesapi.collector.DuplicateIDException)

Aggregations

DuplicateIDException (org.apache.stanbol.ontologymanager.servicesapi.collector.DuplicateIDException)4 IOException (java.io.IOException)3 UnmodifiableOntologyCollectorException (org.apache.stanbol.ontologymanager.servicesapi.collector.UnmodifiableOntologyCollectorException)3 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)3 ArrayList (java.util.ArrayList)2 OntologyInputSource (org.apache.stanbol.ontologymanager.servicesapi.io.OntologyInputSource)2 OntologySpace (org.apache.stanbol.ontologymanager.servicesapi.scope.OntologySpace)2 RootOntologySource (org.apache.stanbol.ontologymanager.sources.owlapi.RootOntologySource)2 BufferedReader (java.io.BufferedReader)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Consumes (javax.ws.rs.Consumes)1 PUT (javax.ws.rs.PUT)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)1 Graph (org.apache.clerezza.commons.rdf.Graph)1 IRI (org.apache.clerezza.commons.rdf.IRI)1 ImmutableGraph (org.apache.clerezza.commons.rdf.ImmutableGraph)1 UnsupportedFormatException (org.apache.clerezza.rdf.core.serializedform.UnsupportedFormatException)1