Search in sources :

Example 1 with FunctionDefNode

use of org.kie.dmn.feel.lang.ast.FunctionDefNode 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 2 with FunctionDefNode

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

the class FEELParserTest method testFunctionDefinition.

@Test
public void testFunctionDefinition() {
    String inputExpression = "{ is minor : function( person's age ) person's age < 21 }";
    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("is minor"));
    assertThat(entry.getValue(), is(instanceOf(FunctionDefNode.class)));
    assertThat(entry.getValue().getText(), is("function( person's age ) person's age < 21"));
    FunctionDefNode isMinorFunc = (FunctionDefNode) entry.getValue();
    assertThat(isMinorFunc.getFormalParameters().size(), is(1));
    assertThat(isMinorFunc.getFormalParameters().get(0).getText(), is("person's age"));
    assertThat(isMinorFunc.isExternal(), is(false));
    assertThat(isMinorFunc.getBody(), is(instanceOf(InfixOpNode.class)));
}
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)

Aggregations

Test (org.junit.Test)2 BaseNode (org.kie.dmn.feel.lang.ast.BaseNode)2 ContextEntryNode (org.kie.dmn.feel.lang.ast.ContextEntryNode)2 ContextNode (org.kie.dmn.feel.lang.ast.ContextNode)2 FunctionDefNode (org.kie.dmn.feel.lang.ast.FunctionDefNode)2 IterationContextNode (org.kie.dmn.feel.lang.ast.IterationContextNode)2 NameDefNode (org.kie.dmn.feel.lang.ast.NameDefNode)2