Handling Node and Edge IDs
Tips & TricksSummary
Description
GraphML IDs
The IDs for graph elements according to the GraphML definition (<node id="n0"/>
) are used internally only. There is no way to obtain these IDs outside the writer or parser.
The IDs stored in the node-2-id and edge-2-id attribute mapper
The IDs for nodes and edges which are used during roundtrip are stored in two mappers which are always registered with the graph's mapper registry at Flex side. At Java and .NET side, these mappers are registered by default when the graph is created by GraphRoundtripSupport
's createRoundtripGraph
or CreateRoundtripGraph
method in Java or .NET, respectively.
At Flex side, the mappers can be retrieved as follows:
var node2idMap:IMapper = graph.mapperRegistry.getMapper(DefaultGraph.MAPPER_KEY_NODE_2_ID);
var edge2idMap:IMapper = graph.mapperRegistry.getMapper(DefaultGraph.MAPPER_KEY_EDGE_2_ID);
At .NET side, the mappers can be retrieved similarly:
// create the graph using GraphRoundtripSupport
IGraph graph = support.CreateRoundtripGraph();
IMapperRegistry reg = graph.Lookup(typeof(IMapperRegistry)) as IMapperRegistry;
// retrieve the mapper
IMapper<INode, String> node2idMap = reg.GetMapper<INode, String>(GraphRoundtripSupport.Node2IdMapperKey);
IMapper<IEdge, String> edge2idMap = reg.GetMapper<IEdge, String>(GraphRoundtripSupport.Edge2IdMapperKey);
At Java side, the mappers are registered as DataProviders. It is safe to cast them to DataMap to have read/write access.
// create the graph using GraphRoundtripSupport
GraphRoundtripSupport grs = new GraphRoundtripSupport();
LayoutGraph graph = grs.createRoundtripGraph();
// retrieve the maps
DataMap node2IdMap = (DataMap)graph.getDataProvider(GraphRoundtripSupport.NODE_2_ID_DPKEY);
DataMap edge2IdMap = (DataMap)graph.getDataProvider(GraphRoundtripSupport.EDGE_2_ID_DPKEY);
In GraphML, the mappers are sent as attribute mappers with the following keys:
<key id="d8" for="node" attr.name="node-2-id" attr.type="string"/>
<key id="d16" for="edge" attr.name="edge-2-id" attr.type="string"/>
The IDs are unique for each element and thus can be used to identify nodes at server and client. If no value is mapped explicitly, the IDs will be auto-generated upon the first read access. As the mapper implementations offer read-write-access it is possible to use custom IDs.
How to create custom IDs
If one decides to use custom IDs, some prerequisites have to be met:
- The ID must be unique for each item, otherwise roundtripping will not work. However, it is no problem if an edge has the same ID as a node.
- The ID must be representable by a String.
- The IDs must be the same at server and client.
Troubleshooting
It is mandatory that the IDs (or to be precise: their String representations) are unique. Otherwise a roundtrip will not be correct. Most common problems with invalid IDs are missing nodes and edges or nodes and edges with wrong styles. Note that auto-creation may fail if only a subset of the graph is sent.
Collapsed or Filtered Graphs
When only a part of the graph is sent to the client it is possible that the auto generation creates an id is created which is already occupied in the filtered part of the graph. In this case it is recommended not to rely on auto creation but to use custom ids.
Creating new Graph Elements
When the graph is held on the server and a new element is created on the client, it is important that the new element will get an id which is not already occupied, especially when graph at the client is only a subset of the full graph.