Search in sources :

Example 1 with Step

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

use of com.walmartlabs.concord.runtime.v2.model.Step 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 3 with Step

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

the class TaskMethodResolver method invoke.

@Override
public Object invoke(ELContext elContext, Object base, Object method, Class<?>[] paramTypes, Object[] params) {
    Step step = context.execution().currentStep();
    if (!(step instanceof Expression) || !(base instanceof Task) || !(method instanceof String)) {
        return null;
    }
    String taskName = getName(base);
    if (taskName == null) {
        return null;
    }
    CallContext callContext = TaskCallInterceptor.CallContext.builder().taskName(taskName).correlationId(context.execution().correlationId()).currentStep(step).processDefinition(context.execution().processDefinition()).build();
    TaskCallInterceptor interceptor = context.execution().runtime().getService(TaskCallInterceptor.class);
    try {
        return interceptor.invoke(callContext, Method.of(base, (String) method, Arrays.asList(params)), () -> super.invoke(elContext, base, method, paramTypes, params));
    } catch (javax.el.MethodNotFoundException e) {
        throw new MethodNotFoundException(base, method, paramTypes);
    } catch (javax.el.ELException e) {
        if (e.getCause() instanceof RuntimeException) {
            throw (RuntimeException) e.getCause();
        }
        throw e;
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Task(com.walmartlabs.concord.runtime.v2.sdk.Task) Expression(com.walmartlabs.concord.runtime.v2.model.Expression) Step(com.walmartlabs.concord.runtime.v2.model.Step) CallContext(com.walmartlabs.concord.runtime.v2.runner.tasks.TaskCallInterceptor.CallContext) MethodNotFoundException(com.walmartlabs.concord.runtime.v2.runner.el.MethodNotFoundException) TaskCallInterceptor(com.walmartlabs.concord.runtime.v2.runner.tasks.TaskCallInterceptor) MethodNotFoundException(com.walmartlabs.concord.runtime.v2.runner.el.MethodNotFoundException)

Example 4 with Step

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

the class IfCompiler method compile.

@Override
public Command compile(CompilerContext context, IfStep step) {
    Command thenCommand = compile(context, step.getThenSteps());
    Command elseCommand = compile(context, step.getElseSteps());
    return new IfCommand(step, thenCommand, elseCommand);
}
Also used : BlockCommand(com.walmartlabs.concord.runtime.v2.runner.vm.BlockCommand) IfCommand(com.walmartlabs.concord.runtime.v2.runner.vm.IfCommand) Command(com.walmartlabs.concord.svm.Command) IfCommand(com.walmartlabs.concord.runtime.v2.runner.vm.IfCommand)

Example 5 with Step

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

Aggregations

ExpressionEvaluator (com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator)7 Context (com.walmartlabs.concord.runtime.v2.sdk.Context)7 Step (com.walmartlabs.concord.runtime.v2.model.Step)5 ContextFactory (com.walmartlabs.concord.runtime.v2.runner.context.ContextFactory)3 BlockCommand (com.walmartlabs.concord.runtime.v2.runner.vm.BlockCommand)3 Command (com.walmartlabs.concord.svm.Command)3 Serializable (java.io.Serializable)3 Expression (com.walmartlabs.concord.runtime.v2.model.Expression)2 ProcessDefinition (com.walmartlabs.concord.runtime.v2.model.ProcessDefinition)2 EvalContext (com.walmartlabs.concord.runtime.v2.runner.el.EvalContext)2 EvalContextFactory (com.walmartlabs.concord.runtime.v2.runner.el.EvalContextFactory)2 Map (java.util.Map)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Injector (com.google.inject.Injector)1 DependencyManager (com.walmartlabs.concord.dependencymanager.DependencyManager)1 ImportManager (com.walmartlabs.concord.imports.ImportManager)1 ImportManagerFactory (com.walmartlabs.concord.imports.ImportManagerFactory)1 ImportProcessingException (com.walmartlabs.concord.imports.ImportProcessingException)1 RunnerConfiguration (com.walmartlabs.concord.runtime.common.cfg.RunnerConfiguration)1 ProjectLoaderV2 (com.walmartlabs.concord.runtime.v2.ProjectLoaderV2)1