Classes | Public Types | Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes | Private Types | Private Member Functions | Private Attributes | Static Private Attributes
RTT::scripting::StateMachine Class Reference

A hierarchical StateMachine which is loaded in the Program Processor. More...

#include <StateMachine.hpp>

Inheritance diagram for RTT::scripting::StateMachine:
Inheritance graph
[legend]

List of all members.

Classes

struct  Status

Public Types

typedef std::vector
< StateMachinePtr
ChildList

Public Member Functions

bool activate ()
void addChild (StateMachinePtr child)
void addState (StateInterface *s)
bool automatic ()
bool createEventTransition (ServicePtr sp, ExecutionEngine *target_engine, const std::string &ename, std::vector< base::DataSourceBase::shared_ptr > args, StateInterface *from, StateInterface *to, ConditionInterface *guard, boost::shared_ptr< ProgramInterface > transprog, StateInterface *elseto=0, boost::shared_ptr< ProgramInterface > elseprog=boost::shared_ptr< ProgramInterface >())
ProgramInterfacecurrentProgram () const
StateInterfacecurrentState () const
bool deactivate ()
bool execute ()
bool executePending (bool stepping=false)
const ChildListgetChildren () const
const std::string & getCurrentStateName () const
StateInterfacegetFinalState () const
base::ActionInterfacegetInitCommand () const
StateInterfacegetInitialState () const
int getLineNumber () const
const std::string & getName () const
StateMachinePtr getParent () const
StateInterfacegetState (const std::string &name) const
std::vector< std::string > getStateList () const
Status::StateMachineStatus getStatus () const
std::string getStatusStr () const
virtual std::string getText () const
bool inError () const
bool inFinalState () const
bool inInitialState () const
bool inState (const std::string &state) const
bool inStrictState (const std::string &state) const
bool interruptible () const
bool inTransition () const
bool isActive () const
bool isAutomatic () const
bool isPaused () const
bool isReactive () const
bool isStopped () const
bool isStrictlyActive () const
void loading ()
StateInterfacenextState ()
bool pause ()
void preconditionSet (StateInterface *state, ConditionInterface *cnd, int line)
bool reactive ()
bool requestFinalState ()
bool requestInitialState ()
StateInterfacerequestNextState (bool stepping=false)
StateInterfacerequestNextStateStep ()
bool requestState (const std::string &statename)
bool requestStateChange (StateInterface *s_n)
bool reset ()
void setFinalState (StateInterface *s)
void setInitCommand (base::ActionInterface *c)
void setInitialState (StateInterface *s)
void setParent (StateMachinePtr parent)
bool start ()
 StateMachine (StateMachinePtr parent, const std::string &name="Default")
bool step ()
bool stepDone () const
bool stop ()
void trace (bool on_off)
void transitionSet (StateInterface *from, StateInterface *to, ConditionInterface *cnd, int priority, int line)
void transitionSet (StateInterface *from, StateInterface *to, ConditionInterface *cnd, boost::shared_ptr< ProgramInterface > transprog, int priority, int line)
void unloading ()
virtual ~StateMachine ()

Protected Types

typedef std::vector
< boost::tuple< ServicePtr,
std::string, std::vector
< base::DataSourceBase::shared_ptr >
, StateInterface
*, ConditionInterface
*, boost::shared_ptr
< ProgramInterface >, Handle,
StateInterface
*, boost::shared_ptr
< ProgramInterface > > > 
EventList
typedef std::map
< StateInterface *, EventList
EventMap
typedef std::multimap
< StateInterface *, std::pair
< ConditionInterface *, int > > 
PreConditionMap
typedef boost::weak_ptr
< StateMachine
StateMachineParentPtr
typedef std::map
< StateInterface *, TransList
TransitionMap
typedef std::vector
< boost::tuple
< ConditionInterface
*, StateInterface *, int, int,
boost::shared_ptr
< ProgramInterface > > > 
TransList

Protected Member Functions

void changeState (StateInterface *s, ProgramInterface *tprog, bool stepping=false)
int checkConditions (StateInterface *state, bool stepping=false)
void disableEvents (StateInterface *s)
void disableGlobalEvents ()
void enableEvents (StateInterface *s)
void enableGlobalEvents ()
void enterState (StateInterface *s)
bool executeProgram (ProgramInterface *&cp, bool stepping)
void handleState (StateInterface *s)
void leaveState (StateInterface *s)
void runState (StateInterface *s)

Protected Attributes

std::vector< StateMachinePtr_children
std::string _name
StateMachineParentPtr _parent
EventMap eventMap
PreConditionMap precondMap
Status::StateMachineStatus smStatus
TransitionMap stateMap

