Search in sources :

Example 26 with Context

use of com.walmartlabs.concord.runtime.v2.sdk.Context 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 27 with Context

use of com.walmartlabs.concord.runtime.v2.sdk.Context 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 28 with Context

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

Example 29 with Context

use of com.walmartlabs.concord.runtime.v2.sdk.Context in project concord by walmartlabs.

the class WithItemsWrapper method eval.

@Override
@SuppressWarnings("unchecked")
public void eval(Runtime runtime, State state, ThreadId threadId) {
    Frame frame = state.peekFrame(threadId);
    frame.pop();
    Serializable value = withItems.value();
    if (value == null) {
        // value is null, not going to run the wrapped command at all
        return;
    }
    Step currentStep = null;
    if (cmd instanceof StepCommand) {
        currentStep = ((StepCommand<?>) cmd).getStep();
    }
    // create the context explicitly
    ContextFactory contextFactory = runtime.getService(ContextFactory.class);
    Context ctx = contextFactory.create(runtime, state, threadId, currentStep);
    ExpressionEvaluator ee = runtime.getService(ExpressionEvaluator.class);
    value = ee.eval(EvalContextFactory.global(ctx), value, Serializable.class);
    // prepare items
    // store items in an ArrayList because it is Serializable
    ArrayList<Serializable> items;
    if (value == null) {
        // value is null, not going to run the wrapped command at all
        return;
    } else if (value instanceof Collection) {
        Collection<Serializable> v = (Collection<Serializable>) value;
        if (v.isEmpty()) {
            // no items, nothing to do
            return;
        }
        items = new ArrayList<>(v);
    } else if (value instanceof Map) {
        Map<Serializable, Serializable> m = (Map<Serializable, Serializable>) value;
        items = m.entrySet().stream().map(e -> new AbstractMap.SimpleImmutableEntry<>(e.getKey(), e.getValue())).collect(Collectors.toCollection(ArrayList::new));
    } else if (value.getClass().isArray()) {
        items = new ArrayList<>(Arrays.asList((Serializable[]) value));
    } else {
        throw new IllegalArgumentException("'withItems' accepts only Lists of items, Java Maps or arrays of values. Got: " + value.getClass());
    }
    items.forEach(WithItemsWrapper::assertItem);
    if (items.isEmpty()) {
        return;
    }
    eval(state, threadId, items);
}
Also used : Context(com.walmartlabs.concord.runtime.v2.sdk.Context) Serializable(java.io.Serializable) Step(com.walmartlabs.concord.runtime.v2.model.Step) ExpressionEvaluator(com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator) ContextFactory(com.walmartlabs.concord.runtime.v2.runner.context.ContextFactory) EvalContextFactory(com.walmartlabs.concord.runtime.v2.runner.el.EvalContextFactory)

Example 30 with Context

use of com.walmartlabs.concord.runtime.v2.sdk.Context in project concord by walmartlabs.

the class TaskV2Provider method createTask.

@Override
public Task createTask(Context ctx, String key) {
    Class<? extends Task> klass = holder.get(key);
    if (klass == null) {
        return null;
    }
    Map<String, Object> defaultVariables = defaultTaskVariables.get(key);
    TaskContext taskContext = new TaskContext(ctx, new MapBackedVariables(defaultVariables));
    return ContextProvider.withContext(taskContext, () -> injector.getInstance(klass));
}
Also used : MapBackedVariables(com.walmartlabs.concord.runtime.v2.sdk.MapBackedVariables) TaskContext(com.walmartlabs.concord.runtime.v2.runner.context.TaskContext)

Aggregations

Context (com.walmartlabs.concord.runtime.v2.sdk.Context)18 ExpressionEvaluator (com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator)15 MapBackedVariables (com.walmartlabs.concord.runtime.v2.sdk.MapBackedVariables)6 HashMap (java.util.HashMap)6 ContextFactory (com.walmartlabs.concord.runtime.v2.runner.context.ContextFactory)5 EvalContext (com.walmartlabs.concord.runtime.v2.runner.el.EvalContext)4 EvalContextFactory (com.walmartlabs.concord.runtime.v2.runner.el.EvalContextFactory)4 TaskResult (com.walmartlabs.concord.runtime.v2.sdk.TaskResult)4 Map (java.util.Map)4 ServerSetupTest (com.icegreen.greenmail.util.ServerSetupTest)3 ApiClient (com.walmartlabs.concord.ApiClient)3 ProcessDefinition (com.walmartlabs.concord.runtime.v2.model.ProcessDefinition)3 BlockCommand (com.walmartlabs.concord.runtime.v2.runner.vm.BlockCommand)3 Variables (com.walmartlabs.concord.runtime.v2.sdk.Variables)3 Command (com.walmartlabs.concord.svm.Command)3 MimeMessage (javax.mail.internet.MimeMessage)3 Test (org.junit.jupiter.api.Test)3 HashiVaultTask (com.walmartlabs.concord.plugins.hashivault.v2.HashiVaultTask)2 Step (com.walmartlabs.concord.runtime.v2.model.Step)2 LogContext (com.walmartlabs.concord.runtime.v2.runner.logging.LogContext)2