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