Search in sources :

Example 21 with BaseNode

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

the class FEELParserTest method testFunctionDecisionTableInvocation.

@Test
public void testFunctionDecisionTableInvocation() {
    String inputExpression = "decision table( " + "    outputs: \"Applicant Risk Rating\"," + "    input expression list: [\"Applicant Age\", \"Medical History\"]," + "    rule list: [" + "        [ >60      , \"good\" , \"Medium\" ]," + "        [ >60      , \"bad\"  , \"High\"   ]," + "        [ [25..60] , -        , \"Medium\" ]," + "        [ <25      , \"good\" , \"Low\"    ]," + "        [ <25      , \"bad\"  , \"Medium\" ] ]," + "    hit policy: \"Unique\" )";
    // need to call parse passing in the input variables
    BaseNode functionBase = parse(inputExpression);
    assertThat(functionBase, is(instanceOf(FunctionInvocationNode.class)));
    assertThat(functionBase.getText(), is(inputExpression));
    FunctionInvocationNode function = (FunctionInvocationNode) functionBase;
    assertThat(function.getName(), is(instanceOf(NameRefNode.class)));
    assertThat(function.getName().getText(), is("decision table"));
    assertThat(function.getParams(), is(instanceOf(ListNode.class)));
    assertThat(function.getParams().getElements().size(), is(4));
    assertThat(function.getParams().getElements().get(0), is(instanceOf(NamedParameterNode.class)));
    assertThat(function.getParams().getElements().get(1), is(instanceOf(NamedParameterNode.class)));
    assertThat(function.getParams().getElements().get(2), is(instanceOf(NamedParameterNode.class)));
    assertThat(function.getParams().getElements().get(3), is(instanceOf(NamedParameterNode.class)));
    NamedParameterNode named = (NamedParameterNode) function.getParams().getElements().get(0);
    assertThat(named.getText(), is("outputs: \"Applicant Risk Rating\""));
    assertThat(named.getName().getText(), is("outputs"));
    assertThat(named.getExpression(), is(instanceOf(StringNode.class)));
    assertThat(named.getExpression().getText(), is("\"Applicant Risk Rating\""));
    named = (NamedParameterNode) function.getParams().getElements().get(1);
    assertThat(named.getName().getText(), is("input expression list"));
    assertThat(named.getExpression(), is(instanceOf(ListNode.class)));
    ListNode list = (ListNode) named.getExpression();
    assertThat(list.getElements().size(), is(2));
    assertThat(list.getElements().get(0), is(instanceOf(StringNode.class)));
    assertThat(list.getElements().get(0).getText(), is("\"Applicant Age\""));
    assertThat(list.getElements().get(1), is(instanceOf(StringNode.class)));
    assertThat(list.getElements().get(1).getText(), is("\"Medical History\""));
    named = (NamedParameterNode) function.getParams().getElements().get(2);
    assertThat(named.getName().getText(), is("rule list"));
    assertThat(named.getExpression(), is(instanceOf(ListNode.class)));
    list = (ListNode) named.getExpression();
    assertThat(list.getElements().size(), is(5));
    assertThat(list.getElements().get(0), is(instanceOf(ListNode.class)));
    ListNode rule = (ListNode) list.getElements().get(0);
    assertThat(rule.getElements().size(), is(3));
    assertThat(rule.getElements().get(0), is(instanceOf(UnaryTestNode.class)));
    assertThat(rule.getElements().get(0).getText(), is(">60"));
    assertThat(rule.getElements().get(1), is(instanceOf(StringNode.class)));
    assertThat(rule.getElements().get(1).getText(), is("\"good\""));
    assertThat(rule.getElements().get(2), is(instanceOf(StringNode.class)));
    assertThat(rule.getElements().get(2).getText(), is("\"Medium\""));
    named = (NamedParameterNode) function.getParams().getElements().get(3);
    assertThat(named.getName().getText(), is("hit policy"));
    assertThat(named.getExpression(), is(instanceOf(StringNode.class)));
    assertThat(named.getExpression().getText(), is("\"Unique\""));
}
Also used : BaseNode(org.kie.dmn.feel.lang.ast.BaseNode) NamedParameterNode(org.kie.dmn.feel.lang.ast.NamedParameterNode) ListNode(org.kie.dmn.feel.lang.ast.ListNode) FunctionInvocationNode(org.kie.dmn.feel.lang.ast.FunctionInvocationNode) Test(org.junit.Test)

Example 22 with BaseNode

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

the class FEELParserTest method testQualifiedName.

@Test
public void testQualifiedName() {
    String inputExpression = "My Person.Full Name";
    MapBackedType personType = new MapBackedType("Person", mapOf(entry("Full Name", BuiltInType.STRING), entry("Age", BuiltInType.NUMBER)));
    BaseNode qualRef = parse(inputExpression, mapOf(entry("My Person", personType)));
    assertThat(qualRef, is(instanceOf(QualifiedNameNode.class)));
    assertThat(qualRef.getResultType(), is(BuiltInType.STRING));
    List<NameRefNode> parts = ((QualifiedNameNode) qualRef).getParts();
    // `My Person` ...
    assertThat(parts.get(0), is(instanceOf(NameRefNode.class)));
    assertThat(parts.get(0).getResultType(), is(personType));
    // ... `.Full Name`
    assertThat(parts.get(1), is(instanceOf(NameRefNode.class)));
    assertThat(parts.get(1).getResultType(), is(BuiltInType.STRING));
    assertLocation(inputExpression, qualRef);
}
Also used : BaseNode(org.kie.dmn.feel.lang.ast.BaseNode) MapBackedType(org.kie.dmn.feel.lang.impl.MapBackedType) QualifiedNameNode(org.kie.dmn.feel.lang.ast.QualifiedNameNode) NameRefNode(org.kie.dmn.feel.lang.ast.NameRefNode) Test(org.junit.Test)

