Search in sources :

Example 16 with CompilerContext

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);
}
Also used : CompilerContext(org.kie.dmn.feel.lang.CompilerContext) CompiledExpression(org.kie.dmn.feel.lang.CompiledExpression)

Example 17 with CompilerContext

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);
    }
}
Also used : CompilerContext(org.kie.dmn.feel.lang.CompilerContext) CompiledExpression(org.kie.dmn.feel.lang.CompiledExpression)

Example 18 with CompilerContext

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"));
}
Also used : HashMap(java.util.HashMap) CompilerContext(org.kie.dmn.feel.lang.CompilerContext) MapBackedType(org.kie.dmn.feel.lang.impl.MapBackedType) CompiledExpression(org.kie.dmn.feel.lang.CompiledExpression) Test(org.junit.Test)

Example 19 with CompilerContext

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());
}
Also used : HashMap(java.util.HashMap) CompilerContext(org.kie.dmn.feel.lang.CompilerContext) MapBackedType(org.kie.dmn.feel.lang.impl.MapBackedType) CompiledExpression(org.kie.dmn.feel.lang.CompiledExpression) Test(org.junit.Test)

Example 20 with CompilerContext

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"));
}
Also used : FEEL(org.kie.dmn.feel.FEEL) CompilerContext(org.kie.dmn.feel.lang.CompilerContext) FEELEventListener(org.kie.dmn.api.feel.runtime.events.FEELEventListener) FEELEvent(org.kie.dmn.api.feel.runtime.events.FEELEvent) Test(org.junit.Test)

Aggregations

CompilerContext (org.kie.dmn.feel.lang.CompilerContext)21 Test (org.junit.Test)13 CompiledExpression (org.kie.dmn.feel.lang.CompiledExpression)12 HashMap (java.util.HashMap)10 FEELEvent (org.kie.dmn.api.feel.runtime.events.FEELEvent)6 FEELEventListener (org.kie.dmn.api.feel.runtime.events.FEELEventListener)6 FEEL (org.kie.dmn.feel.FEEL)5 MapBackedType (org.kie.dmn.feel.lang.impl.MapBackedType)5 Map (java.util.Map)4 ArrayList (java.util.ArrayList)2 List (java.util.List)2 DMNType (org.kie.dmn.api.core.DMNType)2 Type (org.kie.dmn.feel.lang.Type)2 ASTNode (org.kie.dmn.feel.lang.ast.ASTNode)2 FunctionInvocationNode (org.kie.dmn.feel.lang.ast.FunctionInvocationNode)2 GenListType (org.kie.dmn.feel.lang.types.GenListType)2 CompiledNetwork (org.drools.ancompiler.CompiledNetwork)1 CompiledNetworkSources (org.drools.ancompiler.CompiledNetworkSources)1 ObjectTypeNodeCompiler (org.drools.ancompiler.ObjectTypeNodeCompiler)1 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)1