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