public static InfixOpNode newInfixOpNode(ParserRuleContext ctx, BaseNode left, String op, BaseNode right) { return new InfixOpNode( ctx, left, op, right ); }
/** * FEEL spec Table 45 * Delegates to {@link InfixOpNode} except evaluationcontext */ public static Object add(Object left, Object right) { return InfixOpNode.add(left, right, null); }
/** * FEEL spec Table 38 * Delegates to {@link InfixOpNode} except evaluationcontext */ public static Object and(Object left, Object right) { return InfixOpNode.and(left, right, null); }
@Override public DirectCompilerResult visit(InfixOpNode n) { DirectCompilerResult left = n.getLeft().accept(this); DirectCompilerResult right = n.getRight().accept(this); MethodCallExpr expr = Expressions.binary( n.getOperator(), left.getExpression(), right.getExpression()); return DirectCompilerResult.of(expr, BuiltInType.UNKNOWN).withFD(left).withFD(right); }
switch ( operator ) { case ADD: return add( left, right, ctx ); case SUB: return sub( left, right, ctx ); case MULT: return mult( left, right, ctx ); case DIV: return div( left, right, ctx ); case POW: return math( left, right, ctx, (l, r) -> BigDecimalMath.pow( l, r, MathContext.DECIMAL128 ) ); case AND: return and( left, right, ctx ); case OR: return or( left, right, ctx ); case LTE: return EvalHelper.compare( left, right, ctx, (l, r) -> l.compareTo( r ) <= 0 );
@Test public void testComparisonInFixOp() { String inputExpression = "foo >= bar * 10"; BaseNode infix = parse( inputExpression ); assertThat( infix, is( instanceOf( InfixOpNode.class ) ) ); assertThat( infix.getResultType(), is( BuiltInType.BOOLEAN ) ); assertThat( infix.getText(), is( inputExpression ) ); InfixOpNode in = (InfixOpNode) infix; assertThat( in.getLeft(), is( instanceOf( NameRefNode.class ) ) ); assertThat( in.getLeft().getText(), is( "foo" ) ); assertThat( in.getRight(), is( instanceOf( InfixOpNode.class ) ) ); assertThat( in.getRight().getText(), is( "bar * 10" ) ); }
public static Object pow(Object left, Object right) { return InfixOpNode.math(left, right, null, (l, r) -> BigDecimalMath.pow(l, r, MathContext.DECIMAL128)); }
/** * FEEL spec Table 45 * Delegates to {@link InfixOpNode} except evaluationcontext */ public static Object div(Object left, Object right) { return InfixOpNode.div(left, right, null); }
/** * FEEL spec Table 45 * Delegates to {@link InfixOpNode} except evaluationcontext */ public static Object mult(Object left, Object right) { return InfixOpNode.mult(left, right, null); }
} else if( param instanceof InNode ) { return ASTBuilderFactory.newFunctionInvocationNode( ctx, name, params ); } else if( param instanceof InfixOpNode && ((InfixOpNode)param).isBoolean() ) { return ASTBuilderFactory.newFunctionInvocationNode( ctx, name, params ); } else if (param instanceof FunctionInvocationNode) {
@Test public void testConditionalLogicalOp() { String inputExpression = "foo < 10 and bar = \"x\" or baz"; BaseNode infix = parse( inputExpression ); assertThat( infix, is( instanceOf( InfixOpNode.class ) ) ); assertThat( infix.getResultType(), is( BuiltInType.BOOLEAN ) ); assertThat( infix.getText(), is( inputExpression ) ); InfixOpNode or = (InfixOpNode) infix; assertThat( or.getLeft(), is( instanceOf( InfixOpNode.class ) ) ); assertThat( or.getLeft().getText(), is( "foo < 10 and bar = \"x\"" ) ); assertThat( or.getOperator(), is( InfixOpNode.InfixOperator.OR ) ); assertThat( or.getRight(), is( instanceOf( NameRefNode.class ) ) ); assertThat( or.getRight().getText(), is( "baz" ) ); InfixOpNode and = (InfixOpNode) or.getLeft(); assertThat( and.getLeft(), is( instanceOf( InfixOpNode.class ) ) ); assertThat( and.getLeft().getText(), is( "foo < 10" ) ); assertThat( and.getOperator(), is( InfixOpNode.InfixOperator.AND ) ); assertThat( and.getRight(), is( instanceOf( InfixOpNode.class ) ) ); assertThat( and.getRight().getText(), is( "bar = \"x\"" ) ); }
public static Object mult(Object left, Object right, EvaluationContext ctx) { if ( left == null || right == null ) { return null; } else if ( left instanceof Duration && right instanceof Number ) { return ((Duration)left).multipliedBy( ((Number) right).longValue() ); } else if ( left instanceof Number && right instanceof Duration ) { return Duration.ofSeconds( EvalHelper.getBigDecimalOrNull( left ).multiply( EvalHelper.getBigDecimalOrNull( ((Duration)right).getSeconds() ), MathContext.DECIMAL128 ).longValue() ); } else if ( left instanceof Duration && right instanceof Duration ) { return EvalHelper.getBigDecimalOrNull( ((Duration) left).getSeconds() ).multiply( EvalHelper.getBigDecimalOrNull( ((Duration)right).getSeconds() ), MathContext.DECIMAL128 ); } else if ( left instanceof Period && right instanceof Number ) { return Period.ofMonths( EvalHelper.getBigDecimalOrNull( ((Period)left).toTotalMonths() ).multiply( EvalHelper.getBigDecimalOrNull( ((Number) right).longValue() ), MathContext.DECIMAL128 ).intValue() ); } else if ( left instanceof Number && right instanceof Period ) { return Period.ofMonths( EvalHelper.getBigDecimalOrNull( left ).multiply( EvalHelper.getBigDecimalOrNull( ((Period)right).toTotalMonths() ), MathContext.DECIMAL128 ).intValue() ); } else if ( left instanceof Period && right instanceof Period ) { return EvalHelper.getBigDecimalOrNull( ((Period) left).toTotalMonths() ).multiply( EvalHelper.getBigDecimalOrNull( ((Period)right).toTotalMonths() ), MathContext.DECIMAL128 ); } else { return math( left, right, ctx, (l, r) -> l.multiply( r, MathContext.DECIMAL128 ) ); } }
public static Object div(Object left, BigDecimal right) { return right == null || right.signum() == 0 ? null : InfixOpNode.div(left, right, null); }
@Test public void testPower1() { String inputExpression = "y * 5 ** 3"; BaseNode infix = parse( inputExpression, mapOf(entry("y", BuiltInType.NUMBER)) ); assertThat( infix, is( instanceOf( InfixOpNode.class ) ) ); assertThat( infix.getResultType(), is( BuiltInType.NUMBER ) ); assertThat( infix.getText(), is( inputExpression ) ); InfixOpNode mult = (InfixOpNode) infix; assertThat( mult.getLeft(), is( instanceOf( NameRefNode.class ) ) ); assertThat( mult.getLeft().getText(), is( "y" ) ); assertThat( mult.getOperator(), is( InfixOpNode.InfixOperator.MULT ) ); assertThat( mult.getRight(), is( instanceOf( InfixOpNode.class ) ) ); assertThat( mult.getRight().getText(), is( "5 ** 3" ) ); InfixOpNode exp = (InfixOpNode) mult.getRight(); assertThat( exp.getLeft(), is( instanceOf( NumberNode.class ) ) ); assertThat( exp.getLeft().getText(), is( "5" ) ); assertThat( exp.getOperator(), is( InfixOpNode.InfixOperator.POW ) ); assertThat( exp.getRight(), is( instanceOf( NumberNode.class ) ) ); assertThat( exp.getRight().getText(), is( "3" ) ); }
public static Object div(Object left, Object right, EvaluationContext ctx) { if ( left == null || right == null ) { return null; } else if ( left instanceof Duration && right instanceof Number ) { return ((Duration)left).dividedBy( ((Number) right).longValue() ); } else if ( left instanceof Number && right instanceof Duration ) { return Duration.ofSeconds( EvalHelper.getBigDecimalOrNull( left ).divide( EvalHelper.getBigDecimalOrNull( ((Duration)right).getSeconds() ), MathContext.DECIMAL128 ).longValue() ); } else if ( left instanceof Duration && right instanceof Duration ) { return EvalHelper.getBigDecimalOrNull( ((Duration) left).getSeconds() ).divide( EvalHelper.getBigDecimalOrNull( ((Duration)right).getSeconds() ), MathContext.DECIMAL128 ); } else if ( left instanceof Period && right instanceof Number ) { return Period.ofMonths( EvalHelper.getBigDecimalOrNull( ((Period)left).toTotalMonths() ).divide( EvalHelper.getBigDecimalOrNull( ((Number) right).longValue() ), MathContext.DECIMAL128 ).intValue() ); } else if ( left instanceof Number && right instanceof Period ) { return Period.ofMonths( EvalHelper.getBigDecimalOrNull( left ).divide( EvalHelper.getBigDecimalOrNull( ((Period)right).toTotalMonths() ), MathContext.DECIMAL128 ).intValue() ); } else if ( left instanceof Period && right instanceof Period ) { return EvalHelper.getBigDecimalOrNull( ((Period) left).toTotalMonths() ).divide( EvalHelper.getBigDecimalOrNull( ((Period)right).toTotalMonths() ), MathContext.DECIMAL128 ); } else { return math( left, right, ctx, (l, r) -> l.divide( r, MathContext.DECIMAL128 ) ); } }
@Test public void testAdd1() { String inputExpression = "y + 5 * 3"; BaseNode infix = parse( inputExpression, mapOf(entry("y", BuiltInType.NUMBER)) ); assertThat( infix, is( instanceOf( InfixOpNode.class ) ) ); assertThat( infix.getResultType(), is( BuiltInType.NUMBER ) ); assertThat( infix.getText(), is( inputExpression ) ); InfixOpNode add = (InfixOpNode) infix; assertThat( add.getLeft(), is( instanceOf( NameRefNode.class ) ) ); assertThat( add.getLeft().getText(), is( "y" ) ); assertThat( add.getOperator(), is( InfixOpNode.InfixOperator.ADD ) ); assertThat( add.getRight(), is( instanceOf( InfixOpNode.class ) ) ); assertThat( add.getRight().getText(), is( "5 * 3" ) ); InfixOpNode mult = (InfixOpNode) add.getRight(); assertThat( mult.getLeft(), is( instanceOf( NumberNode.class ) ) ); assertThat( mult.getLeft().getText(), is( "5" ) ); assertThat( mult.getOperator(), is( InfixOpNode.InfixOperator.MULT ) ); assertThat( mult.getRight(), is( instanceOf( NumberNode.class ) ) ); assertThat( mult.getRight().getText(), is( "3" ) ); }
return ((OffsetTime) left).minus( (Duration) right); } else { return math( left, right, ctx, (l, r) -> l.subtract( r, MathContext.DECIMAL128 ) );
@Test public void testPower3() { String inputExpression = "y ** 5 * 3"; BaseNode infix = parse( inputExpression, mapOf(entry("y", BuiltInType.NUMBER)) ); assertThat( infix, is( instanceOf( InfixOpNode.class ) ) ); assertThat( infix.getResultType(), is( BuiltInType.NUMBER ) ); assertThat( infix.getText(), is( inputExpression ) ); InfixOpNode mult = (InfixOpNode) infix; assertThat( mult.getLeft(), is( instanceOf( InfixOpNode.class ) ) ); assertThat( mult.getLeft().getText(), is( "y ** 5" ) ); assertThat( mult.getOperator(), is( InfixOpNode.InfixOperator.MULT ) ); assertThat( mult.getRight(), is( instanceOf( NumberNode.class ) ) ); assertThat( mult.getRight().getText(), is( "3" ) ); InfixOpNode exp = (InfixOpNode) mult.getLeft(); assertThat( exp.getLeft(), is( instanceOf( NameRefNode.class ) ) ); assertThat( exp.getLeft().getText(), is( "y" ) ); assertThat( exp.getOperator(), is( InfixOpNode.InfixOperator.POW ) ); assertThat( exp.getRight(), is( instanceOf( NumberNode.class ) ) ); assertThat( exp.getRight().getText(), is( "5" ) ); }
return ((OffsetTime) right).plus( (Duration) left); } else { return math( left, right, ctx, (l, r) -> l.add( r, MathContext.DECIMAL128 ) );
@Test public void testPower4() { String inputExpression = "y ** ( 5 * 3 )"; BaseNode infix = parse( inputExpression, mapOf(entry("y", BuiltInType.NUMBER)) ); assertThat( infix, is( instanceOf( InfixOpNode.class ) ) ); assertThat( infix.getResultType(), is( BuiltInType.NUMBER ) ); assertThat( infix.getText(), is( inputExpression ) ); InfixOpNode exp = (InfixOpNode) infix; assertThat( exp.getLeft(), is( instanceOf( NameRefNode.class ) ) ); assertThat( exp.getLeft().getText(), is( "y" ) ); assertThat( exp.getOperator(), is( InfixOpNode.InfixOperator.POW ) ); assertThat( exp.getRight(), is( instanceOf( InfixOpNode.class ) ) ); assertThat( exp.getRight().getText(), is( "5 * 3" ) ); InfixOpNode mult = (InfixOpNode) exp.getRight(); assertThat( mult.getLeft(), is( instanceOf( NumberNode.class ) ) ); assertThat( mult.getLeft().getText(), is( "5" ) ); assertThat( mult.getOperator(), is( InfixOpNode.InfixOperator.MULT ) ); assertThat( mult.getRight(), is( instanceOf( NumberNode.class ) ) ); assertThat( mult.getRight().getText(), is( "3" ) ); }