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));
}
}
}
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));
}
}
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());
}
}
}
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())));
}
}
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;
}
Aggregations