Search in sources :

Example 51 with MapContext

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

the class JexlTest method testMathMethod.

@Test
public void testMathMethod() {
    JexlEngine jexl = new JexlEngine();
    String jexlExp = "NumberUtils.max(a, b, c)";
    Expression e = jexl.createExpression(jexlExp);
    JexlContext jc = new MapContext();
    jc.set("NumberUtils", new NumberUtils());
    jc.set("a", 7);
    jc.set("b", 5);
    jc.set("c", 9);
    Assert.assertEquals(9, e.evaluate(jc));
}
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) NumberUtils(org.apache.commons.lang.math.NumberUtils) Test(org.testng.annotations.Test)

Example 52 with MapContext

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

the class JexlTest method testJavaExpressionString.

@Test
public void testJavaExpressionString() {
    JexlEngine jexl = new JexlEngine();
    String jexlExp = "name == \"user_a\"";
    Expression e = jexl.createExpression(jexlExp);
    JexlContext jc = new MapContext();
    jc.set("name", "user_a");
    // Now evaluate the expression, getting the result
    Boolean isEqual = (Boolean) e.evaluate(jc);
    Assert.assertTrue(isEqual);
    jc.set("name", "user_b");
    isEqual = (Boolean) e.evaluate(jc);
    Assert.assertFalse(isEqual);
}
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 53 with MapContext

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

the class JexlTest method testJavaSubString.

@Test
public void testJavaSubString() {
    JexlEngine jexl = new JexlEngine();
    String jexlExp = "str.substring(0, 1) == \"a\" ";
    Expression e = jexl.createExpression(jexlExp);
    JexlContext jc = new MapContext();
    jc.set("str", "a1");
    Assert.assertEquals(Boolean.TRUE, e.evaluate(jc));
}
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 54 with MapContext

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

the class JexlTest method testDerived.

@Test
public void testDerived() {
    JexlEngine jexl = new JexlEngine();
    String jexlExp = "(0.00472217*vbase_t1_model_V2R1 + 0.00341543*vbase_t1_model_V2BM)/0.00813760";
    Expression e = jexl.createExpression(jexlExp);
    JexlContext jc = new MapContext();
    jc.set("NumberUtils", new NumberUtils());
    jc.set("vbase_t1_model_V2R1", 238);
    jc.set("vbase_t1_model_V2BM", 289);
    Assert.assertEquals(259.40519686394026, e.evaluate(jc));
}
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) NumberUtils(org.apache.commons.lang.math.NumberUtils) Test(org.testng.annotations.Test)

Example 55 with MapContext

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

the class JexlTest method testJavaCompare.

@Test
public void testJavaCompare() {
    JexlEngine jexl = new JexlEngine();
    String jexlExp = "time_window == 'DEV' and live_xm_send_amount <= 10000.0";
    Expression e = jexl.createExpression(jexlExp);
    JexlContext jc = new MapContext();
    jc.set("time_window", "DEV");
    jc.set("live_xm_send_amount", "50");
    Assert.assertEquals(Boolean.TRUE, e.evaluate(jc));
}
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)

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