Search in sources :

Example 1 with TemplateRegistry

use of org.mvel2.templates.TemplateRegistry 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 TemplateRegistry

use of org.mvel2.templates.TemplateRegistry in project drools by kiegroup.

the class AccumulateTemplateTest method getInvokerTemplateRegistry.

private TemplateRegistry getInvokerTemplateRegistry() {
    TemplateRegistry invokerRegistry = new SimpleTemplateRegistry();
    CompiledTemplate compiled = TemplateCompiler.compileTemplate(JavaRuleBuilderHelper.class.getResourceAsStream("javaInvokers.mvel"), (Map<String, Class<? extends Node>>) null);
    TemplateRuntime.execute(compiled, null, invokerRegistry);
    return invokerRegistry;
}
Also used : TemplateRegistry(org.mvel2.templates.TemplateRegistry) SimpleTemplateRegistry(org.mvel2.templates.SimpleTemplateRegistry) Node(org.mvel2.templates.res.Node) SimpleTemplateRegistry(org.mvel2.templates.SimpleTemplateRegistry) CompiledTemplate(org.mvel2.templates.CompiledTemplate)

Example 3 with TemplateRegistry

use of org.mvel2.templates.TemplateRegistry in project drools by kiegroup.

the class AccumulateTemplateTest method getRuleTemplateRegistry.

private TemplateRegistry getRuleTemplateRegistry() {
    TemplateRegistry ruleRegistry = new SimpleTemplateRegistry();
    CompiledTemplate compiled = TemplateCompiler.compileTemplate(JavaRuleBuilderHelper.class.getResourceAsStream("javaRule.mvel"), (Map<String, Class<? extends Node>>) null);
    TemplateRuntime.execute(compiled, null, ruleRegistry);
    return ruleRegistry;
}
Also used : TemplateRegistry(org.mvel2.templates.TemplateRegistry) SimpleTemplateRegistry(org.mvel2.templates.SimpleTemplateRegistry) Node(org.mvel2.templates.res.Node) SimpleTemplateRegistry(org.mvel2.templates.SimpleTemplateRegistry) CompiledTemplate(org.mvel2.templates.CompiledTemplate)

Example 4 with TemplateRegistry

use of org.mvel2.templates.TemplateRegistry in project drools by kiegroup.

the class JavaRuleBuilderHelper method generateInvokerTemplate.

private static void generateInvokerTemplate(final String invokerTemplate, final RuleBuildContext context, final String className, final Map vars, final Object invokerLookup, final BaseDescr descrLookup) {
    TemplateRegistry registry = getInvokerTemplateRegistry(context.getKnowledgeBuilder().getRootClassLoader());
    final String invokerClassName = context.getPkg().getName() + "." + context.getRuleDescr().getClassName() + StringUtils.ucFirst(className) + "Invoker";
    context.addInvoker(invokerClassName, (String) TemplateRuntime.execute(registry.getNamedTemplate(invokerTemplate), null, new MapVariableResolverFactory(vars), registry));
    context.addInvokerLookup(invokerClassName, invokerLookup);
    context.addDescrLookups(invokerClassName, descrLookup);
}
Also used : TemplateRegistry(org.mvel2.templates.TemplateRegistry) SimpleTemplateRegistry(org.mvel2.templates.SimpleTemplateRegistry) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory)

Example 5 with TemplateRegistry

use of org.mvel2.templates.TemplateRegistry in project drools by kiegroup.

the class PMML4Helper method applyTemplate.

public void applyTemplate(String templateName, Object context, TemplateRegistry registry, Map vars, StringBuilder builder) {
    CompiledTemplate template = registry.getNamedTemplate(templateName);
    String result = (String) TemplateRuntime.execute(template, context, vars, registry);
    builder.append(result);
}
Also used : CompiledTemplate(org.mvel2.templates.CompiledTemplate)

Aggregations

MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)8 CompiledTemplate (org.mvel2.templates.CompiledTemplate)8 SimpleTemplateRegistry (org.mvel2.templates.SimpleTemplateRegistry)7 TemplateRegistry (org.mvel2.templates.TemplateRegistry)7 HashMap (java.util.HashMap)6 InputStream (java.io.InputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 List (java.util.List)3 Map (java.util.Map)3 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 TemplateRuntimeError (org.mvel2.templates.TemplateRuntimeError)3 Cheese (org.drools.compiler.Cheese)2 Person (org.drools.compiler.Person)2 TemplateError (org.mvel2.templates.TemplateError)2 Node (org.mvel2.templates.res.Node)2 PMMLRequestData (org.kie.api.pmml.PMMLRequestData)1