Search in sources :

Example 11 with TemplateRegistry

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

the class Treemodel method addMiningTemplateToRegistry.

@Override
protected void addMiningTemplateToRegistry(TemplateRegistry registry) {
    InputStream inputStream = Scorecard.class.getResourceAsStream(MINING_POJO_TEMPLATE);
    if (inputStream != null) {
        CompiledTemplate ct = TemplateCompiler.compileTemplate(inputStream);
        registry.addNamedTemplate(getMiningPojoTemplateName(), ct);
    }
}
Also used : InputStream(java.io.InputStream) CompiledTemplate(org.mvel2.templates.CompiledTemplate)

Example 12 with TemplateRegistry

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

the class JavaRuleBuilderHelper method generateMethodTemplate.

public static void generateMethodTemplate(final String ruleTemplate, final RuleBuildContext context, final Map vars) {
    TemplateRegistry registry = getRuleTemplateRegistry(context.getKnowledgeBuilder().getRootClassLoader());
    context.addMethod((String) TemplateRuntime.execute(registry.getNamedTemplate(ruleTemplate), null, new MapVariableResolverFactory(vars), registry));
}
Also used : TemplateRegistry(org.mvel2.templates.TemplateRegistry) SimpleTemplateRegistry(org.mvel2.templates.SimpleTemplateRegistry) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory)

Example 13 with TemplateRegistry

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

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

Example 15 with TemplateRegistry

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

the class AbstractModel method getMappedMiningPojo.

public Map.Entry<String, String> getMappedMiningPojo() {
    Map<String, String> result = new HashMap<>();
    if (!templateRegistry.contains(getMiningPojoTemplateName())) {
        this.addMiningTemplateToRegistry(templateRegistry);
    }
    List<PMMLMiningField> dataFields = this.getMiningFields();
    Map<String, Object> vars = new HashMap<>();
    String className = this.getMiningPojoClassName();
    vars.put("pmmlPackageName", PMML_JAVA_PACKAGE_NAME);
    vars.put("className", className);
    vars.put("imports", new ArrayList<>());
    vars.put("dataFields", dataFields);
    vars.put("modelName", this.getModelId());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        TemplateRuntime.execute(templateRegistry.getNamedTemplate(this.getMiningPojoTemplateName()), null, new MapVariableResolverFactory(vars), baos);
    } catch (TemplateRuntimeError tre) {
        // need to figure out logging here
        return null;
    }
    result.put(PMML_JAVA_PACKAGE_NAME + "." + className, new String(baos.toByteArray()));
    return result.entrySet().iterator().next();
}
Also used : TemplateRuntimeError(org.mvel2.templates.TemplateRuntimeError) HashMap(java.util.HashMap) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

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