appkit  1.5.1
RoadNarrows Robotics Application Kit
State.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: State.h
10 //
11 /*! \file
12  *
13  * $LastChangedDate: 2013-05-06 10:03:14 -0600 (Mon, 06 May 2013) $
14  * $Rev: 2907 $
15  *
16  * \brief State base class 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_H
58 #define _RNR_STATE_H
59 
60 #include <stdio.h>
61 
62 #include <string>
63 #include <map>
64 
65 #include "rnr/rnrconfig.h"
66 
67 /*!
68  * \brief RoadNarrows Robotics
69  */
70 namespace rnr
71 {
72  //
73  // Forward declarations
74  //
75  class State;
76 
77 
78  //----------------------------------------------------------------------------
79  // StateEvent Class
80  //----------------------------------------------------------------------------
81 
82  class StateEvent
83  {
84  public:
85  /*!
86  * \brief Initialization constructor.
87  *
88  * \param nEventId Event id.
89  * \param nEventName Event name.
90  * \param nActionName Action name.
91  */
92  StateEvent(int nEventId,
93  const std::string &strEventName="",
94  const std::string &strActionName="");
95 
96  virtual ~StateEvent()
97  {
98  }
99 
100  /*!
101  * \brief Evaluate event guard condition.
102  *
103  * \param pState Associated state.
104  * \param nPrevStateId Previous state id.
105  * \param nEventId Received event id.
106  *
107  * \return Returns true if event is accepted, otherwise returns false.
108  */
109  virtual bool evalGuardExpr(State *pState,
110  int nPrevStateId,
111  int nEventId)
112  {
113  return true;
114  }
115 
116  /*!
117  * \brief Execute action associated with the event.
118  *
119  * \param pState Associated state.
120  * \param nPrevStateId Previous state id.
121  * \param nEventId Received event id.
122  *
123  * \return Returns next state.
124  */
125  virtual int execAction(State *pState,
126  int nPrevStateId,
127  int nEventId);
128 
129  int getEventId() const
130  {
131  return m_nEventId;
132  }
133 
134  std::string getEventName() const
135  {
136  return m_strEventName;
137  }
138 
139  std::string getActionName() const
140  {
141  return m_strActionName;
142  }
143 
144  protected:
145  int m_nEventId;
146  std::string m_strEventName;
147  std::string m_strActionName;
148  };
149 
150 
151  //----------------------------------------------------------------------------
152  // State Class
153  //----------------------------------------------------------------------------
154 
155  /*!
156  * State base class.
157  */
158  class State
159  {
160  public:
161  typedef std::map<int,StateEvent*> TransTblMap_T; ///< transition table type
162 
163  //
164  // Specical reserved state ids.
165  //
166  static const int StateIdUndef = 0; ///< undefined state
167  static const int StateIdStart = 1; ///< start state
168  static const int StateIdPrev = 2; ///< the previous state
169  static const int StateIdThis = 3; ///< the current (this) state
170  static const int StateIdTerminate = 4; ///< terminate state
171  static const int StateIdNumOf = 5; ///< number of reserved state id's
172 
173  /*!
174  * \brief Initialization constructor.
175  *
176  * \param nStateId State id. Must be state machine unique.
177  * \param strStateName State name.
178  * \param strRefTag State reference id.
179  */
180  State(int nStateId,
181  const std::string &strStateName="",
182  const std::string &strRefTag="")
183  {
184  init(nStateId, strStateName, strRefTag);
185  }
186 
187  /*!
188  * \brief List constructor.
189  *
190  * \param nStateId State id. Must be state machine unique.
191  * \param strStateName State name.
192  * \param strRefTag State reference id.
193  * \param listStateEvents Declaration list of allocated state events.
194  * NULL terminated.
195  */
196  State(int nStateId,
197  const std::string &strStateName,
198  const std::string &strRefTag,
199  StateEvent *listStateEvents[]);
200 
201  /*!
202  * \brief Destructor.
203  */
204  virtual ~State();
205 
206  /*!
207  * \brief Add a list of state events to this state.
208  *
209  * This state owns all of the states events and will automatically delete
210  * them when this state is deleted.
211  *
212  * \param pStateEvent First allocated state event object.
213  * \param ... Subsequent allocated state event object.
214  * Terminate list with NULL.
215  *
216  * \return Returns the number of state events added.
217  */
218  int addStateEvents(StateEvent *pStateEvent, ...);
219 
220  /*!
221  * \brief Add state to this state.
222  *
223  * This state machine owns the state and will automatically delete it when
224  * this state machine is deleted.
225  *
226  * \param pStateEvent Allocated state object.
227  */
228  void addStateEvent(StateEvent *pStateEvent);
229 
230  /*!
231  * \brief Delete state event from this state.
232  *
233  * \param nEventId Event id.
234  *
235  * \return Returns true if state event is deleted, false otherwise.
236  */
237  bool deleteStateEvent(int nEventId);
238 
239  /*!
240  * \brief Set recieve event time out.
241  *
242  * \param usecTimeOut Time out in \h_mu seconds. Set to 0 for blocking
243  * until event with no time out.
244  *
245  * \return Returns last time out value.
246  */
247  uint_t setTimeOut(uint_t usecTimeOut)
248  {
249  uint_t usecLast = m_usecTimeOut;
250  m_usecTimeOut = usecTimeOut;
251  return usecLast;
252  }
253 
254  /*!
255  * \brief Get recieve event time out.
256  *
257  * \return Returns time out value.
258  */
259  uint_t getTimeOut()
260  {
261  return m_usecTimeOut;
262  }
263 
264  /*!
265  * \brief Receive next event.
266  *
267  * Randomly chooses an event id listed in the transition table.
268  *
269  * The receive event may set state internal variables used specifically by
270  * the state.
271  *
272  * \return Returns event id.
273  */
274  virtual int receiveEvent();
275 
276  /*!
277  * \brief Queue the next event to be "recieved".
278  *
279  * Subsequent call to receiveEvent() should retrieve this event rather
280  * than doing the state's standard receive operations.
281  */
282  virtual void queueNextEvent(int nEventId)
283  {
284  m_nQueuedEventId = nEventId;
285  m_bHasQueuedEvent = true;
286  }
287 
288  /*!
289  * \brief Dispatch received event by executing associated action and
290  * transitioning to the next state.
291  *
292  * \param nPrevStateId Previous state id.
293  * \param nEventId Received event id.
294  *
295  * \return Returns next state.
296  */
297  virtual int dispatchEvent(int nPrevStateId, int nEventId);
298 
299  /*!
300  * \brief Execute 'enter state' action.
301  *
302  * \param nPrevStateId Previous state id.
303  * \param nEventId Received event id.
304  */
305  virtual void actionEnterState(int nPrevStateId, int nEventId)
306  {
307  }
308 
309  /*!
310  * \brief Execute 'exit state' action.
311  *
312  * \param nNextStateId Next state id.
313  * \param nEventId Received event id.
314  */
315  virtual void actionExitState(int nNextStateId, int nEventId)
316  {
317  }
318 
319  /*!
320  * \brief Execute 'default' action.
321  *
322  * \param nPrevStateId Previous state id.
323  * \param nEventId Received event id.
324  *
325  * \return Returns next state (this).
326  */
327  virtual int actionDefault(int nPrevStateId, int nEventId)
328  {
329  return m_nStateId;
330  }
331 
332  /*!
333  * \brief Get state id.
334  *
335  * \return State id.
336  */
337  int getStateId() const
338  {
339  return m_nStateId;
340  }
341 
342  /*!
343  * \brief Get state name.
344  *
345  * \return String.
346  */
347  std::string getStateName() const
348  {
349  return m_strStateName;
350  }
351 
352  /*!
353  * \brief Get state reference tag.
354  *
355  * \return String.
356  */
357  std::string getRefTag() const
358  {
359  return m_strRefTag;
360  }
361 
362  /*!
363  * \brief Get state event name.
364  *
365  * \param nEventId Event id.
366  *
367  * \return String.
368  */
369  std::string getEventName(int nEventId);
370 
371  /*!
372  * \brief Get state action name.
373  *
374  * \param nEventId Event id.
375  *
376  * \return String.
377  */
378  std::string getActionName(int nEventId);
379 
380  /*!
381  * \brief Print out state.
382  *
383  * \param fp Output file pointer.
384  * \param indent Left indentation.
385  */
386  virtual void printState(FILE *fp=stdout, int indent=0);
387 
388  protected:
389  int m_nStateId; ///< state id
390  std::string m_strStateName; ///< state name
391  std::string m_strRefTag; ///< state tag (short id string)
392  TransTblMap_T m_mapTransTbl; ///< state transition table
393  int m_nQueuedEventId; ///< queue state event
394  bool m_bHasQueuedEvent; ///< [no] queued state event
395  uint_t m_usecTimeOut; ///< receive event time out (useconds)
396 
397  friend class StateEvent; ///< friend
398 
399  /*!
400  * \brief Initialize data.
401  *
402  * \param nStateId State id. Must be state machine unique.
403  * \param strStateName State name.
404  * \param strRefTag State reference id.
405  */
406  void init(int nStateId,
407  const std::string &strStateName="",
408  const std::string &strRefTag="");
409 
410  /*!
411  * \brief Get the reserved state name.
412  *
413  * \param nStateId State id.
414  *
415  * \return String.
416  */
417  virtual std::string getReservedStateName(int nStateId);
418  };
419 
420 } // namespace rnr
421 
422 
423 #endif // _RNR_STATE_H
virtual int actionDefault(int nPrevStateId, int nEventId)
Execute &#39;default&#39; action.
Definition: State.h:327
virtual void actionEnterState(int nPrevStateId, int nEventId)
Execute &#39;enter state&#39; action.
Definition: State.h:305
osManipIndent indent()
Left indent at current stream indentation level.
Definition: IOManip.cxx:115
uint_t setTimeOut(uint_t usecTimeOut)
Set recieve event time out.
Definition: State.h:247
uint_t getTimeOut()
Get recieve event time out.
Definition: State.h:259
virtual int execAction(State *pState, int nPrevStateId, int nEventId)
Execute action associated with the event.
Definition: State.cxx:107
int getStateId() const
Get state id.
Definition: State.h:337
std::string m_strRefTag
state tag (short id string)
Definition: State.h:391
TransTblMap_T m_mapTransTbl
state transition table
Definition: State.h:392
virtual bool evalGuardExpr(State *pState, int nPrevStateId, int nEventId)
Evaluate event guard condition.
Definition: State.h:109
std::map< int, StateEvent * > TransTblMap_T
transition table type
Definition: State.h:161
StateEvent(int nEventId, const std::string &strEventName="", const std::string &strActionName="")
Initialization constructor.
Definition: State.cxx:76
std::string getRefTag() const
Get state reference tag.
Definition: State.h:357
virtual void queueNextEvent(int nEventId)
Queue the next event to be "recieved".
Definition: State.h:282
State(int nStateId, const std::string &strStateName="", const std::string &strRefTag="")
Initialization constructor.
Definition: State.h:180
uint_t m_usecTimeOut
receive event time out (useconds)
Definition: State.h:395
int m_nQueuedEventId
queue state event
Definition: State.h:393
int m_nStateId
state id
Definition: State.h:389
std::string getStateName() const
Get state name.
Definition: State.h:347
bool m_bHasQueuedEvent
[no] queued state event
Definition: State.h:394
std::string m_strStateName
state name
Definition: State.h:390
RoadNarrows Robotics.
Definition: Camera.h:74
virtual void actionExitState(int nNextStateId, int nEventId)
Execute &#39;exit state&#39; action.
Definition: State.h:315