Chapter 10. GraphML Extension Package

Table of Contents

GraphML
What GraphML Looks Like
GraphML Default Extension Mechanism
Customizing the GraphML Extension Mechanism
Using the yFiles GraphML Extension Package
Direct Support for Simple Data Types
General Support for Structured Data
Support for Custom Realizer Implementations
yFiles GraphML Post-processors
Tutorial Demo Code
Advanced Topics
yFiles XSLT Support for GraphML
Reading and Writing Graphs of Arbitrary Type

The yFiles GraphML extension package enables the graph visualization library yFiles to read and write graphs in GraphML and Zip-compressed GraphML file format.

GraphML

The GraphML file format results from the joint effort of the graph drawing community to define a common format for exchanging graph structure data. It uses an XML-based syntax and supports the entire range of possible graph structure constellations including, most notably, hierarchically organized graphs.

What GraphML Looks Like

An abbreviated excerpt of a GraphML file that shows the encoding of a graph is presented in Example 10.1, “Abbreviated GraphML representation”. The basic graph structure is encoded using the GraphML elements <graph>, <node>, and <edge>. Each of these elements has an XML attribute id whose value is used to uniquely identify graphs, nodes, and edges within a GraphML file. XML attributes source and target which are part of the <edge> element, reference unique node IDs to indicate both source and target node of an edge.

Example 10.1, “Abbreviated GraphML representation” also shows yFiles-specific enhancements to the GraphML file format that describe the visual representation of a graph. These enhancements are nested within the <data> element of a node or an edge.

Example 10.1. Abbreviated GraphML representation

<?xml version="1.0" encoding="UTF-8"?>
<!-- This file was written by the JAVA GraphML Library. -->
<graphml xmlns="http://graphml.graphdrawing.org/xmlns/graphml" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns/graphml 
  http://www.yworks.com/xml/schema/graphml/1.0/ygraphml.xsd" 
  xmlns:y="http://www.yworks.com/xml/graphml">
  <key id="d0" for="node" yfiles.type="nodegraphics"/>
  <key id="d1" for="edge" yfiles.type="edgegraphics"/>
  <graph id="G" edgedefault="directed">
    <node id="n0">
      <data key="d0">
        <y:ShapeNode>
          <y:Geometry x="170.5" y="-15.0" width="59.0" height="30.0"/>
          <y:Fill color="#CCCCFF" transparent="false"/>
          <y:BorderStyle type="line" width="1.0" color="#000000"/>
          <y:NodeLabel>January</y:NodeLabel>
          <y:Shape type="rectangle"/>
        </y:ShapeNode>
      </data>
    </node>
    <node id="n1"/>
    <edge id="e1" source="n1" target="n0">
      <data key="d1">
        <y:PolyLineEdge>
          <y:Path sx="0.0" sy="-15.0" tx="29.5" ty="0.0">
            <y:Point x="425.0" y="0.0"/>
          </y:Path>
          <y:LineStyle type="line" width="1.0" color="#000000"/>
          <y:Arrows source="none" target="standard"/>
          <y:EdgeLabel>Happy New Year!</y:EdgeLabel>
          <y:BendStyle smoothed="false"/>
        </y:PolyLineEdge>
      </data>
    </edge>
  </graph>
</graphml>

The GraphML file format is also described in the GraphML Primer. The definition of the XML structure can be seen from the XML schema definition files graphml-structure-1.0rc.xsd and graphml-attributes-1.0rc.xsd, respectively.

GraphML Default Extension Mechanism

The GraphML default extension mechanism allows to declare so-called GraphML attributes that can be used to conveniently store additional information in a GraphML file. In its original form, this mechanism directly supports additional data of type boolean, int, long, float, double, and String. By means of customization to the default mechanism, however, GraphML attributes can be used to store data of arbitrary complexity and type.

A GraphML attribute is declared using the <key> element which comprises a unique identifier as well as scope, name, and the domain of values (i.e., the actual type) for the attribute. Table 10.1, “XML attributes for GraphML <key> element” lists the set of XML attributes and describes their function within the declaration of a GraphML attribute. Note that in a GraphML file, <key> elements must be placed before any <graph> element.

Table 10.1. XML attributes for GraphML <key> element

XML Attribute Value Domain Description
id NMTOKEN Uniquely identifies the GraphML attribute declaration within a GraphML file. Required to enable the GraphML attribute look-up mechanism.
for one of "graph", "node", or "edge" Determines the scope of the GraphML attribute.
attr.name NMTOKEN Identifying name for the GraphML attribute that can be used by an application.
attr.type one of "boolean", "int", "long", "float", "double", or "string" Determines the domain for the values (i.e., the actual type) of the GraphML attribute.

Actual values for a GraphML attribute are defined using the <data> element which is nested within the GraphML elements <graph>, <node>, or <edge>. Required with each <data> element is XML attribute key which is used by a look-up mechanism to find the proper GraphML attribute declaration for the contents of a given <data> element. See also Table 10.2, “XML attribute for GraphML <data> element” for a brief explanation of XML attribute key.

Table 10.2. XML attribute for GraphML <data> element

