AI Toolkit
Loading...
Searching...
No Matches
Finite State Machine

Classes

class  aitoolkit::fsm::state< T >
 A state of the FSM. More...
 
class  aitoolkit::fsm::simple_machine< T >
 A simple FSM. More...
 
class  aitoolkit::fsm::stack_machine< T >
 A stack FSM. More...
 

Typedefs

template<typename T >
using aitoolkit::fsm::state_ptr = std::unique_ptr<state<T>>
 Heap-allocated pointer to a state.
 

Detailed Description

Introduction

A finite state machine (FSM) is a mathematical model of computation. It is an abstract machine that can be in exactly one of a finite number of states at any given time. The FSM can change from one state to another in response to some external inputs; the change from one state to another is called a transition.

This library provides 2 types of FSMs: a simple machiine and a stack machine. The simple machine is the simplest form of FSM, it can only be in one state at a time. The stack machine is a more complex form of FSM, it can be in multiple states at a time.

Usage

First, include the header:

#include <aitoolkit/fsm.hpp>

Then, create a blackboard type:

struct blackboard_type {
// ...
};

Then, create a state type for each of your states:

class state_dummy final : public state<blackboard_type> {
public:
virtual void enter(blackboard_type& blackboard) override {
// ...
}
virtual void exit(blackboard_type& blackboard) override {
// ...
}
virtual void pause(blackboard_type& blackboard) override {
// ...
}
virtual void resume(blackboard_type& blackboard) override {
// ...
}
virtual void update(blackboard_type& blackboard) override {
// ...
}
};

Simple state machine

Create an instance of the FSM:

using namespace aitoolkit::fsm;
A simple FSM.
Definition fsm.hpp:198

To transition to a new state, call set_state():

machine.set_state(state_dummy{}, blackboard);

NB:

  • this will call the exit method of the current state (if any) and the enter method of the new state
  • if the machine is paused while transitioning to a new state, the new state will be paused as well

To pause the machine, call pause():

machine.pause(blackboard);

NB: This will call the pause method of the current state (if any).

To resume the machine, call resume():

machine.resume(blackboard);

NB: This will call the resume method of the current state (if any).

To update the machine, call update():

machine.update(blackboard);

NB:

  • this will call the update method of the current state (if any)
  • if the machine is paused, calling update() will do nothing

To clear any state, call clear_state():

machine.clear_state(blackboard);

NB: This will call the exit method of the current state (if any).

Stack state machine

Create an instance of the FSM:

using namespace aitoolkit::fsm;
A stack FSM.
Definition fsm.hpp:273

To push a new state onto the stack, call push_state():

machine.push_state(state_dummy{}, blackboard);

NB: This will call the pause method of the current state (if any) and the enter method of the new state.

To pop the top state off the stack, call pop_state():

machine.pop_state(blackboard);

NB: This will call the exit method of the current state (if any) and the resume method of the new top state (if any).

To update the machine, call update():

machine.update(blackboard);

NB: This will call the update method of the top state (if any).