Search in sources :

Example 26 with ExpressionEvaluator

use of com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator in project concord by walmartlabs.

the class ExpressionCommand method execute.

@Override
protected void execute(Runtime runtime, State state, ThreadId threadId) {
    state.peekFrame(threadId).pop();
    Context ctx = runtime.getService(Context.class);
    Expression step = getStep();
    String expr = step.getExpr();
    ExpressionEvaluator ee = runtime.getService(ExpressionEvaluator.class);
    Object result = ee.eval(EvalContextFactory.global(ctx), expr, Object.class);
    ExpressionOptions opts = Objects.requireNonNull(step.getOptions());
    if (!opts.outExpr().isEmpty()) {
        ExpressionEvaluator expressionEvaluator = runtime.getService(ExpressionEvaluator.class);
        Map<String, Object> vars = Collections.singletonMap("result", result);
        Map<String, Serializable> out = expressionEvaluator.evalAsMap(EvalContextFactory.global(ctx, vars), opts.outExpr());
        out.forEach((k, v) -> ctx.variables().set(k, v));
    } else if (opts.out() != null) {
        ctx.variables().set(opts.out(), result);
    }
}
Also used : Context(com.walmartlabs.concord.runtime.v2.sdk.Context) Serializable(java.io.Serializable) ExpressionOptions(com.walmartlabs.concord.runtime.v2.model.ExpressionOptions) Expression(com.walmartlabs.concord.runtime.v2.model.Expression) ExpressionEvaluator(com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator)

Example 27 with ExpressionEvaluator

use of com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator in project concord by walmartlabs.

the class FlowCallCommand method execute.

@Override
protected void execute(Runtime runtime, State state, ThreadId threadId) {
    state.peekFrame(threadId).pop();
    Context ctx = runtime.getService(Context.class);
    ExpressionEvaluator ee = runtime.getService(ExpressionEvaluator.class);
    EvalContext evalCtx = EvalContextFactory.global(ctx);
    FlowCall call = getStep();
    // the called flow's name
    String flowName = ee.eval(evalCtx, call.getFlowName(), String.class);
    // the called flow's steps
    Compiler compiler = runtime.getService(Compiler.class);
    ProcessDefinition pd = runtime.getService(ProcessDefinition.class);
    ProcessConfiguration pc = runtime.getService(ProcessConfiguration.class);
    Command steps = CompilerUtils.compile(compiler, pc, pd, flowName);
    FlowCallOptions opts = Objects.requireNonNull(call.getOptions());
    Map<String, Object> input = VMUtils.prepareInput(ee, ctx, opts.input(), opts.inputExpression());
    // the call's frame should be a "root" frame
    // all local variables will have this frame as their base
    Frame innerFrame = Frame.builder().root().commands(steps).locals(input).build();
    // an "out" handler:
    // grab the out variable from the called flow's frame
    // and put it into the callee's frame
    Command processOutVars;
    if (!opts.outExpr().isEmpty()) {
        processOutVars = new EvalVariablesCommand(ctx, opts.outExpr(), innerFrame);
    } else {
        processOutVars = new CopyVariablesCommand(opts.out(), innerFrame, VMUtils::assertNearestRoot);
    }
    // push the out handler first so it executes after the called flow's frame is done
    state.peekFrame(threadId).push(processOutVars);
    state.pushFrame(threadId, innerFrame);
}
Also used : EvalContext(com.walmartlabs.concord.runtime.v2.runner.el.EvalContext) Context(com.walmartlabs.concord.runtime.v2.sdk.Context) Compiler(com.walmartlabs.concord.runtime.v2.sdk.Compiler) FlowCall(com.walmartlabs.concord.runtime.v2.model.FlowCall) EvalContext(com.walmartlabs.concord.runtime.v2.runner.el.EvalContext) ProcessDefinition(com.walmartlabs.concord.runtime.v2.model.ProcessDefinition) ExpressionEvaluator(com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator) FlowCallOptions(com.walmartlabs.concord.runtime.v2.model.FlowCallOptions) ProcessConfiguration(com.walmartlabs.concord.runtime.v2.sdk.ProcessConfiguration)

Example 28 with ExpressionEvaluator

use of com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator in project concord by walmartlabs.

the class IfCommand method execute.

@Override
protected void execute(Runtime runtime, State state, ThreadId threadId) {
    Frame frame = state.peekFrame(threadId);
    frame.pop();
    IfStep step = getStep();
    String expr = step.getExpression();
    Context ctx = runtime.getService(Context.class);
    EvalContext evalContext = EvalContextFactory.global(ctx);
    ExpressionEvaluator ee = runtime.getService(ExpressionEvaluator.class);
    Boolean ifResult = ee.eval(evalContext, expr, Boolean.class);
    if (ifResult != null && ifResult) {
        frame.push(thenCommand);
    } else if (elseCommand != null) {
        frame.push(elseCommand);
    }
}
Also used : Context(com.walmartlabs.concord.runtime.v2.sdk.Context) EvalContext(com.walmartlabs.concord.runtime.v2.runner.el.EvalContext) EvalContext(com.walmartlabs.concord.runtime.v2.runner.el.EvalContext) ExpressionEvaluator(com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator) IfStep(com.walmartlabs.concord.runtime.v2.model.IfStep)

