s_automata_cpp is a C++ template class that ease development of state/transition automatas.
The template arguments are TState, TEvent, and TActions:
- TState: Type for automata states
- TEvent: Type for automata events
- TActions: Class for automata actions. These are class methods with signature void method_name(void). Actions are executed when firing a transition.
template <typename TState, typename TEvent, typename TActions>
class Automata
{
// Automata code
};The public methods are detailled below:
template <typename TState, typename TEvent, typename TActions>
class Automata
{
public:
// Set initial state of automata
bool init(const TState &initialState);
// Handle event and fire corresponding transition. Action is executed before changing state.
bool handleEvent(const TEvent &event, TActions &actions);
// Add a transition to automata
void addTransition(const TState &from, const TState &to, const TEvent &evt, void (TActions::*action)(void));
// Get the current state of automata
const TState &getCurrentState() const;
};See example files.
g++ example1.cpp -o ex1
g++ example2.cpp -o ex2Example 2 automata is depicted below.
Usage and initilisation of Example 2 automata is the following one.
Motor motor;
Automata<States, Event, Motor> motor_automata;
motor_automata.addTransition(S_OFF, S_SPEED_1, E_SPEED_INC, &Motor::start);
motor_automata.addTransition(S_SPEED_1, S_SPEED_2, E_SPEED_INC, &Motor::inc);
motor_automata.addTransition(S_SPEED_2, S_SPEED_1, E_SPEED_DEC, &Motor::dec);
motor_automata.addTransition(S_SPEED_1, S_OFF, E_STOP, &Motor::stop);
motor_automata.addTransition(S_SPEED_2, S_OFF, E_STOP, &Motor::stop);
motor_automata.addTransition(S_SPEED_1, S_OFF, E_SPEED_DEC, &Motor::dec);
motor_automata.init(S_OFF);
std::cout << "Motor state is " << motor_automata.getCurrentState() << std::endl;
std::cout << "Motor speed is " << motor.getSpeed() << std::endl;
motor_automata.handleEvent(E_SPEED_INC, motor);This code was developed in 2004 and was part of an ambitious (and immodest) side project named Sofia. It intended to be a way for developing knowledge.
Its immodest introduction motto was (in french):
Dans la seconde moitié du Ve siècle avant JC, les sophistes firent profession d'enseigner la sofia, c'est à dire tout ce qui rend l'homme plus habile, plus savant, plus vertueux. Philosophes, ils étudiaient la logique, les phénomènes célestes, remettant en question les idées reçues, éveillant les intelligences.
Which is translated in:
In the second half of the 5th century BC, sophists made a profession of teaching sofia, that is, all that makes man more skilful, more learned, more virtuous. Philosophers, they studied logic, celestial phenomena, questioning conventional wisdom, awakening intelligences.
Unconsciously and unrelatedly, Sofia is also the name of my beloved daughter, born in 2015.
If you want to contact me you can reach me at [email protected].
This project uses the following license: MIT.