Private Types

enum  PrivateStatus { nill, gostop, goreset, pausing }

Private Member Functions

bool eventTransition (StateInterface *from, ConditionInterface *c, ProgramInterface *p, StateInterface *to, ProgramInterface *elsep, StateInterface *elseto)

Private Attributes

bool checking_precond
StateInterfacecurrent
ProgramInterfacecurrentEntry
ProgramInterfacecurrentExit
ProgramInterfacecurrentHandle
ProgramInterfacecurrentProg
ProgramInterfacecurrentRun
ProgramInterfacecurrentTrans
int evaluating
os::MutexRecursive execlock
StateInterfacefinistate
base::ActionInterfaceinitc
StateInterfaceinitstate
bool mstep
bool mtrace
StateInterfacenext
std::pair
< PreConditionMap::const_iterator,
PreConditionMap::const_iterator > 
prec_it
TransList::iterator reqend
TransList::iterator reqstep
enum
RTT::scripting::StateMachine::PrivateStatus 
smpStatus

Static Private Attributes

static std::string emptyString

Detailed Description

A hierarchical StateMachine which is loaded in the Program Processor.

A StateMachine can have children and one parent.

Todo:
Implement the whole transition mechanism with the Strategy software pattern to allow cleaner implementation.

Definition at line 72 of file StateMachine.hpp.


Member Typedef Documentation

Definition at line 109 of file StateMachine.hpp.

typedef std::vector< boost::tuple<ServicePtr, std::string, std::vector<base::DataSourceBase::shared_ptr>, StateInterface*, ConditionInterface*, boost::shared_ptr<ProgramInterface>, Handle, StateInterface*, boost::shared_ptr<ProgramInterface> > > RTT::scripting::StateMachine::EventList [protected]

Definition at line 98 of file StateMachine.hpp.

Definition at line 99 of file StateMachine.hpp.

typedef std::multimap< StateInterface*, std::pair<ConditionInterface*, int> > RTT::scripting::StateMachine::PreConditionMap [protected]

Definition at line 92 of file StateMachine.hpp.

Definition at line 101 of file StateMachine.hpp.

Definition at line 91 of file StateMachine.hpp.

typedef std::vector< boost::tuple<ConditionInterface*, StateInterface*, int, int, boost::shared_ptr<ProgramInterface> > > RTT::scripting::StateMachine::TransList [protected]

The key is the current state, the value is the transition condition to another state with a certain priority (int), on a line (int), with a transition program

Definition at line 90 of file StateMachine.hpp.


Member Enumeration Documentation

Enumerator:
nill 
gostop 
goreset 
pausing 

Definition at line 75 of file StateMachine.hpp.


Constructor & Destructor Documentation

The destructor is virtual since ParsedStateMachine still inherits this class.

Definition at line 71 of file StateMachine.cpp.

RTT::StateMachine::StateMachine ( StateMachinePtr  parent,
const std::string &  name = "Default" 
)

Create a new StateMachine with an optional parent. Set parent to zero for the top state machine. The initial Status of a StateMachine is always inactive.

Definition at line 62 of file StateMachine.cpp.


Member Function Documentation

Start this StateMachine. The Initial state will be entered.

Definition at line 1174 of file StateMachine.cpp.

Definition at line 564 of file StateMachine.hpp.

Add a State. If already present, changes nothing.

Definition at line 774 of file StateMachine.cpp.

Enter automatic mode: evaluating the transition conditions continuously.

Definition at line 209 of file StateMachine.cpp.

void RTT::StateMachine::changeState ( StateInterface s,
ProgramInterface tprog,
bool  stepping = false 
) [protected]

Definition at line 415 of file StateMachine.cpp.

int RTT::StateMachine::checkConditions ( StateInterface state,
bool  stepping = false 
) [protected]

Definition at line 704 of file StateMachine.cpp.

bool RTT::StateMachine::createEventTransition ( ServicePtr  sp,
ExecutionEngine target_engine,
const std::string &  ename,
std::vector< base::DataSourceBase::shared_ptr args,
StateInterface from,
StateInterface to,
ConditionInterface guard,
boost::shared_ptr< ProgramInterface transprog,
StateInterface elseto = 0,
boost::shared_ptr< ProgramInterface elseprog = boost::shared_ptr<ProgramInterface>() 
)

Express a possible transition from one state to another when an Event is fired under a certain condition (guard).

Parameters:
enameThe name of the Event under which a transition should be made
argsThe arguments which the event handler must set upon occurence.
fromThe state which should be left
toThe state which should be entered
guardThe Condition under which the transition may succeed
transprogThe program to be executed between exit of from and entry of to.
spThe Service in which ename can be found.
target_engineThe ExecutionEngine which will execute this StateMachine.

