Back to Blog home

Introducing scc: the scxml compiler for the Qt State Machine framework

Published on Monday August 10, 2009 by admin in Qt SCXML XML | Comments

State machine compilers are not a new concept, they've been around for a very long time, and some of them are pretty good. So throwing another one to the world might seem like trying to reinvent the wheel. But this new state-machine compiler is different, because the Qt State Machine framework is different, because Qt is different: QStateMachine and signals/slots allow the generated code to live separately from the rest of the application, and thus be a natural part of the development worklfow ~ some other statechart compilers, though not all of them, generate stubs that you have to manually edit, and then generating again would override your changes...

Since the Qt State Machine framework was built with SCXML in mind, this wasn't a difficult job - it was mainly figuring out how some concepts in SCXML would work with Qt. Some of them translate pretty nicely - SCXML events translate to Qt signals, SCXML data elements translate to Qt properties and the rest translates to Qt states/transitions.

I was actually thinking of hacking on this for a while, as some people requested it, but only got the time for it when I needed to write some project using Qt OAUTH (nice library, by the way), and having to maintain a large QStateMachine instantiation code for incorporating it into my application business logic was a great opportunity.

How it works
The Qt statechart compiler works very similarly to uic from Qt Designer. It takes an SCXML file, and using the XSLT feature in xmlpatterns, translates it into a header file that can be used in your application. The generated header file is a simple subclass of QStateMachine, with signals to represent SCXML events, properties to represent SCXML data elements, and a setupStateMachine() function to initialize. More can be found at the Statechart compiler documentation.

Since xmlpatterns is used for the generation template, the code for scc is pretty maintainable. Though the tools relies on the xmlpatterns library, the generated code doesn't (it only relies on QtCore with the state machine framework).

Adding SCXML statecharts to your project, after installing scc, is very easy, and goes something like:


# myproject.pro
CONFIG += scc
STATECHARTS += foobar.scxml

and then


//myfile.cpp

#include "sc_foobar.h"

MyFunction()
{
...
SMClass_foobar stateMachine;
SMClass_foobar.setupStateMachine();
SMClass_foobar.start();
...
}

MVC applications
The mvclogin example, provided with scc, is an example of the advantages of decoupling business logic from models and engines. In this example the same login flow can be used for different backends, in this case a backend that log ins to gmail, and a "dummy" fallback that is used when SSL is not supported.

Where is it
It's at http://qt.gitorious.org/qt-labs/scxml, the scc directory. Some the documentation is also available. Please check the examples, and reply for any question :)

Subscribe to Our Blog

Stay up to date with the latest marketing, sales and service tips and news.