-
Notifications
You must be signed in to change notification settings - Fork 32
Visualization with GraphVIZ DOT
AutomataLib provides a very convenient way of visualizing automata and graphs, using the well-known GraphVIZ DOT tool. Visualization here means both exporting them to a DOT file, which can be processed for rendering by external tools (such as those distributed with GraphVIZ), and also rendering and displaying them directly from your application using a Java interface. This page will guide you on how to accomplish this.
In order to use DOT for visualizing automata and graphs, the GraphVIZ software has to be installed on your machine (downloads page). Additionally, the dot
(dot.exe
on Windows) utility has to be installed in a directory contained in the PATH
environment variable.
The central interface for customizing how an automaton or graph is rendered is GraphDOTHelper
(source, javadoc) from the automata-core artifact. This interface declares four methods:
-
writePreamble()
can be used to write arbitrary GraphVIZ statements before the actual body of the graph is written (but after the openingdigraph {
). This could, for example, include additional nodes. -
writePostamble()
can be used to write arbitrary GraphVIZ statements after the actual body of the graph has been written. In this method, nodes (states) from the graph (automaton) can be referenced by a mapping providing their ID, which is not possible in thewritePreamble
method. -
getStateProperties()
allows to define GraphVIZ properties for single nodes (states), or signal to suppress rendering them by returningfalse
. For example, invokingproperties.put(SHAPE, "box")
will lead to the current node being rendered as a rectangle instead of a circle. -
getEdgeProperties()
allows to define GraphVIZ properties for single edges (transitions), or signal to suppress rendering them by returningfalse
. For example, invokingproperties.put("style", "dashed")
will lead to the respective edge being drawn using a dashed line.
The class DOT
(source, javadoc) from the automata-commons-dotutil artifact provides several methods that facilitate interaction with the external dot
program. However, at this point we just want to focus on the most convenient way: by creating a special Writer
object (note that Writer
as a subclass of Appendable
can be passed to the GraphDOT.write(...)
methods mentioned above).
This Writer
can be used to write arbitrary GraphVIZ data to. Its close()
method, however, triggers the actual DOT rendering to be performed and opens a window displaying the rendering result (or a message box, if there were errors during rendering).
The Writer
can be instantiated by a call to DOT.createDotWriter(boolean modal)
. The mandatory boolean
parameter indicates whether or not the window shall be modal or not. In the former case, the call to close()
will block, and execution (in the calling thread) will not proceed until the window is closed.
The following listing shows how a simple plottable graph can be visualized in a modal window.
DOTPlottableGraph<?,?> graph = ...;
Writer w = DOT.createDOTWriter(true); // true indicates that the dialog is modal
GraphDOT.write(graph, w); // Requires handling of IOException!
w.close(); // Causes the visualization window to appear. Note that since the dialog
// was set to be modal, this call will block until the window is closed.