Search in sources :

Example 1 with EvalContext

use of com.walmartlabs.concord.runtime.v2.runner.el.EvalContext 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 2 with EvalContext

use of com.walmartlabs.concord.runtime.v2.runner.el.EvalContext 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 3 with EvalContext

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

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

the class LazyExpressionEvaluator method createResolver.

/**
 * Based on the original code from {@link StandardELContext#getELResolver()}.
 * Creates a {@link ELResolver} instance with "sub-resolvers" in the original order.
 */
private ELResolver createResolver(LazyEvalContext evalContext, ExpressionFactory expressionFactory) {
    CompositeELResolver r = new CompositeELResolver();
    if (evalContext.scope() != null) {
        r.add(new VariableResolver(evalContext.scope()));
    }
    r.add(new VariableResolver(evalContext.variables()));
    if (evalContext.context() != null) {
        r.add(new TaskResolver(evalContext.context(), taskProviders));
    }
    r.add(expressionFactory.getStreamELResolver());
    r.add(new StaticFieldELResolver());
    r.add(new MapELResolver());
    r.add(new MethodAccessorResolver());
    r.add(new ResourceBundleELResolver());
    r.add(new ListELResolver());
    r.add(new ArrayELResolver());
    if (evalContext.context() != null) {
        r.add(new TaskMethodResolver(evalContext.context()));
    }
    r.add(new BeanELResolver());
    return r;
}
Also used : BeanELResolver(com.walmartlabs.concord.runtime.v2.runner.el.resolvers.BeanELResolver)

Example 5 with EvalContext

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

the class ExpressionEvaluatorTest method testStrictUndef.

@Test
public void testStrictUndef() {
    ExpressionEvaluator ee = new DefaultExpressionEvaluator(new TaskProviders());
    Map<String, Object> vars = Collections.singletonMap("name", "Concord");
    Map<String, Object> strict = Collections.emptyMap();
    EvalContext ctx = EvalContextFactory.strict(new SingleFrameContext(vars), strict);
    // ---
    try {
        ee.eval(ctx, "Hello ${name}", String.class);
        fail("exception expected");
    } catch (RuntimeException e) {
        assertThat(e.getMessage(), containsString("variable in 'Hello ${name}'"));
    }
    // undef as null
    // ---
    String str = ee.eval(undefAsNull(ctx), "Hello ${name}", String.class);
    assertNull(str);
}
Also used : TaskProviders(com.walmartlabs.concord.runtime.v2.runner.tasks.TaskProviders) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.jupiter.api.Test)

Aggregations

EvalContext (com.walmartlabs.concord.runtime.v2.runner.el.EvalContext)5 ExpressionEvaluator (com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator)5 Context (com.walmartlabs.concord.runtime.v2.sdk.Context)4 TaskProviders (com.walmartlabs.concord.runtime.v2.runner.tasks.TaskProviders)2 ProcessConfiguration (com.walmartlabs.concord.runtime.v2.sdk.ProcessConfiguration)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 Test (org.junit.jupiter.api.Test)2 Form (com.walmartlabs.concord.forms.Form)1 FormService (com.walmartlabs.concord.runtime.common.FormService)1 FlowCall (com.walmartlabs.concord.runtime.v2.model.FlowCall)1 FlowCallOptions (com.walmartlabs.concord.runtime.v2.model.FlowCallOptions)1 IfStep (com.walmartlabs.concord.runtime.v2.model.IfStep)1 ProcessDefinition (com.walmartlabs.concord.runtime.v2.model.ProcessDefinition)1 SwitchStep (com.walmartlabs.concord.runtime.v2.model.SwitchStep)1 DefaultEvalContext (com.walmartlabs.concord.runtime.v2.runner.el.DefaultEvalContext)1 BeanELResolver (com.walmartlabs.concord.runtime.v2.runner.el.resolvers.BeanELResolver)1 Compiler (com.walmartlabs.concord.runtime.v2.sdk.Compiler)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1