Search in sources :

Example 1 with ContextEntryNode

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

the class FEELParserTest method testContextWithMultipleEntries.

@Test
public void testContextWithMultipleEntries() {
    String inputExpression = "{ \"a string key\" : 10," + " a non-string key : foo+bar," + " a key.with + /' odd chars : [10..50] }";
    BaseNode ctxbase = parse(inputExpression, mapOf(entry("foo", BuiltInType.NUMBER), entry("bar", BuiltInType.NUMBER)));
    assertThat(ctxbase, is(instanceOf(ContextNode.class)));
    assertThat(ctxbase.getText(), is(inputExpression));
    ContextNode ctx = (ContextNode) ctxbase;
    assertThat(ctx.getEntries().size(), is(3));
    ContextEntryNode entry = ctx.getEntries().get(0);
    assertThat(entry.getName(), is(instanceOf(NameDefNode.class)));
    NameDefNode name = (NameDefNode) entry.getName();
    assertThat(name.getName(), is(notNullValue()));
    assertThat(name.getName(), is("\"a string key\""));
    assertThat(entry.getValue(), is(instanceOf(NumberNode.class)));
    assertThat(entry.getResultType(), is(BuiltInType.NUMBER));
    assertThat(entry.getValue().getText(), is("10"));
    entry = ctx.getEntries().get(1);
    assertThat(entry.getName(), is(instanceOf(NameDefNode.class)));
    name = (NameDefNode) entry.getName();
    assertThat(name.getParts(), is(notNullValue()));
    assertThat(name.getParts().size(), is(5));
    assertThat(entry.getName().getText(), is("a non-string key"));
    assertThat(entry.getValue(), is(instanceOf(InfixOpNode.class)));
    assertThat(entry.getResultType(), is(BuiltInType.NUMBER));
    assertThat(entry.getValue().getText(), is("foo+bar"));
    entry = ctx.getEntries().get(2);
    assertThat(entry.getName(), is(instanceOf(NameDefNode.class)));
    name = (NameDefNode) entry.getName();
    assertThat(name.getParts(), is(notNullValue()));
    assertThat(name.getParts().size(), is(9));
    assertThat(entry.getName().getText(), is("a key.with + /' odd chars"));
    assertThat(entry.getValue(), is(instanceOf(RangeNode.class)));
    assertThat(entry.getResultType(), is(BuiltInType.RANGE));
    assertThat(entry.getValue().getText(), is("[10..50]"));
}
Also used : 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 ContextEntryNode

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

use of org.kie.dmn.feel.lang.ast.ContextEntryNode 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)

Example 4 with ContextEntryNode

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

the class FEELParserTest method testNestedContexts2.

@Test
public void testNestedContexts2() {
    String inputExpression = "{ an applicant : { " + "    home address : {" + "        street name: \"broadway st\"," + "        city : \"New York\" " + "    } " + " },\n " + " street : an applicant.home address.street name \n" + "}";
    BaseNode ctxbase = parse(inputExpression);
    assertThat(ctxbase, is(instanceOf(ContextNode.class)));
    assertThat(ctxbase.getText(), is(inputExpression));
    ContextNode ctx = (ContextNode) ctxbase;
    assertThat(ctx.getEntries().size(), is(2));
    ContextEntryNode entry = ctx.getEntries().get(1);
    assertThat(entry.getName(), is(instanceOf(NameDefNode.class)));
    assertThat(entry.getResultType(), is(BuiltInType.STRING));
    NameDefNode name = (NameDefNode) entry.getName();
    assertThat(name.getText(), is("street"));
    assertThat(entry.getValue(), is(instanceOf(QualifiedNameNode.class)));
    QualifiedNameNode qnn = (QualifiedNameNode) entry.getValue();
    assertThat(qnn.getParts().get(0).getText(), is("an applicant"));
    assertThat(qnn.getParts().get(1).getText(), is("home address"));
    assertThat(qnn.getParts().get(2).getText(), is("street name"));
}
Also used : 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) QualifiedNameNode(org.kie.dmn.feel.lang.ast.QualifiedNameNode) ContextEntryNode(org.kie.dmn.feel.lang.ast.ContextEntryNode) Test(org.junit.Test)

