Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

CC_FSM Class Reference
[Finite State Machines]

#include <CC_FSM.h>

Inheritance diagram for CC_FSM:

CC_RootObject List of all members.

Detailed Description

This class encapsulates a finite state machine or FSM.

This class implements the FSM core component.

The FSM is responsible for controlling the transition among the states of the finite state machine and for controlling the execution of the actions associated to these states. The states associated to the FSM are loaded into the FSM during the object configuration. The states associated to the FSM are represented by objects of type FsmState. The states associated to the FSM are referred to using integers in the range [0,N-1] where N is the total number of states in the FSM. The 0-th state is by convention the initial state of the FSM. The current state is the state that is active at a particular point in time.

The FMS is intended to be periodically activated by an external entity (perhaps a scheduler). When it is activated, the FSM executes the actions associated to the current state and, if appropriate, it performs a state transition. State transitions can be either autonomous or they can be requested. An autonomous state transition takes place when the current state declares it has terminated all its actions (its isFinished method returns true) and it has a non-null next state. In that case, the FSM performs a transition to the next state offered by the current state. A requested state transition is started when an outside entity (typically, an FsmEvent object) calls method makeTransitionRequest. State transitions take place only when the FSM is activated. Thus, calls to makeTransitionRequest should really be seen as requests for transitions that are buffered and executed when the FSM is activated. If several such state transition requests are made, only the last one will be executed. A transition - either requested or autonomous - takes place only if all the following conditions are satisfied: the transition is enabled, the exit check for the current state returns "exit OK", the entry check for the target state returns "entry OK".

Enabling or disabling of state transitions can be done only in the sense of enabling/disabling the transitions into a particular state or of disabling transitions tout court. That is, it is not possible to enable/disable transitions between particular states.

State transitions can be disabled at two levels. It is possible to disable all transitions into a certain target state or it is possible to disable transitions between two specific states.

The FSM creates event reports to record the following occurrences: a state transition has been performed.

The initialization sequence of an FSM is as follows:

In all cases, the event originator is the FSM component itself.

The initialization sequence must be performed following the order given above. The FSM object is intended to be used "as is" and without changes. All its methods are accordingly declared to be final (i.e. non-virtual).

See also:
FsmState
Author:
Alessandro Pasetti (P&P Software GmbH)
Version:
1.0

Definition at line 96 of file CC_FSM.h.

Public Member Functions

 CC_FSM (void)
 Instantiate a finite state machine (FSM).

void reset (void)
 Bring the FSM to its initial state (ie the state with index 0) and enable all state transitions.

virtual bool isObjectConfigured (void)
 Perform a class-specific configuration check on an FSM object: verify that the number of states has a legal value and that all FsmState objects have been loaded.

void setNumberOfStates (TD_FsmStateIndex numberOfStates)
 Set the number of states to be managed by the FSM.

TD_FsmStateIndex getNumberOfStates (void) const
 Get the number of states to be managed by the FSM.

void setState (TD_FsmStateIndex i, FsmState *state)
 Load the i-th state into the FSM.

FsmStategetState (TD_FsmStateIndex i) const
 Get the FsmState associated to the i-th state of the FSM.

TD_FsmStateIndex getCurrentState (void) const
 Get the current state.

void setTransitionEnableStatus (bool enabled)
 Enable/disable all state transitions.

void setTransitionEnableStatus (TD_FsmStateIndex toState, bool enabled)
 Enable/disable transitions to the toState-th state.

bool isTransitionEnabled (void) const
 Check whether state transitions are enabled.

bool isTransitionEnabled (TD_FsmStateIndex toState) const
 Check whether a transition from the current state to the argument target state is enabled.

void makeTransitionRequest (TD_FsmStateIndex targetState)
 Lodge a request for a transition to the argument target state.

void activate (void)
 Activate the FSM: execute the actions associated to the current state and perform a state transition if required.


Protected Member Functions

void tryTransition (TD_FsmStateIndex targetState)
 Try to perform a state transtion from the current state to the target state.


Constructor & Destructor Documentation

