Search in sources :

Example 21 with Cheese

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

the class CoreConfidenceTests method testIncrementAndAssignWithInputs.

public void testIncrementAndAssignWithInputs() {
    ExpressionCompiler compiler = new ExpressionCompiler("total += cheese");
    compiler.compile();
    assertTrue(compiler.getParserContextState().getInputs().containsKey("total"));
    assertTrue(compiler.getParserContextState().getInputs().containsKey("cheese"));
}
Also used : ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler)

Example 22 with Cheese

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

the class TypesAndInferenceTests method testEgressType.

public void testEgressType() {
    ParserContext context = new ParserContext();
    context.addInput("$cheese", Cheese.class);
    ExpressionCompiler compiler = new ExpressionCompiler("( $cheese )", context);
    assertEquals(Cheese.class, compiler.compile().getKnownEgressType());
}
Also used : ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext)

Example 23 with Cheese

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

the class TypesAndInferenceTests method testStrictTypingCompilationWithVarInsideConstructor.

public void testStrictTypingCompilationWithVarInsideConstructor() {
    ParserContext ctx = new ParserContext();
    ctx.addInput("$likes", String.class);
    ctx.addInput("results", List.class);
    ctx.addImport(Cheese.class);
    ctx.setStrongTyping(true);
    Serializable expr = null;
    try {
        expr = MVEL.compileExpression("Cheese c = new Cheese( $likes, 15 );\nresults.add( c ); ", ctx);
    } catch (CompileException e) {
        e.printStackTrace();
        fail("This should not fail:\n" + e.getMessage());
    }
    List results = new ArrayList();
    Map vars = new HashMap();
    vars.put("$likes", "stilton");
    vars.put("results", results);
    executeExpression(expr, vars);
    assertEquals(new Cheese("stilton", 15), results.get(0));
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CompileException(org.mvel2.CompileException) ArrayList(java.util.ArrayList) List(java.util.List) Cheese(org.mvel2.tests.core.res.Cheese) ParserContext(org.mvel2.ParserContext) HashMap(java.util.HashMap) Map(java.util.Map)

Example 24 with Cheese

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

the class TypesAndInferenceTests method testGetCorrectInputs.

public void testGetCorrectInputs() {
    String str = "total = total + $cheese.price";
    ParserConfiguration pconf = new ParserConfiguration();
    ParserContext pctx = new ParserContext(pconf);
    pctx.setStrongTyping(true);
    pctx.addInput("total", int.class);
    pctx.addInput("$cheese", Cheese.class);
    ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
    assertTrue("Should not contain" + pctx.getVariables(), pctx.getVariables().isEmpty());
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) ParserContext(org.mvel2.ParserContext) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 25 with Cheese

use of org.mvel2.tests.core.res.Cheese in project drools by kiegroup.

the class AccumulateTemplateTest method testMethodGeneration.

@Test
public void testMethodGeneration() {
    final String className = "accumulate0";
    final String[] declarationTypes = new String[] { "String", "int" };
    final Declaration[] declarations = new Declaration[] { new Declaration("name", null, null), new Declaration("age", null, null) };
    final Declaration[] inner = new Declaration[] { new Declaration("cheese", new PatternExtractor(new ClassObjectType(Cheese.class)), null), new Declaration("price", store.getReader(Cheese.class, "price"), null) };
    final String[] globals = new String[] { "aGlobal", "anotherGlobal" };
    final List globalTypes = Arrays.asList(new String[] { "String", "String" });
    final Map map = new HashMap();
    map.put("className", StringUtils.ucFirst(className));
    map.put("instanceName", className);
    map.put("package", "org.drools");
    map.put("ruleClassName", "Rule0");
    map.put("invokerClassName", "Rule0" + StringUtils.ucFirst(className) + "Invoker");
    map.put("declarations", declarations);
    map.put("declarationTypes", declarationTypes);
    map.put("globals", globals);
    map.put("globalTypes", globalTypes);
    map.put("innerDeclarations", inner);
    map.put("attributes", new String[] { "x" });
    map.put("attributesTypes", new String[] { "int" });
    map.put("initCode", "x = 0;");
    map.put("actionCode", "x += 1;");
    map.put("reverseCode", "x -= 1;");
    map.put("resultCode", "x + 10");
    map.put("supportsReverse", "true");
    map.put("resultType", Integer.class);
    map.put("hashCode", new Integer(10));
    TemplateRegistry registry = getRuleTemplateRegistry();
    Object method = TemplateRuntime.execute(registry.getNamedTemplate("accumulateInnerClass"), null, new MapVariableResolverFactory(map), registry);
// System.out.println( method );
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) HashMap(java.util.HashMap) PatternExtractor(org.drools.core.spi.PatternExtractor) Cheese(org.drools.compiler.Cheese) TemplateRegistry(org.mvel2.templates.TemplateRegistry) SimpleTemplateRegistry(org.mvel2.templates.SimpleTemplateRegistry) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) List(java.util.List) Declaration(org.drools.core.rule.Declaration) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)12 ParserContext (org.mvel2.ParserContext)11 HashMap (java.util.HashMap)9 Cheese (org.mvel2.tests.core.res.Cheese)9 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)7 Map (java.util.Map)6 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)5 LinkedHashMap (java.util.LinkedHashMap)4 List (java.util.List)4 ParserConfiguration (org.mvel2.ParserConfiguration)4 ClassObjectType (org.drools.core.base.ClassObjectType)3 Declaration (org.drools.core.rule.Declaration)3 PatternExtractor (org.drools.core.spi.PatternExtractor)3 Test (org.junit.Test)3 SimpleTemplateRegistry (org.mvel2.templates.SimpleTemplateRegistry)3 TemplateRegistry (org.mvel2.templates.TemplateRegistry)3 SimpleDateFormat (java.text.SimpleDateFormat)2 Cheese (org.drools.compiler.Cheese)2 Person (org.drools.compiler.Person)2 ClassImportResolverFactory (org.mvel2.integration.impl.ClassImportResolverFactory)2