Example 5 with ContextEntryNode

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

the class FEELParserTest method testNestedContexts.

@Test
public void testNestedContexts() {
    String inputExpression = "{ a value : 10," + " an applicant : { " + "    first name : \"Edson\", " + "    last + name : \"Tirelli\", " + "    full name : first name + last + name, " + "    address : {" + "        street : \"55 broadway st\"," + "        city : \"New York\" " + "    }, " + "    xxx: last + name" + " } " + "}";
    BaseNode ctxbase = parse(inputExpression);
    assertThat(ctxbase, is(instanceOf(ContextNode.class)));
    assertThat(ctxbase.getText(), is(inputExpression));
    ContextNode ctx = (ContextNode) ctxbase;
    assertThat(ctx.getEntries().size(), is(2));
    ContextEntryNode entry = ctx.getEntries().get(0);
    assertThat(entry.getName(), is(instanceOf(NameDefNode.class)));
    NameDefNode name = (NameDefNode) entry.getName();
    assertThat(name.getText(), is("a value"));
    assertThat(entry.getValue(), is(instanceOf(NumberNode.class)));
    assertThat(entry.getResultType(), is(BuiltInType.NUMBER));
    assertThat(entry.getValue().getText(), is("10"));
    entry = ctx.getEntries().get(1);
    assertThat(entry.getName(), is(instanceOf(NameDefNode.class)));
    name = (NameDefNode) entry.getName();
    assertThat(name.getText(), is("an applicant"));
    assertThat(entry.getValue(), is(instanceOf(ContextNode.class)));
    ContextNode applicant = (ContextNode) entry.getValue();
    assertThat(applicant.getEntries().size(), is(5));
    assertThat(applicant.getEntries().get(0).getName().getText(), is("first name"));
    assertThat(applicant.getEntries().get(0).getResultType(), is(BuiltInType.STRING));
    assertThat(applicant.getEntries().get(1).getName().getText(), is("last + name"));
    assertThat(applicant.getEntries().get(1).getResultType(), is(BuiltInType.STRING));
    assertThat(applicant.getEntries().get(2).getName().getText(), is("full name"));
    assertThat(applicant.getEntries().get(2).getResultType(), is(BuiltInType.STRING));
    assertThat(applicant.getEntries().get(3).getName().getText(), is("address"));
    assertThat(applicant.getEntries().get(3).getValue(), is(instanceOf(ContextNode.class)));
    ContextNode address = (ContextNode) applicant.getEntries().get(3).getValue();
    assertThat(address.getEntries().size(), is(2));
    assertThat(address.getEntries().get(0).getName().getText(), is("street"));
    assertThat(address.getEntries().get(0).getResultType(), is(BuiltInType.STRING));
    assertThat(address.getEntries().get(1).getName().getText(), is("city"));
    assertThat(address.getEntries().get(0).getResultType(), is(BuiltInType.STRING));
}
Also used : 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

BaseNode (org.kie.dmn.feel.lang.ast.BaseNode)6 ContextEntryNode (org.kie.dmn.feel.lang.ast.ContextEntryNode)6 Test (org.junit.Test)5 ContextNode (org.kie.dmn.feel.lang.ast.ContextNode)5 IterationContextNode (org.kie.dmn.feel.lang.ast.IterationContextNode)5 NameDefNode (org.kie.dmn.feel.lang.ast.NameDefNode)5 FunctionDefNode (org.kie.dmn.feel.lang.ast.FunctionDefNode)2 ArrayList (java.util.ArrayList)1 QualifiedNameNode (org.kie.dmn.feel.lang.ast.QualifiedNameNode)1