Search in sources :

Example 1 with StepCommand

use of com.walmartlabs.concord.runtime.v2.runner.vm.StepCommand 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 StepCommand

use of com.walmartlabs.concord.runtime.v2.runner.vm.StepCommand in project concord by walmartlabs.

the class EventRecordingExecutionListener method afterCommand.

@Override
public Result afterCommand(Runtime runtime, VM vm, State state, ThreadId threadId, Command cmd) {
    if (!eventConfiguration.recordEvents()) {
        return Result.CONTINUE;
    }
    if (!(cmd instanceof StepCommand)) {
        return Result.CONTINUE;
    }
    StepCommand<?> s = (StepCommand<?>) cmd;
    if (s.getStep() instanceof TaskCall || s.getStep() instanceof Expression) {
        return Result.CONTINUE;
    }
    ProcessDefinition pd = runtime.getService(ProcessDefinition.class);
    Location loc = s.getStep().getLocation();
    Map<String, Object> m = new HashMap<>();
    m.put("processDefinitionId", ProcessDefinitionUtils.getCurrentFlowName(pd, s.getStep()));
    m.put("fileName", loc.fileName());
    m.put("line", loc.lineNum());
    m.put("column", loc.column());
    m.put("description", getDescription(s.getStep()));
    m.put("correlationId", s.getCorrelationId());
    ProcessEventRequest req = new ProcessEventRequest();
    // TODO constants
    req.setEventType("ELEMENT");
    req.setData(m);
    req.setEventDate(Instant.now().atOffset(ZoneOffset.UTC));
    try {
        eventsApi.event(processInstanceId.getValue(), req);
    } catch (ApiException e) {
        log.warn("afterCommand [{}] -> error while sending an event to the server: {}", cmd, e.getMessage());
    }
    return Result.CONTINUE;
}
Also used : StepCommand(com.walmartlabs.concord.runtime.v2.runner.vm.StepCommand) ProcessEventRequest(com.walmartlabs.concord.client.ProcessEventRequest) HashMap(java.util.HashMap) ApiException(com.walmartlabs.concord.ApiException)

Example 3 with StepCommand

use of com.walmartlabs.concord.runtime.v2.runner.vm.StepCommand 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 4 with StepCommand

use of com.walmartlabs.concord.runtime.v2.runner.vm.StepCommand 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

ContextFactory (com.walmartlabs.concord.runtime.v2.runner.context.ContextFactory)3 EvalContextFactory (com.walmartlabs.concord.runtime.v2.runner.el.EvalContextFactory)3 ExpressionEvaluator (com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator)3 Context (com.walmartlabs.concord.runtime.v2.sdk.Context)3 Step (com.walmartlabs.concord.runtime.v2.model.Step)2 Serializable (java.io.Serializable)2 ApiException (com.walmartlabs.concord.ApiException)1 ProcessEventRequest (com.walmartlabs.concord.client.ProcessEventRequest)1 CompilerContext (com.walmartlabs.concord.runtime.v2.runner.compiler.CompilerContext)1 StepCommand (com.walmartlabs.concord.runtime.v2.runner.vm.StepCommand)1 HashMap (java.util.HashMap)1