Definition at line 508 of file StateMachine.cpp.

Retrieve the current program in execution. Returns null if the StateMachine is not active or no programs are being run.

Definition at line 928 of file StateMachine.cpp.

Retrieve the current state of the state machine. Returns null if the StateMachine is not active.

Definition at line 923 of file StateMachine.cpp.

Stop this StateMachine. The current state is left unconditionally.

Definition at line 1227 of file StateMachine.cpp.

Definition at line 491 of file StateMachine.cpp.

Definition at line 472 of file StateMachine.cpp.

Definition at line 476 of file StateMachine.cpp.

Definition at line 468 of file StateMachine.cpp.

void RTT::StateMachine::enterState ( StateInterface s) [protected]

Definition at line 969 of file StateMachine.cpp.

Internal use only. Make a transition to state 'to' with transitionprogram 'p' under condition 'c'. if from != current or in transition already, discard transition.

Definition at line 569 of file StateMachine.cpp.

bool RTT::StateMachine::execute ( ) [virtual]

Used by the StateMachineProcessor to execute the next action(s) or state transitions.

Implements RTT::base::ExecutableInterface.

Definition at line 263 of file StateMachine.cpp.

bool RTT::StateMachine::executePending ( bool  stepping = false)

Execute any pending State (exit, entry, handle) programs. You must executePending, before calling requestState() or requestNextState(). You should only call requestState() or requestNextState() if executePending returns true.

Due to the pending requests, the currentState() may have changed.

Parameters:
steppingprovide true if the pending programs should be executed one step at a time.
Return values:
trueif nothing was pending
falseif there was some program executing.

Definition at line 988 of file StateMachine.cpp.

bool RTT::StateMachine::executeProgram ( ProgramInterface *&  cp,
bool  stepping 
) [protected]

Definition at line 1122 of file StateMachine.cpp.

Get a list of all child state machines.

Definition at line 559 of file StateMachine.hpp.

const std::string& RTT::scripting::StateMachine::getCurrentStateName ( ) const [inline]

Return name of current state, empty string if not active.

Definition at line 164 of file StateMachine.hpp.

Retrieve the final state of the state machine.

Definition at line 522 of file StateMachine.hpp.

Definition at line 538 of file StateMachine.hpp.

Retrieve the initial state of the state machine.

Definition at line 515 of file StateMachine.hpp.

Returns the current program line in execution,

Returns:
1 if not active

Definition at line 866 of file StateMachine.cpp.

const std::string& RTT::scripting::StateMachine::getName ( ) const [inline]

This method must be overloaded to get a useful hierarchy.

Definition at line 572 of file StateMachine.hpp.

Get the parent, returns zero if no parent.

Definition at line 546 of file StateMachine.hpp.

StateInterface * RTT::StateMachine::getState ( const std::string &  name) const

Lookup a State by name. Returns null if not found.

Definition at line 780 of file StateMachine.cpp.

std::vector< std::string > RTT::StateMachine::getStateList ( ) const

Get a list of the names of all the present states.

Definition at line 764 of file StateMachine.cpp.

Get the status of this state machine.

Definition at line 123 of file StateMachine.cpp.

Get the status in a readable string format.

Definition at line 127 of file StateMachine.cpp.

string RTT::StateMachine::getText ( ) const [virtual]

Return the text to which getLineNumber() refers.

Reimplemented in RTT::scripting::ParsedStateMachine.

Definition at line 883 of file StateMachine.cpp.

Definition at line 958 of file StateMachine.cpp.

bool RTT::scripting::StateMachine::inError ( ) const [inline]

Get the error status of this StateMachine.

Definition at line 216 of file StateMachine.hpp.

Inspect if we are in the final state.

Definition at line 188 of file StateMachine.hpp.

Inspect if we are in the initial state.

Definition at line 181 of file StateMachine.hpp.

bool RTT::scripting::StateMachine::inState ( const std::string &  state) const [inline]

Check if the state machine is in a given state.

Definition at line 143 of file StateMachine.hpp.

bool RTT::scripting::StateMachine::inStrictState ( const std::string &  state) const [inline]

Check if the state machine is in a given state and not in the entry or exit program.

Definition at line 154 of file StateMachine.hpp.

Inspect if the StateMachine is interruptible by events. Only the run program may be interrupted, or if no program is currently executed.

Definition at line 1154 of file StateMachine.cpp.

Inspect if the StateMachine is performing a state transition. A transition is in progress if entry, transition or exit programs are executed.

