Search in sources :

Example 1 with MathScript

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.");
}
Also used : ConversionException(org.springframework.core.convert.ConversionException) Binding(eu.esdihumboldt.hale.common.schema.model.constraint.type.Binding) TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) PropertyValue(eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) Geometry(org.locationtech.jts.geom.Geometry) ScriptException(javax.script.ScriptException) MathScript(eu.esdihumboldt.hale.common.scripting.scripts.mathematical.MathScript)

Aggregations

PropertyValue (eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue)1 TransformationException (eu.esdihumboldt.hale.common.align.transformation.function.TransformationException)1 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)1 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)1 Binding (eu.esdihumboldt.hale.common.schema.model.constraint.type.Binding)1 MathScript (eu.esdihumboldt.hale.common.scripting.scripts.mathematical.MathScript)1 ScriptException (javax.script.ScriptException)1 Geometry (org.locationtech.jts.geom.Geometry)1 ConversionException (org.springframework.core.convert.ConversionException)1