Search in sources :

Example 1 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 2 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 3 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 4 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)

Example 5 with MapVariableResolverFactory

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

the class ForNode method getReducedValueAccelerated.

public Object getReducedValueAccelerated(Object ctx, Object thisValue, VariableResolverFactory factory) {
    VariableResolverFactory ctxFactory = indexAlloc ? factory : new MapVariableResolverFactory(new HashMap<String, Object>(1), factory);
    Object v;
    for (initializer.getValue(ctx, thisValue, ctxFactory); (Boolean) condition.getValue(ctx, thisValue, ctxFactory); after.getValue(ctx, thisValue, ctxFactory)) {
        v = compiledBlock.getValue(ctx, thisValue, ctxFactory);
        if (ctxFactory.tiltFlag())
            return v;
    }
    return null;
}
Also used : VariableResolverFactory(org.mvel2.integration.VariableResolverFactory) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) HashMap(java.util.HashMap) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory)

Aggregations

MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)62 HashMap (java.util.HashMap)33 ParserContext (org.mvel2.ParserContext)21 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)20 CompiledExpression (org.mvel2.compiler.CompiledExpression)16 ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)16 DefaultLocalVariableResolverFactory (org.mvel2.integration.impl.DefaultLocalVariableResolverFactory)13 Debugger (org.mvel2.debug.Debugger)12 Frame (org.mvel2.debug.Frame)12 HashSet (java.util.HashSet)10 Serializable (java.io.Serializable)9 SimpleTemplateRegistry (org.mvel2.templates.SimpleTemplateRegistry)8 TemplateRegistry (org.mvel2.templates.TemplateRegistry)7 TemplateRuntimeError (org.mvel2.templates.TemplateRuntimeError)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 LinkedHashMap (java.util.LinkedHashMap)6 CompiledTemplate (org.mvel2.templates.CompiledTemplate)6 MapObject (org.mvel2.tests.core.res.MapObject)6 Interceptor (org.mvel2.integration.Interceptor)5 IndexedVariableResolverFactory (org.mvel2.integration.impl.IndexedVariableResolverFactory)5