/** * Wraps all {@link NodeLayout} objects into an internal presentation {@link NodeWrapper} and * inserts dummy wrappers into the layers between an object and their predecessing nodes if * necessary. Finally, all nodes are chained over immediate adjacent layers down to their * predecessors. This is necessary to apply the final step of the Sugiyama algorithm to refine the * node position within a layer. * * @param list : List of all {@link NodeLayout} objects within the current layer */ private void addLayer(List<NodeLayout> list) { ArrayList<NodeWrapper> layer = new ArrayList<NodeWrapper>(list.size()); for (NodeLayout node : list) { // wrap each NodeLayout with the internal data object and provide a // corresponding mapping NodeWrapper nw = new NodeWrapper(node, layers.size()); map.put(node, nw); layer.add(nw); // insert dummy nodes if the adjacent layer does not contain the // predecessor for (NodeLayout node_predecessor : node.getPredecessingNodes()) { // for // all // predecessors NodeWrapper nw_predecessor = map.get(node_predecessor); if (nw_predecessor != null) { for (int level = nw_predecessor.layer + 1; level < nw.layer; level++) { // add "virtual" wrappers (dummies) to the layers in // between // virtual wrappers are in fact parts of a double linked // list NodeWrapper nw_dummy = new NodeWrapper(level); nw_dummy.addPredecessor(nw_predecessor); nw_predecessor.addSuccessor(nw_dummy); nw_predecessor = nw_dummy; layers.get(level).add(nw_dummy); } nw.addPredecessor(nw_predecessor); nw_predecessor.addSuccessor(nw); } } } layers.add(layer); updateIndex(layer); }
public List<List<NodeWrapper>> calculateLayers( List<NodeLayout> nodes, Map<NodeLayout, Integer> assignedNodes) { // TODO Auto-generated method stub map.clear(); List<NodeLayout> predecessors = findRoots(nodes); nodes.removeAll(predecessors); // nodes now contains only nodes that are // no roots addLayer(predecessors); for (int level = 1; nodes.isEmpty() == false; level++) { if (level > MAX_LAYERS) throw new RuntimeException( "Graphical tree exceeds maximum depth of " + MAX_LAYERS + "! (Graph not directed? Cycles?)"); List<NodeLayout> layer = new ArrayList<NodeLayout>(); for (NodeLayout item : nodes) { if (predecessors.containsAll(Arrays.asList(item.getPredecessingNodes()))) layer.add(item); } if (layer.size() == 0) layer.add(nodes.get(0)); nodes.removeAll(layer); predecessors.addAll(layer); addLayer(layer); } return layers; }
private static List<NodeLayout> findRoots(List<NodeLayout> list) { List<NodeLayout> roots = new ArrayList<NodeLayout>(); for (NodeLayout iter : list) { // no predecessors means: this is a root, // add it to list if (iter.getPredecessingNodes().length == 0) roots.add(iter); } return (roots); }