HomeHome ArchiveArchive

Generating state machines with Dia

Dia is a nice cross platform application for diagram drawing. It can be scripted via Python, which opens the possibility to generate code from Dia diagrams.

Below we’ll create a python plugin that generates C code from UML state machine diagrams. Doing the same for other languages should be trivial.

The first thing you need is Unai Estébanez Sevilla’s nice finite state machine code generator. The version that we are using here has been abstracted in order to be able to produce code in various languages.

This python plugin implements an exporter to C code. To use it, you need to put it, along with the base exporter into your local plugins directory under \~/.dia/python.

Let’s have a quick look at the code.

First we import the dia python module and the exporter base functionality:

import dia
import uml_stm_export

Then we create our C exporter class that inherits from the generic exporter:

class CDiagramRenderer(uml_stm_export.SimpleSTM):

Next we define how the beginning of our generated code file should look like. That could include general infrastructure independent of the state machine diagram at hand. In our case, we want to encapsulate the generated state machine code within a function:

CODE_PREAMBLE="void config_stm(STM_t* stm) {"

We also define the postamble to close the function. After that come generic functions that implement the class constructor **init**(self) and functions responsible for calling the dia object parser begin\_render(self,data,filename).

Now we define our output generator end\_render(self). We first traverse dia’s objects in order to find the state machine’s initial state:

for transition in self.transitions:
   if(transition.source == "INITIAL_STATE"):

The initial state state gets a special treatment: we have a special function call generated for it:

f.write("    add_initial_state( stm, %s, %s );\n" %
    (initial_state.name, initial_state.doaction))

Next we traverse all states and output code that will create them, along with functions to be called within that state to decide on where to transition next:

for key in self.states.keys():
    f.write("    add_state( stm, %s, %s );\n"
        % (state.name, state.doaction))

And finally we output all the transitions between states:

for transition in self.transitions:
    f.write("    add_transition( stm, %s, %s, %s );\n" %
        (transition.source, transition.trigger, transition.target))

and that’s nearly it. At the end of our generator we make sure to register it with dia:

dia.register_export("State Machine Cstma Dump", "c", CDiagramRenderer())

Done! Simple, isn’t it?

Finally please permit me to thank all the people that created such a powerful tool free for us to use:

  • Unai Estébanez Sevilla for the original STM generator
  • Steffen Macke and Hans Breuer, Dia’s current busy maintaners
  • Alexander Larsson, Dia’s original author
  • all the other contributors to Dia and free software
  • Panter for inviting me to their fabulous work week in Greece where most of the hacking on the generator was done and Combitool who supported this work by needing a state machine generator in their current project.

PS: Unai’s original text generator is now also “just” a “simple” addon

Automatically restarting services after upgrades on Debian and Ubuntu

There are various tools to automatically keep a Debian/Ubuntu system security wise up to date, among others the unattended-upgrades package.

Also, there’s the checkrestart script from the debian-goodies package, that scans all the open files on a system and tries to determine to what service they belong and how that service might be restarted.

The last piece that’d tie all those scripts together and would automatically restart all services that are using stale libraries or files was missing.

With the help of Michal Fiala there however is now the restart-services script, that does just that.

The script has not seen much real world usage and as such should be regarded as experimental (f.ex. by restarting /etc/init.d/screen it will as of the time of writing terminate existing screen sessions).

The script currently lives on Github. If you encounter any problem with the script then we’ll very much wellcome a patch that fixes it…

Tomáš Pospíšek

Update 18.6.2012: The most recent checkrestart (from debian-goodies 0.61) now excludes screen from beeing listed among the services to be restarted.

QGIS - the FOSSGIS week

A great conference begins, with about 400 people attending presentations and workshops over three days.

  • Tuesday, 2012-03-20 14:42: Changeset c27c89045c: “Add WFS support for QGIS server. Provided by René-Luc D’Hont”

Wow.

  • Tuesday, 2012-03-20 14.48: QGIS 1.7.4 uploaded to DebianGIS

Ok, QGIS 1.7.4 is already a few weeks old. But current version on Debian is 1.4.0! This will be a long “new features” list for Debian users. Thanks for your work, Francesco!

GPS support, right click gesture, pinch zooming and offline editing plugin working. Just a few tickets are left for uploading it to Android market. Hopefully we find another great student for this years follow-up GSoC project!

  • Wednesday, 2012-03-21 16.05: Victor Olaya, author of SEXTANTE, announces his work on a QGIS processing framework with toolbox, graphical modeler, batch processing interface, etc.

Wow!!

  • Wednesday, 2012-03-21 18.30: QGIS and GRASS user meeting at FOSSGIS

Explaining whats going on in the QGIS code and the QGIS community. Live demonstration of raster resampling branch.

  • Wednesday, 2012-03-21 20.18: Tim announces QGIS 1.8 RC1 for April 9th.

Test it before the hackfest in Lyon!

  • Thursday, 2012-03-22 08:00: Bad news - three days in a row without any commit from jef!

What happened? Jürgen forgot to bring his power adapter to Dessau :-(

  • Thursday, 2012-03-22 13:30: FOSSGIS is over

with well attended QGIS presentations and workshops.

Jürgen is back home again. Phew!

  • Friday, 2012-03-23 10:26: Changeset 585e58179d: Nathan Woodrow merges native MS SQL provider, written by Tamas Szekeres, into master.

Good news for MS SQL users, but also for Tim: “Finally a way to stop having to use PostGIS all the time…”

What a week for QGIS! Looking forward to more news from the QGIS Hackfest in Lyon.

FOSSGIS Konferenz 2012 in Dessau

Die FOSSGIS und deutschsprachige Open Street Map Konferenz 2012 – die grösste deutschsprachige Anwenderkonferenz für Freie Geo-Informationssysteme und freie Geodaten – findet vom 20. bis 22. März 2012 an der Hochschule Anhalt in Dessau-Rosslau statt.

Sourcepole ist mit folgenden Vorträgen und Workshops vertreten:

Da mehr als 400 Teilnehmende erwartet werden, ist eine Registrierung bis zum 16. März 2012 notwendig. Das Anmeldeformular und andere organisatorische Informationen befinden sich auf der Konferenzseite.