00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef __igstkStateMachine_h
00019 #define __igstkStateMachine_h
00020
00021 #include <iostream>
00022 #include <map>
00023 #include <queue>
00024 #include <string>
00025
00026 #include "igstkMacros.h"
00027 #include "igstkStateMachineState.h"
00028 #include "igstkStateMachineInput.h"
00029
00030
00031 namespace igstk
00032 {
00033
00034
00050 template<class TClass>
00051 class StateMachine
00052 {
00053
00054 public:
00055
00057 typedef StateMachineState<TClass> StateType;
00058
00060 typedef typename StateType::IdentifierType StateIdentifierType;
00061
00063 typedef StateMachineInput<TClass> InputType;
00064
00066 typedef typename InputType::IdentifierType InputIdentifierType;
00067
00069 typedef std::string StateDescriptorType;
00070
00072 typedef std::string InputDescriptorType;
00073
00076 typedef void (TClass::*TMemberFunctionPointer)();
00077
00080 typedef TMemberFunctionPointer ActionType;
00081
00084 StateMachine( TClass * );
00085
00087 ~StateMachine();
00088
00090 void PushInput( const InputType & input );
00091
00094 void PushInputBoolean( bool condition, const InputType & inputIfTrue,
00095 const InputType & inputIfFalse);
00096
00099 void ProcessInputs();
00100
00108 void AddTransition( const StateType & state,
00109 const InputType & input,
00110 const StateType & newstate,
00111 const ActionType & action );
00112
00116 void SetReadyToRun();
00117
00119 void AddState( const StateType & state,
00120 const StateDescriptorType & description );
00121
00123 void AddInput( const InputType & input,
00124 const InputDescriptorType & description );
00125
00129 typedef std::ostream OutputStreamType;
00130
00134 void ExportDescription( OutputStreamType & ostr, bool skipLoops ) const;
00135
00141 void ExportDescriptionToLTS( OutputStreamType & ostr, bool skipLoops) const;
00142
00145 void ExportDescriptionToSCXML( OutputStreamType & ostr, bool skipLoops) const;
00146
00148 void SelectInitialState( const StateType & initialState );
00149
00151 void Print(std::ostream& os, itk::Indent indent) const;
00152
00153 protected:
00154
00156 void PrintSelf( std::ostream& os, itk::Indent indent ) const;
00157
00161 void ProcessInput( const InputIdentifierType & input );
00162
00163 private:
00164
00166 StateIdentifierType m_State;
00167
00171 TClass * m_This;
00172
00173
00180 bool m_ReadyToRun;
00181
00185 bool m_InitialStateSelected;
00186
00188 typedef std::map< StateIdentifierType, StateDescriptorType > StatesContainer;
00189 typedef typename StatesContainer::iterator StatesIterator;
00190 typedef typename StatesContainer::const_iterator StatesConstIterator;
00191
00193 StatesContainer m_States;
00194
00198 StateDescriptorType GetStateDescriptor( const StateIdentifierType & stateId );
00199
00203 InputDescriptorType GetInputDescriptor( const InputIdentifierType & inputId );
00204
00206 typedef std::map< InputIdentifierType, InputDescriptorType > InputsContainer;
00207 typedef typename InputsContainer::iterator InputIterator;
00208 typedef typename InputsContainer::const_iterator InputConstIterator;
00209 typedef std::queue< InputIdentifierType > InputsQueueContainer;
00210
00212 InputsContainer m_Inputs;
00213
00216 class StateActionPair
00217 {
00218 public:
00219 StateActionPair()
00220 {
00221 this->m_StateIdentifier = 0;
00222 this->m_Action = 0;
00223 }
00224 StateActionPair( StateIdentifierType state, ActionType action )
00225 {
00226 this->m_StateIdentifier = state;
00227 this->m_Action = action;
00228 }
00229 StateActionPair( const StateActionPair & in )
00230 {
00231 this->m_StateIdentifier = in.m_StateIdentifier;
00232 this->m_Action = in.m_Action;
00233 }
00234 const StateActionPair & operator=( const StateActionPair & in )
00235 {
00236 this->m_StateIdentifier = in.m_StateIdentifier;
00237 this->m_Action = in.m_Action;
00238 return *this;
00239 }
00240 StateIdentifierType GetStateIdentifier() const
00241 {
00242 return m_StateIdentifier;
00243 }
00244 ActionType GetAction() const
00245 {
00246 return m_Action;
00247 }
00248 private:
00249
00250 StateIdentifierType m_StateIdentifier;
00251 ActionType m_Action;
00252 };
00253
00256 typedef std::map< InputIdentifierType, StateActionPair >
00257 TransitionsPerInputContainer;
00258 typedef std::map< StateIdentifierType, TransitionsPerInputContainer * >
00259 TransitionContainer;
00260 typedef typename TransitionContainer::iterator TransitionIterator;
00261
00262
00263 typedef typename TransitionContainer::const_iterator TransitionConstIterator;
00264 typedef typename TransitionsPerInputContainer::iterator
00265 TransitionsPerInputIterator;
00266 typedef typename TransitionsPerInputContainer::const_iterator
00267 TransitionsPerInputConstIterator;
00268
00269 TransitionContainer m_Transitions;
00270 InputsQueueContainer m_QueuedInputs;
00271 };
00272
00274 template<class TClass>
00275 std::ostream& operator<<(std::ostream& os, const StateMachine<TClass>& o);
00276
00277 }
00278
00279
00280 #ifndef IGSTK_MANUAL_INSTANTIATION
00281 #include "igstkStateMachine.txx"
00282 #endif
00283
00284 #endif