tags:

views:

408

answers:

4

I'd like some (freely reusable) C++ classes to represent states, events and Finite State Machines (FSM).

Maybe each even has a unique ID and string name which can be used when debugging. Maybe each state has the same plus an OnEnter() and OnExit() function, etc and maybe each FSM has a collection of states, each of which accepts various events.

Before I code my own, is there anything available off the shelf? I was thinking about Boost, but they are a little cagey about using it in embedded systems (see http://www.boost.org/doc/libs/1_35_0/libs/statechart/doc/faq.html)

Thanks for any advice, I am ending towards coding my own, as that will meet my needs 100% but it would be silly not to look at anything that's out there for free.

+3  A: 

The only C++ framework I know for modelling finite state machines is part of the Qt Toolkit (see the QStateMachine API for the entry point into the framework).

However, this is possibly overkill for you, so I'd probably model my own set of classes as well, like:

struct Event {
    int id;
    std::string name;
};

struct State;

struct Transition {
    virtual void execute() = 0;
    virtual State *newState() = 0;
};

struct State {
    virtual void onEnter() = 0;
    virtual void onExit() = 0;
    virtual Transition *getTransition( const Event &e );
};

Then you could have an initial State object and from there on call getTransition() (and check the returned transition for which state is the new state) repeatedly.

Frerich Raabe
Mawg
Yes, indeed. A FSM is has a set of states, and each state knows about the transition to perform for a given input event. So each state maintains a 'event -> transition' mapping. However, note that 'State' objects can possibly be static objects, and using functions like 'newState()' is not necessary. Chances are that you can model everything using a static const array of objects.
Frerich Raabe
Virtual methods 'as events' is probably easisest solution to start with. However there is gotcha if you happen to have a lots of different events. Virtual tables are than really huuuge and you pay hunderds of bytes for each new state.
MaR
struct Event { int id; std::string name;};I used an enum for my events and another for my states, which is why I ended up drowning in template classes. Still, enum is better
Mawg
+5  A: 

Free FSM libraries for C/C++ I'm aware of are:

  • RWTH FSA Toolkit
  • The ASTL extension of the STL (GPL).
  • WFST itself is licensed under the GPL but has external dependencies like the AT&T FSM library.

The AT&T FSM libs are excellent but I'm not sure about what the license allows. Same goes for Grail.

If all you need are classes which represent states, though, I also think it's best to implement your own.

ferdystschenko
Jack Nock
+2  A: 

Try SMC - the State Machine Compiler. This lets you define a state machine using their own definition language, then generate the code for a wide range of programming languages, including C++, C#, Java, Ruby, Python, Scala, VB.Net and several others.

Dave Kirby
+5  A: 

You could try Miro Samek's State Machines. http://www.state-machine.com/index.php

Finite state machine, event driven, C/C++, designed for embedded systems, and ported to many microprocessors.

simon
+1 for 'designed for embedded systems'. Samek also has a book, "Practical UML Statecharts in C/C++", detailing the design of the QP state machine framework. Last week some colleagues of mine were quite surprised when they saw I had a book that was devoted entirely to state machines.
Michael Burr
Yes, I did look into this quite some time ago. It seems to have a learning curve, although it would probably be worth it. I need more time !!!
Mawg
Yes, this is the best one. It also supports HSMs and the code is very readable. I think the learning curve is ok, as most of it will be related to 'design using statemachines'. Furthermore, I found his book(s) to be excellent. Starting with the book and then diving into the code is my advise. Note that it is dual license (GPL and commercial royaltyfree).
Adriaan
+1 for Samek's QP. Another thing that's nice about the QP is the support for HSMs as well. Having used QP, I can say the support is also excellent. QP is free to try/evaluate, and the license is very affordable for anything beyond a school/hobby project.
Dan
The license is also affordable for school/hobby, since presumably the GPL license option (as Adriaan mentioned) wouldn't be a problem.
Michael Burr