Returns:
true if it is executing a program (except run or handle program), false if it is not executing a program OR executing the run/handle program.

Definition at line 1149 of file StateMachine.cpp.

bool RTT::scripting::StateMachine::isActive ( ) const [inline]

Returns true if the state machine is activated.

Definition at line 205 of file StateMachine.hpp.

Query if the state machine is reacting to events and evaluating transition conditions.

Definition at line 227 of file StateMachine.hpp.

bool RTT::scripting::StateMachine::isPaused ( ) const [inline]

Query if the state machine is paused.

Definition at line 232 of file StateMachine.hpp.

Query if the state machine is currently reacting only to events.

Definition at line 221 of file StateMachine.hpp.

bool RTT::scripting::StateMachine::isStopped ( ) const [inline]

Returns true if the state machine is in the final state, after a stop() directive.

Definition at line 211 of file StateMachine.hpp.

Strictly active, means active and not in a transition.

Definition at line 174 of file StateMachine.hpp.

void RTT::StateMachine::leaveState ( StateInterface s) [protected]

Definition at line 933 of file StateMachine.cpp.

void RTT::StateMachine::loading ( ) [virtual]

Informs this object that it got loaded in an ExecutionEngine. Called by load() after the engine pointer is set.

Reimplemented from RTT::base::ExecutableInterface.

Definition at line 81 of file StateMachine.cpp.

Search from the current state a candidate next state. If none is found, the current state is returned.

Note:
The mere calling of this method, may influence future possible results. Multiple invocations of nextState() may return different results, so use with care.
See also:
requestNextState()

Definition at line 737 of file StateMachine.cpp.

Pause the state machine.

Definition at line 171 of file StateMachine.cpp.

void RTT::StateMachine::preconditionSet ( StateInterface state,
ConditionInterface cnd,
int  line 
)

Express a precondition for entering a state. The precondition will be chained (Logical AND) with any transition to this state. This means that any transition to this state will only succeed if all preconditions hold. If state is the initial state, the preconditions must be true to make actiate() succeed. If state is the final state, they will not be checked upon requestFinalState, since requestFinalState always succeeds.

Parameters:
stateThe state for which the preconditions must hold
cndThe Pre-Condition under which a transition to this state may succeed
lineThe line number where this precondition was introduced.

Definition at line 887 of file StateMachine.cpp.

Switch to reactive mode from automatic mode.

Definition at line 226 of file StateMachine.cpp.

Request going to the Final State. This will always proceed.

Definition at line 396 of file StateMachine.cpp.

Request going to the Initial State. This function will only proceed if the current state is the Final State or the Initial State. If it fails, one can try to requestNextState() which may lead to the initial state anyway if the transition is set. This path is not tried by this function.

Returns:
The true on success, false if not allowed.

Definition at line 380 of file StateMachine.cpp.

Search from the current state a candidate next state. If none is found, the current state is taken. Next, handle the resulting state.

Note:
This call is not equivalent to this->requestState( this->nextState() ), since multiple invocations of this->nextState() may result in different results, hence, this->requestState( this->nextState() ) may return false. Use this method instead to automatically go to the next state.
Parameters:
steppingprovide true if the transition evaluations should be executed one at a time.
Returns:
The current state.

Definition at line 620 of file StateMachine.cpp.

Go stepwise through evaluations to find out next state.

See also:
requestNextState()
bool RTT::scripting::StateMachine::requestState ( const std::string &  statename) [inline]

Request a transition to a given state.

Definition at line 132 of file StateMachine.hpp.

Request a state transition to a new state. If the transition is not set by transitionSet(), acquiering the state will fail.

Parameters:
s_nThe state to change to
Return values:
trueif the transition is successfull
falseif the transition is not allowed

Definition at line 791 of file StateMachine.cpp.

Reset the state machine from the final state to the initial state and wait for events or requests.

Definition at line 250 of file StateMachine.cpp.

void RTT::StateMachine::runState ( StateInterface s) [protected]

Definition at line 946 of file StateMachine.cpp.

Set the final state of this StateMachine.

Definition at line 1164 of file StateMachine.cpp.

This was added for extra (non-user visible) initialisation when the StateMachine is activated.

Parameters:
cThe command to execute upon each activate. c is aggregated by this state machine and deleted in the destructor.

Definition at line 533 of file StateMachine.hpp.

Set the initial state of this StateMachine.

Definition at line 1158 of file StateMachine.cpp.

Definition at line 551 of file StateMachine.hpp.

Enter automatic mode: evaluating the transition conditions continuously.

Definition at line 204 of file StateMachine.cpp.

Execute a single action if the state machine is paused or evaluate the transition conditions if the state machine is reactive.

