use of eu.esdihumboldt.hale.common.scripting.scripts.mathematical.MathScript 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