Search in sources :

Example 1 with GraphSource

use of org.apache.stanbol.ontologymanager.sources.clerezza.GraphSource in project stanbol by apache.

the class TestClerezzaSpaces method setup.

@BeforeClass
public static void setup() throws Exception {
    offline = new OfflineConfigurationImpl(new Hashtable<String, Object>());
    ScopeRegistry reg = new ScopeRegistryImpl();
    // This one is created from scratch
    Graph ont2 = ClerezzaOWLUtils.createOntology(baseIri2.toString());
    minorSrc = new GraphSource(ont2.getImmutableGraph());
    dropSrc = getLocalSource("/ontologies/droppedcharacters.owl");
    nonexSrc = getLocalSource("/ontologies/nonexistentcharacters.owl");
    inMemorySrc = new ParentPathInputSource(new File(TestClerezzaSpaces.class.getResource("/ontologies/maincharacters.owl").toURI()));
    OWLDataFactory df = OWLManager.getOWLDataFactory();
    OWLClass cHuman = df.getOWLClass(IRI.create(baseIri + "/" + Constants.humanBeing));
    OWLIndividual iLinus = df.getOWLNamedIndividual(IRI.create(baseIri + "/" + Constants.linus));
    linusIsHuman = df.getOWLClassAssertionAxiom(cHuman, iLinus);
    factory = new ClerezzaCollectorFactory(new ClerezzaOntologyProvider(tcManager, offline, parser), new Hashtable<String, Object>());
    factory.setDefaultNamespace(IRI.create("http://stanbol.apache.org/ontology/"));
}
Also used : Hashtable(java.util.Hashtable) OfflineConfigurationImpl(org.apache.stanbol.ontologymanager.core.OfflineConfigurationImpl) ClerezzaOntologyProvider(org.apache.stanbol.ontologymanager.multiplexer.clerezza.ontology.ClerezzaOntologyProvider) Graph(org.apache.clerezza.commons.rdf.Graph) ScopeRegistryImpl(org.apache.stanbol.ontologymanager.core.scope.ScopeRegistryImpl) GraphSource(org.apache.stanbol.ontologymanager.sources.clerezza.GraphSource) ParentPathInputSource(org.apache.stanbol.ontologymanager.sources.owlapi.ParentPathInputSource) OWLClass(org.semanticweb.owlapi.model.OWLClass) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory) File(java.io.File) ScopeRegistry(org.apache.stanbol.ontologymanager.servicesapi.scope.ScopeRegistry) OWLIndividual(org.semanticweb.owlapi.model.OWLIndividual) ClerezzaCollectorFactory(org.apache.stanbol.ontologymanager.multiplexer.clerezza.collector.ClerezzaCollectorFactory) BeforeClass(org.junit.BeforeClass)

Example 2 with GraphSource

use of org.apache.stanbol.ontologymanager.sources.clerezza.GraphSource 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 GraphSource

use of org.apache.stanbol.ontologymanager.sources.clerezza.GraphSource in project stanbol by apache.

the class RefactorEnhancementEngine method computeEnhancements.

