Class GraphRoundtripSupport

Similar to class RoundtripHandler in the yFiles FLEX client API, the yFiles FLEX server API provides the class GraphRoundtripSupport that makes it easy to deserialize GraphML data that was sent from the client and to send a serialized graph instance back to the client.

GraphRoundtripSupport offers the following benefits:

Reading a Graph with GraphRoundtripSupport

There are three overloaded methods that can be used for reading a graph into an IGraph instance:

public virtual void ReadGraph(Stream, IGraph)
Description Read a Graph from a GraphML Stream
public virtual void ReadGraph(StringReader, IGraph)
Description Read a Graph from a StringReader
public virtual bool ReadGraph(HttpRequest, IGraph)
Description Read a Graph from a HTTP Request using the GraphRoundtripSupport.ParamGraph form parameter.

If the graph is read directly from a HTTP request, the ParamGraph parameter is used to retrieve the GraphML string from the request.

All graph reading methods will use CreateHandler to retrieve a preconfigured GraphML I/O handler.

Sending a Graph with GraphRoundtripSupport

There are two overloaded methods that can be used for serializing a graph using the roundtrip support:

public virtual void SendGraph(IGraph graph, Stream stream)
Description Send a Graph to a stream.
public virtual void SendGraph(IGraph graph, HttpResponse response)
Description Write a Graph to a HTTP response.

Folding support

Folding, i.e. expanding and collapsing group nodes, is an important means in breaking large graphs into smaller units. yFiles FLEX supports folding at the client as well as at the server side. The way folded graphs are sent is handled by the GraphRoundtripSupport.

Generally, there are two ways to send a folded graph to the client:

  • Sending the folder nodes as folders together with their nested graphs (the folder contents). This allows to open and close the folders on the client side without contacting the server again. On the other hand, transferring the nested graphs can increase the size of the transferred GraphML significantly, which affects roundtrip time and network traffic.

    This mode can be enabled by setting LocalViewMode on GraphRoundtripSupport to false (the default).

    Note that folding has to be enabled on the client.

  • Sending the closed folder nodes as "normal" nodes. With this option opening a folder requires a new roundtrip to the server. On the other hand, the transferred GraphML will be kept small which results in shorter roundtrip times and less network traffic.

    This mode can be enabled by setting LocalViewMode on GraphRoundtripSupport to true.

Handling IDs with GraphRoundtripSupport

Proper IDs are needed for client-server communication so the client graph can be incrementally updated and the server knows which graph items to act upon. If CreateRoundtripGraph is used for creating graph instances on the server, the roundtrip support registers IMappers for node and edge IDs with the mapper registry of the graph. The corresponding mappers can be obtained from the mapper registry using the mapper keys Node2IdMapperKey and Edge2IdMapperKey. More information about handling IDs can be found in a knowledge base article about this topic.

Adding Custom Attributes

Custom attributes associated with graph items can be easily added to the (de)serialization mechanism using the roundtrip support:

public virtual void AddMapper<K, V>(object tag,
                                       string name) where K : class, IModelItem
Description Add an attribute with the given name that can be queried using the given tag. scope type and key type are derived from the given type parameters.
public virtual void AddMapper<K, V>(object tag,
                                        string name,
                                        KeyScope scopeType,
                                        KeyType keyType)
Description Add an attribute with the given name that can be queried using the given tag. The attribute will be added to the I/O handler for the given scope type and key type. The type parameters are used for the corresponding IMapper that is created and registered with the graph's IMapperRegistry.

Example 8.1. Adding custom graph item data

// Add an attribute mapper that contains string values for nodes. 
roundtripSupport.AddMapper<INode,string>("myNodeData", "myNodeData");

// create the IGraph instance that is used for roundtripping
IGraph graph = roundtripSupport.CreateRoundtripGraph();

// (populate the graph)

// retrieve the graph's mapper registry
IMapperRegistry registry = 
  graph.Lookup(typeof (IMapperRegistry)) as IMapperRegistry;
if (registry != null) {
  // Retrieve the IMapper from the graph's mapper registry using the tag
  // defined above
  IMapper nodeDataMapper = registry.GetMapper<INode, string>("myNodeData");

  // set the node data for each node
  int i = 0;
  foreach (INode node in graph.Nodes) {
    nodeDataMapper.SetValue(node, "node " + (i++));
  }
}

// send the graph to the client.
// the custom node data will be serialized as a custom GraphML attribute.
roundtripSupport.sendGraph(graph, httpResponse);

Alternatively, all attributes in the GraphML can automatically be read by enabling AutoReadMapperData. Mappers which are read using the auto read feature will also be written automatically if AutoWriteMapperData is set to true.

Adding custom graph item attributes for client-server roundtrips is discussed in detail in the corresponding knowledge base article.

Customizing (De)Serialization

GraphRoundtripSupport's send and read methods use a preconfigured GraphMLIOHandler that is created using the protected method CreateHandler. For additional customization of the (de)serialization process, this I/O handler can be customized by overriding the factory method. The default configuration of the I/O handler can also be replaced by a custom configuration by overriding ConfigureHandler.

protected virtual GraphMLIOHandler CreateHandler()
Description Create a preconfigured GraphMLIOHandler.
protected virtual void ConfigureHandler(GraphMLIOHandler handler)
Description Configure the I/O handler.

Sending Error Messages

The roundtrip support class can also be used to send XML formatted error messages to the client:

public virtual void SendError(Exception ex, HttpContext context)
Description Send an Exception's message to the client.
public virtual void SendError(string message, HttpContext context)
Description Send a custom error message to the client.

The XML data sent to the client can be handled on the client as described in the section called “Handling Custom Errors” and is formatted as follows:

<?xml version="1.0" encoding="UTF-8"?>
  <response>
    <errors>
      <error>
        error message 
      <error>
    </errors>
  </response>