use of eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException in project hale by halestudio.
the class CustomGroovyTransformation 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 {
// store script as parameter
if (!CustomPropertyFunctionType.GROOVY.equals(customFunction.getFunctionType())) {
throw new TransformationException("Custom function is not of type groovy");
}
Value scriptValue = customFunction.getFunctionDefinition();
if (scriptValue.isEmpty()) {
throw new NoResultException("Script not defined");
}
ListMultimap<String, ParameterValue> params = ArrayListMultimap.create();
params.put(GroovyTransformation.PARAMETER_SCRIPT, new ParameterValue(scriptValue));
setParameters(params);
// instance builder
InstanceBuilder builder = GroovyTransformation.createBuilder(resultProperty);
// create the script binding
Binding binding = createGroovyBinding(variables, getCell(), getTypeCell(), builder, log, getExecutionContext(), resultProperty.getDefinition().getPropertyType());
Object result;
try {
GroovyService service = getExecutionContext().getService(GroovyService.class);
Script groovyScript = GroovyUtil.getScript(this, binding, service, true);
// evaluate the script
result = GroovyTransformation.evaluate(groovyScript, builder, resultProperty.getDefinition().getPropertyType(), service, log);
} catch (TransformationException | NoResultException e) {
throw e;
} catch (Throwable e) {
throw new TransformationException("Error evaluating the custom function script", e);
}
if (result == null) {
throw new NoResultException();
}
return result;
}
use of eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException in project hale by halestudio.
the class GroovyCreate method createInstances.
private Iterable<MutableInstance> createInstances(TypeDefinition type, TransformationLog log, Cell cell, int index) throws TransformationException {
InstanceBuilder builder = new InstanceBuilder(false);
Binding binding = GroovyUtil.createBinding(builder, cell, cell, log, getExecutionContext(), type);
binding.setProperty(BINDING_INDEX, index);
try {
GroovyService service = getExecutionContext().getService(GroovyService.class);
Script script = GroovyUtil.getScript(this, binding, service);
return GroovyUtil.evaluateAll(script, builder, type, service, log);
} catch (TransformationException e) {
throw e;
} catch (NoResultException e) {
log.info(log.createMessage("Skipping target instance because received NoResultException from script", null));
return Collections.emptyList();
} catch (Exception e) {
throw new TransformationException(e.getMessage(), e);
}
}
use of eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException in project hale by halestudio.
the class GroovyGreedyTransformation 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 {
// determine if instances should be used in variables or their values
boolean useInstanceVariables = getOptionalParameter(PARAM_INSTANCE_VARIABLES, Value.of(false)).as(Boolean.class);
// instance builder
InstanceBuilder builder = GroovyTransformation.createBuilder(resultProperty);
// create the script binding
Binding binding = createGroovyBinding(variables.get(ENTITY_VARIABLE), getCell().getSource().get(ENTITY_VARIABLE), getCell(), getTypeCell(), builder, useInstanceVariables, log, getExecutionContext(), resultProperty.getDefinition().getPropertyType());
Object result;
try {
GroovyService service = getExecutionContext().getService(GroovyService.class);
Script groovyScript = GroovyUtil.getScript(this, binding, service);
// evaluate the script
result = GroovyTransformation.evaluate(groovyScript, builder, resultProperty.getDefinition().getPropertyType(), service, log);
} catch (NoResultException | TransformationException e) {
throw e;
} catch (Throwable e) {
throw new TransformationException("Error evaluating the cell script", e);
}
if (result == null) {
throw new NoResultException();
}
return result;
}
use of eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException in project hale by halestudio.
the class GroovyRetype method execute.
@Override
public void execute(String transformationIdentifier, TransformationEngine engine, Map<String, String> executionParameters, TransformationLog log, Cell cell) throws TransformationException {
// for each source instance create a target instance
TypeDefinition targetType = getTarget().values().iterator().next().getDefinition().getDefinition();
InstanceBuilder builder = new InstanceBuilder(false);
Binding binding = createBinding(getSource(), cell, builder, log, getExecutionContext(), targetType);
try {
GroovyService service = getExecutionContext().getService(GroovyService.class);
Script script = GroovyUtil.getScript(this, binding, service);
Iterable<MutableInstance> targets = GroovyUtil.evaluateAll(script, builder, targetType, service, log);
for (MutableInstance target : targets) {
getPropertyTransformer().publish(getSource(), target, log, cell);
}
} catch (TransformationException e) {
throw e;
} catch (NoResultException e) {
log.info(log.createMessage("Skipping target instance because received NoResultException from script", null));
} catch (Exception e) {
throw new TransformationException(e.getMessage(), e);
}
}
use of eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException in project hale by halestudio.
the class GroovyUtil method evaluate.
/**
* Evaluate a Groovy type script.
*
* @param script the script
* @param builder the instance builder
* @param type the type of the instance to create
* @param service the Groovy service
* @param log the log
* @return the created instance
* @throws TransformationException if the target binding does not contain
* exactly one result after script evaluation or an internal
* error occurs
* @throws NoResultException if the script implies that no result should be
* created
*/
public static MutableInstance evaluate(Script script, final InstanceBuilder builder, final TypeDefinition type, GroovyService service, SimpleLog log) throws TransformationException, NoResultException {
Iterable<MutableInstance> results = evaluateAll(script, builder, type, service, log);
Iterator<MutableInstance> it = results.iterator();
MutableInstance result;
if (it.hasNext()) {
result = it.next();
if (it.hasNext()) {
throw new TransformationException("Cell script does not produce exactly one result.");
}
} else {
throw new NoResultException();
}
return result;
}
Aggregations