use of org.apache.stanbol.enhancer.servicesapi.EnhancementEngine in project stanbol by apache.
the class EnhancementPropertyTest method testSimpleProperty.
@Test
public void testSimpleProperty() throws ChainException {
initExecutionMetadata(new TestChain("test", engines));
//(1) test a a enhancement property overridden with a engine specific one
Map<String, Object> ep = ContentItemHelper.initRequestPropertiesContentPart(contentItem);
assertNotNull("EnhancementProperties ContentPart was not initialised", ep);
//global property
ep.put(PROPERTY_MAX_SUGGESTIONS, "5");
//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));
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 testEngineSpecificProperties.
@Test
public void testEngineSpecificProperties() throws ChainException {
initExecutionMetadata(new TestChain("test", engines));
Collection<String> derefernceLanguages = Arrays.asList("en", "de");
String maxSuggestions = "5";
//(1) test a a enhancement property overridden with a engine specific one
Map<String, Object> ep = ContentItemHelper.initRequestPropertiesContentPart(contentItem);
assertNotNull("EnhancementProperties ContentPart was not initialised", ep);
ep.put(linking.getName() + ':' + PROPERTY_MAX_SUGGESTIONS, maxSuggestions);
ep.put(dereference.getName() + ':' + PROPERTY_DEREFERENCE_LANGUAGES, derefernceLanguages);
//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
}
}
use of org.apache.stanbol.enhancer.servicesapi.EnhancementEngine in project stanbol by apache.
the class EnhancementPropertyTest method testMultiValueProperty.
@Test
public void testMultiValueProperty() throws ChainException {
initExecutionMetadata(new TestChain("test", engines));
Collection<String> derefernceLanguages = Arrays.asList("en", "de");
//(1) test a a enhancement property overridden with a engine specific one
Map<String, Object> ep = ContentItemHelper.initRequestPropertiesContentPart(contentItem);
assertNotNull("EnhancementProperties ContentPart was not initialised", ep);
//global property
ep.put(PROPERTY_DEREFERENCE_LANGUAGES, derefernceLanguages);
//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_DEREFERENCE_LANGUAGES));
Assert.assertEquals(derefernceLanguages, props.get(PROPERTY_DEREFERENCE_LANGUAGES));
}
}
use of org.apache.stanbol.enhancer.servicesapi.EnhancementEngine in project stanbol by apache.
the class EnhancementPropertyTest method testChainScopedEngineSpecificPropertyOverrides.
/**
* This tests that chain scoped chain properties are overridden by
* chain scoped engine specific properties
* @throws ChainException
*/
@Test
public void testChainScopedEngineSpecificPropertyOverrides() throws ChainException {
//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, Integer.valueOf(5));
enhancementProperties.put(null, chainProperties);
Map<String, Object> linkingProperties = new HashMap<String, Object>();
linkingProperties.put(PROPERTY_MAX_SUGGESTIONS, Integer.valueOf(10));
enhancementProperties.put(linking.getName(), linkingProperties);
initExecutionMetadata(new TestChain("test", engines, enhancementProperties));
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(linking.getName())) {
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 ExecutionPlanHelper method calculateExecutionPlan.
/**
* Creates an execution plan based on the
* {@link ServiceProperties#ENHANCEMENT_ENGINE_ORDERING} of the parsed
* EnhancementEngines. NOTE that the parsed list is modified as it is sorted by
* using the {@link EnhancementEngineHelper#EXECUTION_ORDER_COMPARATOR}.<p>
* A second parameter with the set of optional engines can be used to define
* what {@link ExecutionPlan#EXECUTION_NODE} in the execution plan should be
* marked as {@link ExecutionPlan#OPTIONAL}.
* @param chainName the name of the Chain to build the execution plan for
* @param availableEngines the list of engines
* @param optional the names of optional engines.
* @param missing the names of missing engines
* @param enhProps chain scoped enhancement properties. The key of the outer
* map are the name of the engine or <code>null</code> for the chain. The
* inner map uses the property as key and the value(s) as value. Multiple
* values can be parsed as {@link Collection}. Single values will be
* converted to RDF {@link TypedLiteral}s by using the {@link LiteralFactory}.
* For types not supported by the LiteralFactory the <code>toString()</code>
* method will be used. <code>null</code> can be parsed if no enhancement
* properties are present.
* @return the execution plan
* @since 0.12.1
*/
public static ImmutableGraph calculateExecutionPlan(String chainName, List<EnhancementEngine> availableEngines, Set<String> optional, Set<String> missing, Map<String, Map<String, Object>> enhProps) {
if (chainName == null || chainName.isEmpty()) {
throw new IllegalArgumentException("The parsed ChainName MUST NOT be empty!");
}
Collections.sort(availableEngines, EXECUTION_ORDER_COMPARATOR);
//now we have all required and possible also optional engines
// -> build the execution plan
Graph ep = new IndexedGraph();
BlankNodeOrIRI epNode = createExecutionPlan(ep, chainName, enhProps != null ? enhProps.get(null) : null);
Integer prevOrder = null;
Set<BlankNodeOrIRI> prev = null;
Set<BlankNodeOrIRI> current = new HashSet<BlankNodeOrIRI>();
for (String name : missing) {
boolean optionalMissing = optional.contains(name);
BlankNodeOrIRI node = writeExecutionNode(ep, epNode, name, optionalMissing, null, enhProps == null ? null : enhProps.get(name));
if (!optionalMissing) {
current.add(node);
}
// else add missing optional engines without any dependsOn restrictions
}
for (EnhancementEngine engine : availableEngines) {
String name = engine.getName();
Integer order = getEngineOrder(engine);
if (prevOrder == null || !prevOrder.equals(order)) {
prev = current;
current = new HashSet<BlankNodeOrIRI>();
prevOrder = order;
}
try {
BlankNodeOrIRI executionNode = writeExecutionNode(ep, epNode, name, optional.contains(name), prev, enhProps == null ? null : enhProps.get(name));
current.add(executionNode);
} catch (RuntimeException e) {
//add the engine and class to ease debugging in such cases
log.error("Exception while writing ExecutionNode for Enhancement Eninge: " + engine + "(class: " + engine.getClass() + ")", e);
//rethrow it
throw e;
}
}
return ep.getImmutableGraph();
}
Aggregations