State Machine  0.5.0
This repository contains packages for creating and running general purpose state machines.
state_machine::StateMachine Class Reference

#include <StateMachine.hpp>

Inheritance diagram for state_machine::StateMachine:
Inheritance graph

Public Types

using Transitions = std::map< std::string, std::map< std::string, std::string > >
 

Public Member Functions

 StateMachine (ContextPtr context, std::string name, std::set< std::string > outcomes, bool restartOnExecution)
 
 ~StateMachine () override=default
 
void addState (const StatePtr &state, const std::map< std::string, std::string > &transitions)
 
void removeStateByName (const std::string &name) override
 
void clear () override
 
TransitionsgetTransitions ()
 
const TransitionsgetTransitions () const
 
void setInitialStateByName (const std::string &name)
 
bool setInitialStateByNestedName (const NestedName &nestedName) override
 
std::string getInitialStateName () const
 
StatePtr getInitialState () const
 
void getAvailableInitialStateNestedNames (std::set< NestedName > &nestedNames) const override
 
bool isConsistent (std::vector< std::string > &inconsistencies) const override
 
std::string midExecution () override
 
bool requestPauseAfterExecution () override
 
void recallPauseAfterExecution () override
 
bool pauseAfterExecutionRequested () override
 
std::map< NestedName, ProgressgetRunningStates (const bool ignoreBackgroundStates) override
 
- Public Member Functions inherited from state_machine::Container
void addState (const StatePtr &state)
 
bool stateExists (const std::string &name) const
 
StatePtr getState (const std::string &name) const
 
std::map< std::string, StatePtrgetStates () const
 
std::set< std::string > getStateNames () const
 
size_t getNumberOfStates () const
 
void requestPreempt () override
 
void recallPreempt () override
 
void printStateHierarchy (const bool useNestedNames, const bool useIndentation, const unsigned int indentation) const override
 
- Public Member Functions inherited from state_machine::State
 State (ContextPtr context, std::string name, std::set< std::string > outcomes)
 
virtual ~State ()=default
 
ContextPtr getContext () const
 
void setName (const std::string &name)
 
const std::string & getName () const
 
NestedName getNestedName () const
 
const std::set< std::string > & getOutcomes () const
 
const std::string & getType () const
 
void resetIds ()
 
const std::string & getKeyPrefix () const
 
void setParent (State *parent)
 
StategetParent ()
 
void resetParent ()
 
virtual void loadSettings (const Settings &settings)
 
virtual void saveSettings (Settings &settings) const
 
std::string execute ()
 
bool isRunning () const
 
void requestPause ()
 
void requestResume ()
 
bool isPaused ()
 
void servicePreempt ()
 
bool preemptRequested () const
 
void sleepOnce ()
 
void sleepWhilePaused ()
 
virtual Progress getProgress ()
 
void printStateHierarchy ()
 

Protected Attributes

bool restartOnExecution_ = false
 
Transitions transitions_
 
std::string initialStateName_
 
StatePtr runningState_
 
std::atomic< bool > pauseAfterExecutionRequested_
 
- Protected Attributes inherited from state_machine::Container
std::map< std::string, StatePtrstates_
 
- Protected Attributes inherited from state_machine::State
ContextPtr context_
 
std::string name_
 
std::string type_
 
std::set< std::string > outcomes_
 
unsigned int id_ = -1
 
std::string keyPrefix_
 
Stateparent_ = nullptr
 
std::atomic< bool > isRunning_
 
std::atomic< bool > preemptRequested_
 
Progress progress_
 

Additional Inherited Members

- Static Public Member Functions inherited from state_machine::State
static double getTimeStep ()
 
- Protected Member Functions inherited from state_machine::Container
 Container (ContextPtr context, std::string name, std::set< std::string > outcomes)
 
 ~Container () override=default
 
- Protected Member Functions inherited from state_machine::State
void setId ()
 
virtual void preExecution ()
 
virtual void postExecution ()
 
void sleep (const double duration)
 
- Static Protected Attributes inherited from state_machine::State
static std::mutex freeIdMutex_
 
static int freeId_ = 0
 
static double timeStep_ = 0.1
 
static std::atomic< bool > isPaused_
 

Detailed Description

A state machine is a state which runs child states in sequence.

It has an initial state which is executed first. Depending on its outcome, the transitions determine the following state respectively the outcome of the state machine.

Member Typedef Documentation

◆ Transitions

using state_machine::StateMachine::Transitions = std::map<std::string, std::map<std::string, std::string> >

This type stores transitions for all the child states: It contains a map for each child state from its outcome to the following child state name respectively the outcome of the state machine.

Constructor & Destructor Documentation

◆ StateMachine()

state_machine::StateMachine::StateMachine ( ContextPtr  context,
std::string  name,
std::set< std::string >  outcomes,
bool  restartOnExecution 
)

