gilgamash
gilgamash

Reputation: 902

How to dynamically create state machine

I have no idea yet how to create an FSM using boost msm dynamically, such as reading template XML-files, for instance, that describe the machine. Any idea how to address the problem? I want to use the functor approach with boost msm 1.61.

I have made a little progress such that I can create a base class for the front end the common way:

class SMBase : public msmf::state_machine_def<SMBase>
{
 ...
};
using SMBaseBackend = msm::back::state_machine<SMBase>;

class SMDerived : public SMBase
{
 ...
};
using SMDerivedBackend = msm::back::state_machine<SMDerived>;


class SMDerived2 : public SMBase
{
 ...
};
using SMDerived2Backend = msm::back::state_machine<SMDerived2>;

However, the state machine itself is steered by the back-end, and I can see no way so far chosing the latter on runtime (for instance using a

map<int, smart_pointer<SMBaseBackend> >

).

Upvotes: 2

Views: 1697

Answers (2)

gilgamash
gilgamash

Reputation: 902

There is kind of a workaround, which works fine for setups where one needs not to choose from many state machines at runtime:

Define one encompassing parent statemachine and implement the others at submachines. Then on runtime level on can "skip into" the respective submachine by using the transition table of the parent state machine with and calling the respective process_event (respective_event()).

Works very well, only current_state just returns the parent level state now.

Upvotes: 0

Takatoshi Kondo
Takatoshi Kondo

Reputation: 3540

Boost.MSM doesn't support creating state machine structure dynamically. MSM is Meta State Machine, and Meta means compile time in this context. So all state machine structure is build at compile time. You can see why Boost.MSM adopts the approach in the following document: http://www.boost.org/doc/libs/1_61_0/libs/msm/doc/HTML/pr01.html

The second paragraph "Another state machine library? What for?" describes a disadvantage of a dynamic state machine structure creation.

You might want to know alternatives.

Boost.Statechart doen't support neither. The following documents describe the reason:

http://www.boost.org/doc/libs/1_61_0/libs/statechart/doc/rationale.html#DynamicConfigurability

According to the document, Boost.Statechart supports very limited dynamic congigration. The document said that "However, this does not mean that it's impossible to dynamically shape a machine implemented with this library. For example, guards can be used to make different transitions depending on input only available at runtime."

However, there is a state machine library that supports dynamic state machine creation. Qt's QStateMachine is it. See http://doc.qt.io/qt-5/statemachine-api.html

Upvotes: 3

Related Questions