// 组装二元操作符 private Expression populateBinaryOperator( BinaryOperatorImpl binaryOperator, Expression leftParameter, Expression rightParameter) { if (leftParameter instanceof Variable && binaryOperator.isLeftOperandNamed()) leftParameter = new ConstantImpl( ((Variable) leftParameter).getName(), leftParameter.getLocation(), evaluateInterceptors); if (rightParameter instanceof Variable && binaryOperator.isRightOperandNamed()) rightParameter = new ConstantImpl( ((Variable) rightParameter).getName(), rightParameter.getLocation(), evaluateInterceptors); binaryOperator.setOperands(leftParameter, rightParameter); try { // 常量优化算法,将常量先计算,如: // 2 * 3 + coins 将被优化为 6 + coins // (注: 表达式应尽量把常量计算写在前面, 如: 24 * 60 * days, 而不要用 days * 24 * 60) if (leftParameter instanceof Constant && rightParameter instanceof Constant && binaryOperator.isOptimize( ((Constant) leftParameter).getValue(), ((Constant) rightParameter).getValue())) return new ConstantImpl( binaryOperator.evaluate(null), binaryOperator.getLocation(), evaluateInterceptors); } catch (Exception e) { // 抛出任何异常都表示其不支持优化 } return binaryOperator; }
// 组装一元操作符 private Expression populateUnaryOperator( UnaryOperatorImpl unaryOperator, Expression parameter) { if (parameter instanceof Variable && unaryOperator.isOperandNamed()) parameter = new ConstantImpl( ((Variable) parameter).getName(), parameter.getLocation(), evaluateInterceptors); unaryOperator.setOperand(parameter); try { // 常量优化算法,将常量先计算,如: // ! true 将被优化为 false if (parameter instanceof Constant && unaryOperator.isOptimize(((Constant) parameter).getValue()) && !(((Constant) parameter).getValue() instanceof Function)) // Function不优化 return new ConstantImpl( unaryOperator.evaluate(null), unaryOperator.getLocation(), evaluateInterceptors); } catch (Exception e) { // 抛出任何异常都表示其不支持优化 } return unaryOperator; }