Example 23 with BaseNode

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

the class FEELParserTest method testLogicalNegation.

@Test
public void testLogicalNegation() {
    String inputExpression = "not ( true )";
    BaseNode neg = parse(inputExpression);
    assertThat(neg, is(instanceOf(FunctionInvocationNode.class)));
    assertThat(neg.getResultType(), is(BuiltInType.BOOLEAN));
    assertThat(neg.getText(), is("not ( true )"));
    FunctionInvocationNode not = (FunctionInvocationNode) neg;
    assertThat(not.getParams().getElements().get(0), is(instanceOf(BooleanNode.class)));
    assertThat(not.getParams().getElements().get(0).getResultType(), is(BuiltInType.BOOLEAN));
    assertThat(not.getParams().getElements().get(0).getText(), is("true"));
}
Also used : BaseNode(org.kie.dmn.feel.lang.ast.BaseNode) FunctionInvocationNode(org.kie.dmn.feel.lang.ast.FunctionInvocationNode) Test(org.junit.Test)

Example 24 with BaseNode

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

the class FEELParserTest method testExternalFunctionDefinition.

@Test
public void testExternalFunctionDefinition() {
    String inputExpression = "{ trigonometric cosine : function( angle ) external {" + "    java : {" + "        class : \"java.lang.Math\"," + "        method signature : \"cos(double)\"" + "    }" + "}}";
    BaseNode ctxbase = parse(inputExpression);
    assertThat(ctxbase, is(instanceOf(ContextNode.class)));
    assertThat(ctxbase.getText(), is(inputExpression));
    ContextNode ctx = (ContextNode) ctxbase;
    assertThat(ctx.getEntries().size(), is(1));
    ContextEntryNode entry = ctx.getEntries().get(0);
    assertThat(entry.getName(), is(instanceOf(NameDefNode.class)));
    NameDefNode name = (NameDefNode) entry.getName();
    assertThat(name.getText(), is("trigonometric cosine"));
    assertThat(entry.getValue(), is(instanceOf(FunctionDefNode.class)));
    assertThat(entry.getValue().getText(), is("function( angle ) external {" + "    java : {" + "        class : \"java.lang.Math\"," + "        method signature : \"cos(double)\"" + "    }" + "}"));
    FunctionDefNode cos = (FunctionDefNode) entry.getValue();
    assertThat(cos.getFormalParameters().size(), is(1));
    assertThat(cos.getFormalParameters().get(0).getText(), is("angle"));
    assertThat(cos.isExternal(), is(true));
    assertThat(cos.getBody(), is(instanceOf(ContextNode.class)));
    ContextNode body = (ContextNode) cos.getBody();
    assertThat(body.getEntries().size(), is(1));
    ContextEntryNode java = body.getEntries().get(0);
    assertThat(java.getName().getText(), is("java"));
    assertThat(java.getValue(), is(instanceOf(ContextNode.class)));
    ContextNode def = (ContextNode) java.getValue();
    assertThat(def.getEntries().size(), is(2));
    assertThat(def.getEntries().get(0).getName().getText(), is("class"));
    assertThat(def.getEntries().get(0).getValue(), is(instanceOf(StringNode.class)));
    assertThat(def.getEntries().get(0).getValue().getText(), is("\"java.lang.Math\""));
    assertThat(def.getEntries().get(1).getName().getText(), is("method signature"));
    assertThat(def.getEntries().get(1).getValue(), is(instanceOf(StringNode.class)));
    assertThat(def.getEntries().get(1).getValue().getText(), is("\"cos(double)\""));
}
Also used : FunctionDefNode(org.kie.dmn.feel.lang.ast.FunctionDefNode) NameDefNode(org.kie.dmn.feel.lang.ast.NameDefNode) IterationContextNode(org.kie.dmn.feel.lang.ast.IterationContextNode) ContextNode(org.kie.dmn.feel.lang.ast.ContextNode) BaseNode(org.kie.dmn.feel.lang.ast.BaseNode) ContextEntryNode(org.kie.dmn.feel.lang.ast.ContextEntryNode) Test(org.junit.Test)

Example 25 with BaseNode

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

the class FEELParserTest method testExpressionList.

@Test
public void testExpressionList() {
    // TODO review this test is potentially wrong as the list is not homogeneous
    String inputExpression = "[ 10, foo * bar, true ]";
    BaseNode list = parse(inputExpression);
    assertThat(list, is(instanceOf(ListNode.class)));
    assertThat(list.getResultType(), is(BuiltInType.LIST));
    assertThat(list.getText(), is("10, foo * bar, true"));
    ListNode ln = (ListNode) list;
    assertThat(ln.getElements().size(), is(3));
    assertThat(ln.getElements().get(0), is(instanceOf(NumberNode.class)));
    assertThat(ln.getElements().get(1), is(instanceOf(InfixOpNode.class)));
    assertThat(ln.getElements().get(2), is(instanceOf(BooleanNode.class)));
}
Also used : BaseNode(org.kie.dmn.feel.lang.ast.BaseNode) ListNode(org.kie.dmn.feel.lang.ast.ListNode) Test(org.junit.Test)

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