appkit  1.5.1
RoadNarrows Robotics Application Kit
StateMach.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: RoadNarrows Robotics Application Tool Kit
4 //
5 // Link: https://github.com/roadnarrows-robotics/rnr-sdk
6 //
7 // Library: librnr_appkit
8 //
9 // File: StateMach.h
10 //
11 /*! \file
12  *
13  * $LastChangedDate: 2013-05-06 10:03:14 -0600 (Mon, 06 May 2013) $
14  * $Rev: 2907 $
15  *
16  * \brief Finite State Machine interface.
17  *
18  * \author Robin Knight (robin.knight@roadnarrows.com)
19  * \author Daniel Packard (daniel@roadnarrows.com)
20  *
21  * \par Copyright
22  * \h_copy 2012-2017. RoadNarrows LLC.\n
23  * http://www.roadnarrows.com\n
24  * All Rights Reserved
25  */
26 /*
27  * @EulaBegin@
28  *
29  * Permission is hereby granted, without written agreement and without
30  * license or royalty fees, to use, copy, modify, and distribute this
31  * software and its documentation for any purpose, provided that
32  * (1) The above copyright notice and the following two paragraphs
33  * appear in all copies of the source code and (2) redistributions
34  * including binaries reproduces these notices in the supporting
35  * documentation. Substantial modifications to this software may be
36  * copyrighted by their authors and need not follow the licensing terms
37  * described here, provided that the new terms are clearly indicated in
38  * all files where they apply.
39  *
40  * IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
41  * OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
42  * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
43  * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
44  * EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
45  * THE POSSIBILITY OF SUCH DAMAGE.
46  *
47  * THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
48  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
49  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
50  * "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
51  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
52  *
53  * @EulaEnd@
54  */
55 ////////////////////////////////////////////////////////////////////////////////
56 
57 #ifndef _RNR_STATE_MACH_H
58 #define _RNR_STATE_MACH_H
59 
60 #include <stdio.h>
61 
62 #include <string>
63 #include <map>
64 #include <vector>
65 
66 #include "rnr/rnrconfig.h"
67 
68 #include "rnr/appkit/State.h"
69 
70 
71 /*!
72  * \brief RoadNarrows Robotics
73  */
74 namespace rnr
75 {
76  //----------------------------------------------------------------------------
77  // StateMach Class
78  //----------------------------------------------------------------------------
79 
80  /*!
81  * \brief Finite State Machine Class.
82  */
83  class StateMach
84  {
85  public:
86  typedef std::map<int,State*> StateTblMap_T; ///< state table type
87 
88  static const size_t NO_LIMIT = 0; ///< no limit on length of recording
89 
90  /*!
91  * \brief Default contructor.
92  *
93  * \param nStateMachId State machine id.
94  * \param strStateMachName State machine name.
95  */
96  StateMach(int nStateMachId = 0, const std::string &strStateMachName = "sm");
97 
98  /*!
99  * \brief List contructor.
100  *
101  * This state machine owns all of the states and will automatically delete
102  * them when this state machine is deleted.
103  *
104  * \param nStateMachId State machine id.
105  * \param strStateMachName State machine name.
106  * \param nStartStateId State id.
107  * \param listStates Declaration list of allocated states.
108  * NULL terminated.
109  */
110  StateMach(int nStateMachId,
111  const std::string &strStateMachName,
112  int nStartStateId,
113  State *listStates[]);
114 
115  /*!
116  * \brief Destructor.
117  */
118  virtual ~StateMach();
119 
120  /*!
121  * \brief Add a list of states to this state machine.
122  *
123  * This state machine owns all of the states and will automatically delete
124  * them when this state machine is deleted.
125  *
126  * \param pState First allocated state object.
127  * \param ... Subsequent allocated state object. Terminate list with
128  * NULL.
129  *
130  * \return Returns the number of states added.
131  */
132  int addStates(State *pState, ...);
133 
134  /*!
135  * \brief Add state to this state machine.
136  *
137  * This state machine owns the state and will automatically delete it when
138  * this state machine is deleted.
139  *
140  * \param pState Allocated state object.
141  */
142  void addState(State *pState);
143 
144  /*!
145  * \brief Delete state from this state machine.
146  *
147  * \param nStateId State id.
148  *
149  * \return Returns true if state is deleted, false otherwise.
150  */
151  bool deleteState(int nStateId);
152 
153  /*!
154  * \brief Set state machine's start state id.
155  *
156  * \param nStartStateId State id.
157  */
158  void setStartStateId(int nStartStateId)
159  {
160  m_nStartStateId = nStartStateId;
161  }
162 
163  /*!
164  * \brief Run the state machine from the start.
165  *
166  * This function does not return until the terminate state is reached.
167  */
168  virtual void run();
169 
170  /*!
171  * \brief Start recording states ids.
172  *
173  * Any previous recording is erased.
174  *
175  * Typical uses for recording states are work flows, in which the previous
176  * states may need backtracing, given a previous event.
177  *
178  * \param uLimit Limit the length of the recording.
179  */
180  virtual void startRecording(size_t uLimit = NO_LIMIT)
181  {
182  m_stackStateIds.clear();
183  m_uMaxRecording = uLimit;
184  m_bIsRecording = true;
185  }
186 
187  /*!
188  * \brief Stop recording of states ids.
189  *
190  * The recording is not erased.
191  */
192  virtual void stopRecording()
193  {
194  m_bIsRecording = false;
195  }
196 
197  /*!
198  * \brief Resume recording of states ids.
199  *
200  * The recording is not erased.
201  */
202  virtual void resumeRecording()
203  {
204  m_bIsRecording = true;
205  }
206 
207  /*!
208  * \brief Erase recording of states ids.
209  *
210  * The recording start/stop state is not changed.
211  */
212  virtual void eraseRecording()
213  {
214  m_stackStateIds.clear();
215  }
216 
217  /*!
218  * \brief Get state machine id.
219  *
220  * \return State machine id.
221  */
222  int getStateMachId() const
223  {
224  return m_nStateMachId;
225  }
226 
227  /*!
228  * \brief Get state machine name.
229  *
230  * \return String.
231  */
232  std::string getStateMachName() const
233  {
234  return m_strStateMachName;
235  }
236 
237  /*!
238  * \brief Get the current state id.
239  *
240  * \return State id.
241  */
242  int getCurrStateId() const
243  {
244  return m_nCurrStateId;
245  }
246 
247  /*!
248  * \brief Get the previously recorded state id.
249  *
250  * State id recording must be enabled. The previous state id never equals
251  * the current state id.
252  *
253  * \return
254  * If recording is enabled and there are elements on the stack, then the
255  * state id found on the top of the stack is returned.\n
256  * Otherwise \ref State::StateIdUndef is returned.
257  */
258  int getPrevStateId() const
259  {
260  if( m_bIsRecording && (m_stackStateIds.size() > 0) )
261  {
262  return m_stackStateIds[m_stackStateIds.size()-1];
263  }
264  else
265  {
266  return State::StateIdUndef;
267  }
268  }
269 
270  /*!
271  * \brief Print out state machine.
272  *
273  * \param fp Output file pointer.
274  * \param indent Left indentation.
275  */
276  virtual void printStateMach(FILE *fp=stdout, int indent=0);
277 
278  protected:
279  int m_nStateMachId; ///< state machine id
280  std::string m_strStateMachName; ///< state machine name
281  StateTblMap_T m_mapStateTbl; ///< state table
282  int m_nStartStateId; ///< starting state id
283  int m_nCurrStateId; ///< current state id
284  int m_nMarkStateId; ///< mark state id of last run cycle
285  std::vector<int> m_stackStateIds; ///< stack of previous state ids
286  size_t m_uMaxRecording; ///< max recording length
287  bool m_bIsRecording; ///< is [not] recording work flow
288 
289  /*!
290  * \brief Push state id onto stack of state ids.
291  *
292  * The stack preserves the state workflow history.
293  *
294  * \note No consecutive states with the same id are pushed onto the stack.
295  * That is, graph path cylces of length 1 are not recorded. However, cycle
296  * lengths \h_gt 1 are not detected and, so, could be recorded. For example,
297  * the state sequence a b a b (length == 2).
298  *
299  * \param nStateId State id to be pushed.
300  */
301  void pushStateId(int nStateid);
302 
303  /*!
304  * \brief Pop state id from stack of state ids.
305  *
306  * \return
307  * If recording is enabled and there are elements on the stack, then the
308  * state id found on the top of the stack is returned.\n
309  * Otherwise \ref State::StateIdUndef is returned.
310  */
311  int popStateId();
312 
313  /*!
314  * \brief Log state transition.
315  *
316  * \todo Moderate logging.
317  *
318  * \param nCurrStateId Current state id.
319  * \param nEventId Received event id.
320  * \param nNextStateId Next (new) state id.
321  */
322  virtual void logTransition(int nCurrStateId,
323  int nEventId,
324  int nNextStateId);
325  };
326 
327 } // namespace rnr
328 
329 
330 #endif // _RNR_STATE_MACH_H
virtual void startRecording(size_t uLimit=NO_LIMIT)
Start recording states ids.
Definition: StateMach.h:180
std::string m_strStateMachName
state machine name
Definition: StateMach.h:280
int m_nMarkStateId
mark state id of last run cycle
Definition: StateMach.h:284
void pushStateId(int nStateid)
Push state id onto stack of state ids.
Definition: StateMach.cxx:265
std::vector< int > m_stackStateIds
stack of previous state ids
Definition: StateMach.h:285
osManipIndent indent()
Left indent at current stream indentation level.
Definition: IOManip.cxx:115
bool deleteState(int nStateId)
Delete state from this state machine.
Definition: StateMach.cxx:174
int getCurrStateId() const
Get the current state id.
Definition: StateMach.h:242
virtual void resumeRecording()
Resume recording of states ids.
Definition: StateMach.h:202
int m_nCurrStateId
current state id
Definition: StateMach.h:283
virtual void printStateMach(FILE *fp=stdout, int indent=0)
Print out state machine.
Definition: StateMach.cxx:303
virtual void run()
Run the state machine from the start.
Definition: StateMach.cxx:192
virtual ~StateMach()
Destructor.
Definition: StateMach.cxx:122
int m_nStartStateId
starting state id
Definition: StateMach.h:282
bool m_bIsRecording
is [not] recording work flow
Definition: StateMach.h:287
void setStartStateId(int nStartStateId)
Set state machine&#39;s start state id.
Definition: StateMach.h:158
int addStates(State *pState,...)
Add a list of states to this state machine.
Definition: StateMach.cxx:134
static const int StateIdUndef
undefined state
Definition: State.h:166
virtual void stopRecording()
Stop recording of states ids.
Definition: StateMach.h:192
int getPrevStateId() const
Get the previously recorded state id.
Definition: StateMach.h:258
virtual void eraseRecording()
Erase recording of states ids.
Definition: StateMach.h:212
std::map< int, State * > StateTblMap_T
state table type
Definition: StateMach.h:86
int m_nStateMachId
state machine id
Definition: StateMach.h:279
StateMach(int nStateMachId=0, const std::string &strStateMachName="sm")
Default contructor.
State base class interface.
Finite State Machine Class.
Definition: StateMach.h:83
StateTblMap_T m_mapStateTbl
state table
Definition: StateMach.h:281
int getStateMachId() const
Get state machine id.
Definition: StateMach.h:222
void addState(State *pState)
Add state to this state machine.
Definition: StateMach.cxx:151
std::string getStateMachName() const
Get state machine name.
Definition: StateMach.h:232
RoadNarrows Robotics.
Definition: Camera.h:74
virtual void logTransition(int nCurrStateId, int nEventId, int nNextStateId)
Log state transition.
Definition: StateMach.cxx:316
static const size_t NO_LIMIT
no limit on length of recording
Definition: StateMach.h:88
int popStateId()
Pop state id from stack of state ids.
Definition: StateMach.cxx:287
size_t m_uMaxRecording
max recording length
Definition: StateMach.h:286