Search in sources :

Example 1 with ExpressionEvaluator

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

the class LoopWrapper method eval.

@Override
@SuppressWarnings("unchecked")
public void eval(Runtime runtime, State state, ThreadId threadId) {
    Frame frame = state.peekFrame(threadId);
    frame.pop();
    Serializable value = items;
    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(LoopWrapper::assertItem);
    if (items.isEmpty()) {
        return;
    }
    eval(state, threadId, items);
}
Also used : Context(com.walmartlabs.concord.runtime.v2.sdk.Context) CompilerContext(com.walmartlabs.concord.runtime.v2.runner.compiler.CompilerContext) 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 2 with ExpressionEvaluator

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

the class RetryWrapper method eval.

@Override
public void eval(Runtime runtime, State state, ThreadId threadId) {
    Frame frame = state.peekFrame(threadId);
    frame.pop();
    // wrap the command into a frame with an exception handler
    Frame inner = Frame.builder().nonRoot().exceptionHandler(new NextRetry(cmd)).commands(cmd).build();
    // create the context explicitly
    ContextFactory contextFactory = runtime.getService(ContextFactory.class);
    Context ctx = contextFactory.create(runtime, state, threadId, getCurrentStep());
    ExpressionEvaluator ee = runtime.getService(ExpressionEvaluator.class);
    int times = retry.times();
    if (retry.timesExpression() != null) {
        Number n = ee.eval(EvalContextFactory.global(ctx), retry.timesExpression(), Number.class);
        if (n != null) {
            times = n.intValue();
        }
    }
    Duration delay = retry.delay();
    if (retry.delayExpression() != null) {
        Number n = ee.eval(EvalContextFactory.global(ctx), retry.delayExpression(), Number.class);
        if (n != null) {
            delay = Duration.ofSeconds(n.longValue());
        }
    }
    inner.setLocal(Constants.Runtime.RETRY_ATTEMPT_NUMBER, 0);
    inner.setLocal(RETRY_CFG, Retry.builder().from(retry).times(times).delay(delay).build());
    state.pushFrame(threadId, inner);
}
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) Duration(java.time.Duration) ExpressionEvaluator(com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator)

Example 3 with ExpressionEvaluator

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

the class ScriptCallCommand method execute.

@Override
protected void execute(Runtime runtime, State state, ThreadId threadId) {
    state.peekFrame(threadId).pop();
    Context ctx = runtime.getService(Context.class);
    ExpressionEvaluator expressionEvaluator = runtime.getService(ExpressionEvaluator.class);
    ScriptEvaluator scriptEvaluator = runtime.getService(ScriptEvaluator.class);
    ResourceResolver resourceResolver = runtime.getService(ResourceResolver.class);
    ScriptCall call = getStep();
    ScriptCallOptions opts = Objects.requireNonNull(call.getOptions());
    Map<String, Object> input = VMUtils.prepareInput(expressionEvaluator, ctx, opts.input(), opts.inputExpression());
    String language = getLanguage(expressionEvaluator, scriptEvaluator, ctx, call);
    Reader content = getContent(expressionEvaluator, resourceResolver, ctx, call);
    ScriptResult scriptResult;
    try {
        scriptResult = scriptEvaluator.eval(ctx, language, content, input);
    } finally {
        try {
            content.close();
        } catch (IOException e) {
            // we don't have to do anything about it, but we're going to log the error just in case
            log.warn("Error while closing the script's reader: {}", e.getMessage() + ". This is most likely a bug.");
        }
    }
    OutputUtils.process(runtime, ctx, scriptResult.items(), opts.out(), opts.outExpr());
}
Also used : Context(com.walmartlabs.concord.runtime.v2.sdk.Context) ScriptResult(com.walmartlabs.concord.runtime.v2.runner.script.ScriptResult) ScriptEvaluator(com.walmartlabs.concord.runtime.v2.runner.script.ScriptEvaluator) ScriptCallOptions(com.walmartlabs.concord.runtime.v2.model.ScriptCallOptions) ScriptCall(com.walmartlabs.concord.runtime.v2.model.ScriptCall) ResourceResolver(com.walmartlabs.concord.runtime.v2.runner.ResourceResolver) ExpressionEvaluator(com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator)

Example 4 with ExpressionEvaluator

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

the class SetVariablesCommand method execute.

@Override
@SuppressWarnings("unchecked")
protected void execute(Runtime runtime, State state, ThreadId threadId) {
    state.peekFrame(threadId).pop();
    SetVariablesStep step = getStep();
    Context ctx = runtime.getService(Context.class);
    // eval the input
    ExpressionEvaluator ee = runtime.getService(ExpressionEvaluator.class);
    Map<String, Object> vars = ee.evalAsMap(EvalContextFactory.scope(ctx), step.getVars());
    vars.forEach((k, v) -> {
        Object o = ctx.variables().get(k);
        if (o instanceof Map && v instanceof Map) {
            v = deepMerge((Map<String, Object>) o, (Map<String, Object>) v);
        }
        ctx.variables().set(k, v);
    });
}
Also used : Context(com.walmartlabs.concord.runtime.v2.sdk.Context) SetVariablesStep(com.walmartlabs.concord.runtime.v2.model.SetVariablesStep) ExpressionEvaluator(com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator) Map(java.util.Map)

Example 5 with ExpressionEvaluator

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

the class EvalAsMapFunction method evalAsMap.

@SuppressWarnings("unchecked")
public static Map<String, Object> evalAsMap(Object value) {
    Context ctx = ThreadLocalEvalContext.get().context();
    if (ctx == null) {
        return null;
    }
    ExpressionEvaluator ee = ctx.execution().runtime().getService(ExpressionEvaluator.class);
    Map<String, Object> evalValue = ee.evalAsMap(EvalContextFactory.scope(ctx), value);
    Map<String, Object> result = new HashMap<>();
    evalValue.forEach((k, v) -> {
        Object o = ctx.variables().get(k);
        if (o instanceof Map && v instanceof Map) {
            v = deepMerge((Map<String, Object>) o, (Map<String, Object>) v);
        }
        if (v != null) {
            result.put(k, v);
        }
    });
    return result;
}
Also used : Context(com.walmartlabs.concord.runtime.v2.sdk.Context) ThreadLocalEvalContext(com.walmartlabs.concord.runtime.v2.runner.el.ThreadLocalEvalContext) HashMap(java.util.HashMap) ExpressionEvaluator(com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator) Map(java.util.Map) HashMap(java.util.HashMap)

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