@Override
public void computeEnhancements(ContentItem ci) throws EngineException {
    // Prepare the OntoNet environment. First we create the OntoNet session in which run the whole
    final Session session;
    try {
        session = sessionManager.createSession();
    } catch (SessionLimitException e1) {
        throw new EngineException("OntoNet session quota reached. The Refactor Engine requires its own new session to execute.");
    }
    if (session == null)
        throw new EngineException("Failed to create OntoNet session. The Refactor Engine requires its own new session to execute.");
    log.debug("Refactor enhancement job will run in session '{}'.", session.getID());
    // Retrieve and filter the metadata graph for entities recognized by the engines.
    final Graph metadataGraph = ci.getMetadata(), signaturesGraph = new IndexedGraph();
    // FIXME the Stanbol Enhancer vocabulary should be retrieved from somewhere in the enhancer API.
    final IRI ENHANCER_ENTITY_REFERENCE = new IRI("http://fise.iks-project.eu/ontology/entity-reference");
    Iterator<Triple> tripleIt = metadataGraph.filter(null, ENHANCER_ENTITY_REFERENCE, null);
    while (tripleIt.hasNext()) {
        // Get the entity URI
        RDFTerm obj = tripleIt.next().getObject();
        if (!(obj instanceof IRI)) {
            log.warn("Invalid IRI for entity reference {}. Skipping.", obj);
            continue;
        }
        final String entityReference = ((IRI) obj).getUnicodeString();
        log.debug("Trying to resolve entity {}", entityReference);
        // Populate the entity signatures graph, by querying either the Entity Hub or the dereferencer.
        if (engineConfiguration.isEntityHubUsed()) {
            Graph result = populateWithEntity(entityReference, signaturesGraph);
            if (result != signaturesGraph && result != null) {
                log.warn("Entity Hub query added triples to a new graph instead of populating the supplied one!" + " New signatures will be discarded.");
            }
        } else
            try {
                OntologyInputSource<Graph> source = new GraphContentSourceWithPhysicalIRI(dereferencer.resolve(entityReference), org.semanticweb.owlapi.model.IRI.create(entityReference));
                signaturesGraph.addAll(source.getRootOntology());
            } catch (FileNotFoundException e) {
                log.error("Failed to dereference entity " + entityReference + ". Skipping.", e);
                continue;
            }
    }
    try {
        /*
             * The dedicated session for this job will store the following: (1) all the (merged) signatures
             * for all detected entities; (2) the original content metadata graph returned earlier in the
             * chain.
             * 
             * There is no chance that (2) could be null, as it was previously controlled by the JobManager
             * through the canEnhance() method and the computeEnhancement is always called iff the former
             * returns true.
             */
        session.addOntology(new GraphSource(signaturesGraph));
        session.addOntology(new GraphSource(metadataGraph));
    } catch (UnmodifiableOntologyCollectorException e1) {
        throw new EngineException("Cannot add enhancement graph to OntoNet session for refactoring", e1);
    }
    try {
        /*
             * Export the entire session (incl. entities and enhancement graph) as a single merged ontology.
             * 
             * TODO the refactorer should have methods to accommodate an OntologyCollector directly instead.
             */
        OWLOntology ontology = session.export(OWLOntology.class, true);
        log.debug("Refactoring recipe IRI is : " + engineConfiguration.getRecipeId());
        /*
             * We pass the ontology and the recipe IRI to the Refactor that returns the refactored graph
             * expressed by using the given vocabulary.
             * 
             * To perform the refactoring of the ontology to a given vocabulary we use the Stanbol Refactor.
             */
        Recipe recipe = ruleStore.getRecipe(new IRI(engineConfiguration.getRecipeId()));
        log.debug("Recipe {} contains {} rules.", recipe, recipe.getRuleList().size());
        log.debug("The ontology to be refactor is {}", ontology);
        Graph tc = refactorer.graphRefactoring(OWLAPIToClerezzaConverter.owlOntologyToClerezzaGraph(ontology), recipe);
        /*
             * The newly generated ontology is converted to Clarezza format and then added os substitued to
             * the old mGraph.
             */
        if (engineConfiguration.isInGraphAppendMode()) {
            log.debug("Metadata of the content will replace old ones.", this);
        } else {
            metadataGraph.clear();
            log.debug("Content metadata will be appended to the existing ones.", this);
        }
        metadataGraph.addAll(tc);
    } catch (RefactoringException e) {
        String msg = "Refactor engine execution failed on content item " + ci + ".";
        log.error(msg, e);
        throw new EngineException(msg, e);
    } catch (NoSuchRecipeException e) {
        String msg = "Refactor engine could not find recipe " + engineConfiguration.getRecipeId() + " to refactor content item " + ci + ".";
        log.error(msg, e);
        throw new EngineException(msg, e);
    } catch (Exception e) {
        throw new EngineException("Refactor Engine has failed.", e);
    } finally {
        /*
             * The session needs to be destroyed anyhow.
             * 
             * Clear contents before destroying (FIXME only do this until this is implemented in the
             * destroySession() method).
             */
        for (OWLOntologyID id : session.listManagedOntologies()) {
            try {
                String key = ontologyProvider.getKey(id.getOntologyIRI());
                ontologyProvider.getStore().deleteGraph(new IRI(key));
            } catch (Exception ex) {
                log.error("Failed to delete triple collection " + id, ex);
                continue;
            }
        }
        sessionManager.destroySession(session.getID());
    }
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) Recipe(org.apache.stanbol.rules.base.api.Recipe) NoSuchRecipeException(org.apache.stanbol.rules.base.api.NoSuchRecipeException) EngineException(org.apache.stanbol.enhancer.servicesapi.EngineException) FileNotFoundException(java.io.FileNotFoundException) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm) SessionLimitException(org.apache.stanbol.ontologymanager.servicesapi.session.SessionLimitException) 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) Triple(org.apache.clerezza.commons.rdf.Triple) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) Graph(org.apache.clerezza.commons.rdf.Graph) UnmodifiableOntologyCollectorException(org.apache.stanbol.ontologymanager.servicesapi.collector.UnmodifiableOntologyCollectorException) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) GraphSource(org.apache.stanbol.ontologymanager.sources.clerezza.GraphSource) OntologyInputSource(org.apache.stanbol.ontologymanager.servicesapi.io.OntologyInputSource) RefactoringException(org.apache.stanbol.rules.refactor.api.RefactoringException) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) Session(org.apache.stanbol.ontologymanager.servicesapi.session.Session)

