Graphs, GXL, dot and Graphviz
By sundararajan on Dec 13, 2009
Sometimes you may want to quickly generate graphs programmatically and view/analyze those. Examples include, inheritance/type relation diagrams of an object oriented program, function call graphs and any other domain specific graphs (reporting chain of your organization chart for example). I find GXL very useful for this. GXL stands for Graph eXchange Language. It is a simple XML format to specify graphs. A simple graph stating that "JavaFX" language is related to "Java" language is as follows:
File: Test.gxl<gxl> <!-- edgemode tells this is directed or undirected graph --> <graph id="langs" edgemode="directed"> <!-- write your nodes --> <node id="java"/> <node id="javafx"/> <-- now write your edges --> <edge from="java" to="javafx"/> </graph> </gxl>
You can also add number of "attributes" to nodes and edges - like color of the edge, style of the edge and so on. For example, "red" color can be specified for an edge as follows:
<edge from="java" to="javafx"> <attr name="color"><string>red</string></attr> </edge>
Now that we have written a simple graph with two nodes and a single edge between them, we may want to view it. There are number of tools/libraries to view GXL documents -- I've used Graphviz. Graphviz displays it's own native format called ".dot". Graphviz comes with a set of command line tools. One such tool is "gxl2dot", which as you'd have guessed, can be used to convert a .gxl file to a .dot file.
gxl2dot Test.gxl > Test.dot
Once converted the .dot file can be opened in Graphviz GUI and we can export it to .pdf/.jpg/.png and so on. This way you can email the graphs to others and/or publish in your blogs/webpages easily.
The converted .pdf file for the above simple graph is here: test.pdf
I've used GXL graphs in a recent debugging tool related to JavaFX compiler. More on that later...