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

#include <State.hpp>

Inheritance diagram for state_machine::State:
Inheritance graph

Public Member Functions

 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
 
virtual void getAvailableInitialStateNestedNames (std::set< NestedName > &nestedNames) const
 
virtual bool setInitialStateByNestedName (const NestedName &nestedName)
 
virtual bool isConsistent (std::vector< std::string > &inconsistencies) const
 
std::string execute ()
 
virtual std::string midExecution ()=0
 
bool isRunning () const
 
void requestPause ()
 
virtual bool requestPauseAfterExecution ()
 
virtual void recallPauseAfterExecution ()
 
virtual bool pauseAfterExecutionRequested ()
 
void requestResume ()
 
bool isPaused ()
 
virtual void requestPreempt ()
 
virtual void recallPreempt ()
 
void servicePreempt ()
 
bool preemptRequested () const
 
void sleepOnce ()
 
void sleepWhilePaused ()
 
virtual Progress getProgress ()
 
virtual std::map< NestedName, ProgressgetRunningStates (const bool ignoreBackgroundStates)
 
void printStateHierarchy ()
 
virtual void printStateHierarchy (const bool useNestedNames, const bool useIndentation, const unsigned int indentation) const
 

Static Public Member Functions

static double getTimeStep ()
 

Protected Member Functions

void setId ()
 
virtual void preExecution ()
 
virtual void postExecution ()
 
void sleep (const double duration)
 

Protected Attributes

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_
 

Static Protected Attributes

static std::mutex freeIdMutex_
 
static int freeId_ = 0
 
static double timeStep_ = 0.1
 
static std::atomic< bool > isPaused_
 

Detailed Description

A basic state, serving as base class for all kind of states. Main functionalities:

  • execute: Each state can be executed, returning an outcome.
  • pause: If supported by the state, the execution can be paused and resumed.
  • preempt: If supported by the state, the execution can be preempted before it is done.

Constructor & Destructor Documentation

◆ State()

state_machine::State::State ( ContextPtr  context,
std::string  name,
std::set< std::string >  outcomes 
)

General methods. Constructor.

Parameters
contextShared state machine context.
nameName of the state.
outcomesList of possible outcomes.

◆ ~State()

virtual state_machine::State::~State ( )
virtualdefault

Destructor.

Member Function Documentation

◆ execute()

std::string state_machine::State::execute ( )

Execution methods. Execute the state. The execution consists of pre-, mid-, and post-execution. The mid-execution can be overriden by child classes and determines the outcome.

Returns
Outcome of the execution.

◆ getAvailableInitialStateNestedNames()

void state_machine::State::getAvailableInitialStateNestedNames ( std::set< NestedName > &  nestedNames) const
virtual

Get a list of the nested name of the states which can be chosen as initial states.

Parameters
nestedNamesNested names of the potential initial states.

Reimplemented in state_machine::Concurrency, and state_machine::StateMachine.

◆ getContext()

ContextPtr state_machine::State::getContext ( ) const

Get a pointer to the context.

Returns
Pointer to the context.

◆ getKeyPrefix()

const std::string & state_machine::State::getKeyPrefix ( ) const

Get the prefix of the global data keys.

Returns
Prefix of the global data keys.

◆ getName()

const std::string & state_machine::State::getName ( ) const

Get the name of the state.

Returns
Name of the state.

◆ getNestedName()

NestedName state_machine::State::getNestedName ( ) const

Get the nested name of the state.

Returns
Nested name of the state.

◆ getOutcomes()

const std::set< std::string > & state_machine::State::getOutcomes ( ) const

Get the possible outcomes of the state.

Returns
Possible outcomes of the state.

◆ getParent()

State * state_machine::State::getParent ( )

Get a pointer to the parent state.

Returns
Pointer to the parent state. Nullptr if none.

◆ getProgress()

Progress state_machine::State::getProgress ( )
virtual

Get the progress of the state. TODO: setProgress, make progress private. TODO: virtual? nobody seems to override it.

Returns
Progress of the state.

◆ getRunningStates()

std::map< NestedName, Progress > state_machine::State::getRunningStates ( const bool  ignoreBackgroundStates)
virtual

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 in state_machine::Concurrency, and state_machine::StateMachine.

◆ getTimeStep()

double state_machine::State::getTimeStep ( )
static

Get the duration of a single time step.

◆ getType()

const std::string & state_machine::State::getType ( ) const

Get the type of the state. TODO Filled? Use the type as key for plugins.

Returns
type of the state.

◆ isConsistent()

bool state_machine::State::isConsistent ( std::vector< std::string > &  inconsistencies) const
virtual

Check if the concurrency is consistent.

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

Reimplemented in state_machine::Concurrency, state_machine::StateMachine, state_machine_planning::StatePlanner, state_machine::Container, state_machine_basics::task::math::Comparison, state_machine_basics::task::system::UserInteraction, state_machine_basics::task::math::Minimum, and TestingState.

◆ isPaused()

bool state_machine::State::isPaused ( )

Check if the state is paused.

Returns
True if paused.

◆ isRunning()

bool state_machine::State::isRunning ( ) const

Check if the state is being executed.

Returns
True if running.

◆ loadSettings()

