Search in sources :

Example 1 with PropertyValue

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;
}
Also used : PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) ArrayList(java.util.ArrayList) PropertyValue(eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue) PropertyValueImpl(eu.esdihumboldt.hale.common.align.transformation.function.impl.PropertyValueImpl)

Example 2 with PropertyValue

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;
}
Also used : Binding(groovy.lang.Binding) ParameterBinding(eu.esdihumboldt.hale.common.align.model.impl.mdexpl.ParameterBinding) InstanceAccessorArrayList(eu.esdihumboldt.cst.functions.groovy.internal.InstanceAccessorArrayList) PropertyValue(eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue) DefaultCustomPropertyFunctionEntity(eu.esdihumboldt.hale.common.align.custom.DefaultCustomPropertyFunctionEntity) ParameterBinding(eu.esdihumboldt.hale.common.align.model.impl.mdexpl.ParameterBinding)

Example 3 with PropertyValue

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;
}
Also used : Binding(groovy.lang.Binding) ParameterBinding(eu.esdihumboldt.hale.common.align.model.impl.mdexpl.ParameterBinding) Script(groovy.lang.Script) TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) NoResultException(eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException) GroovyService(eu.esdihumboldt.util.groovy.sandbox.GroovyService) PropertyValue(eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue) Value(eu.esdihumboldt.hale.common.core.io.Value) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) InstanceBuilder(eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder)

Example 4 with PropertyValue

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.");
    }
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) DepthFirstInstanceTraverser(eu.esdihumboldt.hale.common.instance.helper.DepthFirstInstanceTraverser) InstanceTraverser(eu.esdihumboldt.hale.common.instance.helper.InstanceTraverser) GeometryProperty(eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty) TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) GeometryFinder(eu.esdihumboldt.hale.common.instance.geometry.GeometryFinder) PropertyValue(eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue) DepthFirstInstanceTraverser(eu.esdihumboldt.hale.common.instance.helper.DepthFirstInstanceTraverser)

Example 5 with PropertyValue

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.");
}
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(com.vividsolutions.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)24 TransformationException (eu.esdihumboldt.hale.common.align.transformation.function.TransformationException)9 NoResultException (eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException)7 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)7 Geometry (com.vividsolutions.jts.geom.Geometry)6 PropertyEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition)5 ConversionException (org.springframework.core.convert.ConversionException)5 EntityDefinition (eu.esdihumboldt.hale.common.align.model.EntityDefinition)4 PropertyValueImpl (eu.esdihumboldt.hale.common.align.transformation.function.impl.PropertyValueImpl)4 Binding (groovy.lang.Binding)4 ArrayList (java.util.ArrayList)4 ParameterValue (eu.esdihumboldt.hale.common.align.model.ParameterValue)3 Value (eu.esdihumboldt.hale.common.core.io.Value)3 GeometryFinder (eu.esdihumboldt.hale.common.instance.geometry.GeometryFinder)3 DepthFirstInstanceTraverser (eu.esdihumboldt.hale.common.instance.helper.DepthFirstInstanceTraverser)3 InstanceTraverser (eu.esdihumboldt.hale.common.instance.helper.InstanceTraverser)3 GeometryProperty (eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty)3 Environment (com.iabcinc.jmep.Environment)2 XExpression (com.iabcinc.jmep.XExpression)2 Constant (com.iabcinc.jmep.hooks.Constant)2