Chapter 3. Using yFiles FLEX Client Layout Extension Functionality

Table of Contents

Connecting Graph Models
Class CopiedLayoutIGraph
Class YGraphAdapter
Layout Morphing
Class LayoutExecutor

This chapter describes how automatic layout and graph analysis algorithms from the yFiles FLEX Client Layout Extension library component can be used with the IGraph-based graph structure of yFiles FLEX.

Connecting Graph Models

The yFiles FLEX graph model is constituted by interface IGraph, its actual implementation, and interfaces INode, IEdge, etc. from the com.yworks.graph.model package. Together, these types present a model that differs from that used with the yFiles Basic and yFiles Layout library components.

In order to use the algorithms provided by yFiles FLEX Client Layout Extension, a proper mapping of the graph models and any necessary data associated with its elements needs to be established. Using the adapter mechanisms provided by classes CopiedLayoutIGraph and YGraphAdapter, this mapping is done transparently.

Class CopiedLayoutIGraph

Adapter class CopiedLayoutIGraph bridges the gap between the IGraph-based yFiles FLEX graph model and the graph model that is used with the yFiles Basic and yFiles Layout library components. It implements interfaces GraphInterface and GraphLayout which together provide the functionality that is needed for the yFiles automatic layout and network analysis algorithms.

CopiedLayoutIGraph is also responsible for providing access to any additional information associated with an IGraph and its elements in a way that is standard with the graph model that the yFiles algorithms use. Specifically, this means that any IMapper instances which are registered with the IGraph's mapper registry are appropriately wrapped using data providers. Among other things, this also covers any grouping-related data that is present with grouped graphs.

The following convenience method in CopiedLayoutIGraph can be used to invoke a layout algorithm:

static function applyLayout(graph:IGraph, layouter:Layouter):void
Description Convenience method in CopiedLayoutIGraph.

In fact, invoking a layout algorithm can be as simple as this one-liner:

Example 3.1. Invoking a layout algorithm

// 'graph' is of type com.yworks.graph.model.IGraph.

CopiedLayoutIGraph.applyLayout(graph, new IncrementalHierarchicLayouter());

Using the services of class CopiedLayoutIGraph enables proper invocation of any implementations of interface Layouter via the doLayout() method. This also includes all yFiles major layout algorithms, which are descendants of abstract class CanonicMultiStageLayouter.

Basically, the following code gets executed when using the aforementioned convenience method to invoke a layout algorithm:

Example 3.2. Invoking a Layouter implementation

function invokeLayouter(layouter:Layouter, graph:IGraph):void {
  var adapter:LayoutGraphAdapter = new LayoutGraphAdapter(graph);
  var copy:CopiedLayoutIGraph = new CopiedLayoutIGraph(adapter);
  layouter.doLayout(copy);
  copy.commitLayoutToOriginalGraph();
}

Note

Committing back the results of the layout calculation to the original graph effectively prevents layout morphing, the effect of smoothly transforming node and bend positions from their original positions to the newly calculated ones. Enabling layout morphing is described in the section called “Layout Morphing”.

Class YGraphAdapter

Connecting graph models especially in the context of calling analysis algorithms from the yFiles Basic library component is conveniently supported by adapter class YGraphAdapter. Similar to class CopiedLayoutIGraph it creates a copy of the IGraph given at creation time. The copy is of type Graph and can be readily used in conjunction with any algorithms from the com.yworks.yfiles.base package. It is accessible via the yGraph property of the adapter class.

Example 3.3. Invoking a graph analysis algorithm

// 'graph' is of type com.yworks.graph.model.IGraph.

var graphAdapter:YGraphAdapter = new YGraphAdapter(graph);
// Class GraphChecker is from the com.yworks.yfiles.base namespace.
var isConnected:Boolean = GraphChecker.isConnected(graphAdapter.yGraph);

Mappings between the graph elements from the IGraph and the copied graph elements in the Graph are available via convenience methods.

YGraphAdapter provides a number of methods that facilitate converting IMapper or Iterator instances both to and from appropriate data type instances which are typically used as parameter types with graph analysis algorithms. Note that conversion of IMapper instances is done by wrapping them.

Example 3.4, “Converting data type instances when invoking a graph analysis algorithm” presents an invocation of an analysis algorithm where data type instances are converted to provide the appropriate input type and to get the algorithm's results from the output type.

Example 3.4. Converting data type instances when invoking a graph analysis algorithm

// 'graph' is of type com.yworks.graph.model.IGraph.
// 'someNodes' is of type com.yworks.support.Iterator.

var graphAdapter:YGraphAdapter = new YGraphAdapter(graph);

// Class GraphConnectivity is from the com.yworks.yfiles.algo package.
var inputNL:NodeList = graphAdapter.createNodeList(someNodes);
var outputNL:NodeList =
  GraphConnectivity.getSuccessors(graphAdapter.yGraph, inputNL, 2);
var successors:Iterator = graphAdapter.createNodeIterator(outputNL);