use of eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue in project hale by halestudio.
the class MathEditor method createPropertyValues.
/**
* Returns an {@link Iterable} for the current variables for use with the
* {@link Script}.
*
* @return an {@link Iterable} for the current variables for use with the
* {@link Script}
*/
protected Iterable<PropertyValue> createPropertyValues() {
Collection<PropertyValue> result = new ArrayList<PropertyValue>(variables.size());
// using double results in no /0 exceptions because of stuff like
// 1/(a-b)
Double one = Double.valueOf(1);
for (PropertyEntityDefinition property : variables) result.add(new PropertyValueImpl(one, property));
return result;
}
use of eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue in project hale by halestudio.
the class CustomGroovyTransformation method createGroovyBinding.
private Binding createGroovyBinding(ListMultimap<String, PropertyValue> variables, Cell cell, Cell typeCell, InstanceBuilder builder, TransformationLog log, ExecutionContext executionContext, TypeDefinition targetInstanceType) {
Binding binding = GroovyUtil.createBinding(builder, cell, typeCell, log, executionContext, targetInstanceType);
// create bindings for inputs
for (DefaultCustomPropertyFunctionEntity source : customFunction.getSources()) {
String varName = source.getName();
boolean useInstanceVariable = useInstanceVariableForSource(source);
List<PropertyValue> values = variables.get(varName);
if (source.isEager() || source.getMaxOccurrence() > 1 || source.getMaxOccurrence() == ParameterDefinition.UNBOUNDED) {
// multiple values
InstanceAccessorArrayList<Object> valueList = new InstanceAccessorArrayList<>();
for (PropertyValue value : values) {
valueList.add(GroovyTransformation.getUseValue(value.getValue(), useInstanceVariable));
}
binding.setVariable(varName, valueList);
} else {
// single value
if (values.isEmpty()) {
// no value
// -> use null value for missing variable
binding.setVariable(varName, null);
} else {
// value
binding.setVariable(varName, GroovyTransformation.getUseValue(values.get(0).getValue(), useInstanceVariable));
}
}
}
// create binding(s) for parameters
binding.setVariable(BINDING_PARAMS, new ParameterBinding(cell, customFunction.getDescriptor()));
return binding;
}
use of eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue 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.PropertyValue in project hale by halestudio.
the class CalculateArea method evaluate.
/**
* @see eu.esdihumboldt.hale.common.align.transformation.function.impl.AbstractSingleTargetPropertyTransformation#evaluate(java.lang.String,
* eu.esdihumboldt.hale.common.align.transformation.engine.TransformationEngine,
* com.google.common.collect.ListMultimap, java.lang.String,
* eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition,
* java.util.Map,
* eu.esdihumboldt.hale.common.align.transformation.report.TransformationLog)
*/
@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 {
// get input geometry
PropertyValue input = variables.get(null).get(0);
Object inputValue = input.getValue();
// depth first traverser that on cancel continues traversal but w/o the
// children of the current object
InstanceTraverser traverser = new DepthFirstInstanceTraverser(true);
GeometryFinder geoFind = new GeometryFinder(null);
traverser.traverse(inputValue, geoFind);
List<GeometryProperty<?>> geoms = geoFind.getGeometries();
Geometry geom = null;
if (geoms.size() > 1) {
int area = 0;
for (GeometryProperty<?> geoProp : geoms) {
area += geoProp.getGeometry().getArea();
}
return area;
} else {
geom = geoms.get(0).getGeometry();
}
if (geom != null) {
return geom.getArea();
} else {
throw new TransformationException("Geometry for calculate area could not be retrieved.");
}
}
use of eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue in project hale by halestudio.
the class NetworkExpansion method evaluate.
/**
* @see AbstractSingleTargetScriptedPropertyTransformation#evaluate(String,
* TransformationEngine, ListMultimap, String,
* PropertyEntityDefinition, Map, TransformationLog)
*/
@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 {
// get the buffer width parameter
String bufferWidthString = getTransformedParameterChecked(PARAMETER_BUFFER_WIDTH).as(String.class);
double bufferWidth;
try {
bufferWidth = Double.parseDouble(bufferWidthString);
} catch (NumberFormatException e) {
// For backwards compatibility try to run the string as script.
MathScript mathScript = new MathScript();
try {
Object result = mathScript.evaluate(bufferWidthString, variables.get(ENTITY_VARIABLE), getExecutionContext());
bufferWidth = ConversionUtil.getAs(result, Double.class);
} catch (ScriptException e1) {
throw new TransformationException("Failed to evaluate buffer width expression.", e1);
} catch (ConversionException e2) {
throw new TransformationException("Failed to convert buffer width expression result to double.", e2);
}
}
// get input geometry
PropertyValue input = variables.get(null).get(0);
Object inputValue = input.getValue();
if (inputValue instanceof Instance) {
inputValue = ((Instance) inputValue).getValue();
}
GeometryProperty<Geometry> result = calculateBuffer(inputValue, bufferWidth, log);
// try to yield a result compatible to the target
if (result != null) {
TypeDefinition targetType = resultProperty.getDefinition().getPropertyType();
// TODO check element type?
Class<?> binding = targetType.getConstraint(Binding.class).getBinding();
if (Geometry.class.isAssignableFrom(binding) && binding.isAssignableFrom(result.getGeometry().getClass())) {
return result.getGeometry();
} else {
return result;
}
}
throw new TransformationException("Geometry for network expansion could not be retrieved.");
}
Aggregations