Orocos Real-Time Toolkit  2.6.0
Public Types | Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes
RTT::scripting::ParsedStateMachine Class Reference

State machine created by the scripting engine which represents a parsed state machine. More...

#include <rtt/scripting/ParsedStateMachine.hpp>

Inheritance diagram for RTT::scripting::ParsedStateMachine:
RTT::scripting::StateMachine RTT::base::ExecutableInterface

List of all members.

Public Types

typedef std::vector
< StateMachinePtr > 
ChildList

Public Member Functions

ParsedStateMachinePtr copy (std::map< const base::DataSourceBase *, base::DataSourceBase * > &replacements, bool instantiate=false) const
 Create a copy, set instantiate to 'true' if instantiating a RootMachine.
void addParameter (const std::string &name, base::AttributeBase *var)
base::AttributeBasegetParameter (const std::string &name) const
VisibleWritableValuesMap getParameters () const
std::vector< std::string > getParameterNames () const
void setName (const std::string &name, bool recursive)
 Set the name of this machine.
std::string getText () const
 Return the text to which getLineNumber() refers.
void setText (std::string text)
boost::shared_ptr
< StateMachineService
getService () const
void setService (boost::shared_ptr< StateMachineService > tc)
bool inState (const std::string &name)
void finish ()
 Call this function if the state machine is parsed.
void trace (bool on_off)
 Turn log(Debug) messages on or off to track state transitions.
bool requestState (const std::string &statename)
 Request a transition to a given state.
bool inState (const std::string &state) const
 Check if the state machine is in a given state.
bool inStrictState (const std::string &state) const
 Check if the state machine is in a given state and not in the entry or exit program.
const std::string & getCurrentStateName () const
 Return name of current state, empty string if not active.
bool isStrictlyActive () const
 Strictly active, means active and not in a transition.
bool inInitialState () const
 Inspect if we are in the initial state.
bool inFinalState () const
 Inspect if we are in the final state.
bool stepDone () const
 When isPaused(), return true if no step is pending, when isReactive(), return isStrictlyActive()
bool isActive () const
 Returns true if the state machine is activated.
bool isStopped () const
 Returns true if the state machine is in the final state, after a stop() directive.
bool inError () const
 Get the error status of this StateMachine.
bool isReactive () const
 Query if the state machine is currently reacting only to events.
bool isAutomatic () const
 Query if the state machine is reacting to events and evaluating transition conditions.
bool isPaused () const
 Query if the state machine is paused.
bool activate ()
 Start this StateMachine.
bool pause ()
 Pause the state machine.
bool step ()
 Execute a single action if the state machine is paused or evaluate the transition conditions if the state machine is reactive.
bool automatic ()
 Enter automatic mode: evaluating the transition conditions continuously.
bool start ()
 Enter automatic mode: evaluating the transition conditions continuously.
bool stop ()
 Bring the state machine to the safe final state and wait for events or requests.
bool reset ()
 Reset the state machine from the final state to the initial state and wait for events or requests.
bool reactive ()
 Switch to reactive mode from automatic mode.
bool deactivate ()
 Stop this StateMachine.
bool execute ()
 Used by the StateMachineProcessor to execute the next action(s) or state transitions.
void loading ()
 Informs this object that it got loaded in an ExecutionEngine.
StateInterfacerequestNextState (bool stepping=false)
 Search from the current state a candidate next state.
StateInterfacerequestNextStateStep ()
 Go stepwise through evaluations to find out next state.
bool requestFinalState ()
 Request going to the Final State.
bool requestInitialState ()
 Request going to the Initial State.
StateInterfacenextState ()
 Search from the current state a candidate next state.
std::vector< std::string > getStateList () const
 Get a list of the names of all the present states.
StateInterfacegetState (const std::string &name) const
 Lookup a State by name.
Status::StateMachineStatus getStatus () const
 Get the status of this state machine.
std::string getStatusStr () const
 Get the status in a readable string format.
void addState (StateInterface *s)
 Add a State.
bool requestStateChange (StateInterface *s_n)
 Request a state transition to a new state.
bool executePending (bool stepping=false)
 Execute any pending State (exit, entry, handle) programs.
void preconditionSet (StateInterface *state, ConditionInterface *cnd, int line)
 Express a precondition for entering a state.
void transitionSet (StateInterface *from, StateInterface *to, ConditionInterface *cnd, int priority, int line)
 Express a possible transition from one state to another under a certain condition.
void 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.
bool createEventTransition (ServicePtr sp, 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).
void setInitialState (StateInterface *s)
 Set the initial state of this StateMachine.
void setFinalState (StateInterface *s)
 Set the final state of this StateMachine.
StateInterfacecurrentState () const
 Retrieve the current state of the state machine.
ProgramInterfacecurrentProgram () const
 Retrieve the current program in execution.
StateInterfacegetInitialState () const
 Retrieve the initial state of the state machine.
StateInterfacegetFinalState () const
 Retrieve the final state of the state machine.
