Search in sources :

Example 1 with Environment

use of com.iabcinc.jmep.Environment in project hale by halestudio.

the class MathExpressionParameterPage method sourcePropertiesChanged.

/**
 * @see TextSourceListParameterPage#sourcePropertiesChanged(Iterable)
 */
@Override
protected void sourcePropertiesChanged(Iterable<EntityDefinition> variables) {
    super.sourcePropertiesChanged(variables);
    // update environment
    environment = new Environment();
    for (EntityDefinition variable : variables) {
        environment.addVariable(getVariableName(variable), new Constant(new Double(1)));
    }
    // re set text to get modify event
    textField.setText(textField.getText());
}
Also used : EntityDefinition(eu.esdihumboldt.hale.common.align.model.EntityDefinition) Constant(com.iabcinc.jmep.hooks.Constant) Environment(com.iabcinc.jmep.Environment)

Example 2 with Environment

use of com.iabcinc.jmep.Environment in project hale by halestudio.

the class MathematicalExpression method evaluateExpression.

/**
 * Evaluate a mathematical expression.
 *
 * @param expression the mathematical expression. It may contain references
 *            to variables
 * @param vars the list of available property values that may be bound to
 *            variables
 * @return the evaluated expression, which can be Double, Integer or String
 * @throws XExpression if the expression could not be evaluated
 */
public static Object evaluateExpression(String expression, List<PropertyValue> vars) throws XExpression {
    Environment env = new Environment();
    for (PropertyValue var : vars) {
        // add the variable to the environment
        // determine the variable value
        Object value = var.getValue();
        Number number;
        if (value instanceof Number) {
            number = (Number) value;
        } else {
            // try conversion to Double as default
            number = var.getValueAs(Double.class);
        }
        // Floats
        if (!(number instanceof Integer) && !(number instanceof Double)) {
            number = number.doubleValue();
        }
        // determine the variable name
        String name = var.getProperty().getDefinition().getName().getLocalPart();
        Constant varValue = new Constant(number);
        // name is overridden
        if (env.getVariable(name) == null || var.getProperty().getPropertyPath().size() == 1) {
            env.addVariable(name, varValue);
        }
        // add with long name if applicable
        if (var.getProperty().getPropertyPath().size() > 1) {
            List<String> names = new ArrayList<String>();
            for (ChildContext context : var.getProperty().getPropertyPath()) {
                names.add(context.getChild().getName().getLocalPart());
            }
            String longName = Joiner.on('.').join(names);
            env.addVariable(longName, varValue);
        }
    }
    Expression ex = new Expression(expression, env);
    return ex.evaluate();
}
Also used : Expression(com.iabcinc.jmep.Expression) XExpression(com.iabcinc.jmep.XExpression) Constant(com.iabcinc.jmep.hooks.Constant) ArrayList(java.util.ArrayList) Environment(com.iabcinc.jmep.Environment) PropertyValue(eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue) ChildContext(eu.esdihumboldt.hale.common.align.model.ChildContext)

Example 3 with Environment

use of com.iabcinc.jmep.Environment in project hale by halestudio.

the class MathScript method createEnvironment.

private Environment createEnvironment(Iterable<PropertyValue> variables) {
    Environment env = new Environment();
    for (PropertyValue var : variables) {
        // add the variable to the environment
        // determine the variable value
        Object value = var.getValue();
        Number number;
        if (value instanceof Number) {
            number = (Number) value;
        } else {
            // try conversion to Double as default
            try {
                number = var.getValueAs(Double.class);
            } catch (ConversionException ce) {
                // expression for better exception messages
                continue;
            }
        }
        // Floats
        if (!(number instanceof Integer) && !(number instanceof Double)) {
            number = number.doubleValue();
        }
        Constant varValue = new Constant(number);
        // add with short name, if it does not override something
        String name = var.getProperty().getDefinition().getName().getLocalPart();
        if (!env.getVariables().containsKey(name))
            env.addVariable(name, varValue);
        // add with full name
        env.addVariable(getVariableName(var.getProperty()), varValue);
    }
    return env;
}
Also used : ConversionException(org.springframework.core.convert.ConversionException) Constant(com.iabcinc.jmep.hooks.Constant) Environment(com.iabcinc.jmep.Environment) PropertyValue(eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue)

Aggregations

Environment (com.iabcinc.jmep.Environment)3 Constant (com.iabcinc.jmep.hooks.Constant)3 PropertyValue (eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue)2 Expression (com.iabcinc.jmep.Expression)1 XExpression (com.iabcinc.jmep.XExpression)1 ChildContext (eu.esdihumboldt.hale.common.align.model.ChildContext)1 EntityDefinition (eu.esdihumboldt.hale.common.align.model.EntityDefinition)1 ArrayList (java.util.ArrayList)1 ConversionException (org.springframework.core.convert.ConversionException)1