Search in sources :

Example 1 with EnhancementEngine

use of org.apache.stanbol.enhancer.servicesapi.EnhancementEngine in project stanbol by apache.

the class EnhancementJobHandler method processEvent.

/**
     * @param job
     * @param execution
     */
private void processEvent(EnhancementJob job, BlankNodeOrIRI execution) {
    String engineName = getEngine(job.getExecutionPlan(), job.getExecutionNode(execution));
    //(1) execute the parsed ExecutionNode
    EnhancementEngine engine = engineManager.getEngine(engineName);
    if (engine != null) {
        //execute the engine
        Exception exception = null;
        int engineState;
        try {
            engineState = engine.canEnhance(job.getContentItem());
        } catch (EngineException e) {
            exception = e;
            log.warn("Unable to check if engine '" + engineName + "'(type: " + engine.getClass() + ") can enhance ContentItem '" + job.getContentItem().getUri() + "'!", e);
            engineState = EnhancementEngine.CANNOT_ENHANCE;
        }
        if (engineState == EnhancementEngine.ENHANCE_SYNCHRONOUS) {
            //ensure that this engine exclusively access the content item
            log.trace("++ w: {}: {}", "start sync execution", engine.getName());
            job.getLock().writeLock().lock();
            log.trace(">> w: {}: {}", "start sync execution", engine.getName());
            try {
                engine.computeEnhancements(job.getContentItem());
                job.setCompleted(execution);
            } catch (EngineException e) {
                log.warn(e.getMessage(), e);
                job.setFailed(execution, engine, e);
            } catch (RuntimeException e) {
                log.warn(e.getMessage(), e);
                job.setFailed(execution, engine, e);
            } finally {
                log.trace("<< w: {}: {}", "finished sync execution", engine.getName());
                job.getLock().writeLock().unlock();
            }
        } else if (engineState == EnhancementEngine.ENHANCE_ASYNC) {
            try {
                log.trace("++ n: start async execution of Engine {}", engine.getName());
                engine.computeEnhancements(job.getContentItem());
                log.trace("++ n: finished async execution of Engine {}", engine.getName());
                job.setCompleted(execution);
            } catch (EngineException e) {
                log.warn(e.getMessage(), e);
                job.setFailed(execution, engine, e);
            } catch (RuntimeException e) {
                log.warn(e.getMessage(), e);
                job.setFailed(execution, engine, e);
            }
        } else {
            //CANNOT_ENHANCE
            if (exception != null) {
                job.setFailed(execution, engine, exception);
            } else {
                //can not enhance is not an error
                //it just says this engine can not enhance this content item
                job.setCompleted(execution);
            }
        }
    } else {
        //engine with that name is not available
        job.setFailed(execution, null, null);
    }
}
Also used : EngineException(org.apache.stanbol.enhancer.servicesapi.EngineException) EnhancementEngine(org.apache.stanbol.enhancer.servicesapi.EnhancementEngine) EngineException(org.apache.stanbol.enhancer.servicesapi.EngineException)

Example 2 with EnhancementEngine

use of org.apache.stanbol.enhancer.servicesapi.EnhancementEngine in project stanbol by apache.

the class WeightedJobManager method enhanceContent.

@Override
public void enhanceContent(ContentItem ci) throws EngineException {
    log.debug("enhanceContent({}), {} engines available", ci, sortedEngineList.size());
    Iterator<EnhancementEngine> engines;
    //changed.
    synchronized (sortedEngineList) {
        engines = sortedEngineList.iterator();
    }
    long start = System.currentTimeMillis();
    while (engines.hasNext()) {
        EnhancementEngine engine = engines.next();
        long startEngine = System.currentTimeMillis();
        if (engine.canEnhance(ci) == EnhancementEngine.CANNOT_ENHANCE) {
            log.debug("[{}] cannot be enhanced by engine [{}], skipping", ci.getUri().getUnicodeString(), engine);
        } else {
            // TODO should handle sync/async enhancing. All sync for now.
            engine.computeEnhancements(ci);
            log.debug("ContentItem [{}] enhanced by engine [{}] in {}ms", new Object[] { ci.getUri().getUnicodeString(), engine, System.currentTimeMillis() - startEngine });
        }
    }
    log.debug("ContentItem [{}] enhanced in {}ms", ci.getUri().getUnicodeString(), (System.currentTimeMillis() - start));
}
Also used : EnhancementEngine(org.apache.stanbol.enhancer.servicesapi.EnhancementEngine)

Example 3 with EnhancementEngine

use of org.apache.stanbol.enhancer.servicesapi.EnhancementEngine in project stanbol by apache.

the class WeightedChain method createExecutionPlan.

/**
     * Creates a new execution plan based on the configured {@link #chain} and
     * the currently available {@link EnhancementEngine}s. If required
     * {@link EnhancementEngine}s are missing a {@link ChainException} will be
     * thrown.
     * @return the execution plan
     * @throws ChainException if a required {@link EnhancementEngine} of the
     * configured {@link #chain} is not active.
     */
private ImmutableGraph createExecutionPlan() throws ChainException {
    List<EnhancementEngine> availableEngines = new ArrayList<EnhancementEngine>(chain.size());
    Set<String> optionalEngines = new HashSet<String>();
    Set<String> missingEngines = new HashSet<String>();
    for (Entry<String, Map<String, List<String>>> entry : chain.entrySet()) {
        boolean optional = getState(entry.getValue(), "optional");
        EnhancementEngine engine = tracker.getEngine(entry.getKey());
        if (engine != null) {
            availableEngines.add(engine);
        } else {
            missingEngines.add(entry.getKey());
        }
        if (optional) {
            optionalEngines.add(entry.getKey());
        }
    }
    return calculateExecutionPlan(getName(), availableEngines, optionalEngines, missingEngines, chainScopedEnhProps);
}
Also used : ArrayList(java.util.ArrayList) EnhancementEngine(org.apache.stanbol.enhancer.servicesapi.EnhancementEngine) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 4 with EnhancementEngine

