use of com.walmartlabs.concord.runtime.v2.sdk.Context 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());
}
use of com.walmartlabs.concord.runtime.v2.sdk.Context 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.sdk.Context 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;
}
use of com.walmartlabs.concord.runtime.v2.sdk.Context in project concord by walmartlabs.
the class CheckpointCommand method execute.
@Override
protected void execute(Runtime runtime, State state, ThreadId threadId) {
state.peekFrame(threadId).pop();
// eval the name in case it contains an expression
Context ctx = runtime.getService(Context.class);
ExpressionEvaluator ee = runtime.getService(ExpressionEvaluator.class);
String name = ee.eval(EvalContextFactory.global(ctx), getStep().getName(), String.class);
runtime.getService(SynchronizationService.class).point(() -> {
CheckpointService checkpointService = runtime.getService(CheckpointService.class);
ProcessDefinition processDefinition = runtime.getService(ProcessDefinition.class);
// cleanup the internal state to reduce the serialized data size
state.gc();
// TODO validate checkpoint name
checkpointService.create(threadId, getCorrelationId(), name, runtime, ProcessSnapshot.builder().vmState(state).processDefinition(processDefinition).build());
});
}
use of com.walmartlabs.concord.runtime.v2.sdk.Context in project concord by walmartlabs.
the class FormCallCommand method execute.
@Override
protected void execute(Runtime runtime, State state, ThreadId threadId) {
String eventRef = UUID.randomUUID().toString();
Context ctx = runtime.getService(Context.class);
ExpressionEvaluator expressionEvaluator = runtime.getService(ExpressionEvaluator.class);
EvalContext evalContext = EvalContextFactory.global(ctx);
FormCall call = getStep();
String formName = expressionEvaluator.eval(evalContext, call.getName(), String.class);
ProcessDefinition processDefinition = runtime.getService(ProcessDefinition.class);
ProcessConfiguration processConfiguration = runtime.getService(ProcessConfiguration.class);
List<FormField> fields = assertFormFields(expressionEvaluator, evalContext, processConfiguration, processDefinition, formName, call);
Form form = Form.builder().name(formName).eventName(eventRef).options(buildFormOptions(expressionEvaluator, evalContext, call)).fields(buildFormFields(expressionEvaluator, evalContext, fields, Objects.requireNonNull(call.getOptions()).values())).build();
FormService formService = runtime.getService(FormService.class);
formService.save(form);
state.peekFrame(threadId).pop();
state.setEventRef(threadId, eventRef);
state.setStatus(threadId, ThreadStatus.SUSPENDED);
}
Aggregations