void setInitCommand (base::ActionInterface *c)
 This was added for extra (non-user visible) initialisation when the StateMachine is activated.
base::ActionInterfacegetInitCommand () const
StateMachinePtr getParent () const
 Get the parent, returns zero if no parent.
void setParent (StateMachinePtr parent)
const ChildList & getChildren () const
 Get a list of all child state machines.
void addChild (StateMachinePtr child)
const std::string & getName () const
 This method must be overloaded to get a useful hierarchy.
int getLineNumber () const
 Returns the current program line in execution,.
bool inTransition () const
 Inspect if the StateMachine is performing a state transition.
bool interruptible () const
 Inspect if the StateMachine is interruptible by events.
void loaded (ExecutionEngine *ee)
 Called by the ExecutionEngine ee to tell this object it is being loaded.
void unloaded ()
 Called by the ExecutionEngine ee to tell this object it is being unloaded.
bool isLoaded ()
 Returns true if this object is loaded in an ExecutionEngine.
ExecutionEnginegetEngine ()
 Returns the ExecutionEngine this object is loaded into or null otherwise.

Protected Types

typedef std::vector
< boost::tuple
< ConditionInterface
*, StateInterface *, int, int,
boost::shared_ptr
< ProgramInterface > > > 
TransList
 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.
typedef std::map
< StateInterface *, TransList
TransitionMap
typedef std::multimap
< StateInterface *, std::pair
< ConditionInterface *, int > > 
PreConditionMap
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 boost::weak_ptr
< StateMachine
StateMachineParentPtr

Protected Member Functions

virtual void unloading ()
 Informs this object that it got unloaded from an ExecutionEngine.
void changeState (StateInterface *s, ProgramInterface *tprog, bool stepping=false)
void leaveState (StateInterface *s)
void enterState (StateInterface *s)
void runState (StateInterface *s)
void handleState (StateInterface *s)
bool executeProgram (ProgramInterface *&cp, bool stepping)
int checkConditions (StateInterface *state, bool stepping=false)
void enableGlobalEvents ()
void disableGlobalEvents ()
void enableEvents (StateInterface *s)
void disableEvents (StateInterface *s)

Protected Attributes

std::vector< StateMachinePtr > _children
StateMachineParentPtr _parent
std::string _name
Status::StateMachineStatus smStatus
TransitionMap stateMap
 A map keeping track of all States and conditional transitions between two states.
PreConditionMap precondMap
 A map keeping track of all preconditions of a state.
EventMap eventMap
 A map keeping track of all events of a state.
ExecutionEngineengine

Detailed Description

State machine created by the scripting engine which represents a parsed state machine.

Definition at line 54 of file ParsedStateMachine.hpp.


Member Function Documentation

bool RTT::StateMachine::activate ( ) [inherited]
void RTT::StateMachine::addState ( StateInterface s) [inherited]

Add a State.

If already present, changes nothing.

Definition at line 762 of file StateMachine.cpp.

References RTT::scripting::StateMachine::stateMap.

Referenced by RTT::scripting::StateMachine::StateMachine().

ParsedStateMachinePtr RTT::ParsedStateMachine::copy ( std::map< const base::DataSourceBase *, base::DataSourceBase * > &  replacements,
bool  instantiate = false 
) const

Create a copy, set instantiate to 'true' if instantiating a RootMachine.

Todo:
1.

add copy/clone semantics to StateInterface and StateMachine. 2a. rewrite ParsedStateMachine::copy to use (1) 2b. remove all old StateMachine in DataSource code ( code is deprecated by StateMachineCommands implementation) 3. refactor SM copying code in StateGraphParser to this file (and vice versa). 4. in the end, no dynamic_casts should be needed anymore.

Definition at line 54 of file ParsedStateMachine.cpp.

References RTT::scripting::ConditionInterface::copy(), and RTT::Logger::log().

bool RTT::StateMachine::createEventTransition ( ServicePtr  sp,
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>() 
) [inherited]

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.

Definition at line 507 of file StateMachine.cpp.

References RTT::scripting::StateMachine::eventMap, RTT::Logger::log(), RTT::Handle::ready(), and RTT::scripting::StateMachine::stateMap.

Retrieve the current program in execution.

Returns null if the StateMachine is not active or no programs are being run.

Definition at line 910 of file StateMachine.cpp.

Retrieve the current state of the state machine.

Returns null if the StateMachine is not active.

Definition at line 905 of file StateMachine.cpp.

Referenced by RTT::scripting::StateMachine::unloading().

bool RTT::StateMachine::deactivate ( ) [inherited]

Stop this StateMachine.

The current state is left unconditionally.

Definition at line 1197 of file StateMachine.cpp.

References RTT::scripting::StateMachine::executePending(), and RTT::scripting::ProgramInterface::inError().

bool RTT::StateMachine::executePending ( bool  stepping = false) [inherited]

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 983 of file StateMachine.cpp.

References RTT::scripting::StateInterface::getName(), RTT::scripting::StateMachine::inError(), and RTT::scripting::StateMachine::stateMap.

