Search in sources :

Example 71 with BaseNode

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

the class FEELParserTest method testEmptyContext.

@Test
public void testEmptyContext() {
    String inputExpression = "{}";
    BaseNode context = parse(inputExpression);
    assertThat(context, is(instanceOf(ContextNode.class)));
    assertThat(context.getText(), is(inputExpression));
    ContextNode ctx = (ContextNode) context;
    assertThat(ctx.getEntries(), is(empty()));
}
Also used : IterationContextNode(org.kie.dmn.feel.lang.ast.IterationContextNode) ContextNode(org.kie.dmn.feel.lang.ast.ContextNode) BaseNode(org.kie.dmn.feel.lang.ast.BaseNode) Test(org.junit.Test)

Example 72 with BaseNode

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

the class FEELParserTest method testFunctionInvocationNamedParams.

@Test
public void testFunctionInvocationNamedParams() {
    String inputExpression = "my.test.Function( named parameter 1 : x+10, named parameter 2 : \"foo\" )";
    BaseNode functionBase = parse(inputExpression);
    assertThat(functionBase, is(instanceOf(FunctionInvocationNode.class)));
    assertThat(functionBase.getText(), is(inputExpression));
    FunctionInvocationNode function = (FunctionInvocationNode) functionBase;
    assertThat(function.getName(), is(instanceOf(QualifiedNameNode.class)));
    assertThat(function.getName().getText(), is("my.test.Function"));
    assertThat(function.getParams(), is(instanceOf(ListNode.class)));
    assertThat(function.getParams().getElements().size(), is(2));
    assertThat(function.getParams().getElements().get(0), is(instanceOf(NamedParameterNode.class)));
    assertThat(function.getParams().getElements().get(1), is(instanceOf(NamedParameterNode.class)));
    NamedParameterNode named = (NamedParameterNode) function.getParams().getElements().get(0);
    assertThat(named.getText(), is("named parameter 1 : x+10"));
    assertThat(named.getName().getText(), is("named parameter 1"));
    assertThat(named.getExpression(), is(instanceOf(InfixOpNode.class)));
    assertThat(named.getExpression().getText(), is("x+10"));
    named = (NamedParameterNode) function.getParams().getElements().get(1);
    assertThat(named.getText(), is("named parameter 2 : \"foo\""));
    assertThat(named.getName().getText(), is("named parameter 2"));
    assertThat(named.getExpression(), is(instanceOf(StringNode.class)));
    assertThat(named.getExpression().getText(), is("\"foo\""));
}
Also used : BaseNode(org.kie.dmn.feel.lang.ast.BaseNode) NamedParameterNode(org.kie.dmn.feel.lang.ast.NamedParameterNode) FunctionInvocationNode(org.kie.dmn.feel.lang.ast.FunctionInvocationNode) Test(org.junit.Test)

Example 73 with BaseNode

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

the class FEELParserTest method testInUnaryTestList.

@Test
public void testInUnaryTestList() {
    String inputExpression = "x ** y in ( <=1000, >t, null, (2000..z[, ]z..2000], [(10+5)..(a*b)) )";
    BaseNode inNode = parse(inputExpression);
    assertThat(inNode, is(instanceOf(InNode.class)));
    assertThat(inNode.getResultType(), is(BuiltInType.BOOLEAN));
    assertThat(inNode.getText(), is(inputExpression));
    InNode in = (InNode) inNode;
    assertThat(in.getValue(), is(instanceOf(InfixOpNode.class)));
    assertThat(in.getValue().getText(), is("x ** y"));
    assertThat(in.getExprs(), is(instanceOf(ListNode.class)));
    assertThat(in.getExprs().getText(), is("<=1000, >t, null, (2000..z[, ]z..2000], [(10+5)..(a*b))"));
    ListNode list = (ListNode) in.getExprs();
    assertThat(list.getElements().get(0), is(instanceOf(UnaryTestNode.class)));
    assertThat(list.getElements().get(0).getText(), is("<=1000"));
    assertThat(list.getElements().get(1), is(instanceOf(UnaryTestNode.class)));
    assertThat(list.getElements().get(1).getText(), is(">t"));
    assertThat(list.getElements().get(2), is(instanceOf(NullNode.class)));
    assertThat(list.getElements().get(2).getText(), is("null"));
    assertThat(list.getElements().get(3), is(instanceOf(RangeNode.class)));
    RangeNode interval = (RangeNode) list.getElements().get(3);
    assertThat(interval.getText(), is("(2000..z["));
    assertThat(interval.getLowerBound(), is(RangeNode.IntervalBoundary.OPEN));
    assertThat(interval.getUpperBound(), is(RangeNode.IntervalBoundary.OPEN));
    assertThat(interval.getStart(), is(instanceOf(NumberNode.class)));
    assertThat(interval.getEnd(), is(instanceOf(NameRefNode.class)));
    assertThat(list.getElements().get(4), is(instanceOf(RangeNode.class)));
    interval = (RangeNode) list.getElements().get(4);
    assertThat(interval.getText(), is("]z..2000]"));
    assertThat(interval.getLowerBound(), is(RangeNode.IntervalBoundary.OPEN));
    assertThat(interval.getUpperBound(), is(RangeNode.IntervalBoundary.CLOSED));
    assertThat(interval.getStart(), is(instanceOf(NameRefNode.class)));
    assertThat(interval.getEnd(), is(instanceOf(NumberNode.class)));
    assertThat(list.getElements().get(5), is(instanceOf(RangeNode.class)));
    interval = (RangeNode) list.getElements().get(5);
    assertThat(interval.getText(), is("[(10+5)..(a*b))"));
    assertThat(interval.getLowerBound(), is(RangeNode.IntervalBoundary.CLOSED));
    assertThat(interval.getUpperBound(), is(RangeNode.IntervalBoundary.OPEN));
    assertThat(interval.getStart(), is(instanceOf(InfixOpNode.class)));
    assertThat(interval.getEnd(), is(instanceOf(InfixOpNode.class)));
}
Also used : InNode(org.kie.dmn.feel.lang.ast.InNode) BaseNode(org.kie.dmn.feel.lang.ast.BaseNode) ListNode(org.kie.dmn.feel.lang.ast.ListNode) RangeNode(org.kie.dmn.feel.lang.ast.RangeNode) Test(org.junit.Test)

