State machines consist of a collection of states (represented by the State class), a number of directed arcs encoding transitions between these states and finally, an initial state. RHexLib provides an abstract base class StateMachine, derived from the Module class to encapsulate implementations of the state machine formality within RHexLib.
In order to create a state machine, developers need to subclass StateMachine, and define desired states, events, arcs and the initial state in its constructor. In particular, states and events are encoded through class members subclassed from State and Event, constructed with proper arguments. Convenient macros StateObject and EventObject are provided to define these derived classes and pointers in the state machine subclass declaration.
The initial state can be chosen with the StateMachine::initialize method, which must be called from within the class constructor. Finally, events can be defined within the constructor through the use of the macro Transition which internally defines objects of type Arc to encode transitions between states.
Developers can also override all of the standard module methods. However, it is very important that the implementations provided by the StateMachine class are explicitly called to ensure proper operation of state machine transitions.
The execution of the state machine follows the usual semantics of state machines. Each state provides three methods, State::entry, State::exit and State::during. The entry and exit states are called when the state machine enters and exits a particular state, respectively. In contrast, the during method is periodically called for the current state whenever the state machine module is active. The periodicity of this depends on how the state machine subclass is added into the module manager's database.
Once the state machine module is activated, it starts from the specified initial state. All the event objects associated with outgoing arcs from the current state are then periodically checked (through Event::check) to detect their firing. If an event fires, the exit method of the current state is called and the current state is changed according to the arc definition. The entry method of the new state is also called. The during method of the new state is then periodically called until a new event is detected.
Compounds | |
class | Event |
Abstract base class for all state machine events. More... | |
class | State |
Abstract base class for all state machine states. More... | |
class | StateMachine |
Abstract base class for all state machine implementations. More... |