/** * @param <V> the vertex type for the graph Supplier * @param <E> the edge type for the graph Supplier * @return a {@code Supplier} that creates an instance of this graph type. */ public static final <V,E> Supplier<Tree<V,E>> getFactory() { return new Supplier<Tree<V,E>> () { public Tree<V,E> get() { return new DelegateTree<V,E>(new DirectedSparseMultigraph<V,E>()); } }; }
/** * Add an edge to the tree, connecting v1, the parent and v2, the child. * v1 must already exist in the tree, and v2 must not already exist * the passed edge must be unique in the tree. * * @param e a unique edge to add * @param v1 the parent node * @param v2 the child node * @return true if this call mutates the underlying graph * @see edu.uci.ics.jung.graph.Graph#addEdge(java.lang.Object, java.lang.Object, java.lang.Object) */ @Override public boolean addEdge(E e, V v1, V v2) { return addChild(e, v1, v2); }
public List<ClusterVO> getSiblings(Integer tenantId, ClusterVO cluster) { DelegateTree<ClusterVO, ItemAssocVO<Integer,Integer>> tree = clusters.get(tenantId); if (tree.isRoot(cluster)) return null; //root has no siblings ClusterVO parent = tree.getParent(cluster); Collection<ClusterVO> sibl = tree.getChildren(parent); List<ClusterVO> siblings = new ArrayList<ClusterVO>(); for (ClusterVO clusterVO : sibl) { if (!clusterVO.equals(cluster)) siblings.add(clusterVO); } return siblings; }
/** * Computes and returns the height of the tree. * * @return the height */ public int getHeight() { int height = 0; for(V v : getVertices()) { height = Math.max(height, getDepth(v)); } return height; }
public void renameCluster(Integer tenantId, String clusterName, String newName) throws ClusterException { ClusterVO cluster = loadCluster(tenantId, idMappingDAO.lookup(clusterName), itemTypeDAO.getIdOfType(tenantId, ClusterService.ITEMTYPE_CLUSTER)); DelegateTree<ClusterVO, ItemAssocVO<Integer,Integer>> tree = clusters.get(tenantId); if (tree.isRoot(cluster)) throw new ClusterException("ROOT cluster cannot be renamed!"); ClusterVO parent = tree.getParent(cluster); ClusterVO newCluster = new ClusterVO(tenantId, idMappingDAO.lookup(newName), itemTypeDAO.getIdOfType(tenantId, ClusterService.ITEMTYPE_CLUSTER), newName, cluster.getDescription()); // add the new cluster addCluster(newCluster, parent); // move all children to the new cluster for (ClusterVO child : tree.getChildren(cluster)) { moveCluster(tenantId, child.getName(), newName); } if (logger.isDebugEnabled()) { logger.debug("removed all children: " + tree.isLeaf(cluster)); } // move all items to the new cluster for (ItemVO<Integer, Integer> item : getItemsOfCluster(cluster)) { addItemToCluster(newCluster, item); } // delete the old cluster removeCluster(tenantId, clusterName); }
/** * remove the passed node, and all nodes that are descendants of the * passed node. * @param vertex the vertex to remove * @return <code>true</code> iff the tree was modified * @see edu.uci.ics.jung.graph.Graph#removeVertex(java.lang.Object) */ @Override public boolean removeVertex(V vertex) { if (!delegate.containsVertex(vertex)) return false; for(V v : getChildren(vertex)) { removeVertex(v); vertex_depths.remove(v); } // recalculate height vertex_depths.remove(vertex); return delegate.removeVertex(vertex); }
/** * Returns <code>true</code> if the passed node has no children. * * @return <code>true</code> if the passed node has no children */ public boolean isLeaf(V v) { if (!delegate.containsVertex(v)) { return false; } return getChildren(v).size() == 0; }
/** * computes whether the passed node is a root node (has no children) */ public boolean isRoot(V v) { if (!delegate.containsVertex(v)) { return false; } return getParent(v) == null; }
public void moveCluster(Integer tenantId, String clusterName, String newParent) { DelegateTree<ClusterVO, ItemAssocVO<Integer,Integer>> tree = clusters.get(tenantId); ClusterVO cluster = loadCluster(tenantId, idMappingDAO.lookup(clusterName), itemTypeDAO.getIdOfType(tenantId, ClusterService.ITEMTYPE_CLUSTER)); // disconnect subtree ItemAssocVO<Integer,Integer> parentEdge = tree.getParentEdge(cluster); itemAssocService.removeItemAssoc(parentEdge.getId()); tree.removeChild(cluster); // add cluster as child to new parent ClusterVO parent = loadCluster(tenantId, idMappingDAO.lookup(newParent), itemTypeDAO.getIdOfType(tenantId, ClusterService.ITEMTYPE_CLUSTER)); ItemAssocVO<Integer,Integer> edge = new ItemAssocVO<Integer,Integer>( parent.getItem().getTenant(), // tenantId parent.getItem(), //itemFrom assocTypeDAO.getIdOfType(parent.getItem().getTenant(), ClusterService.ASSOCTYPE_CLUSTER), //assocType 1.0, //assocValue cluster.getItem(), //itemTo sourceTypeDAO.getIdOfType(parent.getItem().getTenant(), SourceTypeDAO.SOURCETYPE_MANUALLY_CREATED), //sourceType "", //comment viewTypeDAO.getIdOfType(parent.getItem().getTenant(), ViewTypeDAO.VIEWTYPE_ADMIN), true); //timeStamp itemAssocService.insertOrUpdateItemAssoc(edge); tree.addChild(edge, parent, cluster); buildTree(tree, cluster); }
new DelegateTree<ClusterVO, ItemAssocVO<Integer,Integer>>(); tenantClusters.setRoot(root); clusters.put(tenantVO.getId(), tenantClusters);
if (tree.containsVertex(cluster)) throw new ClusterException("Cannot add cluster! Cluster already exists!"); if (tree.containsVertex(parent)) { storeCluster(cluster); ItemAssocVO<Integer,Integer> edge = true); //timeStamp itemAssocService.insertOrUpdateItemAssoc(edge); tree.addChild(edge, parent, cluster); } else { throw new ClusterException("Parent cluster could not be found!");
public void updateClusterDescription(Integer tenantId, String clusterName, String newDescription) throws ClusterException { ClusterVO cluster = loadCluster(tenantId, idMappingDAO.lookup(clusterName), itemTypeDAO.getIdOfType(tenantId, ClusterService.ITEMTYPE_CLUSTER)); DelegateTree<ClusterVO, ItemAssocVO<Integer,Integer>> tree = clusters.get(tenantId); if (tree.isRoot(cluster)) throw new ClusterException("ROOT cluster description cannot be changed!"); cluster.setDescription(newDescription); storeCluster(cluster); }
@SuppressWarnings("unchecked") @Override public boolean addEdge(E edge, Collection<? extends V> vertices) { Pair<V> pair = null; if (vertices instanceof Pair) { pair = (Pair<V>) vertices; } else { pair = new Pair<V>(vertices); } return addEdge(edge, pair.getFirst(), pair.getSecond()); }
/** * sets the root to the passed value, only if the root is * previously unset * @param root the initial tree root */ public void setRoot(V root) { addVertex(root); }
DelegateTree.<MyNode, MyLink> getFactory(), wtTransformer);
/** * Returns <code>true</code> if <code>v</code> is neither a leaf nor the * root of this tree. * * @return <code>true</code> if <code>v</code> is neither a leaf nor the * root of this tree */ public boolean isInternal(V v) { if (!delegate.containsVertex(v)) { return false; } return isLeaf(v) == false && isRoot(v) == false; }
/** * remove the passed node, and all nodes that are descendants of the passed * node. * * @param vertex * @return <code>true</code> iff the tree was modified * @see edu.uci.ics.jung.graph.Graph#removeVertex(java.lang.Object) */ @Override public boolean removeVertex(V vertex) { if (!delegate.containsVertex(vertex)) { return false; } for (V v : getChildren(vertex)) { removeVertex(v); vertex_depths.remove(v); } // recalculate height vertex_depths.remove(vertex); return delegate.removeVertex(vertex); }
/** * get the number of children of the passed parent node */ @Override public int getChildCount(V parent) { if (!delegate.containsVertex(parent)) { return 0; } return getChildren(parent).size(); }