base.hpp

Go to the documentation of this file.
00001 
00027 #ifndef SGDK_AI_FSM_BASE_HPP
00028 #define SGDK_AI_FSM_BASE_HPP
00029 
00030 /*
00031  * Defines the sgdk::IndexTypeTransitionFunction class template.
00032  */
00033 #include <swiss_gd_knife/ai/fsm/transition.hpp>
00034 
00035 namespace sgdk {
00036 
00038 
00096 template <
00097 #ifndef SGDK_DOX
00098     typename StateInputMatrix
00099   , typename Index = unsigned int
00100 #endif  /* SGDK_DOX */
00101 >
00102 class IndexTypeFSM
00103 {
00104  public:
00109 #ifdef SGDK_DOX
00110     typedef implementation_defined TransitionFunction;
00111 #else
00112     typedef IndexTypeTransitionFunction<StateInputMatrix,Index>
00113             TransitionFunction;
00114 #endif  /* SGDK_DOX */
00115 
00116  private:
00117     TransitionFunction _transition_function;
00118     Index              _start_state;
00119     Index              _current_state;
00120     Index              _end_state;
00121 
00122  public:
00124 
00127     IndexTypeFSM()
00128       : _transition_function()
00129       , _start_state()
00130       , _current_state()
00131       , _end_state()
00132     {
00133     }
00134 
00140     template <typename IndexTypeFSMBuilder>
00141     explicit IndexTypeFSM(const IndexTypeFSMBuilder& fsm_builder)
00142       : _transition_function()
00143       , _start_state()
00144       , _current_state()
00145       , _end_state()
00146     {
00147         initialize(fsm_builder);
00148     }
00149 
00151 
00157     IndexTypeFSM(const IndexTypeFSM& copy)
00158       : _transition_function(copy._transition_function)
00159       , _start_state(copy._start_state)
00160       , _current_state(copy._current_state)
00161       , _end_state(copy._end_state)
00162     {
00163     }
00164 
00166 
00173     IndexTypeFSM& operator=(const IndexTypeFSM& copy)
00174     {
00175         _transition_function = copy._transition_function;
00176         _start_state         = copy._start_state;
00177         _current_state       = copy._current_state;
00178         _end_state           = copy._end_state;
00179         return *this;
00180     }
00181 
00201     template <typename IndexTypeFSMBuilder>
00202     void initialize(const IndexTypeFSMBuilder& fsm_builder);
00203 
00211     inline const TransitionFunction& getTransitionFunction() const
00212     {
00213         return _transition_function;
00214     }
00215 
00220     inline void reset()
00221     {
00222         _current_state = _start_state;
00223     }
00224 
00232     inline Index getCurrentState() const
00233     {
00234         return _current_state;
00235     }
00236 
00247     template <typename BackInsertionSequence>
00248     void makeNontrivialInputs(BackInsertionSequence& sequence) const;
00249 
00261     inline Index getNextState(const Index input) const
00262     {
00263         if (_current_state != _end_state)
00264         {
00265             return _transition_function(_current_state, input);
00266         }
00267         else
00268         {
00269             return _current_state;
00270         }
00271     }
00272 
00283     inline void processInput(const Index input)
00284     {
00285         if (_current_state != _end_state)
00286         {
00287             _current_state = _transition_function(_current_state, input);
00288         }
00289     }
00290 
00298     inline bool hasReachedTargetState() const
00299     {
00300         return _current_state == _end_state;
00301     }
00302 };
00303 
00304 #ifndef SGDK_DOX
00305 template <typename StateInputMatrix, typename Index>
00306 template <typename IndexTypeFSMBuilder>
00307 #endif  /* SGDK_DOX */
00308 void
00309     IndexTypeFSM<
00310 #ifndef SGDK_DOX
00311         StateInputMatrix
00312       , Index
00313 #endif  /* SGDK_DOX */
00314     >::initialize(const IndexTypeFSMBuilder& fsm_builder)
00315 {
00316     fsm_builder.buildTransitionFunction(_transition_function);
00317     _start_state = fsm_builder.getSourceState();
00318     _current_state = _start_state;
00319     _end_state = fsm_builder.getTargetState();
00320 }
00321 
00322 #ifndef SGDK_DOX
00323 template <typename StateInputMatrix, typename Index>
00324 template <typename BackInsertionSequence>
00325 #endif  /* SGDK_DOX */
00326 void
00327     IndexTypeFSM<
00328 #ifndef SGDK_DOX
00329         StateInputMatrix
00330       , Index
00331 #endif  /* SGDK_DOX */
00332     >::makeNontrivialInputs(BackInsertionSequence& sequence) const
00333 {
00334     for (
00335         Index input = 0;
00336         input < _transition_function.getInputCount();
00337         ++input
00338     )
00339     {
00340         if (_current_state != _transition_function(_current_state, input))
00341         {
00342             sequence.push_back(input);
00343         }
00344     }
00345 }
00346 }  // namespace sgdk
00347 
00348 #endif  /* SGDK_AI_FSM_BASE_HPP */

Swiss GD Knife is hosted by SourceForge.net.