use of eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue in project hale by halestudio.
the class GroovyScript method createGroovyBinding.
/**
* Create a Groovy binding from the list of variables.
*
* FIXME why is here an additional implementation of this as already used in
* GroovyTransformation? Could the implementation in GroovyTransformation be
* used instead? FIXME It could (should?) only be the other way around
* (because of dependencies); or it should be at another "common"-place
*
* @param variables the variables
* @param useNullForMissingBindings if the binding should provide
* <code>null</code> values for variables that are not provided
* in the given variable list
* @return the binding for use with {@link GroovyShell}
*/
private Binding createGroovyBinding(Iterable<PropertyValue> variables, boolean useNullForMissingBindings) {
Binding binding;
if (useNullForMissingBindings) {
binding = new Binding() {
@Override
public Object getVariable(String name) {
try {
return super.getVariable(name);
} catch (MissingPropertyException mpe) {
// use null value for variables that are not defined
return null;
}
}
};
} else
binding = new Binding();
for (PropertyValue var : variables) {
// add the variable to the environment
// determine the variable value
Object value = var.getValue();
if (value instanceof Instance) {
// XXX check if there are
value = ((Instance) value).getValue();
// any properties?
}
if (value instanceof Number) {
// use numbers as is
} else {
// try conversion to String as default
try {
value = var.getValueAs(String.class);
} catch (ConversionException ce) {
// XXX currently ignored conversion exception
continue;
}
}
// add with short name, if it does not override something
String name = var.getProperty().getDefinition().getName().getLocalPart();
if (!binding.getVariables().containsKey(name))
binding.setVariable(name, value);
// add with full name
binding.setVariable(getVariableName(var.getProperty()), value);
}
return binding;
}
use of eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue in project hale by halestudio.
the class AbstractScriptedPropertyTransformation method evaluate.
@Override
protected final ListMultimap<String, Object> evaluate(String transformationIdentifier, E engine, ListMultimap<String, PropertyValue> variables, ListMultimap<String, PropertyEntityDefinition> resultNames, Map<String, String> executionParameters, TransformationLog log) throws TransformationException {
ListMultimap<String, ParameterValue> originalParameters = getParameters();
ListMultimap<String, Value> transformedParameters = ArrayListMultimap.create();
if (originalParameters != null) {
for (Map.Entry<String, ParameterValue> entry : originalParameters.entries()) {
if (!entry.getValue().needsProcessing()) {
Value value = entry.getValue().intern();
if (!value.isRepresentedAsDOM()) {
value = Value.simple(getExecutionContext().getVariables().replaceVariables(value.getStringRepresentation()));
}
transformedParameters.put(entry.getKey(), value);
} else {
// type is a script
ScriptFactory factory = ScriptExtension.getInstance().getFactory(entry.getValue().getType());
if (factory == null)
throw new TransformationException("Couldn't find factory for script id " + entry.getValue().getType());
Script script;
try {
script = factory.createExtensionObject();
} catch (Exception e) {
throw new TransformationException("Couldn't create script from factory", e);
}
Object result;
try {
String scriptStr = entry.getValue().as(String.class);
if (script.requiresReplacedTransformationVariables()) {
// replace transformation variables
scriptStr = getExecutionContext().getVariables().replaceVariables(scriptStr);
}
result = script.evaluate(scriptStr, variables.values(), getExecutionContext());
} catch (ScriptException e) {
throw new TransformationException("Couldn't evaluate a transformation parameter", e);
}
// XXX use conversion service instead of valueOf?
transformedParameters.put(entry.getKey(), Value.simple(result));
}
}
}
this.transformedParameters = Multimaps.unmodifiableListMultimap(transformedParameters);
return evaluateImpl(transformationIdentifier, engine, variables, resultNames, executionParameters, log);
}
use of eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue in project hale by halestudio.
the class ClassificationMapping method evaluate.
@Override
protected Object evaluate(String transformationIdentifier, TransformationEngine engine, ListMultimap<String, PropertyValue> variables, String resultName, PropertyEntityDefinition resultProperty, Map<String, String> executionParameters, TransformationLog log) throws TransformationException, NoResultException {
String source = variables.values().iterator().next().getValueAs(String.class);
LookupTable lookup = ClassificationMappingUtil.getClassificationLookup(getParameters(), getExecutionContext());
if (lookup == null) {
// throw new TransformationException("No classification specified");
log.warn(log.createMessage("No classification specified", null));
} else {
Value target = lookup.lookup(Value.of(source));
if (target != null) {
// return value w/ transformation variables replaced
return getExecutionContext().getVariables().replaceVariables(target);
}
}
String notClassifiedAction = getOptionalParameter(PARAMETER_NOT_CLASSIFIED_ACTION, Value.of(USE_NULL_ACTION)).as(String.class);
if (USE_SOURCE_ACTION.equals(notClassifiedAction)) {
return source;
} else if (notClassifiedAction.startsWith(USE_FIXED_VALUE_ACTION_PREFIX)) {
String notClassified = notClassifiedAction.substring(notClassifiedAction.indexOf(':') + 1);
// return w/ transformation variables replaced
return getExecutionContext().getVariables().replaceVariables(notClassified);
} else {
// return null;
throw new NoResultException();
}
}
use of eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue in project hale by halestudio.
the class GroovyTransformationPage method validate.
@Override
protected boolean validate(String document) {
super.validate(document);
List<PropertyValue> values = new ArrayList<PropertyValue>();
for (EntityDefinition var : getVariables()) {
if (var instanceof PropertyEntityDefinition) {
PropertyEntityDefinition property = (PropertyEntityDefinition) var;
values.add(new PropertyValueImpl(testValues.get(property), property));
}
}
Property targetProperty = (Property) CellUtil.getFirstEntity(getWizard().getUnfinishedCell().getTarget());
if (targetProperty == null) {
// not yet selected (NewRelationWizard)
return false;
}
InstanceBuilder builder = GroovyTransformation.createBuilder(targetProperty.getDefinition());
Cell cell = getWizard().getUnfinishedCell();
boolean useInstanceValues = CellUtil.getOptionalParameter(cell, GroovyTransformation.PARAM_INSTANCE_VARIABLES, Value.of(false)).as(Boolean.class);
AlignmentService as = PlatformUI.getWorkbench().getService(AlignmentService.class);
GroovyService gs = HaleUI.getServiceProvider().getService(GroovyService.class);
Script script = null;
try {
Collection<? extends Cell> typeCells = as.getAlignment().getTypeCells(cell);
// select one matching type cell, the script has to run for all
// matching cells
// if there is no matching cell it may produce a npe, which is okay
Cell typeCell = null;
if (!typeCells.isEmpty()) {
typeCell = typeCells.iterator().next();
}
CellLog log = new CellLog(new DefaultTransformationReporter("dummy", false), cell);
ExecutionContext context = new DummyExecutionContext(HaleUI.getServiceProvider());
groovy.lang.Binding binding;
if (cell.getTransformationIdentifier().equals(GroovyGreedyTransformation.ID)) {
binding = GroovyGreedyTransformation.createGroovyBinding(values, null, cell, typeCell, builder, useInstanceValues, log, context, targetProperty.getDefinition().getDefinition().getPropertyType());
} else {
binding = GroovyTransformation.createGroovyBinding(values, null, cell, typeCell, builder, useInstanceValues, log, context, targetProperty.getDefinition().getDefinition().getPropertyType());
}
script = gs.parseScript(document, binding);
GroovyTransformation.evaluate(script, builder, targetProperty.getDefinition().getDefinition().getPropertyType(), gs, log);
} catch (NoResultException e) {
// continue
} catch (final Exception e) {
return handleValidationResult(script, e);
}
return handleValidationResult(script, null);
}
use of eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue in project hale by halestudio.
the class FunctionExecutor method executeTransformation.
/**
* Execute a property transformation.
*
* @param transformation the transformation factory
* @param cell the alignment cell
* @param sources the named source entities and nodes
* @param targets the named target entities and nodes
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
protected void executeTransformation(PropertyTransformationFactory transformation, Cell cell, ListMultimap<String, Pair<SourceNode, Entity>> sources, ListMultimap<String, Pair<TargetNode, Entity>> targets) {
TransformationLog cellLog = new CellLog(reporter, cell);
PropertyTransformation<?> function;
try {
// TODO cache function objects?
function = transformation.createExtensionObject();
} catch (Exception e) {
cellLog.error(cellLog.createMessage("Error creating transformation function.", e));
return;
}
TransformationEngine engine = engines.get(transformation.getEngineId(), cellLog);
if (engine == null) {
// TODO instead try another transformation
cellLog.error(cellLog.createMessage("Skipping property transformation: No matching transformation engine found", null));
return;
}
// configure function
// set expected result
ListMultimap<String, PropertyEntityDefinition> expectedResult = ArrayListMultimap.create(targets.keySet().size(), 1);
for (Entry<String, Pair<TargetNode, Entity>> targetEntry : targets.entries()) {
EntityDefinition def = targetEntry.getValue().getSecond().getDefinition();
expectedResult.put(targetEntry.getKey(), toPropertyEntityDefinition(def));
}
function.setExpectedResult(expectedResult);
// set source variables
ListMultimap<String, PropertyValue> variables = ArrayListMultimap.create();
for (Entry<String, Pair<SourceNode, Entity>> sourceEntry : sources.entries()) {
EntityDefinition def = sourceEntry.getValue().getSecond().getDefinition();
SourceNode sourceNode = sourceEntry.getValue().getFirst();
if (TransformationTreeUtil.isEager(cell, sourceNode, cellLog, context.getServiceProvider())) {
// eager source - all values
Object[] values = sourceNode.getAllValues();
if (values != null) {
for (int i = 0; i < values.length; i++) {
PropertyValue propertyValue = new PropertyValueImpl(values[i], toPropertyEntityDefinition(def));
variables.put(sourceEntry.getKey(), propertyValue);
}
}
} else {
// non-eager source - one value
Object value = sourceNode.getValue();
PropertyValue propertyValue = new PropertyValueImpl(value, toPropertyEntityDefinition(def));
variables.put(sourceEntry.getKey(), propertyValue);
}
}
function.setVariables(variables);
// set parameters
function.setParameters(cell.getTransformationParameters());
// set context
function.setExecutionContext(context.getCellContext(cell));
// set target type
TypeDefinition targetType = null;
if (!targets.isEmpty()) {
TargetNode target = targets.values().iterator().next().getFirst();
targetType = target.getEntityDefinition().getType();
}
function.setTargetType(targetType);
function.setTypeCell(typeCell.get());
// execute function
try {
((PropertyTransformation) function).execute(transformation.getIdentifier(), engine, transformation.getExecutionParameters(), cellLog, cell);
} catch (Throwable e) {
// TODO instead try another transformation?
cellLog.error(cellLog.createMessage("Skipping property transformation: Executing property transformation failed.", e));
return;
}
// apply function results
ListMultimap<String, Object> results = function.getResults();
if (results != null) {
for (String name : results.keySet()) {
List<Object> values = results.get(name);
List<Pair<TargetNode, Entity>> nodes = targets.get(name);
if (nodes.size() > values.size()) {
cellLog.warn(cellLog.createMessage(MessageFormat.format("Transformation result misses values for result with name {0}", name), null));
}
if (values.size() > nodes.size()) {
cellLog.warn(cellLog.createMessage(MessageFormat.format("More transformation results than target nodes for result with name {0}", name), null));
}
int count = Math.min(values.size(), nodes.size());
// node...
for (int i = 0; i < count; i++) {
Object value = values.get(i);
TargetNode node = nodes.get(i).getFirst();
if (value instanceof MultiValue) {
MultiValue originalValue = (MultiValue) value;
MultiValue processedValue = new MultiValue(originalValue.size());
for (Object o : originalValue) {
processedValue.add(processValue(cellLog, function, o, node));
}
value = processedValue;
} else {
value = processValue(cellLog, function, value, node);
}
/*
* TODO
*
* set node value only if no result has already been set. If
* a value is already there and we are in a lower priority
* executor, we do not overwrite.
*/
if (!node.isDefined()) {
node.setResult(value);
}
}
}
}
}
Aggregations