Referenced by RTT::scripting::StateMachine::activate(), RTT::scripting::StateMachine::deactivate(), and RTT::scripting::StateMachine::execute().

Returns the ExecutionEngine this object is loaded into or null otherwise.

Returns:

Definition at line 102 of file ExecutableInterface.hpp.

Referenced by RTT::scripting::StateMachine::loading(), and RTT::scripting::StateMachine::~StateMachine().

int RTT::StateMachine::getLineNumber ( ) const [inherited]

Returns the current program line in execution,.

Returns:
1 if not active

Definition at line 848 of file StateMachine.cpp.

References RTT::scripting::StateInterface::getEntryPoint(), and RTT::scripting::ProgramInterface::getLineNumber().

Referenced by RTT::scripting::StateMachine::unloading().

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

Lookup a State by name.

Returns null if not found.

Definition at line 768 of file StateMachine.cpp.

References RTT::scripting::StateMachine::stateMap.

bool RTT::StateMachine::interruptible ( ) const [inherited]

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 1127 of file StateMachine.cpp.

Referenced by RTT::scripting::StateMachine::requestFinalState(), RTT::scripting::StateMachine::requestInitialState(), RTT::scripting::StateMachine::requestNextState(), and RTT::scripting::StateMachine::requestStateChange().

bool RTT::StateMachine::inTransition ( ) const [inherited]

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 1122 of file StateMachine.cpp.

Referenced by RTT::scripting::StateMachine::execute().

bool RTT::base::ExecutableInterface::isLoaded ( ) [inline, inherited]
void RTT::base::ExecutableInterface::loaded ( ExecutionEngine ee) [inline, inherited]

Called by the ExecutionEngine ee to tell this object it is being loaded.

The engine pointer is set first and the user's loading() function is called next.

Parameters:
eeThe pointer to the engine calling us.

Definition at line 70 of file ExecutableInterface.hpp.

Referenced by RTT::ExecutionEngine::runFunction().

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

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.

References RTT::scripting::StateMachine::eventMap, RTT::base::ExecutableInterface::getEngine(), and RTT::scripting::StateMachine::stateMap.

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 726 of file StateMachine.cpp.

References RTT::scripting::StateMachine::stateMap.

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

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 869 of file StateMachine.cpp.

References RTT::scripting::StateMachine::precondMap, and RTT::scripting::StateMachine::stateMap.

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 375 of file StateMachine.cpp.

References RTT::scripting::StateMachine::interruptible(), and RTT::scripting::StateMachine::requestStateChange().

Referenced by RTT::scripting::StateMachine::execute().

StateInterface * RTT::StateMachine::requestNextState ( bool  stepping = false) [inherited]

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 610 of file StateMachine.cpp.

References RTT::scripting::StateMachine::interruptible(), and RTT::scripting::StateMachine::stateMap.

Referenced by RTT::scripting::StateMachine::execute().

Go stepwise through evaluations to find out next state.

See also:
requestNextState()

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 779 of file StateMachine.cpp.

References RTT::scripting::StateMachine::interruptible(), and RTT::scripting::StateMachine::stateMap.

Referenced by RTT::scripting::StateMachine::requestFinalState(), and RTT::scripting::StateMachine::requestInitialState().

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 531 of file StateMachine.hpp.

void RTT::ParsedStateMachine::setName ( const std::string &  name,
bool  recursive 
)

Set the name of this machine.

If recurisive == true, this also sets subMachines' names, to the given name + "." + the name they have been instantiated by in this machine.

Definition at line 253 of file ParsedStateMachine.cpp.

References RTT::scripting::StateMachine::getChildren(), and setName().

Referenced by setName().

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

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 878 of file StateMachine.cpp.

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

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 883 of file StateMachine.cpp.

References RTT::scripting::StateInterface::getName(), and RTT::scripting::StateMachine::stateMap.

void RTT::base::ExecutableInterface::unloaded ( ) [inline, inherited]

Called by the ExecutionEngine ee to tell this object it is being unloaded.

The user's loading() function is called first and the engine pointer is cleared next.

Definition at line 78 of file ExecutableInterface.hpp.

Referenced by RTT::ExecutionEngine::removeFunction().

void RTT::ParsedStateMachine::unloading ( ) [protected, virtual]

Informs this object that it got unloaded from an ExecutionEngine.

Called by unload() before the engine pointer is cleared.

Reimplemented from RTT::scripting::StateMachine.

Definition at line 215 of file ParsedStateMachine.cpp.


Member Data Documentation

EventMap RTT::scripting::StateMachine::eventMap [protected, inherited]

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 618 of file StateMachine.hpp.

Referenced by RTT::scripting::StateMachine::createEventTransition(), and RTT::scripting::StateMachine::loading().

PreConditionMap RTT::scripting::StateMachine::precondMap [protected, inherited]

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 612 of file StateMachine.hpp.

Referenced by RTT::scripting::StateMachine::preconditionSet().

TransitionMap RTT::scripting::StateMachine::stateMap [protected, inherited]

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