public static Object evaluate( String expression ) { return evaluate( expression, null ); } }
/** * Functions which will return the true value if the condition is true, false * value if not. * * @param condititon the condition. * @param trueValue the true value. * @param falseValue the false value. * @return a String. */ public static Object condition( String condititon, Object trueValue, Object falseValue ) { return ExpressionUtils.isTrue( condititon, null ) ? trueValue : falseValue; }
/** * Evaluates the given expression. The given variables will be substituted * in the expression. Converts the result of the evaluation to a Double. * Throws an IllegalStateException if the result could not be converted to * a Double * * @param expression the expression. * @param vars the variables, can be null. * @return the result of the evaluation. */ public static Double evaluateToDouble( String expression, Map<String, Object> vars ) { Object result = evaluate( expression, vars ); if ( result == null ) { throw new IllegalStateException( "Result must be not null" ); } if ( !isNumeric( String.valueOf( result ) ) ) { throw new IllegalStateException( "Result must be numeric: " + result + ", " + result.getClass() ); } return Double.valueOf( String.valueOf( result ) ); }
params.getProgramIndicator(), false, params.getEarliestStartDate(), params.getLatestEndDate() ); String sqlFilter = ExpressionUtils.asSql( filter );
@Transactional public String filterIsValid( String filter ) { String expr = getSubstitutedSQLFunc( getSubstitutedExpression( filter ) ); if ( ProgramIndicator.INVALID_IDENTIFIERS_IN_EXPRESSION.equals( expr ) || ProgramIndicator.UNKNOWN_VARIABLE.equals( expr ) ) { return expr; } if ( !ExpressionUtils.isBoolean( expr, null ) ) { return ProgramIndicator.FILTER_NOT_EVALUATING_TO_TRUE_OR_FALSE; } return ProgramIndicator.VALID; }
@Transactional public String expressionIsValid( String expression ) { String expr = getSubstitutedSQLFunc( getSubstitutedExpression( expression ) ); if ( ProgramIndicator.INVALID_IDENTIFIERS_IN_EXPRESSION.equals( expr ) || ProgramIndicator.UNKNOWN_VARIABLE.equals( expr ) ) { return expr; } if ( !ExpressionUtils.isValid( expr, null ) ) { return ProgramIndicator.EXPRESSION_NOT_VALID; } return ProgramIndicator.VALID; }
params.getProgramIndicator(), false, params.getEarliestStartDate(), params.getLatestEndDate() ); String sqlFilter = ExpressionUtils.asSql( filter );
/** * Evaluates the given expression. The given variables will be substituted * in the expression. * * @param expression the expression. * @param vars the variables, can be null. * @return the result of the evaluation. */ public static Object evaluate( String expression, Map<String, Object> vars ) { return evaluate( expression, vars, false ); }
/** * Evaluates the given expression to true or false. The given variables will * be substituted in the expression. * * @param expression the expression. * @param vars the variables, can be null. * @return true or false. */ public static boolean isTrue( String expression, Map<String, Object> vars ) { Object result = evaluate( expression, vars ); return ( result != null && result instanceof Boolean ) ? (Boolean) result : false; }
/** * Indicates whether the given expression is valid and evaluates to true or * false. * * @param expression the expression. * @param vars the variables, can be null. * @return true or false. */ public static boolean isBoolean( String expression, Map<String, Object> vars ) { try { Object result = evaluate( expression, vars ); return ( result instanceof Boolean ); } catch ( JexlException | NumberFormatException ex ) { return false; } }
/** * Indicates whether the given expression is valid, i.e. can be successfully * evaluated. * * @param expression the expression. * @param vars the variables, can be null. * @return true or false. */ public static boolean isValid( String expression, Map<String, Object> vars ) { try { Object result = evaluate( expression, vars, true ); return result != null; } catch ( JexlException | NumberFormatException ex ) { if ( ex.getMessage().contains( "divide error" ) ) { return true; //TODO division by zero masking } return false; } }
/** * Return string value of boolean output. False will be returned in case * of wrongly created expression * * @param expression to be evaluated. * @return string value of boolean true/false. */ @Nonnull @Override public String evaluate( @Nonnull String expression ) { String result = ""; try { result = ExpressionUtils.evaluate( expression ).toString(); } catch ( JexlException je ) { result = "false"; log.debug( DebugUtils.getStackTrace( je.getCause() ) ); } return result; } }