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);
}
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;
}
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);
}
}
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);
}
Aggregations