Example 74 with BaseNode

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

the class FEELImpl method evaluateUnaryTests.

@Override
public List<UnaryTest> evaluateUnaryTests(String expression, Map<String, Type> variableTypes) {
    // DMN defines a special case where, unless the expressions are unary tests
    // or ranges, they need to be converted into an equality test unary expression.
    // This way, we have to compile and check the low level AST nodes to properly
    // deal with this case
    CompilerContext ctx = newCompilerContext();
    for (Map.Entry<String, Type> e : variableTypes.entrySet()) {
        ctx.addInputVariableType(e.getKey(), e.getValue());
    }
    CompiledExpressionImpl compiledExpression = (CompiledExpressionImpl) compileExpressionList(expression, ctx);
    if (compiledExpression != null) {
        ListNode listNode = (ListNode) compiledExpression.getExpression();
        List<BaseNode> tests = new ArrayList<>();
        for (BaseNode o : listNode.getElements()) {
            if (o == null) {
                // not much we can do, so just skip it. Error was reported somewhere else
                continue;
            } else if (o instanceof UnaryTestNode || o instanceof DashNode) {
                tests.add(o);
            } else if (o instanceof RangeNode || o instanceof ListNode) {
                tests.add(new UnaryTestNode("in", o));
            } else {
                tests.add(new UnaryTestNode("=", o));
            }
        }
        listNode.setElements(tests);
        compiledExpression.setExpression(listNode);
        // now we can evaluate the expression to build the list of unary tests
        List<UnaryTest> uts = (List<UnaryTest>) evaluate(compiledExpression, FEELImpl.EMPTY_INPUT);
        return uts;
    }
    return Collections.emptyList();
}
Also used : UnaryTestNode(org.kie.dmn.feel.lang.ast.UnaryTestNode) CompilerContext(org.kie.dmn.feel.lang.CompilerContext) BaseNode(org.kie.dmn.feel.lang.ast.BaseNode) ArrayList(java.util.ArrayList) ListNode(org.kie.dmn.feel.lang.ast.ListNode) UnaryTest(org.kie.dmn.feel.runtime.UnaryTest) Type(org.kie.dmn.feel.lang.Type) ArrayList(java.util.ArrayList) List(java.util.List) DashNode(org.kie.dmn.feel.lang.ast.DashNode) HashMap(java.util.HashMap) Map(java.util.Map) RangeNode(org.kie.dmn.feel.lang.ast.RangeNode)

Example 75 with BaseNode

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

the class FEELImpl method compile.

@Override
public CompiledExpression compile(String expression, CompilerContext ctx) {
    FEEL_1_1Parser parser = FEELParser.parse(getEventsManager(ctx.getListeners()), expression, ctx.getInputVariableTypes(), ctx.getInputVariables(), ctx.getFEELFunctions(), profiles);
    ParseTree tree = parser.compilation_unit();
    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)

Aggregations

BaseNode (org.kie.dmn.feel.lang.ast.BaseNode)84 Test (org.junit.Test)54 ListNode (org.kie.dmn.feel.lang.ast.ListNode)12 InfixOpNode (org.kie.dmn.feel.lang.ast.InfixOpNode)11 FunctionInvocationNode (org.kie.dmn.feel.lang.ast.FunctionInvocationNode)7 IterationContextNode (org.kie.dmn.feel.lang.ast.IterationContextNode)7 NameDefNode (org.kie.dmn.feel.lang.ast.NameDefNode)7 ContextEntryNode (org.kie.dmn.feel.lang.ast.ContextEntryNode)6 ContextNode (org.kie.dmn.feel.lang.ast.ContextNode)6 PathExpressionNode (org.kie.dmn.feel.lang.ast.PathExpressionNode)4 SignedUnaryNode (org.kie.dmn.feel.lang.ast.SignedUnaryNode)4 ParseTree (org.antlr.v4.runtime.tree.ParseTree)3 InNode (org.kie.dmn.feel.lang.ast.InNode)3 InstanceOfNode (org.kie.dmn.feel.lang.ast.InstanceOfNode)3 RangeNode (org.kie.dmn.feel.lang.ast.RangeNode)3 ArrayList (java.util.ArrayList)2 CompiledExpression (org.kie.dmn.feel.lang.CompiledExpression)2 FunctionDefNode (org.kie.dmn.feel.lang.ast.FunctionDefNode)2 NamedParameterNode (org.kie.dmn.feel.lang.ast.NamedParameterNode)2 QualifiedNameNode (org.kie.dmn.feel.lang.ast.QualifiedNameNode)2