Search in sources :

Example 56 with MapContext

use of org.apache.commons.jexl3.MapContext in project shifu by ShifuML.

the class JexlTest method testJavaDouble.

@Test
public void testJavaDouble() {
    JexlEngine jexl = new JexlEngine();
    String jexlExp = "columnA + columnB";
    Expression e = jexl.createExpression(jexlExp);
    JexlContext jc = new MapContext();
    // Now evaluate the expression, getting the result
    Integer val = (Integer) e.evaluate(jc);
    Assert.assertEquals(val, Integer.valueOf(0));
    jc.set("columnA", "0.3");
    double value = (Double) e.evaluate(jc);
    Assert.assertEquals(0.3, value);
    jc.set("columnB", "0.7");
    value = (Double) e.evaluate(jc);
    Assert.assertEquals(value, 1.0);
}
Also used : JexlEngine(org.apache.commons.jexl2.JexlEngine) Expression(org.apache.commons.jexl2.Expression) JexlContext(org.apache.commons.jexl2.JexlContext) MapContext(org.apache.commons.jexl2.MapContext) Test(org.testng.annotations.Test)

Example 57 with MapContext

use of org.apache.commons.jexl3.MapContext in project aries by apache.

the class JexlPropertyEvaluator method evaluate.

public Object evaluate(String expression, Map<String, Object> properties) {
    try {
        JexlEngine engine = new JexlEngine();
        MapContext context = new MapContext(properties);
        Expression exp = engine.createExpression(expression);
        return exp.evaluate(context);
    } catch (Exception e) {
        LOGGER.info("Could not evaluate expression: {}", expression);
        LOGGER.info("Exception:", e);
        return null;
    }
}
Also used : JexlEngine(org.apache.commons.jexl2.JexlEngine) Expression(org.apache.commons.jexl2.Expression) MapContext(org.apache.commons.jexl2.MapContext)

Example 58 with MapContext

use of org.apache.commons.jexl3.MapContext in project dhis2-core by dhis2.

the class ExpressionUtils method evaluate.

/**
 * @param expression the expression.
 * @param vars the variables, can be null.
 * @param strict indicates whether to use strict or lenient engine mode.
 * @return the result of the evaluation.
 */
private static Object evaluate(String expression, Map<String, Object> vars, boolean strict) {
    expression = expression.replaceAll(IGNORED_KEYWORDS_REGEX, StringUtils.EMPTY);
    JexlEngine engine = strict ? JEXL_STRICT : JEXL;
    Expression exp = engine.createExpression(expression);
    JexlContext context = vars != null ? new MapContext(vars) : new MapContext();
    return exp.evaluate(context);
}
Also used : JexlEngine(org.apache.commons.jexl2.JexlEngine) Expression(org.apache.commons.jexl2.Expression) JexlContext(org.apache.commons.jexl2.JexlContext) MapContext(org.apache.commons.jexl2.MapContext)

Aggregations

MapContext (org.apache.commons.jexl2.MapContext)32 MapContext (org.apache.commons.jexl3.MapContext)26 JexlContext (org.apache.commons.jexl2.JexlContext)23 Expression (org.apache.commons.jexl2.Expression)20 JexlEngine (org.apache.commons.jexl2.JexlEngine)20 JexlContext (org.apache.commons.jexl3.JexlContext)17 Test (org.testng.annotations.Test)13 HashMap (java.util.HashMap)7 Map (java.util.Map)7 HashSet (java.util.HashSet)4 JexlException (org.apache.commons.jexl2.JexlException)4 JexlBuilder (org.apache.commons.jexl3.JexlBuilder)4 JexlExpression (org.apache.commons.jexl3.JexlExpression)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Method (java.lang.reflect.Method)3 ArrayList (java.util.ArrayList)3 JexlEngine (org.apache.commons.jexl3.JexlEngine)3 Test (org.junit.jupiter.api.Test)3 Device (org.traccar.model.Device)3 IOException (java.io.IOException)2