CC_FSM::CC_FSM void   ) 
 

Instantiate a finite state machine (FSM).

The number of states is initialized to an illegal value to signify that the finite state machine is not yet configured.

Definition at line 21 of file CC_FSM.cpp.


Member Function Documentation

void CC_FSM::activate void   ) 
 

Activate the FSM: execute the actions associated to the current state and perform a state transition if required.

An event report is created when a state transition takes place. This method would typically be called by an external scheduler. A pseudo-code implementation for this method is as follows:

   if (transition request pending)   // request lodged to make transition
                                     // to some targetState
   {  tryTransition(targetState);
      currentState.doContinue();
      return;
   }
   if ( (currentState.isFinished()) &&
         (currentState.getNextState()!=pNULL) )
      tryTransition(currentState.getNextState());
   currentState.doContinue();
   return; 
See also:
tryTransition

Definition at line 132 of file CC_FSM.cpp.

TD_FsmStateIndex CC_FSM::getCurrentState void   )  const [inline]
 

Get the current state.

Returns:
the current state of the FSM

Definition at line 24 of file CC_FSM_inl.h.

TD_FsmStateIndex CC_FSM::getNumberOfStates void   )  const
 

Get the number of states to be managed by the FSM.

If the number of states has not yet been set, a negative value is returned.

Returns:
the number of states managed by the FSM

Definition at line 92 of file CC_FSM.cpp.

FsmState * CC_FSM::getState TD_FsmStateIndex  i  )  const [inline]
 

Get the FsmState associated to the i-th state of the FSM.

The index i should denote a legal state (ie. it should lie in the interval [0,N-1] where N is the total number of states). No check on its legality is performed.

See also:
setState
Parameters:
i the state indicator
Returns:
the FsmState associated to the i-th state

Definition at line 18 of file CC_FSM_inl.h.

bool CC_FSM::isTransitionEnabled TD_FsmStateIndex  toState  )  const [inline]
 

Check whether a transition from the current state to the argument target state is enabled.

The index toState should denote a legal state (ie. it should lie in the interval [0,N-1] where N is the total number of states). No check on its legality is performed.

See also:
activate
Parameters:
toState the target state
Returns:
true if a transition from the current into the target state is enabled

Definition at line 29 of file CC_FSM_inl.h.

bool CC_FSM::isTransitionEnabled void   )  const [inline]
 

Check whether state transitions are enabled.

See also:
setTransitionEnableStatus
Returns:
true if state transitions are enabled, false otherwise

Definition at line 35 of file CC_FSM_inl.h.

void CC_FSM::makeTransitionRequest TD_FsmStateIndex  targetState  ) 
 

Lodge a request for a transition to the argument target state.

The transition will be considered for execution at the next activation of the FSM. If a previous transition request had already been done, it is lost. Only the last request is preserved and will be considered at activation time. The index targetState should denote a legal state (ie. it should lie in the interval [0,N-1] where N is the total number of states). If an illegal value is used, then the method generates an event report of type EVT_ILLEGAL_ST and returns without taking any action.

See also:
activate
Parameters:
targetState the target state for the requested transition

Definition at line 121 of file CC_FSM.cpp.

void CC_FSM::reset void   ) 
 

Bring the FSM to its initial state (ie the state with index 0) and enable all state transitions.

Unless the method is called just after the FSM was instantiated, a termination action is performed on the state in which the FSM is at the time the method is called. The initialization action is performed on the initial state. Finally, a call to this method causes the nextState array to be loaded. Element nextState[i] holds the index of the next state associated to the i-th state. This array is useful to speed up execution of an autonomous state transition (see method activate in this class). When an autonomous state transition is executed, the FSM asks the currentState for its associated nextState and makes a transition to this nextState. The transition however can be done efficiently only if the target state is specified through its index. The nextState array makes precisely this information available. Note that, as a consequence, changes to the nextState associated to a given state become active only after the FSM has been reset.

The execution time of this method grows quadratically with the number of states in the FSM.