void state_machine::State::loadSettings ( const Settings settings)
virtual

Load the settings of the state.

Parameters
settingsSettings object for reading settings from.

Reimplemented in state_machine_basics::task::math::Comparison, and TestingState.

◆ midExecution()

◆ pauseAfterExecutionRequested()

bool state_machine::State::pauseAfterExecutionRequested ( )
virtual

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

Returns
True if requested.

Reimplemented in state_machine::StateMachine, and state_machine::Container.

◆ postExecution()

void state_machine::State::postExecution ( )
protectedvirtual

◆ preemptRequested()

bool state_machine::State::preemptRequested ( ) const

Check if preemption has been requested.

Returns
True if paused.

◆ preExecution()

void state_machine::State::preExecution ( )
protectedvirtual

◆ printStateHierarchy() [1/2]

void state_machine::State::printStateHierarchy ( )

Analysis and debugging methods. Print the state hierarchy to the console using the default parameters. This is a separate method, as the clang standard does not allow default parameters for virtual methods.

◆ printStateHierarchy() [2/2]

void state_machine::State::printStateHierarchy ( const bool  useNestedNames,
const bool  useIndentation,
const unsigned int  indentation 
) const
virtual

Print the state hierarchy to the console.

Parameters
useNestedNamesPrint names as nested names.
useIndentationPrint indentations.
indentationIndentation for printing.

Reimplemented in state_machine::Container.

◆ recallPauseAfterExecution()

void state_machine::State::recallPauseAfterExecution ( )
virtual

Recall the request to pause the state after its execution.

Reimplemented in state_machine::StateMachine, and state_machine::Container.

◆ recallPreempt()

void state_machine::State::recallPreempt ( )
virtual

Recall the request to preempt the state.

Reimplemented in state_machine::Container.

◆ requestPause()

void state_machine::State::requestPause ( )

Request to pause the state.

◆ requestPauseAfterExecution()

bool state_machine::State::requestPauseAfterExecution ( )
virtual

Request to pause the state after its execution.

Returns
True if successful.

Reimplemented in state_machine::Concurrency, and state_machine::StateMachine.

◆ requestPreempt()

void state_machine::State::requestPreempt ( )
virtual

Request to preempt the state.

Reimplemented in state_machine::Container.

◆ requestResume()

void state_machine::State::requestResume ( )

Request to resume the state.

◆ resetIds()

void state_machine::State::resetIds ( )

Reset the free ID counter. // TODO 1: Move to dedicated class. TODO 2: Use UID such that this is not necessary anymore!

◆ resetParent()

void state_machine::State::resetParent ( )

Reset the parent state.

◆ saveSettings()

void state_machine::State::saveSettings ( Settings settings) const
virtual

Save the settings of the state.

Parameters
settingsSettings object for writing settings into.

Reimplemented in state_machine_basics::task::math::Comparison, and TestingState.

◆ servicePreempt()

void state_machine::State::servicePreempt ( )

Signal that the state has been preempted, resetting the request to preempt.

◆ setId()

void state_machine::State::setId ( )
protected

Get the next free ID and set it.

◆ setInitialStateByNestedName()

bool state_machine::State::setInitialStateByNestedName ( const NestedName nestedName)
virtual

Set the initial state by nested name.

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

Reimplemented in state_machine::Concurrency, and state_machine::StateMachine.

◆ setName()

void state_machine::State::setName ( const std::string &  name)

Set the name of the state. TODO: Use ID instead of name for transitions etc., so state can be renamed safely.

Parameters
nameName of the state.

◆ setParent()

void state_machine::State::setParent ( State parent)

Setup methods. Set the parent state.

Parameters
parentPointer to the parent state.

◆ sleep()

void state_machine::State::sleep ( const double  duration)
protected

Sleep for a given duration.

Parameters
durationDuration in seconds.

◆ sleepOnce()

void state_machine::State::sleepOnce ( )

Sleep for one time step.

◆ sleepWhilePaused()

void state_machine::State::sleepWhilePaused ( )

Sleep until the state is resumed.

Member Data Documentation

◆ context_

ContextPtr state_machine::State::context_
protected

Members set during setup.

◆ freeId_

int state_machine::State::freeId_ = 0
staticprotected

◆ freeIdMutex_

std::mutex state_machine::State::freeIdMutex_
staticprotected

Static members used by all states.

◆ id_

unsigned int state_machine::State::id_ = -1
protected

◆ isPaused_

std::atomic< bool > state_machine::State::isPaused_
staticprotected

◆ isRunning_

std::atomic<bool> state_machine::State::isRunning_
protected

Members set during execution.

◆ keyPrefix_

std::string state_machine::State::keyPrefix_
protected

◆ name_

std::string state_machine::State::name_
protected

◆ outcomes_

std::set<std::string> state_machine::State::outcomes_
protected

◆ parent_

State* state_machine::State::parent_ = nullptr
protected

◆ preemptRequested_

std::atomic<bool> state_machine::State::preemptRequested_
protected

◆ progress_

Progress state_machine::State::progress_
protected

◆ timeStep_

double state_machine::State::timeStep_ = 0.1
staticprotected

◆ type_

std::string state_machine::State::type_
protected

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