Search in sources :

Example 31 with ExpressionEvaluator

use of com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator 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)

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