use of org.kie.dmn.feel.lang.ast.ContextNode 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]"));
}
use of org.kie.dmn.feel.lang.ast.ContextNode 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)\""));
}
use of org.kie.dmn.feel.lang.ast.ContextNode 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)));
}
use of org.kie.dmn.feel.lang.ast.ContextNode 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"));
}
use of org.kie.dmn.feel.lang.ast.ContextNode 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));
}
Aggregations