Simple recursion: applying the same layout to all subgraphs

You can apply the same layout where both the following conditions hold:
  • The same layout algorithm needs to be applied to the topmost graph and all its subgraphs.
  • The settings of the layout algorithm (that is, the layout parameters) need to be the same for the topmost graph as for all the subgraphs.
The following figure shows an example where a Tree Layout is applied to the topmost graph as well as to all its subgraphs. Moreover, the settings of the Tree Layout algorithm are the same for all the graphs: the application does not need, for example, one flow direction in the topmost graph and a different one in the subgraphs.
Example
of a recursive layout of a nested graph showing on the left, Part
1 containing Network 1 with two subgraphs, Network 1.1 and 1.2, and
on the right, Part 2 containing Network 2 with two subgraphs, Network
2.1 and 2.2. Read Part 1 before Part 2. Network 1 contains one node
at the first level, three nodes at the second level, and two nodes
at the third level. The nodes on the second and third levels are laid
out horizontally. Node 1 links to all nodes on the second level. The
middle node on the second level links to both nodes on the third level.
Network 2 has the same layout as Network 1. Network 1.1 contains one
node at the first level, two nodes at the second level, and three
nodes at the third level. The nodes on the second and third levels
are laid out horizontally. Node 1 links to all nodes on the second
level. The node on the right of the second level links to all the
nodes on the third level. Network 2.1 has the same layout as Network
1.1. Network 1.2 contains one node at the first level and three nodes
at the second level. The nodes on the second level are laid out horizontally.
Node 1 links to all nodes on the second level. Network 2.2 has the
same layout as Network 1.2.
Example of a recursive layout of a nested graph
Obtaining such recursive layouts is easy. You must specify the graph layout on the top-level Diagram or Graph instance and call the performGraphLayout method. The layout is done recursively, because the recursive argument of the method is true by default.

Internal mechanism

The internal mechanism is based on the principle that a layout instance is used for only one graph and is not reused for its subgraphs. Therefore, the Tree Layout instance is automatically “cloned” using the ibm_ilog.graphlayout.GraphLayout.copy() method.
The graph layout is applied to a graph model, and the same principle holds for the graph models: a graph model instance is used for only one graph and is not reused for subgraphs.
The graph models for the subgraphs are created by calls to the ibm_ilog.graphlayout.GraphLayout.copy() method, which in turn creates the graph model using the method ibm_ilog.graphlayout.GraphLayout.getGraphModel().
All these operations are done automatically, in a transparent way. All you have to do is to call the method performGraphLayout with the recursive argument set to true or omitted.

Dojo code example

The following code example shows how to apply a single layout algorithm to a nested graph:
var diagram = dijit.byId("diagram");
var treeLayout = new ibm_ilog.graphlayout.tree.TreeLayout();
diagram.attr("nodeLayout", treeLayout);
diagram.performGraphLayout(true);