/** * */ package coverage.logic; import java.util.ArrayList; import java.util.BitSet; import java.util.Hashtable; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Vector; /** *It is the main class for calculating the logic coverage here * * @author Wuzhi Xu * * Date: Jan 23, 2007 * * TODO: */ public class Logic { Expression expr; List vars; TruthTable table;//a variable of TruthTable final static boolean[] TF=new boolean[]{true, false}; /** * * @param expr the expression that need to calculate the coverage */ public Logic(Expression expr) { //initialization this.expr = expr; vars = new ArrayList(); findVariables(null, expr, true); table = createTruthTable(); } /** * This method should be deprecated in the future, because it expose the inside data structure to the user * Also, two-demension array is too much, replace array with bitset in the future. * * @return boolean[][] a two dimension array that contains the truth table */ public TruthTable getTruthTable() { return table; } /** * * @return int the number of variables in the expression */ public int sizeOfVariables() { return vars.size(); } /** * @deprecated replaced by getVariableArray(); * @return Iterator the interator that contains all variables */ public Iterator getVariableInterator() { return vars.iterator(); } /** * to avoid exposing the inside data structure, return array * * @return VarExpression[] an array that contains all variables */ public VarExpression[] getVariableArray() { return vars.toArray(new VarExpression[0]); } /** * If there is three variables, there are * three array in the list. The array is int[*][2](* means the length might be 0..n). The order of array in * the list is the same as the order of variables in the variables array and truth table. * * @return List A list of all test pairs for each variables. */ public List> getAllGACC() { //create a new List> List> result = new ArrayList>(); //go through each variable for(int k = 0;k < vars.size();k++) { //a List storing row numbers as the corresponding predicate is true List t = new ArrayList(); //a List storing row numbers as the corresponding predicate is false List f = new ArrayList(); //go through each row of the table for(int i = 0;i < table.getRowNum();i++) { //in each row, calculate if this variable determines the predicate //if it does, put it to t List else put it to f List //First, in this row, set the corresponding values from the TruthTable to the variables for(int j = 0;j < vars.size();j++) { vars.get(j).setValue(table.getRow(i).get(j)); } //Second, calculate P (x = true) (x is the variable) vars.get(k).setValue(true); boolean tvar = expr.getValue(); //calculate P (x = false) (x is the variable) vars.get(k).setValue(false); boolean fvar = expr.getValue(); //Last but not least, if Px = P (x = true) exclusive OR P (x = false) is true, the variable determines the predicate if(tvar^fvar) { //in this row, if the variable is true, add it to t, else to f if(table.getRow(i).get(k)) t.add(i); else f.add(i); }//end if }//end for loop of the variable i //int[][] gacc=new int[t.size()*f.size()][2]; //create a List List gacc = new ArrayList(); //take the matched elements from t and f lists and put them into gacc for(int i = 0;i < t.size();i++) for(int j = 0;j < f.size();j++) { int[] pair = new int[2]; //get an element from t pair[0] = t.get(i); //get another element from f pair[1] = f.get(j); gacc.add(pair); } //add one gacc result.add(gacc); }//end for loop of the variable k return result; } /** * Same as getAllGACC * * @return List A list of all test pairs for each variables. */ public List> getAllCACC() { //create a new List> List> result=new ArrayList>(); //go through each variable for(int k = 0;k < vars.size();k++) { //a List storing row numbers as the corresponding predicate is true List t = new ArrayList(); //a List storing row numbers as the corresponding predicate is false List f = new ArrayList(); //go through each row of the table for(int i = 0;i < table.getRowNum();i++) { //in each row, calculate if this variable determines the predicate //if it does, put it to t List else put it to f List //First, in this row, set the corresponding values from the TruthTable to the variables for(int j = 0;j < vars.size();j++) { vars.get(j).setValue(table.getRow(i).get(j)); } //Second, calculate P (x = true) (x is the variable) vars.get(k).setValue(true); boolean tvar = expr.getValue(); //calculate P (x = false) (x is the variable) vars.get(k).setValue(false); boolean fvar = expr.getValue(); //Last but not least, if Px = P (x = true) exclusive OR P (x = false) is true, the variable determines the predicate if(tvar^fvar) { //in this row, if the variable is true, add it to t, else to f if(table.getRow(i).get(k)) t.add(i); else f.add(i); }//end if }//end for loop with variable i //create a List List cacc = new ArrayList(); for(int i = 0;i < t.size();i++) for(int j = 0;j < f.size();j++) { //check if values of predicate are opposite //get rid of the elements of which their predicate values are the same if(table.getRowValue(t.get(i))^ table.getRowValue(f.get(j))) { int[] pair = new int[2]; pair[0] = t.get(i); pair[1] = f.get(j); cacc.add(pair); } } result.add(cacc); } return result; } /** * Same as getAllGACC * * @return List A list of all test pairs for each variables. */ public List> getAllRACC() { List> result=new ArrayList>(); for(int k=0;k t=new ArrayList(); List f=new ArrayList(); for(int i=0;i racc=new ArrayList(); for(int i=0;i A list of all test pairs for each variables. * */ public List> getAllGICC() { List> result = new ArrayList>(); for(int k = 0;k < vars.size();k++) { /* List tt=new ArrayList(); List tf=new ArrayList(); List ft=new ArrayList(); List ff=new ArrayList(); */ List t = new ArrayList(); List f = new ArrayList(); for(int i = 0;i < table.getRowNum();i++) { for(int j = 0;j < vars.size();j++) { vars.get(j).setValue(table.getValue(i, j)); } vars.get(k).setValue(true); boolean tvar = expr.getValue(); vars.get(k).setValue(false); boolean fvar = expr.getValue(); if(!(tvar^fvar)) { /*if(table.getValue(i, k)) { if(table.getRowValue(i)) tt.add(i); else tf.add(i); } else { if(table.getRowValue(i)) ft.add(i); else ff.add(i); }*/ if(table.getRow(i).get(k)) t.add(i); else f.add(i); }//end if }//end for loop of variable i List gicc = new ArrayList(); List giccForTrue = new ArrayList(); List giccForFalse = new ArrayList(); /* for(int i=0;i> getAllRICC() { List> result = new ArrayList>(); for(int k = 0;k < vars.size();k++) { /* List tt=new ArrayList(); List tf=new ArrayList(); List ft=new ArrayList(); List ff=new ArrayList(); */ List t = new ArrayList(); List f = new ArrayList(); for(int i = 0;i < table.getRowNum();i++) { for(int j = 0;j < vars.size();j++) { vars.get(j).setValue(table.getValue(i, j)); } vars.get(k).setValue(true); boolean tvar=expr.getValue(); vars.get(k).setValue(false); boolean fvar=expr.getValue(); if(!(tvar^fvar)) { /*if(table.getValue(i, k)) { if(table.getRowValue(i)) tt.add(i); else tf.add(i); } else { if(table.getRowValue(i)) ft.add(i); else ff.add(i); }*/ if(table.getRow(i).get(k)) t.add(i); else f.add(i); } } List ricc = new ArrayList(); List riccForTrue = new ArrayList(); List riccForFalse = new ArrayList(); /* for(int i=0;i