Search in sources :

Example 21 with IndexedGraph

use of org.apache.stanbol.commons.indexedgraph.IndexedGraph in project stanbol by apache.

the class TextAnnotationNewModelEngineTest method init.

@BeforeClass
public static void init() throws IOException, ConfigurationException {
    InputStream in = TextAnnotationNewModelEngineTest.class.getClassLoader().getResourceAsStream(TEST_ENHANCEMENTS);
    Assert.assertNotNull("Unable to load reaource '" + TEST_ENHANCEMENTS + "' via Classpath", in);
    origEnhancements = new IndexedGraph();
    rdfParser.parse(origEnhancements, in, SupportedFormat.RDF_XML, null);
    Assert.assertFalse(origEnhancements.isEmpty());
    //parse the ID of the ContentItem form the enhancements
    Iterator<Triple> it = origEnhancements.filter(null, Properties.ENHANCER_EXTRACTED_FROM, null);
    Assert.assertTrue(it.hasNext());
    RDFTerm id = it.next().getObject();
    Assert.assertTrue(id instanceof IRI);
    ciUri = (IRI) id;
    //validate that the enhancements in the file are valid
    //NOTE: the input data are no longer fully valid to test some features of this engine
    //      because of that this initial test is deactivated
    //        EnhancementStructureHelper.validateAllTextAnnotations(
    //            origEnhancements, SINGLE_SENTENCE, null,
    //            false); //those do not yet contain fise:selection-prefix/suffix values
    //init the engine
    engine = new TextAnnotationsNewModelEngine();
    Dictionary<String, Object> config = new Hashtable<String, Object>();
    config.put(EnhancementEngine.PROPERTY_NAME, "test-engine");
    config.put(TextAnnotationsNewModelEngine.PROPERTY_PREFIX_SUFFIX_SIZE, Integer.valueOf(10));
    ctx = new MockComponentContext(config);
    engine.activate(ctx);
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) InputStream(java.io.InputStream) Hashtable(java.util.Hashtable) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm) Triple(org.apache.clerezza.commons.rdf.Triple) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) BeforeClass(org.junit.BeforeClass)

Example 22 with IndexedGraph

use of org.apache.stanbol.commons.indexedgraph.IndexedGraph 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 23 with IndexedGraph

use of org.apache.stanbol.commons.indexedgraph.IndexedGraph in project stanbol by apache.

the class RefactorEnhancementEngine method populateWithEntity.

/**
     * Fetch the OWLOntology containing the graph associated to an entity from Linked Data. It uses the Entity
     * Hub for accessing LOD and fetching entities.
     * 
     * @param entityURI
     *            {@link String}
     * @return the {@link OWLOntology} of the entity
     */
private Graph populateWithEntity(String entityURI, Graph target) {
    log.debug("Requesting signature of entity {}", entityURI);
    Graph graph = target != null ? target : new IndexedGraph();
    // Query the Entity Hub
    Entity signature = referencedSiteManager.getEntity(entityURI);
    if (signature != null) {
        RdfRepresentation rdfSignature = RdfValueFactory.getInstance().toRdfRepresentation(signature.getRepresentation());
        graph.addAll(rdfSignature.getRdfGraph());
    }
    return graph;
}
Also used : Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) Graph(org.apache.clerezza.commons.rdf.Graph) RdfRepresentation(org.apache.stanbol.entityhub.model.clerezza.RdfRepresentation) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph)

Example 24 with IndexedGraph

use of org.apache.stanbol.commons.indexedgraph.IndexedGraph in project stanbol by apache.

the class LDPathHelper method executeLDPath.

/**
     * Executes the LDPath program on the contexts stored in the backend and
     * returns the result as an RDF graph 
     * @param contexts the contexts to execute the program on
     * @param ldpath the LDPath program to execute
     * @param backend the {@link RDFBackend} to use
     * @return The results stored within an RDF graph
     * @throws LDPathParseException if the parsed LDPath program is invalid
     */
private static Graph executeLDPath(RDFBackend<Object> backend, String ldpath, Set<String> contexts) throws LDPathParseException {
    Graph data = new IndexedGraph();
    RdfValueFactory vf = new RdfValueFactory(data);
    EntityhubLDPath ldPath = new EntityhubLDPath(backend, vf);
    Program<Object> program = ldPath.parseProgram(getReader(ldpath));
    if (log.isDebugEnabled()) {
        log.debug("Execute on Context(s) '{}' LDPath program: \n{}", contexts, program.getPathExpression(backend));
    }
    /*
         * NOTE: We do not need to process the Representations returned by
         * EntityhubLDPath#exdecute, because the RdfValueFactory used uses
         * the local variable "Graph data" to backup all created
         * RdfRepresentation. Because of this all converted data will be
         * automatically added the Graph. The only thing we need to do is to
         * wrap the Graph in the response.
         */
    for (String context : contexts) {
        ldPath.execute(vf.createReference(context), program);
    }
    return data;
}
Also used : IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) Graph(org.apache.clerezza.commons.rdf.Graph) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) RdfValueFactory(org.apache.stanbol.entityhub.model.clerezza.RdfValueFactory) EntityhubLDPath(org.apache.stanbol.entityhub.ldpath.EntityhubLDPath)

Example 25 with IndexedGraph

use of org.apache.stanbol.commons.indexedgraph.IndexedGraph in project stanbol by apache.

the class RdfValueFactoryTest method testNullNodeRepresentation.

@Test(expected = IllegalArgumentException.class)
public void testNullNodeRepresentation() {
    Graph graph = new IndexedGraph();
    valueFactory.createRdfRepresentation(null, graph);
}
Also used : Graph(org.apache.clerezza.commons.rdf.Graph) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) Test(org.junit.Test) ValueFactoryTest(org.apache.stanbol.entityhub.test.model.ValueFactoryTest)

Aggregations

IndexedGraph (org.apache.stanbol.commons.indexedgraph.IndexedGraph)34 Graph (org.apache.clerezza.commons.rdf.Graph)27 IRI (org.apache.clerezza.commons.rdf.IRI)20 HashSet (java.util.HashSet)9 InputStream (java.io.InputStream)8 TripleImpl (org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)8 RdfValueFactory (org.apache.stanbol.entityhub.model.clerezza.RdfValueFactory)8 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)8 Triple (org.apache.clerezza.commons.rdf.Triple)7 BlankNodeOrIRI (org.apache.clerezza.commons.rdf.BlankNodeOrIRI)6 SimpleGraph (org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph)5 RdfQueryResultList (org.apache.stanbol.entityhub.query.clerezza.RdfQueryResultList)5 Test (org.junit.Test)5 EntityhubLDPath (org.apache.stanbol.entityhub.ldpath.EntityhubLDPath)4 RdfRepresentation (org.apache.stanbol.entityhub.model.clerezza.RdfRepresentation)4 Entity (org.apache.stanbol.entityhub.servicesapi.model.Entity)4 BeforeClass (org.junit.BeforeClass)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 IOException (java.io.IOException)3 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)3