Search in sources :

Example 1 with Cheese

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

the class AccumulateTemplateTest method testInvokerGenerationSinglePattern.

@Test
public void testInvokerGenerationSinglePattern() {
    final String className = "accumulate0";
    final String[] declarationTypes = new String[] { "String", "int" };
    final Declaration[] declarations = new Declaration[] { new Declaration("name", store.getReader(Person.class, "name"), null), new Declaration("age", store.getReader(Person.class, "age"), 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 Attribute[] { new Attribute("int", "x") });
    map.put("initCode", "x = 0;");
    map.put("actionCode", "x += 1;");
    map.put("reverseCode", "");
    map.put("resultCode", "x + 10");
    map.put("supportsReverse", "false");
    map.put("resultType", Integer.class);
    map.put("hashCode", new Integer(10));
    map.put("isMultiPattern", Boolean.FALSE);
    TemplateRegistry registry = getInvokerTemplateRegistry();
    Object method = TemplateRuntime.execute(registry.getNamedTemplate("accumulateInvoker"), 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) Person(org.drools.compiler.Person) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 2 with Cheese

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

the class CoreConfidenceTests method testDynamicImportsOnNestedExpressions.

public void testDynamicImportsOnNestedExpressions() {
    ParserContext ctx = new ParserContext();
    ctx.addPackageImport("org.mvel2.tests.core.res");
    ExpressionCompiler compiler = new ExpressionCompiler("new Cheesery(\"bobbo\", new Cheese(\"cheddar\", 15))");
    Cheesery p1 = new Cheesery("bobbo", new Cheese("cheddar", 15));
    Cheesery p2 = (Cheesery) executeExpression(compiler.compile(ctx), new DefaultLocalVariableResolverFactory());
    assertEquals(p1, p2);
}
Also used : DefaultLocalVariableResolverFactory(org.mvel2.integration.impl.DefaultLocalVariableResolverFactory) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler)

Example 3 with Cheese

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

the class TypesAndInferenceTests method testEgressType.

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

Example 4 with Cheese

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

the class TypesAndInferenceTests method testDataConverterStrictMode.

public void testDataConverterStrictMode() throws Exception {
    OptimizerFactory.setDefaultOptimizer("ASM");
    DataConversion.addConversionHandler(Date.class, new MVELDateCoercion());
    ParserContext ctx = new ParserContext();
    ctx.addImport("Cheese", Cheese.class);
    ctx.setStrongTyping(true);
    ctx.setStrictTypeEnforcement(true);
    Locale.setDefault(Locale.US);
    Cheese expectedCheese = new Cheese();
    expectedCheese.setUseBy(new SimpleDateFormat("dd-MMM-yyyy").parse("10-Jul-1974"));
    ExpressionCompiler compiler = new ExpressionCompiler("c = new Cheese(); c.useBy = '10-Jul-1974'; return c");
    Cheese actualCheese = (Cheese) executeExpression(compiler.compile(ctx), createTestMap());
    assertEquals(expectedCheese.getUseBy(), actualCheese.getUseBy());
}
Also used : ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) SimpleDateFormat(java.text.SimpleDateFormat)

Example 5 with Cheese

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

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)

Aggregations

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