/** * */ package coverage.graph; import java.util.ArrayList; import java.util.Iterator; import java.util.List; /** * The graph for data flow. A additional data structure is a list of variables * * @author wuzhi * Modified by Nan Li * */ public class DFGraph extends Graph { List vars; /** * It is for a CFGraph down cast to DFGraph * * @param nodes * @param edges * @param start * @param ends */ DFGraph(List nodes, List edges, List starts, List ends) { this.nodes = nodes; this.edges = edges; this.ends = ends; this.starts = starts; vars = new ArrayList(); } public DFGraph() { super(); vars = new ArrayList(); } /** * It is better not to use this method, instead of createVariable(String name) * @deprecated * @param v */ void addVariable(Variable v) { vars.add(v); } /** * return an iterator of defs on nodes * @param v */ public Iterator getDefsOnNodes(Variable v){ return v.getDefIterator(); } /** * return an iterator of defs on nodes * @param v */ public Iterator getUsesOnNodes(Variable v){ return v.getUseIterator(); } /** * * @param v * @return a list of edges of have definitions */ public List getDefsOnEdges(Variable v){ return v.getDefsOnEdges(); } /** * * @param v * @return a list of edges of have definitions */ public List getUsesOnEdges(Variable v){ return v.getUsesOnEdges(); } /** * * @param name * @return the existed variable if the requested variable already exists in the Data Flow Graph * @return else return a new variable with the requested name. */ public Variable createVariable(String name) { //iterate all variables and see if the requested variable has existed for(int i = 0;i < vars.size();i++) if(vars.get(i).getName().equals(name)) return vars.get(i); //if the requested variable is not there, create a new variable with the requested name Variable v = new Variable(name); vars.add(v); return v; } /** * * @return an iterator of variables */ public Iterator getVariableIterator() { return vars.iterator(); } /** * @return number of variables */ public int sizeOfVariables() { return vars.size(); } /** * remove all variables in a data flow graph * */ public void removeVariables() { vars = new ArrayList(); } /** * * @param v: Variable * @return test paths of All Def Coverage * @throws InvalidGraphException */ public List findAllDef(Variable v) throws InvalidGraphException { if(!vars.contains(v)) return new ArrayList(); return generateTests(v.findAllDef()); } /** * * @param v: Variable * @return test paths of All Use Coverage * @throws InvalidGraphException */ public List findAllUse(Variable v) throws InvalidGraphException { if(!vars.contains(v)) return new ArrayList(); return generateTests(v.findAllUse()); } /** * * @param v: Variable * @return test paths of All DU Path Coverage * @throws InvalidGraphException */ public List findAllDUPath(Variable v) throws InvalidGraphException { if(!vars.contains(v)) return new ArrayList(); return generateTests(v.findDUPath()); } /** * * @param v * @return DU Pairs in a data flow graph */ public List findDuPairs(Variable v) throws InvalidGraphException{ if(!vars.contains(v)) return new ArrayList(); List dup = new ArrayList(); dup = v.findDuPairs(); return dup; } /** * * @param v * @return DU paths in a data flow graph */ public List findDUPaths(Variable v) throws InvalidGraphException{ if(!vars.contains(v)) return new ArrayList(); List duPaths = new ArrayList(); duPaths = v.findDUPath(); return duPaths; } /** * * @param paths * @return test paths for test requirements in data flow graphs * @throws InvalidGraphException */ private List generateTests(List paths) throws InvalidGraphException { List result = new ArrayList(); List tests = findTestPath(); for(int i=0;i