Search in sources :

Example 21 with ParserConfiguration

use of org.mule.mvel2.ParserConfiguration in project mvel by mvel.

the class ArithmeticTests method testStaticMathCeilWithJavaClassStyleLiterals.

public void testStaticMathCeilWithJavaClassStyleLiterals() {
    MVEL.COMPILER_OPT_SUPPORT_JAVA_STYLE_CLASS_LITERALS = true;
    try {
        String str = "java.lang.Math.ceil( x/3 )";
        ParserConfiguration pconf = new ParserConfiguration();
        ParserContext pctx = new ParserContext(pconf);
        pctx.setStrongTyping(true);
        pctx.addInput("x", int.class);
        ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
        Map vars = new HashMap();
        vars.put("x", 4);
        assertEquals(Math.ceil((double) 4 / 3), MVEL.executeExpression(stmt, vars));
    } finally {
        MVEL.COMPILER_OPT_SUPPORT_JAVA_STYLE_CLASS_LITERALS = false;
    }
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ParserContext(org.mvel2.ParserContext) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 22 with ParserConfiguration

use of org.mule.mvel2.ParserConfiguration in project mvel by mvel.

the class ArithmeticTests method testStaticMathCeil.

public void testStaticMathCeil() {
    int x = 4;
    // demonstrating it's perfectly valid java
    int m = (int) java.lang.Math.ceil(x / 3);
    String str = "int m = (int) java.lang.Math.ceil( x/3 ); return m;";
    ParserConfiguration pconf = new ParserConfiguration();
    ParserContext pctx = new ParserContext(pconf);
    pctx.setStrongTyping(true);
    pctx.addInput("x", int.class);
    ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
    Map vars = new HashMap();
    vars.put("x", 4);
    assertEquals(new Integer(2), MVEL.executeExpression(stmt, vars));
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ParserContext(org.mvel2.ParserContext) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 23 with ParserConfiguration

use of org.mule.mvel2.ParserConfiguration in project mvel by mvel.

the class ArithmeticTests method testModExpr.

public void testModExpr() {
    String str = "$y % 4 == 0 && $y % 100 != 0 || $y % 400 == 0 ";
    ParserConfiguration pconf = new ParserConfiguration();
    ParserContext pctx = new ParserContext(pconf);
    pctx.setStrictTypeEnforcement(true);
    pctx.setStrongTyping(true);
    pctx.addInput("$y", int.class);
    Map<String, Object> vars = new HashMap<String, Object>();
    ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
    for (int i = 0; i < 500; i++) {
        int y = i;
        boolean expected = y % 4 == 0 && y % 100 != 0 || y % 400 == 0;
        vars.put("$y", y);
        assertEquals(expected, MVEL.eval(str, vars));
        assertEquals(expected, ((Boolean) MVEL.executeExpression(stmt, null, vars)).booleanValue());
    }
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ParserContext(org.mvel2.ParserContext) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 24 with ParserConfiguration

use of org.mule.mvel2.ParserConfiguration in project mvel by mvel.

the class ArithmeticTests method testIntsWithDivision.

public void testIntsWithDivision() {
    String str = "0 == x - (y/2)";
    ParserConfiguration pconf = new ParserConfiguration();
    ParserContext pctx = new ParserContext(pconf);
    pctx.setStrongTyping(true);
    pctx.addInput("x", int.class);
    pctx.addInput("y", int.class);
    ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
    Map vars = new HashMap();
    vars.put("x", 50);
    vars.put("y", 100);
    Boolean result = (Boolean) MVEL.executeExpression(stmt, vars);
    assertTrue(result);
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ParserContext(org.mvel2.ParserContext) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 25 with ParserConfiguration

use of org.mule.mvel2.ParserConfiguration in project mvel by mvel.

the class ArraysTests method testMultiDimensionalArrayType.

public void testMultiDimensionalArrayType() {
    String str = "$c.cheeses[0][0] = new Cheese('brie', 15)";
    ParserConfiguration pconf = new ParserConfiguration();
    pconf.addImport(Cheese.class);
    ParserContext pctx = new ParserContext(pconf);
    pctx.addInput("$c", Column.class);
    pctx.setStrongTyping(true);
    ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
    Map<String, Object> vars = new HashMap<String, Object>();
    Column c = new Column("x", 1);
    c.setCheeses(new Cheese[5][5]);
    vars.put("$c", c);
    MVEL.executeExpression(stmt, null, vars);
    assertEquals(new Cheese("brie", 15), c.getCheeses()[0][0]);
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) HashMap(java.util.HashMap) Cheese(org.mvel2.tests.core.res.Cheese) ParserContext(org.mvel2.ParserContext) ParserConfiguration(org.mvel2.ParserConfiguration)

Aggregations

ParserConfiguration (org.mvel2.ParserConfiguration)74 ParserContext (org.mvel2.ParserContext)70 HashMap (java.util.HashMap)39 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)32 LinkedHashMap (java.util.LinkedHashMap)29 Map (java.util.Map)23 Serializable (java.io.Serializable)10 MVELExpressionLanguageContext (org.mule.runtime.core.internal.el.mvel.MVELExpressionLanguageContext)7 CompileException (org.mvel2.CompileException)7 ParserConfiguration (org.mule.mvel2.ParserConfiguration)6 MVELDialectRuntimeData (org.drools.core.rule.MVELDialectRuntimeData)5 MapObject (org.mvel2.tests.core.res.MapObject)5 Method (java.lang.reflect.Method)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Before (org.junit.Before)3 CachedMapVariableResolverFactory (org.mule.mvel2.integration.impl.CachedMapVariableResolverFactory)3 MVELExpressionExecutor (org.mule.runtime.core.internal.el.mvel.MVELExpressionExecutor)3 Bar (org.mvel2.tests.core.res.Bar)3