XML Attribute Value Domain Description
key NMTOKEN Refers to the declaration of a GraphML attribute by matching its unique ID, i.e., the value given for the id attribute of a GraphML <key> element.

This GraphML attribute look-up mechanism is responsible for matching the value given for the key attribute of a <data> element to the unique ID defined by the id attribute of a <key> element. The mapping that is established by this match is essential for delegating the parsing of a given <data> element to the proper parser code that handles the specific contents. Example 10.2, “GraphML attribute look-up mechanism” shows the correct setup to refer to a specific GraphML attribute when defining its values using the <data> element.

Example 10.2. GraphML attribute look-up mechanism

<!-- Declaration of a GraphML attribute. -->
<key id="someValidIdentifier" ... />

<!-- Referring to a specific GraphML attribute by matching the (unique) -->
<!-- identifier of its declaration. -->
<data key="someValidIdentifier">
...
</data>

Customizing the GraphML Extension Mechanism

The GraphML default extension mechanism can be customized to enable GraphML attributes that hold arbitrarily complex data. The basic technique to enhance the extension mechanism is to define a new XML attribute with the <key> element and nest user-defined XML elements inside the <data> element.

The newly introduced XML attribute plays a crucial role when reading GraphML file format: All <key> elements available in a file are parsed early on, and their respective set of XML attributes is handed over for inspection to any registered parser. Parser logic that corresponds to a GraphML attribute declaration then signals acceptance based on the outcome of this inspection. Subsequently, when the <data> elements of the file are parsed, the GraphML attribute look-up mechanism is used to find the matching unique ID of a <key> element, which in turn selects the corresponding parser logic that handles the contents of the <data> element.

The yFiles GraphML extension package defines the additional XML attribute yfiles.type to have the GraphML parser logic branch to code that handles yFiles-specific complex data such as that relating to the visual representation of a Graph2D from a GraphML file. As listed in Table 10.3, “Valid combinations for XML attributes for and yfiles.type” there is a set of values predefined for yfiles.type, where each is valid only in conjunction with an appropriate value for XML attribute for.

Table 10.3. Valid combinations for XML attributes for and yfiles.type

Combination of Values Description
for= "node" yfiles.type= "nodegraphics" Used to declare a GraphML attribute with node scope whose values comprise structured data that encodes the positional, dimensional, and graphical attributes available with the yFiles class NodeRealizer.
for= "edge" yfiles.type= "edgegraphics" Used to declare a GraphML attribute with edge scope whose values comprise structured data that encodes the positional and graphical attributes available with the yFiles class EdgeRealizer.
for= "graph" yfiles.type= "postprocessors" Used to declare a GraphML attribute with graph scope whose values comprise structured data that describe so-called post-processors. See also the section yFiles GraphML Post-processors.
for= "edge" yfiles.type= "portconstraints" Used to declare a GraphML attribute with edge scope whose values comprise structured data that encodes a so-called port constraint, i.e., yFiles class PortConstraint, and also edge groups as defined in interface PortConstraintKeys. Note that this GraphML attribute is not enabled by default.

The declaration of the GraphML attributes that hold all data relating to the visual representation of nodes and edges is shown in Example 10.3, “yFiles-specific GraphML attributes for realizer-related data”.

Example 10.3. yFiles-specific GraphML attributes for realizer-related data

<!-- Node realizer-related data. -->
<key id="d0" for="node" yfiles.type="nodegraphics"/>

<!-- Edge realizer-related data. -->
<key id="d1" for="edge" yfiles.type="edgegraphics"/>

Any data specific to the realizer of a graph element is nested within the <data> element for the respective node or edge. Example 10.4, “Edge realizer-related data” shows the entire encoding for an edge realizer.

Example 10.4. Edge realizer-related data

<!-- Edge realizer-related data nested within the <data> element of its -->
<!-- respective edge. -->
<edge id="e0" source="n0" target="n1">
  <data key="d1">
    <y:PolyLineEdge>
      <y:Path sx="0.0" sy="0.0" tx="0.0" ty="0.0"/>
      <y:LineStyle type="line" width="1.0" color="#000000"/>
      <y:Arrows source="none" target="standard"/>
      <y:EdgeLabel x="47.0" y="-21.555956740606405" 
                   width="4.0" height="4.0" 
                   visible="true" 
                   alignment="center" 
                   fontFamily="Dialog" fontSize="12" fontStyle="plain" 
                   textColor="#000000" 
                   modelName="six_pos" modelPosition="tail" 
                   preferredPlacement="anywhere" 
                   distance="2.0" ratio="0.5">A simple edge label</y:EdgeLabel>
      <y:BendStyle smoothed="false"/>
    </y:PolyLineEdge>
  </data>
</edge>

The XML structure of the enhanced GraphML file format as defined by the yFiles GraphML extension package can be seen from the following XML schema definition files:

  • ygraphml.xsd declares additional XML elements to be nested within GraphML <data> elements
  • ygraphics.xsd defines new XML elements that encode predefined yFiles realizer implementations and also all kinds of data types to be used with these newly defined XML elements
  • yprocessors.xsd defines new XML elements that encode so-called post-processors