Example 4 with GraphSource

use of org.apache.stanbol.ontologymanager.sources.clerezza.GraphSource in project stanbol by apache.

the class AbstractOntologyCollectorImpl method addOntology.

@Override
public synchronized OWLOntologyID addOntology(OntologyInputSource<?> ontologySource) throws UnmodifiableOntologyCollectorException {
    // Check for error conditions.
    if (locked)
        throw new UnmodifiableOntologyCollectorException(this);
    if (ontologySource == null)
        throw new IllegalArgumentException("Ontology source cannot be null.");
    log.debug("Adding ontology to collector {}", getID());
    OWLOntologyID key = null;
    if (ontologySource.hasRootOntology()) {
        long before = System.currentTimeMillis();
        Object o = ontologySource.getRootOntology();
        // Check the origin anyhow, as it may be useful for setting aliases with physical locations etc.
        if (ontologySource.hasOrigin())
            key = ontologyProvider.loadInStore(o, false, ontologySource.getOrigin());
        else
            key = ontologyProvider.loadInStore(o, false);
        if (key != null) {
            managedOntologies.add(key);
            // Note that imported ontologies are not considered as managed! TODO should we change this?
            log.info("Add ontology completed in {} ms.", (System.currentTimeMillis() - before));
            // Fire the event
            fireOntologyAdded(key);
        }
    } else if (ontologySource.hasOrigin()) {
        // Just the origin : see if it is satisfiable
        log.debug("Checking origin satisfiability...");
        Origin<?> origin = ontologySource.getOrigin();
        Object ref = origin.getReference();
        log.debug("Origin wraps a {}", ref.getClass().getCanonicalName());
        if (ref instanceof org.semanticweb.owlapi.model.IRI)
            try {
                log.debug("Deferring addition to physical IRI {} (if available).", ref);
                key = addOntology(new RootOntologySource((org.semanticweb.owlapi.model.IRI) ref));
            } catch (OWLOntologyCreationException e) {
                throw new RuntimeException(e);
            }
        else if (ref instanceof IRI) {
            log.debug("Deferring addition to stored Clerezza graph {} (if available).", ref);
            key = addOntology(new GraphSource((IRI) ref));
        } else if (ref instanceof OWLOntologyID) {
            OWLOntologyID idref = (OWLOntologyID) ref;
            log.debug("Deferring addition to stored ontology with public key {} (if available).", ref);
            if (!ontologyProvider.hasOntology(idref))
                throw new MissingOntologyException(this, idref);
            key = idref;
            if (managedOntologies.add(idref))
                fireOntologyAdded(idref);
        } else
            throw new IllegalArgumentException("Invalid origin " + origin);
    } else
        throw new IllegalArgumentException("Ontology source must provide either an ontology object, or a way to reference one (i.e. an origin).");
    log.info("Public key : {}", key);
    return key;
}
Also used : Origin(org.apache.stanbol.ontologymanager.servicesapi.io.Origin) IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) MissingOntologyException(org.apache.stanbol.ontologymanager.servicesapi.collector.MissingOntologyException) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) UnmodifiableOntologyCollectorException(org.apache.stanbol.ontologymanager.servicesapi.collector.UnmodifiableOntologyCollectorException) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) RootOntologySource(org.apache.stanbol.ontologymanager.sources.owlapi.RootOntologySource) GraphSource(org.apache.stanbol.ontologymanager.sources.clerezza.GraphSource)

Aggregations

GraphSource (org.apache.stanbol.ontologymanager.sources.clerezza.GraphSource)4 Graph (org.apache.clerezza.commons.rdf.Graph)3 UnmodifiableOntologyCollectorException (org.apache.stanbol.ontologymanager.servicesapi.collector.UnmodifiableOntologyCollectorException)3 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)3 IOException (java.io.IOException)2 IRI (org.apache.clerezza.commons.rdf.IRI)2 DuplicateIDException (org.apache.stanbol.ontologymanager.servicesapi.collector.DuplicateIDException)2 OntologyInputSource (org.apache.stanbol.ontologymanager.servicesapi.io.OntologyInputSource)2 RootOntologySource (org.apache.stanbol.ontologymanager.sources.owlapi.RootOntologySource)2 OWLOntology (org.semanticweb.owlapi.model.OWLOntology)2 OWLOntologyID (org.semanticweb.owlapi.model.OWLOntologyID)2 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 ArrayList (java.util.ArrayList)1 Hashtable (java.util.Hashtable)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 BlankNodeOrIRI (org.apache.clerezza.commons.rdf.BlankNodeOrIRI)1