Definition at line 187 of file StateMachine.cpp.

bool RTT::scripting::StateMachine::stepDone ( ) const [inline]

When isPaused(), return true if no step is pending, when isReactive(), return isStrictlyActive()

Definition at line 196 of file StateMachine.hpp.

Bring the state machine to the safe final state and wait for events or requests.

Definition at line 238 of file StateMachine.cpp.

void RTT::StateMachine::trace ( bool  on_off)

Turn log(Debug) messages on or off to track state transitions.

Definition at line 1170 of file StateMachine.cpp.

void RTT::StateMachine::transitionSet ( StateInterface from,
StateInterface to,
ConditionInterface cnd,
int  priority,
int  line 
)

Express a possible transition from one state to another under a certain condition.

Parameters:
fromThe state which should be left
toThe state which should be entered
cndThe Condition under which the transition may succeed
priorityThe priority of this transition; low number (like -1000) is low priority high number is high priority (like + 1000). Transitions of equal priority are traversed in an unspecified way.
lineThe line number where this transition was introduced.
Postcondition:
All transitions from from to to will succeed under condition cnd

Definition at line 896 of file StateMachine.cpp.

void RTT::StateMachine::transitionSet ( StateInterface from,
StateInterface to,
ConditionInterface cnd,
boost::shared_ptr< ProgramInterface transprog,
int  priority,
int  line 
)

Express a possible transition from one state to another under a certain condition.

Parameters:
fromThe state which should be left
toThe state which should be entered
cndThe Condition under which the transition may succeed
transprogThe program to be executed between exit of from and entry of to. May be null to indicate the empty program.
priorityThe priority of this transition; low number (like -1000) is low priority high number is high priority (like + 1000). Transitions of equal priority are traversed in an unspecified way.
lineThe line number where this transition was introduced.
Postcondition:
All transitions from from to to will succeed under condition cnd

Definition at line 901 of file StateMachine.cpp.

void RTT::StateMachine::unloading ( ) [virtual]

Informs this object that it got unloaded from an ExecutionEngine. Called by unload() before the engine pointer is cleared.

Reimplemented from RTT::base::ExecutableInterface.

Reimplemented in RTT::scripting::ParsedStateMachine.

Definition at line 105 of file StateMachine.cpp.


Member Data Documentation

Definition at line 100 of file StateMachine.hpp.

std::string RTT::scripting::StateMachine::_name [protected]

Definition at line 104 of file StateMachine.hpp.

Definition at line 102 of file StateMachine.hpp.

Definition at line 684 of file StateMachine.hpp.

The current state the Machine is in. current == 0 means that the state machine is currently inactive.

Definition at line 664 of file StateMachine.hpp.

Definition at line 676 of file StateMachine.hpp.

Definition at line 674 of file StateMachine.hpp.

Definition at line 675 of file StateMachine.hpp.

Definition at line 673 of file StateMachine.hpp.

Definition at line 677 of file StateMachine.hpp.

Definition at line 678 of file StateMachine.hpp.

std::string RTT::StateMachine::emptyString [static, private]

Definition at line 77 of file StateMachine.hpp.

Definition at line 687 of file StateMachine.hpp.

A map keeping track of all events of a state. Not all states need to be present as a key in this map.

Definition at line 620 of file StateMachine.hpp.

Definition at line 689 of file StateMachine.hpp.

The Final State.

Definition at line 658 of file StateMachine.hpp.

Definition at line 671 of file StateMachine.hpp.

The Initial State.

Definition at line 653 of file StateMachine.hpp.

Definition at line 685 of file StateMachine.hpp.

Definition at line 685 of file StateMachine.hpp.

The next state the Machine will go to.

Definition at line 669 of file StateMachine.hpp.

std::pair<PreConditionMap::const_iterator,PreConditionMap::const_iterator> RTT::scripting::StateMachine::prec_it [private]

Definition at line 683 of file StateMachine.hpp.

A map keeping track of all preconditions of a state. Not all states need to be present as a key in this map.

Definition at line 614 of file StateMachine.hpp.

TransList::iterator RTT::scripting::StateMachine::reqend [private]

Definition at line 681 of file StateMachine.hpp.

TransList::iterator RTT::scripting::StateMachine::reqstep [private]

Definition at line 680 of file StateMachine.hpp.

Definition at line 105 of file StateMachine.hpp.

A map keeping track of all States and conditional transitions between two states. Every state of this StateMachine must be] present as a key in this map.

Definition at line 608 of file StateMachine.hpp.


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


rtt
Author(s): RTT Developers
autogenerated on Sat Jun 8 2019 18:46:40