Search in sources :

Example 11 with MapVariableResolverFactory

use of org.mvel2.integration.impl.MapVariableResolverFactory in project mvel by mikebrock.

the class TemplateTests method testInclusionOfNamedTemplate.

// public void testTemplateFile2() {
// String s = (String) TemplateRuntime.eval(new File("src/test/java/org/mvel2/tests/templates/templateDeclareTest.mv"),
// base, new MapVariableResolverFactory(map), null);
// 
// System.out.println(s);
// 
// }
public void testInclusionOfNamedTemplate() {
    SimpleTemplateRegistry registry = new SimpleTemplateRegistry();
    registry.addNamedTemplate("footemplate", compileTemplate("@{_foo_}@{_bar_}"));
    registry.addNamedTemplate("bartemplate", compileTemplate("@{_bar_}@{_foo_}"));
    String s = "@includeNamed{'footemplate'}  ::  @includeNamed{'bartemplate'}";
    assertEquals("FooBar  ::  BarFoo", TemplateRuntime.eval(s, map, registry));
}
Also used : SimpleTemplateRegistry(org.mvel2.templates.SimpleTemplateRegistry)

Example 12 with MapVariableResolverFactory

use of org.mvel2.integration.impl.MapVariableResolverFactory 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 13 with MapVariableResolverFactory

use of org.mvel2.integration.impl.MapVariableResolverFactory 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 14 with MapVariableResolverFactory

use of org.mvel2.integration.impl.MapVariableResolverFactory in project drools by kiegroup.

the class AbstractModel method getMappedOutputPojo.

public Map.Entry<String, String> getMappedOutputPojo() {
    Map<String, String> result = new HashMap<>();
    if (!templateRegistry.contains(getOutputPojoTemplateName())) {
        this.addOutputTemplateToRegistry(templateRegistry);
    }
    List<PMMLOutputField> dataFields = this.getOutputFields();
    Map<String, Object> vars = new HashMap<>();
    String className = this.getOutputPojoClassName();
    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.getOutputPojoTemplateName()), null, new MapVariableResolverFactory(vars), baos);
    } catch (TemplateError te) {
        return null;
    } catch (TemplateRuntimeError tre) {
        // need to figure out logging here
        return null;
    }
    result.put(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) TemplateError(org.mvel2.templates.TemplateError)

Example 15 with MapVariableResolverFactory

use of org.mvel2.integration.impl.MapVariableResolverFactory in project drools by kiegroup.

the class SimpleSetSegmentPredicate method getPredicateRule.

@Override
public String getPredicateRule() {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    CompiledTemplate ct = getTemplate();
    if (ct != null) {
        Map<String, Object> vars = new HashMap<>();
        vars.put("fieldName", this.getValueFieldName());
        vars.put("operator", getOperatorText());
        vars.put("setType", setType);
        vars.put("values", getValueObjects());
        TemplateRuntime.execute(ct, null, new MapVariableResolverFactory(vars), baos);
    }
    return new String(baos.toByteArray());
}
Also used : HashMap(java.util.HashMap) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CompiledTemplate(org.mvel2.templates.CompiledTemplate)

Aggregations

MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)35 HashMap (java.util.HashMap)19 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)10 ParserContext (org.mvel2.ParserContext)8 CompiledExpression (org.mvel2.compiler.CompiledExpression)8 ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)8 DefaultLocalVariableResolverFactory (org.mvel2.integration.impl.DefaultLocalVariableResolverFactory)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 Debugger (org.mvel2.debug.Debugger)6 Frame (org.mvel2.debug.Frame)6 SimpleTemplateRegistry (org.mvel2.templates.SimpleTemplateRegistry)6 HashSet (java.util.HashSet)5 CompiledTemplate (org.mvel2.templates.CompiledTemplate)5 TemplateRegistry (org.mvel2.templates.TemplateRegistry)5 TemplateRuntimeError (org.mvel2.templates.TemplateRuntimeError)5 Serializable (java.io.Serializable)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