Search in sources :

Example 1 with SwitchStep

use of com.walmartlabs.concord.runtime.v2.model.SwitchStep in project concord by walmartlabs.

the class SwitchCompiler method compile.

@Override
public Command compile(CompilerContext context, SwitchStep step) {
    List<Map.Entry<String, Command>> caseCommands = new ArrayList<>();
    for (Map.Entry<String, List<Step>> kv : step.getCaseSteps()) {
        caseCommands.add(new AbstractMap.SimpleEntry<>(kv.getKey(), compile(context, kv.getValue())));
    }
    Command defaultCommand = compile(context, step.getDefaultSteps());
    return new SwitchCommand(step, caseCommands, defaultCommand);
}
Also used : AbstractMap(java.util.AbstractMap) SwitchCommand(com.walmartlabs.concord.runtime.v2.runner.vm.SwitchCommand) SwitchCommand(com.walmartlabs.concord.runtime.v2.runner.vm.SwitchCommand) BlockCommand(com.walmartlabs.concord.runtime.v2.runner.vm.BlockCommand) Command(com.walmartlabs.concord.svm.Command) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) AbstractMap(java.util.AbstractMap) Map(java.util.Map)

Example 2 with SwitchStep

use of com.walmartlabs.concord.runtime.v2.model.SwitchStep 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)

Aggregations

Map (java.util.Map)2 SwitchStep (com.walmartlabs.concord.runtime.v2.model.SwitchStep)1 EvalContext (com.walmartlabs.concord.runtime.v2.runner.el.EvalContext)1 ExpressionEvaluator (com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator)1 BlockCommand (com.walmartlabs.concord.runtime.v2.runner.vm.BlockCommand)1 SwitchCommand (com.walmartlabs.concord.runtime.v2.runner.vm.SwitchCommand)1 Context (com.walmartlabs.concord.runtime.v2.sdk.Context)1 Command (com.walmartlabs.concord.svm.Command)1 AbstractMap (java.util.AbstractMap)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1