How to invoke a yFiles layout algorithm
Applies to: yFiles 2.4, yFiles 2.3, yFiles 2.2, yFiles 2.1, yFiles 2.0, yFiles.NET 2.4, yFiles.NET 2.3, yFiles.NET 2.2 print article email article

Type: Tips & Tricks

Categories this article belongs to:
yFiles for Java > yFiles Layout > Automatic Graph Layout > Concepts

This article explains why it is almost always a good idea to make use BufferedLayouter when calculating a new graph layout.

All yFiles layout algorithms implement the interface Layouter. The simplest, but often not the best method, to invoke a layout algorithm is to directly use its main layout method doLayout(LayoutGraph). This will assign a new layout to the given graph. One should note when using this method that calculation of the layout is performed directly on the given graph. This has some drawbacks that should be pointed out:
  • Depending on the implementation of the given graph, it might be that the algorithm performs badly in terms of memory consumption and execution time, since the implementation of crucial graph methods like createNode or getPathList might not be optimized for layout tasks. For instance invoking a layout algorithm directly on an instance of Graph2D is often not a good idea, since its nodes and edges are quite heavy.
  • Some algorithms need to temporarily add/remove nodes or edges to/from the given graph. Registered graph listener will be notified about such temporary structural changes. Subsequent listener action might be unnecessary or harmful depending on the application type.
  • Even though it is guaranteed that a layout algorithm will not change the set of nodes and edges contained in the given graph, it is not unusual for a layout algorithm to modify the ordering of nodes and/or edges in the graph. Since the ordering gets modified, one cannot rely on the index()index feature of nodes and edges.
  • In rare cases it might happen that a layout algorithm will crash during a calculation due to a bug. It will then immediatelly return with an exception leaving the input graph in an intermediate, often broken, state. No recovery will be possible of the original input graph.
  • A call to doLayout will not return the calculated coordinates but assign them right away to the given graph. Therefore it is not possible to assign the coordiantes externally in a different way (e.g. in an animated fashion using coordinate interpolation).
Fortunatelly, there is a simple solution to all of the problems mentioned above. Simply wrap a layout algorithm by the layout stage BufferedLayouter:
new BufferedLayouter(layouter).doLayout(graph)
BufferedLayouter will invoke its core layout algorithm with a layout-optimized copy of the input graph. All layout calculation will be performed on this optimized copy. After the core layout algorithm has finisied its job BufferedLayouter copies the calculated coordinates to the original input graph or returns a separate GraphLayout object that encapsulates the calculated layout.
GraphLayout gl = new BufferedLayouter(layouter).calcLayout(graph)
In the latter case there will be no modification at all of the input graph. In the former case the only modification will be the new assignment of layout information.
Warning
Direct invokation of a layout algorithms has serious drawbacks. It is almost always a good idea to make use BufferedLayouter when calculating a new graph layout.

Keywords: layout - layout algorithm - layouter - bufferedlayouter - graphlayout - graph copy - node order - edge order - buffering - buffer

Provide feedback:
How useful was this article?    less 1 2 3 4 5 more
Email address (optional):
COPYRIGHT © 2008 yWorks · ALL RIGHTS RESERVED imprint | top | home