@Override public DirectCompilerResult visit(ContextEntryNode n) { DirectCompilerResult key = n.getName().accept(this); DirectCompilerResult value = n.getValue().accept(this); if (key.resultType != BuiltInType.STRING) { throw new IllegalArgumentException( "a Context Entry Key must be a valid FEEL String type"); } String keyText = key.getExpression().asStringLiteralExpr().getValue(); // .setEntry(key, value) MethodCallExpr setEntryContextCall = FeelCtx.setEntry(keyText, value.getExpression()); return DirectCompilerResult.of( setEntryContextCall, value.resultType, value.getFieldDeclarations()); }
@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 ) ) ); }
@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") ); }
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\"," + "}" ) ); FunctionDefNode cos = (FunctionDefNode) entry.getValue(); assertThat( cos.getFormalParameters().size(), is( 1 ) ); assertThat( cos.getFormalParameters().get( 0 ).getText(), is( "angle" ) ); 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)\"" ) );
assertThat(nameNode.getText(), is(notNullValue())); assertThat(nameNode.getText(), is("\"a string key\"")); // Reference String literal test, BaseNode#getText() return the FEEL equivalent expression, in this case quoted. assertThat( entry.getValue(), is( instanceOf( NumberNode.class ) ) ); assertThat( entry.getResultType(), is( BuiltInType.NUMBER ) ); assertThat( entry.getValue().getText(), is("10") ); 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" ) ); 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]" ) );
assertThat( name.getParts().size(), is( 5 ) ); assertThat( entry.getName().getText(), is("a variable with in keyword") ); assertThat( entry.getValue(), is( instanceOf( NumberNode.class ) ) ); assertThat( entry.getResultType(), is( BuiltInType.NUMBER ) ); assertThat( entry.getValue().getText(), is("10") ); assertThat( name.getParts().size(), is( 2 ) ); assertThat( entry.getName().getText(), is("another variable") ); assertThat( entry.getValue(), is( instanceOf( InfixOpNode.class ) ) ); assertThat( entry.getResultType(), is( BuiltInType.NUMBER ) ); assertThat( entry.getValue().getText(), is( "a variable with in keyword + 20" ) ); assertThat( name.getParts().size(), is( 3 ) ); assertThat( entry.getName().getText(), is("another in variable") ); assertThat( entry.getValue(), is( instanceOf( InfixOpNode.class ) ) ); assertThat( entry.getResultType(), is( BuiltInType.NUMBER ) ); assertThat( entry.getValue().getText(), is( "an external in variable / 2" ) );
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") ); 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( 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") );