Search in sources :

Example 1 with Expression

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

the class MathExpressionParameterPage method configure.

/**
 * @see TextSourceListParameterPage#configure(Text)
 */
@Override
protected void configure(final Text textField) {
    super.configure(textField);
    this.textField = textField;
    final TransformationVariableReplacer replacer = new TransformationVariableReplacer();
    textField.addModifyListener(new ModifyListener() {

        @Override
        public void modifyText(ModifyEvent e) {
            try {
                String exprStr = textField.getText();
                exprStr = replacer.replaceVariables(exprStr);
                Expression ex = new Expression(exprStr, environment);
                ex.evaluate();
                setErrorMessage(null);
                setPageComplete(true);
            } catch (Exception ex) {
                String message = ex.getLocalizedMessage();
                if (message != null && !message.isEmpty())
                    setErrorMessage(ex.getLocalizedMessage());
                else
                    setErrorMessage("Invalid variable");
                setPageComplete(false);
            }
        }
    });
}
Also used : TransformationVariableReplacer(eu.esdihumboldt.hale.ui.transformation.TransformationVariableReplacer) ModifyEvent(org.eclipse.swt.events.ModifyEvent) ModifyListener(org.eclipse.swt.events.ModifyListener) Expression(com.iabcinc.jmep.Expression)

Example 2 with Expression

use of com.iabcinc.jmep.Expression 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 Expression

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

the class MathScript method evaluate.

/**
 * @see Script#evaluate(String, Iterable, ServiceProvider)
 */
@Override
public Object evaluate(String script, Iterable<PropertyValue> variables, ServiceProvider provider) throws ScriptException {
    Object result;
    try {
        Expression ex = new Expression(script, createEnvironment(variables));
        result = ex.evaluate();
    } catch (XExpression e) {
        // XXX message + exception?
        throw new ScriptException(e);
    }
    return result;
}
Also used : ScriptException(javax.script.ScriptException) Expression(com.iabcinc.jmep.Expression) XExpression(com.iabcinc.jmep.XExpression) XExpression(com.iabcinc.jmep.XExpression)

Aggregations

Expression (com.iabcinc.jmep.Expression)3 XExpression (com.iabcinc.jmep.XExpression)2 Environment (com.iabcinc.jmep.Environment)1 Constant (com.iabcinc.jmep.hooks.Constant)1 ChildContext (eu.esdihumboldt.hale.common.align.model.ChildContext)1 PropertyValue (eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue)1 TransformationVariableReplacer (eu.esdihumboldt.hale.ui.transformation.TransformationVariableReplacer)1 ArrayList (java.util.ArrayList)1 ScriptException (javax.script.ScriptException)1 ModifyEvent (org.eclipse.swt.events.ModifyEvent)1 ModifyListener (org.eclipse.swt.events.ModifyListener)1