A pseudo-code implementation for this method is as follows:

   for (all states in the FSM)
      transitionEnabled[i] = ENABLED;
   for (all states in the FSM)
      nextState[i] = index of ( state[i].getNextState() );
   if (!initialization)
      currentState.doExit();
   currentState=initialState;
   currentState.doInit(); 
Note that no exit check is performed on the currentState and no entry check is performed on the initial state.

Todo:
the implementation of this method is not robust against any of the states having been loaded with an illegal nextState. It is not clear whether this kind of robustness should be built into the method since an illegal nextState would typically be the result of a configuration error and the framework classes are not requierd to be robust against configuration errors (but there should at least be an assertion-level protection).
See also:
activate

Definition at line 29 of file CC_FSM.cpp.

void CC_FSM::setNumberOfStates TD_FsmStateIndex  numberOfStates  ) 
 

Set the number of states to be managed by the FSM.

The FSM maintains an internal data structure where all FsmState objects are held. This method causes memory for this data structure to be allocated and the data structure to be initialized. The number of states must be greater than zero.

This is an initialization method. It should only be called once.

Parameters:
numberOfStates the number of states to be managed by the FSM

Definition at line 69 of file CC_FSM.cpp.

void CC_FSM::setState TD_FsmStateIndex  i,
FsmState state
 

Load the i-th state into the FSM.

The state to be loaded is seen as an instance of the abstract class FsmState. The index i should denote a legal state (ie. it should lie in the interval [0,N-1] where N is the total number of states). If an illegal value of i is used, then the method generates an event report of type EVT_ILLEGAL_ST and returns without taking any action.

See also:
setNumberOfStates
Parameters:
i the state to be loaded
state the FsmState object to be loaded

Definition at line 96 of file CC_FSM.cpp.

void CC_FSM::setTransitionEnableStatus TD_FsmStateIndex  toState,
bool  enabled
 

Enable/disable transitions to the toState-th state.

Both requested and autonomous transitions are disabled. The index toState should denote a legal state (ie. it should lie in the interval [0,N-1] where N is the total number of states). If an illegal value is used, then the method generates an event report of type EVT_ILLEGAL_ST and returns without taking any action.

See also:
activate
Parameters:
toState the state into which transitions are enabled/disabled
enabled the transition is disabled if false, enabled if true

Definition at line 107 of file CC_FSM.cpp.

void CC_FSM::setTransitionEnableStatus bool  enabled  ) 
 

Enable/disable all state transitions.

Both requested and autonomous transitions are disabled.

See also:
activate
Parameters:
enabled the transition are disabled if false, enabled if true

Definition at line 117 of file CC_FSM.cpp.

void CC_FSM::tryTransition TD_FsmStateIndex  targetState  )  [protected]
 

Try to perform a state transtion from the current state to the target state.

The transition is performed if all the following conditions are satisfied: the transition is enabled, the exit check for the current state returns "exit OK", the entry check for the target state returns "entry OK". If a transition is performed, an event report is created to record the fact. Event reports are also created when the state transition fails. A pseudo-code implementation for this method is as follows:

   if (!isTransitionEnabled())
   {  createEventReport(EVT_FSM_ALL_TRANSITION_DISABLED);
      return;
   }
   if (!isTransitionEnabled(targetState))
   {  createEventReport(EVT_FSM_TRANSITION_DISABLED);
      return;
   }
   if (!currentState.canExit())
   {  createEventReport(EVT_FSM_EXIT_FAILED);
      return;
   }
   if (!targetState.canEnter())
   {  createEventReport(EVT_FSM_ENTER_FAILED)
      return;
   }
   currentState.doExit();
   currentState=targetState;
   currentState.doInit();
   createEventReport(EVT_FSM_TRANSITION);
   return; 
The index targetState should denote a legal state (ie. it should lie in the interval [0,N-1] where N is the total number of states). No check on its legality is performed.
See also:
activate
Parameters:
targetState the target state

Definition at line 151 of file CC_FSM.cpp.


The documentation for this class was generated from the following files:
Copyright 2003 P&P Software GmbH - All Rights Reserved