public static <V,E> void addFromSubTree(Forest<V,E> tree, Forest<V,E> subTree, E edge, V parent, V root) { // add edge connecting parent and root to tree if(edge != null && parent != null) { tree.addEdge(edge, parent, root); } else { tree.addVertex(root); } Collection<E> outEdges = subTree.getOutEdges(root); for(E e : outEdges) { V opposite = subTree.getOpposite(root, e); addFromSubTree(tree, subTree, e, root, opposite); } } }
@Override public Collection<V> getChildren(V vertex) { return ((Forest<V, E>) delegate).getChildren(vertex); }
@Override public V getParent(V vertex) { return ((Forest<V, E>) delegate).getParent(vertex); }
@Override public synchronized boolean addService(@Nonnull T service) { checkNotNull(service); log.debug("adding service"); if (graph.containsVertex(service)) { return false; } return graph.addVertex(service); }
/** * Populates <code>subtree</code> with the subtree of <code>tree</code> * which is rooted at <code>root</code>. * @param <V> the vertex type * @param <E> the edge type * @param tree the tree whose subtree is to be extracted * @param subTree the tree instance which is to be populated with the subtree of <code>tree</code> * @param root the root of the subtree to be extracted */ public static <V,E> void growSubTree(Forest<V,E> tree, Forest<V,E> subTree, V root) { if(tree.getSuccessorCount(root) > 0) { Collection<E> edges = tree.getOutEdges(root); for(E e : edges) { subTree.addEdge(e, tree.getEndpoints(e)); } Collection<V> kids = tree.getSuccessors(root); for(V kid : kids) { growSubTree(tree, subTree, kid); } } }
V root, Map<E, Double> weights) { if(forest.getVertexCount() != 0) { throw new IllegalArgumentException("Supplied Forest must be empty"); this.forest.addVertex(root); updateForest(forest.getVertices(), unfinishedEdges);
for(E e : unfinishedEdges) { if(forest.getEdges().contains(e)) continue; forest.addEdge(nextEdge, currentVertex, nextVertex); updateForest(forest.getVertices(), unfinishedEdges); leftovers.removeAll(forest.getVertices()); if(leftovers.size() > 0) { V anotherRoot = leftovers.iterator().next(); forest.addVertex(anotherRoot); updateForest(forest.getVertices(), unfinishedEdges);
private void createTree() { graph.addVertex("V0"); graph.addEdge(edgeFactory.get(), "V0", "V1"); graph.addEdge(edgeFactory.get(), "V0", "V2"); graph.addEdge(edgeFactory.get(), "V1", "V4"); graph.addEdge(edgeFactory.get(), "V2", "V3"); graph.addEdge(edgeFactory.get(), "V2", "V5"); graph.addEdge(edgeFactory.get(), "V4", "V6"); graph.addEdge(edgeFactory.get(), "V4", "V7"); graph.addEdge(edgeFactory.get(), "V3", "V8"); graph.addEdge(edgeFactory.get(), "V6", "V9"); graph.addEdge(edgeFactory.get(), "V4", "V10"); graph.addVertex("A0"); graph.addEdge(edgeFactory.get(), "A0", "A1"); graph.addEdge(edgeFactory.get(), "A0", "A2"); graph.addEdge(edgeFactory.get(), "A0", "A3"); graph.addVertex("B0"); graph.addEdge(edgeFactory.get(), "B0", "B1"); graph.addEdge(edgeFactory.get(), "B0", "B2"); graph.addEdge(edgeFactory.get(), "B1", "B4"); graph.addEdge(edgeFactory.get(), "B2", "B3"); graph.addEdge(edgeFactory.get(), "B2", "B5"); graph.addEdge(edgeFactory.get(), "B4", "B6"); graph.addEdge(edgeFactory.get(), "B4", "B7"); graph.addEdge(edgeFactory.get(), "B3", "B8"); graph.addEdge(edgeFactory.get(), "B6", "B9"); }
@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)); }
@Override public synchronized void createDependency(@Nonnull T dependant, @Nonnull T dependency, @Nonnull Dependency.Type type) { checkNotNull(dependant); checkNotNull(dependency); checkNotNull(type); if (hasDependency(dependant, dependency)) { throw new DuplicateDependencyException("There can only be one dependency between the same two services"); } if (!graph.containsVertex(dependant)) { graph.addVertex(dependant); } if (!graph.containsVertex(dependency)) { graph.addVertex(dependency); } ServiceEdge edge = new ServiceEdge(type); graph.addEdge(edge, dependant, dependency); if (detectCycle(dependant, dependency)) { throw new CycleDetectedException("Creating dependency from " + dependant + " to " + dependency + " has caused a loop"); } }
/** * Returns the subtree of <code>tree</code> which is rooted at * <code>root</code> as a <code>Forest<V,E></code>. The tree returned is an * independent entity, although it uses the same vertex and edge objects. * * @param <V> * the vertex type * @param <E> * the edge type * @param forest * the tree whose subtree is to be extracted * @param root * the root of the subtree to be extracted * @return the subtree of <code>tree</code> which is rooted at * <code>root</code> * @throws InstantiationException * if a new tree of the same type cannot be created * @throws IllegalAccessException */ public static <V, E> Tree<V, E> getSubTree(Forest<V, E> forest, V root) { if (!forest.containsVertex(root)) { throw new IllegalArgumentException( "Specified tree does not contain the specified root as a vertex"); } Forest<V, E> subforest = (Forest<V, E>) forest.newInstance(); subforest.addVertex(root); growSubTree(forest, subforest, root); return subforest.getTrees().iterator().next(); }
/** * Returns a list of all the entries in the tree * * @return */ public Collection<V> getEntries() { return graph.getVertices(); }
/** * Returns the subtree of <code>tree</code> which is rooted at <code>root</code> as a <code>Forest</code>. * The tree returned is an independent entity, although it uses the same vertex and edge objects. * @param <V> the vertex type * @param <E> the edge type * @param forest the tree whose subtree is to be extracted * @param root the root of the subtree to be extracted * @return the subtree of <code>tree</code> which is rooted at <code>root</code> * @throws InstantiationException if a new tree of the same type cannot be created * @throws IllegalAccessException if a new tree of the same type cannot be created */ @SuppressWarnings("unchecked") public static <V,E> Tree<V,E> getSubTree(Forest<V,E> forest, V root) throws InstantiationException, IllegalAccessException { if (!forest.containsVertex(root)) throw new IllegalArgumentException("Specified tree does not contain the specified root as a vertex"); Forest<V,E> subforest = forest.getClass().newInstance(); subforest.addVertex(root); growSubTree(forest, subforest, root); return subforest.getTrees().iterator().next(); }
@Override public synchronized int size() { return graph.getVertexCount(); }
@Override public Collection<Tree<V, E>> getTrees() { return ((Forest<V, E>) delegate).getTrees(); }
public void addNode(V node) { graph.addVertex(node); }
/** * Adds a {@code childNode} to {@code parentNode}. Note that if {@code parentNode} is not already in the tree, it * will be added - which may mean that you no longer have a single root * * @param parentNode * @param childNode */ public void addChild(V parentNode, V childNode) { if (parentNode == null) { addNode(childNode); } else { graph.addEdge(newEdge(), parentNode, childNode); } }
/** * Connects <code>subTree</code> to <code>tree</code> by attaching it as a child * of <code>node</code> with edge <code>connectingEdge</code>. * @param <V> the vertex type * @param <E> the edge type * @param tree the tree to which <code>subTree</code> is to be added * @param subTree the tree which is to be grafted on to <code>tree</code> * @param node the parent of <code>subTree</code> in its new position in <code>tree</code> * @param connectingEdge the edge used to connect <code>subtree</code>'s root as a child of <code>node</code> */ public static <V,E> void addSubTree(Forest<V,E> tree, Forest<V,E> subTree, V node, E connectingEdge) { if (node != null && !tree.containsVertex(node)) throw new IllegalArgumentException("Specified tree does not contain the specified node as a vertex"); V root = subTree.getTrees().iterator().next().getRoot(); addFromSubTree(tree, subTree, connectingEdge, node, root); }
private int calculateDimensionX(V v) { int size = 0; int childrenNum = graph.getSuccessors(v).size(); if (childrenNum != 0) { for (V element : graph.getSuccessors(v)) { size += calculateDimensionX(element) + distX; } } size = Math.max(0, size - distX); basePositions.put(v, size); return size; }
@Override public int getChildCount(V vertex) { return ((Forest<V, E>) delegate).getChildCount(vertex); }