Example 29 with ExpressionEvaluator

use of com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator in project concord by walmartlabs.

the class SwitchCommand method execute.

@Override
protected void execute(Runtime runtime, State state, ThreadId threadId) {
    Frame frame = state.peekFrame(threadId);
    frame.pop();
    SwitchStep step = getStep();
    String expr = step.getExpression();
    Context ctx = runtime.getService(Context.class);
    EvalContext evalContext = EvalContextFactory.global(ctx);
    ExpressionEvaluator ee = runtime.getService(ExpressionEvaluator.class);
    String switchResult = ee.eval(evalContext, expr, String.class);
    boolean caseFound = false;
    for (Map.Entry<String, Command> kv : caseCommands) {
        String caseLabel = ee.eval(evalContext, kv.getKey(), String.class);
        if (Objects.equals(switchResult, caseLabel)) {
            frame.push(kv.getValue());
            caseFound = true;
            break;
        }
    }
    if (!caseFound && defaultCommand != null) {
        frame.push(defaultCommand);
    }
// TODO: log case not found?
}
Also used : Context(com.walmartlabs.concord.runtime.v2.sdk.Context) EvalContext(com.walmartlabs.concord.runtime.v2.runner.el.EvalContext) EvalContext(com.walmartlabs.concord.runtime.v2.runner.el.EvalContext) ExpressionEvaluator(com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator) Map(java.util.Map) SwitchStep(com.walmartlabs.concord.runtime.v2.model.SwitchStep)

Example 30 with ExpressionEvaluator

use of com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator in project concord by walmartlabs.

the class UpdateLocalsCommand method eval.

@Override
public void eval(Runtime runtime, State state, ThreadId threadId) {
    // don't "pop" the stack, this command is a special case and evaluated separately
    // create the context explicitly as this command is evaluated outside or the regular
    // loop and doesn't inherit StepCommand
    ContextFactory contextFactory = runtime.getService(ContextFactory.class);
    Context ctx = contextFactory.create(runtime, state, threadId, null);
    ExpressionEvaluator ee = runtime.getService(ExpressionEvaluator.class);
    Map<String, Object> m = ee.evalAsMap(EvalContextFactory.scope(ctx), input);
    Collection<ThreadId> threads = threadIds;
    if (threads.isEmpty()) {
        threads = Collections.singletonList(threadId);
    }
    for (ThreadId tid : threads) {
        Frame root = VMUtils.assertNearestRoot(state, tid);
        VMUtils.putLocals(root, m);
    }
}
Also used : ContextFactory(com.walmartlabs.concord.runtime.v2.runner.context.ContextFactory) EvalContextFactory(com.walmartlabs.concord.runtime.v2.runner.el.EvalContextFactory) Context(com.walmartlabs.concord.runtime.v2.sdk.Context) ExpressionEvaluator(com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator)

Aggregations

ExpressionEvaluator (com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator)16 TaskProviders (com.walmartlabs.concord.runtime.v2.runner.tasks.TaskProviders)16 Test (org.junit.jupiter.api.Test)15 Matchers.containsString (org.hamcrest.Matchers.containsString)14 Context (com.walmartlabs.concord.runtime.v2.sdk.Context)13 EvalContext (com.walmartlabs.concord.runtime.v2.runner.el.EvalContext)5 ContextFactory (com.walmartlabs.concord.runtime.v2.runner.context.ContextFactory)4 EvalContextFactory (com.walmartlabs.concord.runtime.v2.runner.el.EvalContextFactory)4 ProcessDefinition (com.walmartlabs.concord.runtime.v2.model.ProcessDefinition)3 Serializable (java.io.Serializable)3 Map (java.util.Map)3 Step (com.walmartlabs.concord.runtime.v2.model.Step)2 Compiler (com.walmartlabs.concord.runtime.v2.sdk.Compiler)2 ProcessConfiguration (com.walmartlabs.concord.runtime.v2.sdk.ProcessConfiguration)2 HashMap (java.util.HashMap)2 Form (com.walmartlabs.concord.forms.Form)1 FormService (com.walmartlabs.concord.runtime.common.FormService)1 Expression (com.walmartlabs.concord.runtime.v2.model.Expression)1 ExpressionOptions (com.walmartlabs.concord.runtime.v2.model.ExpressionOptions)1 FlowCall (com.walmartlabs.concord.runtime.v2.model.FlowCall)1