Chapter 8. Using yFiles for Silverlight Algorithms Functionality

Table of Contents

Connecting Graph Models
Class CopiedLayoutIGraph
Class YGraphAdapter
Layout Modules
Layout Morphing
Class LayoutExecutor

This chapter describes how automatic layout and graph analysis algorithms from the yFiles for Silverlight Algorithms library component can be used with the IGraph-based graph structure of yFiles for Silverlight Viewer.

Connecting Graph Models

The yFiles for Silverlight Viewer graph model is constituted by interface IGraph, its actual implementation, and interfaces INode, IEdge, etc. from the yWorks.yFiles.UI.Model namespace. Together, these types present a model that differs from that used with the yFiles for Silverlight Algorithms library component.

In order to use the algorithms provided by yFiles for Silverlight Algorithms, 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 for Silverlight Viewer graph model and the graph model that is used with the yFiles for Silverlight Algorithms library component. It implements interfaces IGraphInterface and IGraphLayout 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 void ApplyLayout(IGraph graph, ILayouter layouter)
Description Convenience method in CopiedLayoutIGraph.

These methods in other classes basically delegate to the aforementioned in CopiedLayoutIGraph:

void ApplyLayout(ILayouter layouter)
Description Convenience (extension) method(s) in IGraph.
void DoLayout(IGraph graph)
Description Convenience (extension) method in ILayouter.

Invoking a layout algorithm can be as simple as this one-liner that implicitly uses the services of class CopiedLayoutIGraph:

Example 8.1. Invoking a layout algorithm

// 'graph' is of type yWorks.yFiles.UI.Model.IGraph.

graph.ApplyLayout(new IncrementalHierarchicLayouter());

Using the services of class CopiedLayoutIGraph enables proper invocation of any implementations of interface ILayouter 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 either of the aforementioned convenience methods to invoke a layout algorithm:

Example 8.2. Invoking an ILayouter implementation

void InvokeLayouter(ILayouter layouter, IGraph graph) {
  CopiedLayoutIGraph copy = new CopiedLayoutIGraph(graph);
  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”.

Internally, CopiedLayoutIGraph uses class LayoutGraphAdapter to be able to apply a layout algorithm to an IGraph. LayoutGraphAdapter provides the transformation from an IGraph to a layout graph. Among other things, this also includes specific support for edge-to-edge connections, which are possible in an IGraph, but are not available in an IGraphInterface. The following property is enabled by default:

bool EdgeToEdgeConnectionsIncluded { get; set; }
Description If enabled, dummy nodes for each source port and target port of an IEdge that is connected to another IEdge in a given IGraph are inserted into the created layout graph.

Class YGraphAdapter

Connecting graph models especially in the context of calling analysis algorithms from the yFiles for Silverlight Algorithms 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 yWorks.yFiles.Algorithms namespace. It is accessible via the YGraph property of the adapter class.

Example 8.3. Invoking a graph analysis algorithm

// 'graph' is of type yWorks.yFiles.UI.Model.IGraph.

YGraphAdapter graphAdapter = new YGraphAdapter(graph);
// Class GraphChecker is from the yWorks.yFiles.Algorithms namespace.
bool isConnected = 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<K, V> or IEnumerable<T> 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<K, V> instances is done by wrapping them.

Example 8.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 8.4. Converting data type instances when invoking a graph analysis algorithm

// 'graph' is of type yWorks.yFiles.UI.Model.IGraph.
// 'someNodes' is of type System.Collections.Generic.IEnumerable<INode>.

YGraphAdapter graphAdapter = new YGraphAdapter(graph);

// Class GraphConnectivity is from the yWorks.yFiles.Algorithms namespace.
NodeList inputNL = graphAdapter.CreateNodeList(someNodes);
NodeList outputNL =
  GraphConnectivity.GetSuccessors(graphAdapter.YGraph, inputNL, 2);
IEnumerable<INode> successors = graphAdapter.CreateNodeEnumerable(outputNL);