/** * */ package coverage.graph; import java.util.ArrayList; import java.util.Iterator; import java.util.List; /** * * * * The path is a list of nodes. The nodes can not be removed from a path. * But the path can be extended. The path can generate a subpath from the path. * * @author Jeff Offutt, modified by Wuzhi Xu, Date: Dec 12, 2006 * modified by Nan Li * */ public class Path { List path; public Path(){ path = new ArrayList(); } /** * * @param e */ public Path (Edge e) { path = new ArrayList(); path.add (e.getSrc()); path.add (e.getDest()); } /** * * @param n */ public Path (Node n) { path = new ArrayList(); path.add(n); } /** * * @param n */ public Path (Path p) { this.path = new ArrayList(); for(Node node:p.path) this.path.add(node); } /** * In order not to expose the inside 'list' data structure, return an iterator. * @return */ public Iterator getNodeIterator() { return path.iterator(); } /** * return a sequence of edges represented by the path. If size of the path is less than 2, * return an empty list * @return */ public List getEdgeList() { List edges = new ArrayList(); if(size() > 1) for(int i = 0;i < path.size()-1;i++) edges.add(new Edge(path.get(i), path.get(i+1))); return edges; } /** * attach the argument path at the end of this path * @param p */ public Path immutableExtendedPath(Path p) { Iterator nodes = p.getNodeIterator(); Iterator nodes1 = path.iterator(); Path newPath = new Path(); while(nodes1.hasNext()){ Node node = nodes1.next(); newPath.path.add(node); } while(nodes.hasNext()) { newPath.path.add(nodes.next()); } return newPath; } /** * attach the argument path at the end of this path * @param p */ public void extendPath(Path p) { Iterator nodes=p.getNodeIterator(); while(nodes.hasNext()) { path.add(nodes.next()); } } /** * add the node at the end of this path * @param n */ public void extendPath (Node n) { path.add(n); } /** *decide if the current path forms a cycle,that is, the first and the last nodes are the same * *@return true if it is a cycle */ public boolean isCycle () { if (path.get(0).equals (path.get(path.size()-1))) return true; else return false; } /** * @return a new path identical to the object */ public Object clone () { Path p = new Path(path.get(0)); for (int i = 1; i < path.size(); i++) p.extendPath(path.get(i)); return (p); } /** *@return number of nodes in the path */ public int size () { return (path.size()); } /** *@return the node at the specified position */ public Node get (int index) { return (path.get (index)); } /** * remove a node from the path based on an index *@return void */ public void remove (int index) { path.remove(index); } /** * remove a node *@return void */ public void remove (Node node) { path.remove(node); } /** *@param Node n *@return index of node n at the first appeared position, -1 if not present */ public int indexOf (Node n) { for(int i = 0;i < path.size();i++) if(path.get(i).equals(n)) return i; return -1; } /** *@param Node n, index of node n at the last appeared position *@return index of node n at the next appeared position, -1 if not present */ public int nextIndexOf (Node n, int index) { if(index <= -1) return -1; if(index >= path.size()) return -1; for(int i = index + 1;i < path.size();i++) if(path.get(i).equals(n) && i != index) return i; return -1; } /** * * @param Node n * @return index of node n at the last appeared position, -1 if not present */ public int lastIndexOf(Node n) { for(int i = path.size() - 1;i > -1;i--) if(path.get(i).equals(n)) return i; return -1; } /** * * @return the last node of path */ public Node getEnd() { return (path.get(path.size()-1)); } /** * It is exactly implementation of detour in the textbook. * Tour with Detours: * Test path p is said to tour subpath q with detours * if and only if every node in q is also in p in the same order * p1.detour(p2) means that p1 tours p2 with Detours * @param p * @return true if p1 tours p2 with detours else return false */ public boolean detour(Path p2) { //get the iterator for the path p2 Iterator it=p2.getNodeIterator(); int pointer=0; //if the path p2 has more node, check if path has the same node in the same order //if the nodes are in the same order detour is assigned with a true value else a false value while(it.hasNext()) { boolean detour=false; Node n=it.next(); for(;pointer edges=getEdgeList(); List e=p.getEdgeList(); int pointer=0; // if the path p2 has more node, check if path has the same node in the same order //if the nodes are in the same order detour is assigned with a true value else a false value for(int i=0;i