## vendredi 11 avril 2014

### State machine diagrams with Graphviz

Once in a while, I need to draw a simple state machine diagram. These are a quick way to show in a visual way how a system works.

While these can be drawn with general drawing tools, or even with more dedicated tools, I usually prefer the textual way. Describing a drawing through some design language with an acceptable learning curve and letting some application do the drawing is IMO a better approach: editing the graph afterwards is just a matter of editing the source file.

Okay, so what tool ? Some are... funny (and I mean it!), but are not really usable for anything more than a small graph, or anything that needs to be edited many times.

No, this post is about Graphviz and it's associate set of tools. It does truely have some oddities buts its the best around.
Among its oddities, the default size units are in distance units. For an image, yes, no pixels here. Wait, that's not all ! The default units are inches. Inches !
I suppose this is for historical reasons and it seems that there is no option to change this. After thinking about it, once you go with distance, then, as metrication of image density is still not used, you might as well stay with inches.

Another thing, don't expect to be able to define precisely the position of nodes and edges: these are done by a placement algorithm, and adjusting this is not easy although some commands should help.

Nevertheless, say you want to describe some simple state machine. Just a light bulb connected to a switch.

#### Elementary graph

The associate state machine can be described by the following text file:
digraph g{
rankdir="LR";
edge[splines="curved"]
ON -> OFF;
OFF -> ON;
}


Assuming you have Graphviz correctly installed, the following shell command will generate the image:

dot -Tpng:cairo myfile.dot >myfile.png

#### Transitions

Ok, now lets add the transitions (the switch action). Lets call it "sw": if sw=1, the light bulb will be on, if 0, it will be off.

Ah. Here some problem appear. In the field of electrical engineering, the transitions between the states are frequently based on some boolean variable. Notation for the complement operation seems to be country-dependent. In France, this is usually expressed by a bar over the expression ("sw barre"), in Latex math-syntax, it will be $\bar{sw}$.

So, how can we manage this issue ?

First (and easiest), forget about the "bar" thing, and just go for plain text:

digraph g{
rankdir="LR";
edge[splines="curved"]
ON -> OFF [label="sw=0"];
OFF -> ON [label="sw=1"];
}


This is not very satisfying, it clutters the diagram.

Second solution: use Unicode. Graphviz natively supports it, and Unicode provides some special character that is supposed to handle this situation. So just enter:
digraph g{
rankdir="LR";
edge[splines="curved"]
OFF -> ON [label="sw"];
ON -> OFF [label="s̅w̅"];
}
(sorry, seems that the current hosting of this blog does not correctly display this, this is why the bar isn't exactly over the two letters).

In GTK+ based apps (Gedit, for instance), Unicode can be entered by hitting CTRL+SHIFT+U, then entering the desired character code (here '+0305') after each letter. Here, you need to do this manually after each letter of the label .

Unfortunately, the final rendering depends on the font used by the layout engine. It seems that the default png output of Graphviz does not use the Cairo library. Or if it does, it does not provide any control on the used font, so the final result looks quite ugly:

#### Direct insertion into Latex source file

If the graph image is intended to end up in a Latex source file, then check out the Graphviz package. It allows you to insert directly the graph command into the main Latex document. Unfortunately, this does not mean you suddenly have all the associated formatting power: this package only calls the 'dot' command himself, the only benefit is that you don't have to do it yourself and then import the image file into the Latex document. So for the issue detailed up here, it is of no help.

Another tool, dot2tex, has been specifically designed to have it all: direct editing of dot file inside Latex file and Latex formatting for labels and edges. Basically, it converts the dot file into PSTricks and/or PGF/TikZ format using some Python magic, then process it as regular Latex code.
Unfortunately, installation on my machine seems to suffer from some obscure Python bug, so I can't tell more at present! I hope to be able to try this soon.

Edit 2015/05: for more precise positioning of your nodes and vertices and better rendering, you'd better go off with a Latex-based solution. Tikz seems to be the easiest, see for example this sample.
For my own record, here are some relevant links: