Search in sources :

Example 6 with EnhancementEngine

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

the class EnhancementPropertyTest method testEngineSpecificPropertyOverrides.

@Test
public void testEngineSpecificPropertyOverrides() throws ChainException {
    initExecutionMetadata(new TestChain("test", engines));
    //(1) test a a enhancement property overridden with a engine specific one
    String specific = linking.getName();
    Map<String, Object> ep = ContentItemHelper.initRequestPropertiesContentPart(contentItem);
    assertNotNull("EnhancementProperties ContentPart was not initialised", ep);
    //global property
    ep.put(PROPERTY_MAX_SUGGESTIONS, "5");
    //engine specific
    ep.put(specific + ':' + PROPERTY_MAX_SUGGESTIONS, "10");
    //we expect enhancer.maxSuggestions=5 for the langdetect engine
    for (EnhancementEngine engine : engines) {
        Map<String, Object> props = EnhancementEngineHelper.getEnhancementProperties(engine, contentItem);
        assertNotNull(props);
        assertEquals(1, props.size());
        Assert.assertTrue(props.containsKey(PROPERTY_MAX_SUGGESTIONS));
        if (engine.getName().equals(specific)) {
            Assert.assertEquals("10", props.get(PROPERTY_MAX_SUGGESTIONS));
        } else {
            Assert.assertEquals("5", props.get(PROPERTY_MAX_SUGGESTIONS));
        }
    }
}
Also used : EnhancementEngine(org.apache.stanbol.enhancer.servicesapi.EnhancementEngine) Test(org.junit.Test)

Example 7 with EnhancementEngine

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

the class EnhancementPropertyTest method testSimpleChainScopedProperty.

@Test
public void testSimpleChainScopedProperty() throws ChainException {
    //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> chainProperties = new HashMap<String, Object>();
    chainProperties.put(PROPERTY_MAX_SUGGESTIONS, Integer.valueOf(5));
    enhancementProperties.put(null, chainProperties);
    initExecutionMetadata(new TestChain("test", engines, enhancementProperties));
    //we expect enhancer.maxSuggestions=5 for the all engine
    for (EnhancementEngine engine : engines) {
        Map<String, Object> props = EnhancementEngineHelper.getEnhancementProperties(engine, contentItem);
        assertNotNull(props);
        assertEquals(1, props.size());
        Assert.assertTrue(props.containsKey(PROPERTY_MAX_SUGGESTIONS));
        Assert.assertEquals("5", props.get(PROPERTY_MAX_SUGGESTIONS));
    }
}
Also used : HashMap(java.util.HashMap) EnhancementEngine(org.apache.stanbol.enhancer.servicesapi.EnhancementEngine) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 8 with EnhancementEngine

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

the class EnhancementPropertyTest method testRequestScopedPropertiesOverrideChainScopedProperties.

@Test
public void testRequestScopedPropertiesOverrideChainScopedProperties() throws ChainException {
    //This tests two cases:
    // 1. enhancer.maxSuggestions=5 set as chain scoped chain property overridden
    //    by enhancer.maxSuggestions=10 set as request scoped property for the
    //    linking engine
    Integer chainMaxSuggestion = 5;
    String requestMaySuggestion = "10";
    // 2. engine.dereference.language=[en,de] set as chain scoped dereference engine
    //    specific property is overridden by engine.dereference.language=[it,fr]
    //    set as request scoped property for the same engine
    Collection<String> chainDerefLang = Arrays.asList("en", "de");
    Collection<String> requestDerefLang = Arrays.asList("it", "fr");
    //set enhancer.maxSuggestions=5 as chain property (applies for all engines)
    //and enhancer.maxSuggestions=10 for the linking engine
    Map<String, Map<String, Object>> enhancementProperties = new HashMap<String, Map<String, Object>>();
    Map<String, Object> chainProperties = new HashMap<String, Object>();
    chainProperties.put(PROPERTY_MAX_SUGGESTIONS, chainMaxSuggestion);
    Map<String, Object> dereferenceProperties = new HashMap<String, Object>();
    dereferenceProperties.put(PROPERTY_DEREFERENCE_LANGUAGES, chainDerefLang);
    enhancementProperties.put(dereference.getName(), dereferenceProperties);
    enhancementProperties.put(null, chainProperties);
    initExecutionMetadata(new TestChain("test", engines, enhancementProperties));
    Map<String, Object> ep = ContentItemHelper.initRequestPropertiesContentPart(contentItem);
    assertNotNull("EnhancementProperties ContentPart was not initialised", ep);
    ep.put(linking.getName() + ':' + PROPERTY_MAX_SUGGESTIONS, requestMaySuggestion);
    ep.put(dereference.getName() + ':' + PROPERTY_DEREFERENCE_LANGUAGES, requestDerefLang);
    for (EnhancementEngine engine : engines) {
        Map<String, Object> props = EnhancementEngineHelper.getEnhancementProperties(engine, contentItem);
        assertNotNull(props);
        assertEquals(engine.getName().equals(dereference.getName()) ? 2 : 1, props.size());
        Assert.assertTrue(props.containsKey(PROPERTY_MAX_SUGGESTIONS));
        if (engine.getName().equals(linking.getName())) {
            Assert.assertEquals("10", props.get(PROPERTY_MAX_SUGGESTIONS));
        } else {
            Assert.assertEquals("5", props.get(PROPERTY_MAX_SUGGESTIONS));
        }
        if (engine.getName().equals(dereference.getName())) {
            assertTrue(props.containsKey(PROPERTY_DEREFERENCE_LANGUAGES));
            Object value = props.get(PROPERTY_DEREFERENCE_LANGUAGES);
            assertTrue(value instanceof Collection<?>);
            assertTrue(requestDerefLang.containsAll((Collection<?>) value));
            assertEquals(requestDerefLang.size(), ((Collection<?>) value).size());
        }
    }
}
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)