use of org.apache.stanbol.enhancer.servicesapi.EnhancementEngine in project stanbol by apache.

the class ExecutionPlanHelper method getActiveEngines.

/**
     * Calculates a sorted list of active EnhancementEngines form the given
     * ExecutinPlan
     * @param engineManager The engine manager (OSGI service or {@link EnginesTracker})
     * @param ep the execution plan
     * @return
     */
public static List<EnhancementEngine> getActiveEngines(EnhancementEngineManager engineManager, Graph ep) {
    List<EnhancementEngine> engines = new ArrayList<EnhancementEngine>();
    Set<BlankNodeOrIRI> visited = new HashSet<BlankNodeOrIRI>();
    Set<BlankNodeOrIRI> executeable;
    do {
        executeable = getExecutable(ep, visited);
        for (BlankNodeOrIRI node : executeable) {
            String engineName = getString(ep, node, ENGINE);
            EnhancementEngine engine = engineManager.getEngine(engineName);
            if (engine != null) {
                engines.add(engine);
            }
            visited.add(node);
        }
    } while (!executeable.isEmpty());
    return engines;
}
Also used : ArrayList(java.util.ArrayList) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) EnhancementEngineHelper.getString(org.apache.stanbol.enhancer.servicesapi.helper.EnhancementEngineHelper.getString) EnhancementEngine(org.apache.stanbol.enhancer.servicesapi.EnhancementEngine) HashSet(java.util.HashSet)

Example 5 with EnhancementEngine

use of org.apache.stanbol.enhancer.servicesapi.EnhancementEngine in project stanbol by apache.

the class EnhancementPropertyTest method testEngineSpecificChainScopedProperty.

@Test
public void testEngineSpecificChainScopedProperty() throws ChainException {
    Collection<String> derefernceLanguages = Arrays.asList("en", "de");
    String maxSuggestions = "5";
    //set up the map with the enhancement properties we want to set for the
    //Enhancement Chain
    Map<String, Map<String, Object>> enhancementProperties = new HashMap<String, Map<String, Object>>();
    //set enhancer.maxSuggestions=5 as chain property (applies for all engines)
    Map<String, Object> linkingProperties = new HashMap<String, Object>();
    linkingProperties.put(PROPERTY_MAX_SUGGESTIONS, Integer.valueOf(maxSuggestions));
    enhancementProperties.put(linking.getName(), linkingProperties);
    Map<String, Object> dereferenceProperties = new HashMap<String, Object>();
    dereferenceProperties.put(PROPERTY_DEREFERENCE_LANGUAGES, derefernceLanguages);
    enhancementProperties.put(dereference.getName(), dereferenceProperties);
    initExecutionMetadata(new TestChain("test", engines, enhancementProperties));
    //we expect enhancer.maxSuggestions=5 for the langdetect engine
    for (EnhancementEngine engine : engines) {
        Map<String, Object> props = EnhancementEngineHelper.getEnhancementProperties(engine, contentItem);
        assertNotNull(props);
        if (engine.getName().equals(linking.getName()) || engine.getName().equals(dereference.getName())) {
            assertEquals(1, props.size());
        } else {
            assertTrue(props.isEmpty());
        }
        if (engine.getName().equals(linking.getName())) {
            assertTrue(props.containsKey(PROPERTY_MAX_SUGGESTIONS));
            assertEquals(maxSuggestions, props.get(PROPERTY_MAX_SUGGESTIONS));
        } else if (engine.getName().equals(dereference.getName())) {
            assertTrue(props.containsKey(PROPERTY_DEREFERENCE_LANGUAGES));
            Object value = props.get(PROPERTY_DEREFERENCE_LANGUAGES);
            assertTrue(value instanceof Collection<?>);
            assertTrue(derefernceLanguages.containsAll((Collection<?>) value));
            assertEquals(derefernceLanguages.size(), ((Collection<?>) value).size());
        }
    //else empty
    }
}
Also used : HashMap(java.util.HashMap) Collection(java.util.Collection) EnhancementEngine(org.apache.stanbol.enhancer.servicesapi.EnhancementEngine) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

EnhancementEngine (org.apache.stanbol.enhancer.servicesapi.EnhancementEngine)16 Test (org.junit.Test)8 HashMap (java.util.HashMap)6 Map (java.util.Map)6 HashSet (java.util.HashSet)4 ArrayList (java.util.ArrayList)3 Collection (java.util.Collection)3 BlankNodeOrIRI (org.apache.clerezza.commons.rdf.BlankNodeOrIRI)2 EnhancementEngineHelper.getString (org.apache.stanbol.enhancer.servicesapi.helper.EnhancementEngineHelper.getString)2 ServiceReference (org.osgi.framework.ServiceReference)2 Entry (java.util.Map.Entry)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 PlainLiteralImpl (org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl)1 TripleImpl (org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)1 IndexedGraph (org.apache.stanbol.commons.indexedgraph.IndexedGraph)1 ChainException (org.apache.stanbol.enhancer.servicesapi.ChainException)1 EngineException (org.apache.stanbol.enhancer.servicesapi.EngineException)1