Search in sources :

Example 6 with ExpressionEvaluator

use of com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator 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());
    });
}
Also used : Context(com.walmartlabs.concord.runtime.v2.sdk.Context) SynchronizationService(com.walmartlabs.concord.runtime.v2.runner.SynchronizationService) CheckpointService(com.walmartlabs.concord.runtime.v2.runner.checkpoints.CheckpointService) ProcessDefinition(com.walmartlabs.concord.runtime.v2.model.ProcessDefinition) ExpressionEvaluator(com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator)

Example 7 with ExpressionEvaluator

use of com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator 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);
}
Also used : EvalContext(com.walmartlabs.concord.runtime.v2.runner.el.EvalContext) Context(com.walmartlabs.concord.runtime.v2.sdk.Context) ProcessConfiguration(com.walmartlabs.concord.runtime.v2.sdk.ProcessConfiguration) Form(com.walmartlabs.concord.forms.Form) FormService(com.walmartlabs.concord.runtime.common.FormService) EvalContext(com.walmartlabs.concord.runtime.v2.runner.el.EvalContext) ExpressionEvaluator(com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator)

Example 8 with ExpressionEvaluator

use of com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator in project concord by walmartlabs.

the class OutVariablesProcessor method afterProcessEnds.

@Override
public void afterProcessEnds(Runtime runtime, State state, Frame lastFrame) {
    if (outVariables.isEmpty() || lastFrame == null) {
        return;
    }
    Map<String, Object> vars = Collections.unmodifiableMap(lastFrame.getLocals());
    ExpressionEvaluator ee = runtime.getService(ExpressionEvaluator.class);
    EvalContext evalContext = DefaultEvalContext.builder().from(EvalContextFactory.strict(vars)).undefinedVariableAsNull(true).build();
    Map<String, Object> outValues = new HashMap<>();
    for (String out : outVariables) {
        // TODO sanitize
        Object v = ee.eval(evalContext, "${" + out + "}", Object.class);
        if (v == null) {
            continue;
        }
        outValues.put(out, v);
    }
    if (outValues.isEmpty()) {
        return;
    }
    persistenceService.persistFile(Constants.Files.OUT_VALUES_FILE_NAME, out -> objectMapper.writeValue(out, outValues));
}
Also used : HashMap(java.util.HashMap) EvalContext(com.walmartlabs.concord.runtime.v2.runner.el.EvalContext) DefaultEvalContext(com.walmartlabs.concord.runtime.v2.runner.el.DefaultEvalContext) ExpressionEvaluator(com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator)

Example 9 with ExpressionEvaluator

use of com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator in project concord by walmartlabs.

the class ExpressionEvaluatorTest method testStrict.

@Test
public void testStrict() {
    ExpressionEvaluator ee = new DefaultExpressionEvaluator(new TaskProviders());
    Map<String, Object> vars = Collections.singletonMap("name", "Concord");
    Map<String, Object> strict = Collections.singletonMap("name", "Concord!!!");
    EvalContext ctx = EvalContextFactory.strict(new SingleFrameContext(vars), strict);
    // ---
    String str = ee.eval(ctx, "Hello ${name}", String.class);
    assertEquals("Hello Concord!!!", str);
}
Also used : TaskProviders(com.walmartlabs.concord.runtime.v2.runner.tasks.TaskProviders) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.jupiter.api.Test)

Example 10 with ExpressionEvaluator

use of com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator in project concord by walmartlabs.

the class ExpressionEvaluatorTest method testAllVariables.

@Test
public void testAllVariables() {
    String str = "${allVariables()}";
    ExpressionEvaluator ee = new DefaultExpressionEvaluator(new TaskProviders());
    // ---
    Map<String, Object> vars = new HashMap<>();
    vars.put("a", Collections.singletonList("b"));
    vars.put("b", "bb");
    Map<String, Object> result = ee.evalAsMap(global(vars), str);
    assertEquals(vars, result);
}
Also used : TaskProviders(com.walmartlabs.concord.runtime.v2.runner.tasks.TaskProviders) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.jupiter.api.Test)

Aggregations

ExpressionEvaluator (com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator)16 TaskProviders (com.walmartlabs.concord.runtime.v2.runner.tasks.TaskProviders)16 Test (org.junit.jupiter.api.Test)15 Matchers.containsString (org.hamcrest.Matchers.containsString)14 Context (com.walmartlabs.concord.runtime.v2.sdk.Context)13 EvalContext (com.walmartlabs.concord.runtime.v2.runner.el.EvalContext)5 ContextFactory (com.walmartlabs.concord.runtime.v2.runner.context.ContextFactory)4 EvalContextFactory (com.walmartlabs.concord.runtime.v2.runner.el.EvalContextFactory)4 ProcessDefinition (com.walmartlabs.concord.runtime.v2.model.ProcessDefinition)3 Serializable (java.io.Serializable)3 Map (java.util.Map)3 Step (com.walmartlabs.concord.runtime.v2.model.Step)2 Compiler (com.walmartlabs.concord.runtime.v2.sdk.Compiler)2 ProcessConfiguration (com.walmartlabs.concord.runtime.v2.sdk.ProcessConfiguration)2 HashMap (java.util.HashMap)2 Form (com.walmartlabs.concord.forms.Form)1 FormService (com.walmartlabs.concord.runtime.common.FormService)1 Expression (com.walmartlabs.concord.runtime.v2.model.Expression)1 ExpressionOptions (com.walmartlabs.concord.runtime.v2.model.ExpressionOptions)1 FlowCall (com.walmartlabs.concord.runtime.v2.model.FlowCall)1