Example 9 with EnhancementEngine

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

the class EnhancerUtils method addActiveEngines.

/**
     * Create the RDF data for the currently active EnhancementEngines.<p>
     * Note the the parsed rootUrl MUST already consider offsets configured
     * for the Stanbol RESTful service. When called from within a
     * {@link BaseStanbolResource} the following code segment should be used:<p>
     * <code><pre>
     *     String rootUrl = uriInfo.getBaseUriBuilder().path(getRootUrl()).build().toString();
     * </pre></code>
     * @param activeEngines the active enhancement engines as {@link Entry entries}.
     * @param graph the RDF graph to add the triples
     * @param rootUrl the root URL used by the current request
     * @see EnhancerUtils#buildEnginesMap(EnhancementEngineManager)
     */
public static void addActiveEngines(Iterable<Entry<ServiceReference, EnhancementEngine>> activeEngines, Graph graph, String rootUrl) {
    IRI enhancerResource = new IRI(rootUrl + "enhancer");
    graph.add(new TripleImpl(enhancerResource, RDF.type, Enhancer.ENHANCER));
    for (Entry<ServiceReference, EnhancementEngine> entry : activeEngines) {
        IRI engineResource = new IRI(rootUrl + "enhancer/engine/" + entry.getValue().getName());
        graph.add(new TripleImpl(enhancerResource, Enhancer.HAS_ENGINE, engineResource));
        graph.add(new TripleImpl(engineResource, RDF.type, ENHANCEMENT_ENGINE));
        graph.add(new TripleImpl(engineResource, RDFS.label, new PlainLiteralImpl(entry.getValue().getName())));
    }
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) PlainLiteralImpl(org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl) EnhancementEngine(org.apache.stanbol.enhancer.servicesapi.EnhancementEngine) ServiceReference(org.osgi.framework.ServiceReference)

Example 10 with EnhancementEngine

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

the class EnhancerUtils method buildEnginesMap.

/**
     * Uses the parsed {@link EnhancementEngineManager} to build a Map
     * representing the current snapshot of the active enhancement engines.
     * 
     * @param engineManager The engine manager used to build the snapshot
     * @return the map with the names as key and an Entry with the {@link ServiceReference}
     * and the {@link EnhancementEngine} instance as value.
     */
public static Map<String, Entry<ServiceReference, EnhancementEngine>> buildEnginesMap(EnhancementEngineManager engineManager) {
    Map<String, Entry<ServiceReference, EnhancementEngine>> engines = new HashMap<String, Map.Entry<ServiceReference, EnhancementEngine>>();
    for (String engineName : engineManager.getActiveEngineNames()) {
        ServiceReference engineRef = engineManager.getReference(engineName);
        if (engineRef != null) {
            EnhancementEngine engine = engineManager.getEngine(engineRef);
            if (engine != null) {
                Map<ServiceReference, EnhancementEngine> m = Collections.singletonMap(engineRef, engine);
                engines.put(engineName, m.entrySet().iterator().next());
            }
        }
    }
    return engines;
}
Also used : Entry(java.util.Map.Entry) HashMap(java.util.HashMap) EnhancementEngine(org.apache.stanbol.enhancer.servicesapi.EnhancementEngine) HashMap(java.util.HashMap) Map(java.util.Map) ServiceReference(org.osgi.framework.ServiceReference)

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