Search in sources :

Example 26 with MapVariableResolverFactory

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

the class TemplateTests method testMVEL244.

public void testMVEL244() {
    Foo244 foo = new Foo244("plop");
    String template = "@foreach{val : foo.liste[0].liste} plop @end{}";
    CompiledTemplate compiledTemplate = TemplateCompiler.compileTemplate(template);
    Map<String, Object> model = new HashMap<String, Object>();
    model.put("foo", foo);
    System.out.println(TemplateRuntime.execute(compiledTemplate, new ParserContext(), new MapVariableResolverFactory(model)));
}
Also used : MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ParserContext(org.mvel2.ParserContext) CompiledTemplate(org.mvel2.templates.CompiledTemplate)

Example 27 with MapVariableResolverFactory

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

the class RuleSetNodeInstance method processOutputs.

private void processOutputs(Map<String, Object> objects) {
    RuleSetNode ruleSetNode = getRuleSetNode();
    if (ruleSetNode != null) {
        for (Iterator<DataAssociation> iterator = ruleSetNode.getOutAssociations().iterator(); iterator.hasNext(); ) {
            DataAssociation association = iterator.next();
            if (association.getTransformation() != null) {
                Transformation transformation = association.getTransformation();
                DataTransformer transformer = DataTransformerRegistry.get().find(transformation.getLanguage());
                if (transformer != null) {
                    Object parameterValue = transformer.transform(transformation.getCompiledExpression(), objects);
                    VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, association.getTarget());
                    if (variableScopeInstance != null && parameterValue != null) {
                        variableScopeInstance.setVariable(association.getTarget(), parameterValue);
                    } else {
                        logger.warn("Could not find variable scope for variable {}", association.getTarget());
                        logger.warn("Continuing without setting variable.");
                    }
                    if (parameterValue != null) {
                        variableScopeInstance.setVariable(association.getTarget(), parameterValue);
                    }
                }
            } else if (association.getAssignments() == null || association.getAssignments().isEmpty()) {
                VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, association.getTarget());
                if (variableScopeInstance != null) {
                    Object value = objects.get(association.getSources().get(0));
                    if (value == null) {
                        try {
                            value = MVELSafeHelper.getEvaluator().eval(association.getSources().get(0), new MapVariableResolverFactory(objects));
                        } catch (Throwable t) {
                        // do nothing
                        }
                    }
                    Variable varDef = variableScopeInstance.getVariableScope().findVariable(association.getTarget());
                    DataType dataType = varDef.getType();
                    // exclude java.lang.Object as it is considered unknown type
                    if (!dataType.getStringType().endsWith("java.lang.Object") && value instanceof String) {
                        value = dataType.readValue((String) value);
                    }
                    variableScopeInstance.setVariable(association.getTarget(), value);
                } else {
                    logger.warn("Could not find variable scope for variable {}", association.getTarget());
                }
            }
        }
    }
}
Also used : Transformation(org.jbpm.workflow.core.node.Transformation) Variable(org.jbpm.process.core.context.variable.Variable) DataTransformer(org.kie.api.runtime.process.DataTransformer) VariableScopeInstance(org.jbpm.process.instance.context.variable.VariableScopeInstance) RuleSetNode(org.jbpm.workflow.core.node.RuleSetNode) DataAssociation(org.jbpm.workflow.core.node.DataAssociation) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) DataType(org.jbpm.process.core.datatype.DataType)

Example 28 with MapVariableResolverFactory

use of org.mule.mvel2.integration.impl.MapVariableResolverFactory in project mule by mulesoft.

the class MVELExpressionLanguageContextTestCase method testGetVariableResolverNextFactory.

@Test
public void testGetVariableResolverNextFactory() {
    parserConfig.addImport(String.class);
    MuleBaseVariableResolverFactory resoverFactory = new MVELExpressionLanguageContext(parserConfig, muleContext);
    resoverFactory.setNextFactory(new MapVariableResolverFactory(Collections.singletonMap("foo", "val")));
    assertNotNull(resoverFactory.getVariableResolver("foo"));
    assertEquals("val", resoverFactory.getVariableResolver("foo").getValue());
    assertNull(resoverFactory.getVariableResolver("bar"));
}
Also used : MuleBaseVariableResolverFactory(org.mule.runtime.core.internal.el.mvel.MuleBaseVariableResolverFactory) MVELExpressionLanguageContext(org.mule.runtime.core.internal.el.mvel.MVELExpressionLanguageContext) MapVariableResolverFactory(org.mule.mvel2.integration.impl.MapVariableResolverFactory) Test(org.junit.Test)

Example 29 with MapVariableResolverFactory

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

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)

Example 30 with MapVariableResolverFactory

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

the class CompiledForEachNode method eval.

public Object eval(TemplateRuntime runtime, TemplateOutputStream appender, Object ctx, VariableResolverFactory factory) {
    Iterator[] iters = new Iterator[item.length];
    Object o;
    for (int i = 0; i < iters.length; i++) {
        if ((o = MVEL.executeExpression(ce[i], ctx, factory)) instanceof Iterable) {
            iters[i] = ((Iterable) o).iterator();
        } else if (o instanceof Object[]) {
            iters[i] = new ArrayIterator((Object[]) o);
        } else if (o instanceof Integer) {
            iters[i] = new CountIterator((Integer) o);
        } else {
            throw new TemplateRuntimeError("cannot iterate object type: " + o.getClass().getName());
        }
    }
    Map<String, Object> locals = new HashMap<String, Object>();
    MapVariableResolverFactory localFactory = new MapVariableResolverFactory(locals, factory);
    int iterate = iters.length;
    while (true) {
        for (int i = 0; i < iters.length; i++) {
            if (!iters[i].hasNext()) {
                iterate--;
                locals.put(item[i], "");
            } else {
                locals.put(item[i], iters[i].next());
            }
        }
        if (iterate != 0) {
            nestedNode.eval(runtime, appender, ctx, localFactory);
            if (sepExpr != null) {
                for (Iterator it : iters) {
                    if (it.hasNext()) {
                        appender.append(String.valueOf(MVEL.executeExpression(cSepExpr, ctx, factory)));
                        break;
                    }
                }
            }
        } else
            break;
    }
    return next != null ? next.eval(runtime, appender, ctx, factory) : null;
}
Also used : CountIterator(org.mvel2.templates.util.CountIterator) HashMap(java.util.HashMap) TemplateRuntimeError(org.mvel2.templates.TemplateRuntimeError) Iterator(java.util.Iterator) ArrayIterator(org.mvel2.templates.util.ArrayIterator) CountIterator(org.mvel2.templates.util.CountIterator) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ArrayIterator(org.mvel2.templates.util.ArrayIterator)

Aggregations

MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)62 HashMap (java.util.HashMap)31 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)20 ParserContext (org.mvel2.ParserContext)18 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 SimpleTemplateRegistry (org.mvel2.templates.SimpleTemplateRegistry)7 TemplateRegistry (org.mvel2.templates.TemplateRegistry)7 TemplateRuntimeError (org.mvel2.templates.TemplateRuntimeError)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 Serializable (java.io.Serializable)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