/** * Checks the proposed connection between parent and child nodes, and returns true if a cycle would be created by * adding the child to the parent, or false if not * * @param parentNode dependant * @param childNode dependency * @return true if a cycle detected */ protected boolean detectCycle(T parentNode, T childNode) { if (parentNode.equals(childNode)) { return true; } Stack<T> stack = new Stack<>(); stack.push(parentNode); while (!stack.isEmpty()) { T node = stack.pop(); Collection<T> predecessors = graph.getPredecessors(node); if (predecessors != null) { for (T pred : predecessors) { if (pred == childNode) { return true; } } stack.addAll(predecessors); } } return false; }
/** * Checks the proposed connection between parent and child nodes, and returns true if a cycle would be created by * adding the child to the parent, or false if not * * @param parentNode * @param childNode * @return */ protected boolean detectCycle(V parentNode, V childNode) { if (parentNode == childNode) { return true; } Stack<V> stack = new Stack<>(); stack.push(parentNode); while (!stack.isEmpty()) { V node = stack.pop(); Collection<V> predecessors = getGraph().getPredecessors(node); if (predecessors != null) { for (V pred : predecessors) { if (pred == childNode) { return true; } } stack.addAll(predecessors); } } return false; }
@SuppressWarnings({ "unchecked", "rawtypes" }) public void expand(Forest tree, Forest subTree) { Object parent = null; Object edge = null; if(tree.getPredecessorCount(subTree) > 0) { parent = tree.getPredecessors(subTree).iterator().next(); edge = tree.getInEdges(subTree).iterator().next(); } tree.removeVertex(subTree); TreeUtils.addSubTree(tree, subTree, parent, edge); } }
/** * Checks the proposed connection between parent and child nodes, and returns true if a cycle would be created by * adding the child to the parent, or false if not * * @param parentNode * @param childNode * * @return */ protected boolean detectCycle(V parentNode, V childNode) { if (parentNode == childNode) { return true; } Stack<V> stack = new Stack<>(); stack.push(parentNode); while (!stack.isEmpty()) { V node = stack.pop(); Collection<V> predecessors = getGraph().getPredecessors(node); if (predecessors != null) { for (V pred : predecessors) { if (pred == childNode) { return true; } } stack.addAll(predecessors); } } return false; }
@SuppressWarnings({ "unchecked", "rawtypes" }) public void collapse(Layout layout, Forest tree, Object subRoot) throws InstantiationException, IllegalAccessException { // get a sub tree from subRoot Forest subTree = TreeUtils.getSubTree(tree, subRoot); Object parent = null; Object edge = null; if(tree.getPredecessorCount(subRoot) > 0) { parent = tree.getPredecessors(subRoot).iterator().next(); edge = tree.getInEdges(subRoot).iterator().next(); } tree.removeVertex(subRoot); if(parent != null) { tree.addEdge(edge, parent, subTree); } else { tree.addVertex(subTree); } layout.setLocation(subTree, (Point2D)layout.apply(subRoot)); }