Search in sources :

Example 6 with PatternExtractor

use of org.drools.core.spi.PatternExtractor 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)

Example 7 with PatternExtractor

use of org.drools.core.spi.PatternExtractor in project drools by kiegroup.

the class AccumulateTemplateTest method testInvokerGenerationMultiPattern.

@Test
public void testInvokerGenerationMultiPattern() {
    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("$person", new PatternExtractor(new ClassObjectType(Person.class)), 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.TRUE);
    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) 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)

Aggregations

ClassObjectType (org.drools.core.base.ClassObjectType)7 PatternExtractor (org.drools.core.spi.PatternExtractor)7 HashMap (java.util.HashMap)5 Declaration (org.drools.core.rule.Declaration)5 Test (org.junit.Test)4 List (java.util.List)3 Map (java.util.Map)3 Cheese (org.drools.compiler.Cheese)3 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)3 SimpleTemplateRegistry (org.mvel2.templates.SimpleTemplateRegistry)3 TemplateRegistry (org.mvel2.templates.TemplateRegistry)3 Person (org.drools.compiler.Person)2 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)2 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)2 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)2 RuleBuildContext (org.drools.compiler.rule.builder.RuleBuildContext)2 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)2 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)2 Pattern (org.drools.core.rule.Pattern)2 ObjectType (org.drools.core.spi.ObjectType)2