/** * 对一个ExpressionNode建立其表达式变量并返回 * * @param root the root of a expression tree which provided by symbolic execution environment * @return */ public IntegerExpressionVariable constraintVariableModeling(ExpressionNode root) { IntegerExpressionVariable result = null; ExpressionType type = root.getType(); if (type == ExpressionType.expression) { ExpressionOperator operator = root.getOperator(); ExpressionNode leftPart = root.getLeft(); ExpressionNode rightPart = root.getRight(); ExpressionType leftType = leftPart.getType(); ExpressionType rightType = rightPart.getType(); /* * 下述步骤是将表达式递归的构建成IntegerExpressionVariable,以提供给最后的Constraint、 * 主要思路是针对左右两部分不同的类型进行表达式变量的构建,对int,variable和expression进行各种组合 * 需要注意的是在组合中没有(int, int),这是因为如果两个子节点都是int具体数值的话,那么这个表达式将是可以 * 被计算出来的,其将会被计算结果代替。这一步的工作可能在符号执行过程中完成,也可能通过一个函数对表达式树进行 * 后序遍历来完成 */ switch (operator) { case minus: { // int and variable if (leftType == ExpressionType.single_int && rightType == ExpressionType.single_variable) { int leftValue = Integer.parseInt(leftPart.getValue()); IntegerVariable rightValue = (IntegerVariable) envVariableValueHashMap.get(rightPart.getValue()); print((IntegerVariable) envVariableValueHashMap.get(rightPart.getValue())); result = Choco.minus(leftValue, rightValue); } else if (rightType == ExpressionType.single_int && leftType == ExpressionType.single_variable) { int rightValue = Integer.parseInt(rightPart.getValue()); IntegerVariable leftValue = (IntegerVariable) envVariableValueHashMap.get(leftPart.getValue()); print((IntegerVariable) envVariableValueHashMap.get(leftPart.getValue())); result = Choco.minus(leftValue, rightValue); } // int and expression else if (leftType == ExpressionType.single_int && rightType == ExpressionType.expression) { int leftValue = Integer.parseInt(leftPart.getValue()); IntegerExpressionVariable rightValue = constraintVariableModeling(rightPart); result = Choco.minus(leftValue, rightValue); } else if (rightType == ExpressionType.single_int && leftType == ExpressionType.expression) { int rightValue = Integer.parseInt(rightPart.getValue()); IntegerExpressionVariable leftValue = constraintVariableModeling(leftPart); result = Choco.minus(leftValue, rightValue); } // expression and variable else if (leftType == ExpressionType.expression && rightType == ExpressionType.single_variable) { IntegerVariable rightValue = (IntegerVariable) envVariableValueHashMap.get(rightPart.getValue()); IntegerExpressionVariable leftValue = constraintVariableModeling(leftPart); result = Choco.minus(leftValue, rightValue); } else if (rightType == ExpressionType.expression && leftType == ExpressionType.single_variable) { IntegerVariable leftValue = (IntegerVariable) envVariableValueHashMap.get(leftPart.getValue()); IntegerExpressionVariable rightValue = constraintVariableModeling(rightPart); result = Choco.minus(leftValue, rightValue); } // both are expression else if (leftType == ExpressionType.expression && rightType == ExpressionType.expression) { IntegerExpressionVariable leftValue = constraintVariableModeling(leftPart); IntegerExpressionVariable rightValue = constraintVariableModeling(rightPart); result = Choco.minus(leftValue, rightValue); } // both are variable else if (leftType == ExpressionType.single_variable && rightType == ExpressionType.single_variable) { IntegerVariable leftValue = (IntegerVariable) envVariableValueHashMap.get(leftPart.getValue()); IntegerVariable rightValue = (IntegerVariable) envVariableValueHashMap.get(rightPart.getValue()); result = Choco.minus(leftValue, rightValue); } break; } case plus: { // int and variable if (leftType == ExpressionType.single_int && rightType == ExpressionType.single_variable) { int leftValue = Integer.parseInt(leftPart.getValue()); IntegerVariable rightValue = (IntegerVariable) envVariableValueHashMap.get(rightPart.getValue()); result = Choco.plus(leftValue, rightValue); } else if (rightType == ExpressionType.single_int && leftType == ExpressionType.single_variable) { int rightValue = Integer.parseInt(rightPart.getValue()); IntegerVariable leftValue = (IntegerVariable) envVariableValueHashMap.get(leftPart.getValue()); result = Choco.plus(leftValue, rightValue); } // int and expression else if (leftType == ExpressionType.single_int && rightType == ExpressionType.expression) { int leftValue = Integer.parseInt(leftPart.getValue()); IntegerExpressionVariable rightValue = constraintVariableModeling(rightPart); result = Choco.plus(leftValue, rightValue); } else if (rightType == ExpressionType.single_int && leftType == ExpressionType.expression) { int rightValue = Integer.parseInt(rightPart.getValue()); IntegerExpressionVariable leftValue = constraintVariableModeling(leftPart); result = Choco.plus(leftValue, rightValue); } // expression and variable else if (leftType == ExpressionType.expression && rightType == ExpressionType.single_variable) { IntegerVariable rightValue = (IntegerVariable) envVariableValueHashMap.get(rightPart.getValue()); IntegerExpressionVariable leftValue = constraintVariableModeling(leftPart); result = Choco.plus(leftValue, rightValue); } else if (rightType == ExpressionType.expression && leftType == ExpressionType.single_variable) { IntegerVariable leftValue = (IntegerVariable) envVariableValueHashMap.get(leftPart.getValue()); IntegerExpressionVariable rightValue = constraintVariableModeling(rightPart); result = Choco.plus(leftValue, rightValue); } // both are expression else if (leftType == ExpressionType.expression && rightType == ExpressionType.expression) { IntegerExpressionVariable leftValue = constraintVariableModeling(leftPart); IntegerExpressionVariable rightValue = constraintVariableModeling(rightPart); result = Choco.plus(leftValue, rightValue); } // both are variable else if (leftType == ExpressionType.single_variable && rightType == ExpressionType.single_variable) { IntegerVariable leftValue = (IntegerVariable) envVariableValueHashMap.get(leftPart.getValue()); IntegerVariable rightValue = (IntegerVariable) envVariableValueHashMap.get(rightPart.getValue()); result = Choco.plus(leftValue, rightValue); } break; } case multi: { // int and variable if (leftType == ExpressionType.single_int && rightType == ExpressionType.single_variable) { int leftValue = Integer.parseInt(leftPart.getValue()); IntegerVariable rightValue = (IntegerVariable) envVariableValueHashMap.get(rightPart.getValue()); result = Choco.mult(leftValue, rightValue); } else if (rightType == ExpressionType.single_int && leftType == ExpressionType.single_variable) { int rightValue = Integer.parseInt(rightPart.getValue()); IntegerVariable leftValue = (IntegerVariable) envVariableValueHashMap.get(leftPart.getValue()); result = Choco.mult(leftValue, rightValue); } // int and expression else if (leftType == ExpressionType.single_int && rightType == ExpressionType.expression) { int leftValue = Integer.parseInt(leftPart.getValue()); IntegerExpressionVariable rightValue = constraintVariableModeling(rightPart); result = Choco.mult(leftValue, rightValue); } else if (rightType == ExpressionType.single_int && leftType == ExpressionType.expression) { int rightValue = Integer.parseInt(rightPart.getValue()); IntegerExpressionVariable leftValue = constraintVariableModeling(leftPart); result = Choco.mult(leftValue, rightValue); } // expression and variable else if (leftType == ExpressionType.expression && rightType == ExpressionType.single_variable) { IntegerVariable rightValue = (IntegerVariable) envVariableValueHashMap.get(rightPart.getValue()); IntegerExpressionVariable leftValue = constraintVariableModeling(leftPart); result = Choco.mult(leftValue, rightValue); } else if (rightType == ExpressionType.expression && leftType == ExpressionType.single_variable) { IntegerVariable leftValue = (IntegerVariable) envVariableValueHashMap.get(leftPart.getValue()); IntegerExpressionVariable rightValue = constraintVariableModeling(rightPart); result = Choco.mult(leftValue, rightValue); } // both are expression else if (leftType == ExpressionType.expression && rightType == ExpressionType.expression) { IntegerExpressionVariable leftValue = constraintVariableModeling(leftPart); IntegerExpressionVariable rightValue = constraintVariableModeling(rightPart); result = Choco.mult(leftValue, rightValue); } // both are variable else if (leftType == ExpressionType.single_variable && rightType == ExpressionType.single_variable) { IntegerVariable leftValue = (IntegerVariable) envVariableValueHashMap.get(leftPart.getValue()); IntegerVariable rightValue = (IntegerVariable) envVariableValueHashMap.get(rightPart.getValue()); result = Choco.mult(leftValue, rightValue); } break; } case div: { // int and variable if (leftType == ExpressionType.single_int && rightType == ExpressionType.single_variable) { int leftValue = Integer.parseInt(leftPart.getValue()); IntegerVariable rightValue = (IntegerVariable) envVariableValueHashMap.get(rightPart.getValue()); result = Choco.div(leftValue, rightValue); } else if (rightType == ExpressionType.single_int && leftType == ExpressionType.single_variable) { int rightValue = Integer.parseInt(rightPart.getValue()); IntegerVariable leftValue = (IntegerVariable) envVariableValueHashMap.get(leftPart.getValue()); result = Choco.div(leftValue, rightValue); } // int and expression else if (leftType == ExpressionType.single_int && rightType == ExpressionType.expression) { int leftValue = Integer.parseInt(leftPart.getValue()); IntegerExpressionVariable rightValue = constraintVariableModeling(rightPart); result = Choco.div(leftValue, rightValue); } else if (rightType == ExpressionType.single_int && leftType == ExpressionType.expression) { int rightValue = Integer.parseInt(rightPart.getValue()); IntegerExpressionVariable leftValue = constraintVariableModeling(leftPart); result = Choco.div(leftValue, rightValue); } // expression and variable else if (leftType == ExpressionType.expression && rightType == ExpressionType.single_variable) { IntegerVariable rightValue = (IntegerVariable) envVariableValueHashMap.get(rightPart.getValue()); IntegerExpressionVariable leftValue = constraintVariableModeling(leftPart); result = Choco.div(leftValue, rightValue); } else if (rightType == ExpressionType.expression && leftType == ExpressionType.single_variable) { IntegerVariable leftValue = (IntegerVariable) envVariableValueHashMap.get(leftPart.getValue()); IntegerExpressionVariable rightValue = constraintVariableModeling(rightPart); result = Choco.div(leftValue, rightValue); } // both are expression else if (leftType == ExpressionType.expression && rightType == ExpressionType.expression) { IntegerExpressionVariable leftValue = constraintVariableModeling(leftPart); IntegerExpressionVariable rightValue = constraintVariableModeling(rightPart); result = Choco.div(leftValue, rightValue); } // both are variable else if (leftType == ExpressionType.single_variable && rightType == ExpressionType.single_variable) { IntegerVariable leftValue = (IntegerVariable) envVariableValueHashMap.get(leftPart.getValue()); IntegerVariable rightValue = (IntegerVariable) envVariableValueHashMap.get(rightPart.getValue()); result = Choco.div(leftValue, rightValue); } break; } } } else if (type == ExpressionType.single_variable) { result = (IntegerVariable) envVariableValueHashMap.get(root.getValue()); } else if (type == ExpressionType.single_int) { System.out.println("Try to make a integer into a variable: " + root.getValue()); } else { System.out.println("can't generate the cp variable" + type); } /* * 被注释掉的这段应该是用不着的 */ return result; }
public Object minus(Object exp, int value) { return Choco.minus((IntegerExpressionVariable) exp, value); }
public Object minus(Object exp1, Object exp2) { return Choco.minus((IntegerExpressionVariable) exp1, (IntegerExpressionVariable) exp2); }
public Object minus(int value, Object exp) { return Choco.minus(value, (IntegerExpressionVariable) exp); }