IGSTK
igstkStateMachine.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Image Guided Surgery Software Toolkit
4  Module: $RCSfile: igstkStateMachine.h,v $
5  Language: C++
6  Date: $Date: 2008-02-11 01:41:51 $
7  Version: $Revision: 1.32 $
8 
9  Copyright (c) ISC Insight Software Consortium. All rights reserved.
10  See IGSTKCopyright.txt or http://www.igstk.org/copyright.htm for details.
11 
12  This software is distributed WITHOUT ANY WARRANTY; without even
13  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the above copyright notices for more information.
15 
16 =========================================================================*/
17 
18 #ifndef __igstkStateMachine_h
19 #define __igstkStateMachine_h
20 
21 #include <iostream>
22 #include <map>
23 #include <queue>
24 #include <string>
25 
26 #include "igstkMacros.h"
27 #include "igstkStateMachineState.h"
28 #include "igstkStateMachineInput.h"
29 
30 
31 namespace igstk
32 {
33 
34 
50 template<class TClass>
52 {
53 
54 public:
55 
58 
61 
64 
67 
69  typedef std::string StateDescriptorType;
70 
72  typedef std::string InputDescriptorType;
73 
76  typedef void (TClass::*TMemberFunctionPointer)();
77 
81 
84  StateMachine( TClass * );
85 
87  ~StateMachine();
88 
90  void PushInput( const InputType & input );
91 
94  void PushInputBoolean( bool condition, const InputType & inputIfTrue,
95  const InputType & inputIfFalse);
96 
99  void ProcessInputs();
100 
108  void AddTransition( const StateType & state,
109  const InputType & input,
110  const StateType & newstate,
111  const ActionType & action );
112 
116  void SetReadyToRun();
117 
119  void AddState( const StateType & state,
120  const StateDescriptorType & description );
121 
123  void AddInput( const InputType & input,
124  const InputDescriptorType & description );
125 
129  typedef std::ostream OutputStreamType;
130 
134  void ExportDescription( OutputStreamType & ostr, bool skipLoops ) const;
135 
141  void ExportDescriptionToLTS( OutputStreamType & ostr, bool skipLoops) const;
142 
145  void ExportDescriptionToSCXML( OutputStreamType & ostr, bool skipLoops) const;
146 
148  void SelectInitialState( const StateType & initialState );
149 
151  void Print(std::ostream& os, itk::Indent indent) const;
152 
153 protected:
154 
156  void PrintSelf( std::ostream& os, itk::Indent indent ) const;
157 
161  void ProcessInput( const InputIdentifierType & input );
162 
163 private:
164 
166  StateIdentifierType m_State;
167 
171  TClass * m_This;
172 
173 
180  bool m_ReadyToRun;
181 
185  bool m_InitialStateSelected;
186 
188  typedef std::map< StateIdentifierType, StateDescriptorType > StatesContainer;
189  typedef typename StatesContainer::iterator StatesIterator;
190  typedef typename StatesContainer::const_iterator StatesConstIterator;
191 
193  StatesContainer m_States;
194 
198  StateDescriptorType GetStateDescriptor( const StateIdentifierType & stateId );
199 
203  InputDescriptorType GetInputDescriptor( const InputIdentifierType & inputId );
204 
206  typedef std::map< InputIdentifierType, InputDescriptorType > InputsContainer;
207  typedef typename InputsContainer::iterator InputIterator;
208  typedef typename InputsContainer::const_iterator InputConstIterator;
209  typedef std::queue< InputIdentifierType > InputsQueueContainer;
210 
212  InputsContainer m_Inputs;
213 
216  class StateActionPair
217  {
218  public:
219  StateActionPair()
220  {
221  this->m_StateIdentifier = 0;
222  this->m_Action = 0;
223  }
224  StateActionPair( StateIdentifierType state, ActionType action )
225  {
226  this->m_StateIdentifier = state;
227  this->m_Action = action;
228  }
229  StateActionPair( const StateActionPair & in )
230  {
231  this->m_StateIdentifier = in.m_StateIdentifier;
232  this->m_Action = in.m_Action;
233  }
234  const StateActionPair & operator=( const StateActionPair & in )
235  {
236  this->m_StateIdentifier = in.m_StateIdentifier;
237  this->m_Action = in.m_Action;
238  return *this;
239  }
240  StateIdentifierType GetStateIdentifier() const
241  {
242  return m_StateIdentifier;
243  }
244  ActionType GetAction() const
245  {
246  return m_Action;
247  }
248  private:
249 
250  StateIdentifierType m_StateIdentifier;
251  ActionType m_Action;
252  };
253 
256  typedef std::map< InputIdentifierType, StateActionPair >
257  TransitionsPerInputContainer;
258  typedef std::map< StateIdentifierType, TransitionsPerInputContainer * >
259  TransitionContainer;
260  typedef typename TransitionContainer::iterator TransitionIterator;
261 
262 
263  typedef typename TransitionContainer::const_iterator TransitionConstIterator;
264  typedef typename TransitionsPerInputContainer::iterator
265  TransitionsPerInputIterator;
266  typedef typename TransitionsPerInputContainer::const_iterator
267  TransitionsPerInputConstIterator;
268 
269  TransitionContainer m_Transitions;
270  InputsQueueContainer m_QueuedInputs;
271 };
272 
274 template<class TClass>
275 std::ostream& operator<<(std::ostream& os, const StateMachine<TClass>& o);
276 
277 } // end namespace igstk
278 
279 
280 #ifndef IGSTK_MANUAL_INSTANTIATION
281 #include "igstkStateMachine.txx"
282 #endif
283 
284 #endif
Generic implementation of the Input in a State Machine model.
void PushInputBoolean(bool condition, const InputType &inputIfTrue, const InputType &inputIfFalse)
Push one of two inputs onto the queue, according two whether a condition is true or false...
InputType::IdentifierType InputIdentifierType
Type used to represent the unique identifier of the inputs.
~StateMachine()
Destructor.
void(TClass::* TMemberFunctionPointer)()
Type of the action member funtion of TClass to be invoked at the end of a state transition.
void ExportDescription(OutputStreamType &ostr, bool skipLoops) const
Export the schematic description of the state machine to a stream.
std::ostream OutputStreamType
This extra typedef is necessary for preventing an Internal Compiler Error in Microsoft Visual C++ 6...
void AddState(const StateType &state, const StateDescriptorType &description)
Set the descriptor of a state.
void AddTransition(const StateType &state, const InputType &input, const StateType &newstate, const ActionType &action)
Set the new state to be assume as a reaction to receiving the input code while the StateMachine is in...
StateMachine(TClass *)
Constructor.
StateMachineState< TClass > StateType
Type used to represent the codes of the states.
TMemberFunctionPointer ActionType
Type for the Action to be taken.
void Print(std::ostream &os, itk::Indent indent) const
Print out the content of the class.
defines standard system-wide macros, constants, and other common parameters in the IGSTK Library...
void AddInput(const InputType &input, const InputDescriptorType &description)
Set the descriptor of an input.
Generic implementation of the State Machine model.
void ExportDescriptionToSCXML(OutputStreamType &ostr, bool skipLoops) const
Export the schematic description of the state machine to a stream.
void PrintSelf(std::ostream &os, itk::Indent indent) const
Print the object information in a stream.
std::string StateDescriptorType
Type for the description of the States.
void SetReadyToRun()
This method terminates the programming mode in which AddTransition() can be invoked and pass to the r...
StateType::IdentifierType StateIdentifierType
Type used to represent the unique identifier of the states.
std::string InputDescriptorType
Type for the description of the Inputs.
StateMachineInput< TClass > InputType
Type used to represent the codes of the inputs.
void ProcessInputs()
Perform the state transition and invoke the corresponding action for every pending input stored in th...
unsigned long IdentifierType
Type used to represent the codes of the inputs.
Definition: igstkToken.h:49
Generic implementation of the State in a State Machine model.
void SelectInitialState(const StateType &initialState)
Select Initial state.
void PushInput(const InputType &input)
Push a new input in the queue of inputs to be processed.
void ExportDescriptionToLTS(OutputStreamType &ostr, bool skipLoops) const
Export the schematic description of the state machine to a stream.
void ProcessInput(const InputIdentifierType &input)
Perform the state transition, invoke the corresponding action.