Search in sources :

Example 1 with CompiledExpression

use of org.kie.dmn.feel.lang.CompiledExpression in project drools by kiegroup.

the class FEELImpl method compileExpressionList.

public CompiledExpression compileExpressionList(String expression, CompilerContext ctx) {
    FEEL_1_1Parser parser = FEELParser.parse(getEventsManager(ctx.getListeners()), expression, ctx.getInputVariableTypes(), ctx.getInputVariables(), ctx.getFEELFunctions(), profiles);
    ParseTree tree = parser.expressionList();
    ASTBuilderVisitor v = new ASTBuilderVisitor(ctx.getInputVariableTypes());
    BaseNode expr = v.visit(tree);
    CompiledExpression ce = new CompiledExpressionImpl(expr);
    return ce;
}
Also used : ASTBuilderVisitor(org.kie.dmn.feel.parser.feel11.ASTBuilderVisitor) BaseNode(org.kie.dmn.feel.lang.ast.BaseNode) FEEL_1_1Parser(org.kie.dmn.feel.parser.feel11.FEEL_1_1Parser) ParseTree(org.antlr.v4.runtime.tree.ParseTree) CompiledExpression(org.kie.dmn.feel.lang.CompiledExpression)

Example 2 with CompiledExpression

use of org.kie.dmn.feel.lang.CompiledExpression in project drools by kiegroup.

the class CompileEvaluateTest method test2.

@Test
public void test2() {
    CompilerContext ctx = feel.newCompilerContext();
    ctx.addInputVariableType("MyPerson", new MapBackedType().addField("FullName", BuiltInType.STRING));
    CompiledExpression compiledExpression = feel.compile("MyPerson.fullName", ctx);
    assertThat(errors.toString(), errors.size(), is(1));
    Map<String, Object> inputs = new HashMap<>();
    inputs.put("MyPerson", prototype(entry("FullName", "John Doe")));
    Object result = feel.evaluate(compiledExpression, inputs);
    assertThat(result, nullValue());
}
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 3 with CompiledExpression

use of org.kie.dmn.feel.lang.CompiledExpression in project drools by kiegroup.

the class CompileEvaluateTest method testHyphenInPropertyOfCollectionForAccessor.

@Test
public void testHyphenInPropertyOfCollectionForAccessor() {
    MapBackedType compositeType = new MapBackedType().addField("Primary-Key", BuiltInType.STRING).addField("Value", BuiltInType.STRING);
    CompilerContext ctx = feel.newCompilerContext();
    ctx.addInputVariableType("my input", new GenListType(compositeType));
    CompiledExpression compiledExpression = feel.compile("my input[1].Primary-Key", ctx);
    assertTrue(errors.isEmpty());
    Map<String, Object> inputs = new HashMap<>();
    inputs.put("my input", Arrays.asList(prototype(entry("Primary-Key", "k987"))));
    Object result = feel.evaluate(compiledExpression, inputs);
    assertThat(result, is("k987"));
    assertTrue(errors.isEmpty());
}
Also used : HashMap(java.util.HashMap) GenListType(org.kie.dmn.feel.lang.types.GenListType) 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 4 with CompiledExpression

use of org.kie.dmn.feel.lang.CompiledExpression in project drools by kiegroup.

the class CompileEvaluateTest method test_isDynamicResolution.

@Test
public void test_isDynamicResolution() {
    CompilerContext ctx = feel.newCompilerContext();
    ctx.addInputVariableType("Person List", BuiltInType.LIST);
    CompiledExpression compiledExpression = feel.compile("Person List[My Variable 1 = \"A\"]", ctx);
    assertThat(errors.toString(), errors.size(), is(0));
    Map<String, Object> inputs = new HashMap<>();
    List<Map<String, ?>> pList = new ArrayList<>();
    inputs.put("Person List", pList);
    pList.add(prototype(entry("Full Name", "Edson Tirelli"), entry("My Variable 1", "A")));
    pList.add(prototype(entry("Full Name", "Matteo Mortari"), entry("My Variable 1", "B")));
    Object result = feel.evaluate(compiledExpression, inputs);
    assertThat(result, instanceOf(List.class));
    assertThat((List<?>) result, hasSize(1));
    assertThat(((Map<?, ?>) ((List<?>) result).get(0)).get("Full Name"), is("Edson Tirelli"));
}
Also used : HashMap(java.util.HashMap) CompilerContext(org.kie.dmn.feel.lang.CompilerContext) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) CompiledExpression(org.kie.dmn.feel.lang.CompiledExpression) Test(org.junit.Test)

Example 5 with CompiledExpression

use of org.kie.dmn.feel.lang.CompiledExpression in project drools by kiegroup.

the class CompileEvaluateTest method testHyphenInPropertyOfCollectionForProjection.

@Test
public void testHyphenInPropertyOfCollectionForProjection() {
    MapBackedType compositeType = new MapBackedType().addField("Primary-Key", BuiltInType.STRING).addField("Value", BuiltInType.STRING);
    CompilerContext ctx = feel.newCompilerContext();
    ctx.addInputVariableType("input", new GenListType(compositeType));
    CompiledExpression compiledExpression = feel.compile("input.Primary-Key", ctx);
    assertTrue(errors.isEmpty());
    Map<String, Object> inputs = new HashMap<>();
    inputs.put("input", Arrays.asList(prototype(entry("Primary-Key", "k987"))));
    Object result = feel.evaluate(compiledExpression, inputs);
    assertThat(result, is(Arrays.asList("k987")));
    assertTrue(errors.isEmpty());
}
Also used : HashMap(java.util.HashMap) GenListType(org.kie.dmn.feel.lang.types.GenListType) 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)

Aggregations

CompiledExpression (org.kie.dmn.feel.lang.CompiledExpression)23 CompilerContext (org.kie.dmn.feel.lang.CompilerContext)12 Test (org.junit.Test)11 HashMap (java.util.HashMap)7 MapBackedType (org.kie.dmn.feel.lang.impl.MapBackedType)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Collections (java.util.Collections)3 Collectors (java.util.stream.Collectors)3 DMNType (org.kie.dmn.api.core.DMNType)3 DMNLiteralExpressionEvaluator (org.kie.dmn.core.ast.DMNLiteralExpressionEvaluator)3 Map (java.util.Map)2 Optional (java.util.Optional)2 UUID (java.util.UUID)2 Collectors.toList (java.util.stream.Collectors.toList)2 QName (javax.xml.namespace.QName)2 ParseTree (org.antlr.v4.runtime.tree.ParseTree)2 DMNMessage (org.kie.dmn.api.core.DMNMessage)2 BusinessKnowledgeModelNode (org.kie.dmn.api.core.ast.BusinessKnowledgeModelNode)2 DecisionNode (org.kie.dmn.api.core.ast.DecisionNode)2