use of org.kie.dmn.feel.lang.CompilerContext in project drools by kiegroup.
the class FEELImpl method evaluate.
@Override
public Object evaluate(String expression, EvaluationContext ctx) {
CompilerContext compilerCtx = newCompilerContext(ctx.getListeners());
Map<String, Object> inputVariables = ctx.getAllValues();
if (inputVariables != null) {
inputVariables.entrySet().stream().forEach(e -> compilerCtx.addInputVariable(e.getKey(), e.getValue()));
}
CompiledExpression expr = compile(expression, compilerCtx);
return evaluate(expr, ctx);
}
use of org.kie.dmn.feel.lang.CompilerContext in project drools by kiegroup.
the class FEELImpl method evaluate.
@Override
public Object evaluate(String expression, Map<String, Object> inputVariables) {
CompilerContext ctx = newCompilerContext();
if (inputVariables != null) {
inputVariables.entrySet().stream().forEach(e -> ctx.addInputVariable(e.getKey(), e.getValue()));
}
CompiledExpression expr = compile(expression, ctx);
if (inputVariables == null) {
return evaluate(expr, EMPTY_INPUT);
} else {
return evaluate(expr, inputVariables);
}
}
use of org.kie.dmn.feel.lang.CompilerContext in project drools by kiegroup.
the class CompileEvaluateTest method test2OK.
@Test
public void test2OK() {
CompilerContext ctx = feel.newCompilerContext();
ctx.addInputVariableType("MyPerson", new MapBackedType().addField("FullName", BuiltInType.STRING));
CompiledExpression compiledExpression = feel.compile("MyPerson.FullName", ctx);
Map<String, Object> inputs = new HashMap<>();
inputs.put("MyPerson", prototype(entry("FullName", "John Doe")));
Object result = feel.evaluate(compiledExpression, inputs);
assertThat(result, is("John Doe"));
}
use of org.kie.dmn.feel.lang.CompilerContext in project drools by kiegroup.
the class CompileEvaluateTest method testHyphenInProperty.
@Test
public void testHyphenInProperty() {
CompilerContext ctx = feel.newCompilerContext();
ctx.addInputVariableType("input", new MapBackedType().addField("Primary-Key", BuiltInType.STRING).addField("Value", BuiltInType.STRING));
CompiledExpression compiledExpression = feel.compile("input.Primary-Key", ctx);
assertTrue(errors.isEmpty());
Map<String, Object> inputs = new HashMap<>();
inputs.put("input", prototype(entry("Primary-Key", "k987")));
Object result = feel.evaluate(compiledExpression, inputs);
assertThat(result, is("k987"));
assertTrue(errors.isEmpty());
}
use of org.kie.dmn.feel.lang.CompilerContext in project drools by kiegroup.
the class FEELErrorMessagesTest method ifWithoutThen2.
@Test
public void ifWithoutThen2() {
final FEEL feel = FEEL.newInstance();
final FEELEventListener fel = Mockito.mock(FEELEventListener.class);
feel.addListener(fel);
final CompilerContext ctx = feel.newCompilerContext();
feel.compile("if true 123", ctx);
final ArgumentCaptor<FEELEvent> captor = ArgumentCaptor.forClass(FEELEvent.class);
verify(fel, times(2)).onEvent(captor.capture());
Assert.assertThat(captor.getAllValues().size(), is(2));
Assert.assertThat(captor.getAllValues().get(0), is(instanceOf(SyntaxErrorEvent.class)));
Assert.assertThat(((SyntaxErrorEvent) captor.getAllValues().get(0)).getMessage(), startsWith("missing 'then' at '123'"));
Assert.assertThat(captor.getAllValues().get(1), is(instanceOf(SyntaxErrorEvent.class)));
Assert.assertThat(((SyntaxErrorEvent) captor.getAllValues().get(1)).getMessage(), startsWith("Detected 'if' expression without 'then' part"));
}
Aggregations