Search in sources :

Example 16 with Foo

use of org.mvel2.tests.core.res.Foo in project mvel by mikebrock.

the class TemplateTests method testMVEL244.

public void testMVEL244() {
    Foo244 foo = new Foo244("plop");
    String template = "@foreach{val : foo.liste[0].liste} plop @end{}";
    CompiledTemplate compiledTemplate = TemplateCompiler.compileTemplate(template);
    Map<String, Object> model = new HashMap<String, Object>();
    model.put("foo", foo);
    System.out.println(TemplateRuntime.execute(compiledTemplate, new ParserContext(), new MapVariableResolverFactory(model)));
}
Also used : MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ParserContext(org.mvel2.ParserContext) CompiledTemplate(org.mvel2.templates.CompiledTemplate)

Example 17 with Foo

use of org.mvel2.tests.core.res.Foo in project mvel by mikebrock.

the class TemplateTests method setupVarsMVEL219.

private Map<String, Object> setupVarsMVEL219() {
    Map<String, Object> vars = new LinkedHashMap<String, Object>();
    vars.put("bal", new BigDecimal("999.99"));
    vars.put("word", "ball");
    vars.put("object", new CoreConfidenceTests.Dog());
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("foo", "bar");
    map.put("fu", new CoreConfidenceTests.Dog());
    map.put("trueValue", true);
    map.put("falseValue", false);
    map.put("one", 1);
    map.put("zero", 0);
    vars.put("map", map);
    return vars;
}
Also used : BigDecimal(java.math.BigDecimal) CoreConfidenceTests(org.mvel2.tests.core.CoreConfidenceTests)

Example 18 with Foo

use of org.mvel2.tests.core.res.Foo in project mvel by mikebrock.

the class IndexedVariablesTests method testVariableInjection3.

public void testVariableInjection3() {
    String[] varNames = { "x", "y", "z" };
    Object[] values = { 10, 20, 30 };
    String expr = "def add(a,b) { a + b }; foo = -1; res = x + y + z;\n" + "if (x > 9) {\n" + "   res = z - y - x;\n" + "   int k = 5;\n" + "   foo = add(5,10);" + "}; \n" + "for (i = 0; i < 100000; i++) { foo++; }; foo;";
    ParserContext ctx = ParserContext.create();
    ctx.addIndexedInput(varNames);
    ctx.setIndexAllocation(true);
    SimpleVariableSpaceModel model = VariableSpaceCompiler.compile(expr, ctx);
    Serializable indexCompile = MVEL.compileExpression(expr, ctx);
    Serializable dynamicCompile = MVEL.compileExpression(expr, ParserContext.create());
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("x", 10);
    map.put("y", 20);
    map.put("z", 30);
    assertEquals(MVEL.executeExpression(dynamicCompile, map), MVEL.executeExpression(indexCompile, model.createFactory(values)));
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) SimpleVariableSpaceModel(org.mvel2.util.SimpleVariableSpaceModel) ParserContext(org.mvel2.ParserContext)

Example 19 with Foo

use of org.mvel2.tests.core.res.Foo in project mvel by mikebrock.

the class InlineCollectionsTests method testToListStrictMode.

@SuppressWarnings({ "UnnecessaryBoxing" })
public void testToListStrictMode() {
    String text = "misc.toList(foo.bar.name, 'hello', 42, ['key1' : 'value1'," + " c : [ foo.bar.age, 'car', 42 ]], [42, [c : 'value1']] )";
    ParserContext ctx = new ParserContext();
    ctx.addInput("misc", MiscTestClass.class);
    ctx.addInput("foo", Foo.class);
    ctx.addInput("c", String.class);
    ctx.setStrictTypeEnforcement(true);
    ExpressionCompiler compiler = new ExpressionCompiler(text);
    List list = (List) executeExpression(compiler.compile(ctx), createTestMap());
    assertSame("dog", list.get(0));
    assertEquals("hello", list.get(1));
    assertEquals(new Integer(42), list.get(2));
    Map map = (Map) list.get(3);
    assertEquals("value1", map.get("key1"));
    List nestedList = (List) map.get("cat");
    assertEquals(14, nestedList.get(0));
    assertEquals("car", nestedList.get(1));
    assertEquals(42, nestedList.get(2));
    nestedList = (List) list.get(4);
    assertEquals(42, nestedList.get(0));
    map = (Map) nestedList.get(1);
    assertEquals("value1", map.get("cat"));
}
Also used : ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) List(java.util.List) ArrayList(java.util.ArrayList) ParserContext(org.mvel2.ParserContext) Map(java.util.Map) HashMap(java.util.HashMap)

Example 20 with Foo

use of org.mvel2.tests.core.res.Foo in project mvel by mikebrock.

the class TypesAndInferenceTests method testCompileTimeCoercion.

public void testCompileTimeCoercion() {
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addInput("foo", Foo.class);
    assertEquals(true, executeExpression(new ExpressionCompiler("foo.bar.woof == 'true'").compile(ctx), createTestMap()));
}
Also used : ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler)

Aggregations

ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)27 Foo (org.mvel2.tests.core.res.Foo)22 ParserContext (org.mvel2.ParserContext)18 CompiledExpression (org.mvel2.compiler.CompiledExpression)15 Serializable (java.io.Serializable)14 HashMap (java.util.HashMap)12 Test (org.junit.Test)9 KieServices (org.kie.api.KieServices)9 KieFileSystem (org.kie.api.builder.KieFileSystem)9 ReleaseId (org.kie.api.builder.ReleaseId)9 KieContainer (org.kie.api.runtime.KieContainer)9 PropertyAccessException (org.mvel2.PropertyAccessException)9 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)9 KieSession (org.kie.api.runtime.KieSession)8 ConsequenceException (org.kie.api.runtime.rule.ConsequenceException)7 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)7 Map (java.util.Map)5 DefaultLocalVariableResolverFactory (org.mvel2.integration.impl.DefaultLocalVariableResolverFactory)5 Interceptor (org.mvel2.integration.Interceptor)4 ASTNode (org.mvel2.ast.ASTNode)3