General methods. Constructor.

Parameters
contextShared state machine context.
nameName of the state machine.
outcomesList of possible outcomes.
restartOnExecutionRestart the state machine on every execution.

◆ ~StateMachine()

state_machine::StateMachine::~StateMachine ( )
overridedefault

Destructor.

Member Function Documentation

◆ addState()

void state_machine::StateMachine::addState ( const StatePtr state,
const std::map< std::string, std::string > &  transitions 
)

Setup methods. Add a child state to the state machine.

Parameters
stateState to add.
transitionsMap from the outcome of the child state to the following state or outcome of the state machine.

◆ clear()

void state_machine::StateMachine::clear ( )
overridevirtual

Clear the state machine, removing all contained child states.

Reimplemented from state_machine::Container.

◆ getAvailableInitialStateNestedNames()

void state_machine::StateMachine::getAvailableInitialStateNestedNames ( std::set< NestedName > &  nestedNames) const
overridevirtual

Get the available initial states of the contained child states.

Parameters
nestedNamesReturn argument, will contain the nested names of the available initial states.

Reimplemented from state_machine::State.

◆ getInitialState()

StatePtr state_machine::StateMachine::getInitialState ( ) const

Get a pointer to the initial state of the state machine.

Returns
Pointer to the initial state.

◆ getInitialStateName()

std::string state_machine::StateMachine::getInitialStateName ( ) const

Get the name of the initial state of the state machine.

Returns
Name of the initial state.

◆ getRunningStates()

std::map< NestedName, Progress > state_machine::StateMachine::getRunningStates ( const bool  ignoreBackgroundStates)
overridevirtual

Get a list of the running states and their progresses.

Parameters
ignoreBackgroundStatesIf true, the list will not contain background states.
Returns
List of the nested names of the states and their progresses.

Reimplemented from state_machine::State.

◆ getTransitions() [1/2]

StateMachine::Transitions & state_machine::StateMachine::getTransitions ( )

Get all transitions by reference.

Returns
All transitions.

◆ getTransitions() [2/2]

const StateMachine::Transitions & state_machine::StateMachine::getTransitions ( ) const

Get all transitions by constant reference.

Returns
All transitions.

◆ isConsistent()

bool state_machine::StateMachine::isConsistent ( std::vector< std::string > &  inconsistencies) const
overridevirtual

Check if the state machine is consistent.

Parameters
inconsistenciesReturn argument, will contain a list of inconsistencies for printing.
Returns
True if consistent.

Reimplemented from state_machine::Container.

Reimplemented in state_machine_planning::StatePlanner.

◆ midExecution()

std::string state_machine::StateMachine::midExecution ( )
overridevirtual

Execution methods. Mid-execution of the state machine.

Returns
Outcome of the execution.

Implements state_machine::State.

◆ pauseAfterExecutionRequested()

bool state_machine::StateMachine::pauseAfterExecutionRequested ( )
overridevirtual

Check if pausing the state after its execution has been requested.

Returns
True if requested.

Reimplemented from state_machine::Container.

◆ recallPauseAfterExecution()

void state_machine::StateMachine::recallPauseAfterExecution ( )
overridevirtual

Recall the request to pause the state after its execution.

Reimplemented from state_machine::Container.

◆ removeStateByName()

void state_machine::StateMachine::removeStateByName ( const std::string &  name)
overridevirtual

Remove a child state from the state machine.

Parameters
nameName of the child state to remove.

Reimplemented from state_machine::Container.

◆ requestPauseAfterExecution()

bool state_machine::StateMachine::requestPauseAfterExecution ( )
overridevirtual

Request to pause after executing the currently running state.

Returns
True if successful.

Reimplemented from state_machine::State.

◆ setInitialStateByName()

void state_machine::StateMachine::setInitialStateByName ( const std::string &  name)

Set the initial state of the state machine.

Parameters
nameName of the initial state.

◆ setInitialStateByNestedName()

bool state_machine::StateMachine::setInitialStateByNestedName ( const NestedName nestedName)
overridevirtual

Set the initial state of the state machine and its contained child states.

Parameters
nestedNameNested name of the initial state.
Returns
True if successful.

Reimplemented from state_machine::State.

Member Data Documentation

◆ initialStateName_

std::string state_machine::StateMachine::initialStateName_
protected

◆ pauseAfterExecutionRequested_

std::atomic<bool> state_machine::StateMachine::pauseAfterExecutionRequested_
protected

◆ restartOnExecution_

bool state_machine::StateMachine::restartOnExecution_ = false
protected

Members set during setup.

◆ runningState_

StatePtr state_machine::StateMachine::runningState_
protected

Members set during execution.

◆ transitions_

Transitions state_machine::StateMachine::transitions_
protected

The documentation for this class was generated from the following files: