appkit  1.5.1
RoadNarrows Robotics Application Kit
rnr::State Class Reference

#include <State.h>

Inheritance diagram for rnr::State:
rnr::StateCvCamera rnr::StateGstCamera rnr::StateImage rnr::StateKb rnr::StateWin rnr::StateWinCamera

Public Types

typedef std::map< int, StateEvent * > TransTblMap_T
 transition table type
 

Public Member Functions

 State (int nStateId, const std::string &strStateName="", const std::string &strRefTag="")
 Initialization constructor. More...
 
 State (int nStateId, const std::string &strStateName, const std::string &strRefTag, StateEvent *listStateEvents[])
 List constructor. More...
 
virtual ~State ()
 Destructor.
 
int addStateEvents (StateEvent *pStateEvent,...)
 Add a list of state events to this state. More...
 
void addStateEvent (StateEvent *pStateEvent)
 Add state to this state. More...
 
bool deleteStateEvent (int nEventId)
 Delete state event from this state. More...
 
uint_t setTimeOut (uint_t usecTimeOut)
 Set recieve event time out. More...
 
uint_t getTimeOut ()
 Get recieve event time out. More...
 
virtual int receiveEvent ()
 Receive next event. More...
 
virtual void queueNextEvent (int nEventId)
 Queue the next event to be "recieved". More...
 
virtual int dispatchEvent (int nPrevStateId, int nEventId)
 Dispatch received event by executing associated action and transitioning to the next state. More...
 
virtual void actionEnterState (int nPrevStateId, int nEventId)
 Execute 'enter state' action. More...
 
virtual void actionExitState (int nNextStateId, int nEventId)
 Execute 'exit state' action. More...
 
virtual int actionDefault (int nPrevStateId, int nEventId)
 Execute 'default' action. More...
 
int getStateId () const
 Get state id. More...
 
std::string getStateName () const
 Get state name. More...
 
std::string getRefTag () const
 Get state reference tag. More...
 
std::string getEventName (int nEventId)
 Get state event name. More...
 
std::string getActionName (int nEventId)
 Get state action name. More...
 
virtual void printState (FILE *fp=stdout, int indent=0)
 Print out state. More...
 
 State (Session &session, int nInitAction=UIActionNone)
 
virtual int getNextAction (Session &session, int nTimeoutMs)
 
virtual int setNextAction (int nAction)
 Set the next input action to process. More...
 
int getCurAction ()
 Get the currently retrieved input action. More...
 
bool hasFatal ()
 Test if state has fatal condition. More...
 
void setFatal ()
 
bool isModified ()
 
void setModifiedState (bool bModified)
 
void maskHardButton (bool bEnDis)
 Enable/disable masking of hard button input. More...
 
virtual bool isHardButtonPushed (Session &session)
 
virtual void prompt (Session &session)
 Prompt user, given the state. More...
 
virtual void setMenuStates (Session &session)
 
virtual void transQuit (Session &session)
 
virtual void transPrev (Session &session)
 

Static Public Attributes

static const int StateIdUndef = 0
 undefined state
 
static const int StateIdStart = 1
 start state
 
static const int StateIdPrev = 2
 the previous state
 
static const int StateIdThis = 3
 the current (this) state
 
static const int StateIdTerminate = 4
 terminate state
 
static const int StateIdNumOf = 5
 number of reserved state id's
 

Protected Member Functions

void init (int nStateId, const std::string &strStateName="", const std::string &strRefTag="")
 Initialize data. More...
 
virtual std::string getReservedStateName (int nStateId)
 Get the reserved state name. More...
 
virtual void buildInterface (Session &session)
 
virtual void destroyInterface ()
 
virtual void initButtonMenu ()
 

Protected Attributes

int m_nStateId
 state id
 
std::string m_strStateName
 state name
 
std::string m_strRefTag
 state tag (short id string)
 
TransTblMap_T m_mapTransTbl
 state transition table
 
int m_nQueuedEventId
 queue state event
 
bool m_bHasQueuedEvent
 [no] queued state event
 
uint_t m_usecTimeOut
 receive event time out (useconds)
 
Window * m_pWin
 bound window
 
Menu * m_pMenu
 button menu
 
int m_nInitAction
 initial state input action
 
int m_nCurAction
 current state input action
 
bool m_bFatal
 fatal error
 
bool m_bModified
 state [not] modified
 
bool m_bHardBttnEnable
 enable/disable hard button input
 

Friends

class StateEvent
 friend
 

Detailed Description

State base class.

Workflow Simplified State Machine Class

Definition at line 158 of file State.h.

Constructor & Destructor Documentation

rnr::State::State ( int  nStateId,
const std::string &  strStateName = "",
const std::string &  strRefTag = "" 
)
inline

Initialization constructor.

Parameters
nStateIdState id. Must be state machine unique.
strStateNameState name.
strRefTagState reference id.

Definition at line 180 of file State.h.

183  {
184  init(nStateId, strStateName, strRefTag);
185  }
void init(int nStateId, const std::string &strStateName="", const std::string &strRefTag="")
Initialize data.
Definition: State.cxx:318
rnr::State::State ( int  nStateId,
const std::string &  strStateName,
const std::string &  strRefTag,
StateEvent listStateEvents[] 
)

List constructor.

Parameters
nStateIdState id. Must be state machine unique.
strStateNameState name.
strRefTagState reference id.
listStateEventsDeclaration list of allocated state events. NULL terminated.

Member Function Documentation

virtual int rnr::State::actionDefault ( int  nPrevStateId,
int  nEventId 
)
inlinevirtual

Execute 'default' action.

Parameters
nPrevStateIdPrevious state id.
nEventIdReceived event id.
Returns
Returns next state (this).

Definition at line 327 of file State.h.

328  {
329  return m_nStateId;
330  }
int m_nStateId
state id
Definition: State.h:389
virtual void rnr::State::actionEnterState ( int  nPrevStateId,
int  nEventId 
)
inlinevirtual

Execute 'enter state' action.

Parameters
nPrevStateIdPrevious state id.
nEventIdReceived event id.

Reimplemented in rnr::StateWin.

Definition at line 305 of file State.h.

Referenced by rnr::StateMach::run().

306  {
307  }
virtual void rnr::State::actionExitState ( int  nNextStateId,
int  nEventId 
)
inlinevirtual

Execute 'exit state' action.

Parameters
nNextStateIdNext state id.
nEventIdReceived event id.

Reimplemented in rnr::StateWin.

Definition at line 315 of file State.h.

Referenced by rnr::StateMach::run().

316  {
317  }
void State::addStateEvent ( StateEvent pStateEvent)

Add state to this state.

This state machine owns the state and will automatically delete it when this state machine is deleted.

Parameters
pStateEventAllocated state object.

Definition at line 162 of file State.cxx.

163 {
164  TransTblMap_T::iterator pos;
165 
166  if( pStateEvent != NULL )
167  {
168  if( (pos = m_mapTransTbl.find(pStateEvent->getEventId()))
169  != m_mapTransTbl.end() )
170  {
171  LOGWARN("%s: Event %s.%d already exists - replacing.",
172  m_strStateName.c_str(),
173  pos->second->getEventName().c_str(), pos->second->getEventId());
174  delete pos->second;
175  m_mapTransTbl.erase(pos);
176  }
177 
178  m_mapTransTbl[pStateEvent->getEventId()] = pStateEvent;
179 
180  LOGDIAG3("%s: %s.%d state event added.",
181  m_strStateName.c_str(),
182  pStateEvent->getEventName().c_str(), pStateEvent->getEventId());
183  }
184 }
TransTblMap_T m_mapTransTbl
state transition table
Definition: State.h:392
std::string m_strStateName
state name
Definition: State.h:390
int State::addStateEvents ( StateEvent pStateEvent,
  ... 
)

Add a list of state events to this state.

This state owns all of the states events and will automatically delete them when this state is deleted.

Parameters
pStateEventFirst allocated state event object.
...Subsequent allocated state event object. Terminate list with NULL.
Returns
Returns the number of state events added.

Definition at line 145 of file State.cxx.

146 {
147  va_list ap;
148  int i;
149 
150  va_start(ap, pStateEvent);
151 
152  for(i=0; pStateEvent != NULL; pStateEvent = va_arg(ap, StateEvent *), ++i)
153  {
154  addStateEvent(pStateEvent);
155  }
156 
157  va_end(ap);
158 
159  return i;
160 }
void addStateEvent(StateEvent *pStateEvent)
Add state to this state.
Definition: State.cxx:162
bool State::deleteStateEvent ( int  nEventId)

Delete state event from this state.

Parameters
nEventIdEvent id.
Returns
Returns true if state event is deleted, false otherwise.

Definition at line 186 of file State.cxx.

187 {
188  TransTblMap_T::iterator pos;
189 
190  if( (pos = m_mapTransTbl.find(nEventId)) != m_mapTransTbl.end() )
191  {
192  delete pos->second;
193  m_mapTransTbl.erase(pos);
194  return true;
195  }
196  else
197  {
198  LOGERROR("%s: No event found in transition table with id=%d.",
199  m_strStateName.c_str(), nEventId);
200  return false;
201  }
202 }
TransTblMap_T m_mapTransTbl
state transition table
Definition: State.h:392
std::string m_strStateName
state name
Definition: State.h:390
int State::dispatchEvent ( int  nPrevStateId,
int  nEventId 
)
virtual

Dispatch received event by executing associated action and transitioning to the next state.

Parameters
nPrevStateIdPrevious state id.
nEventIdReceived event id.
Returns
Returns next state.

Definition at line 225 of file State.cxx.

References rnr::StateEvent::evalGuardExpr(), and rnr::StateEvent::execAction().

Referenced by rnr::StateMach::run().

226 {
227  TransTblMap_T::iterator pos;
228  StateEvent *pEvent;
229  int nNextStateId;
230 
231  //
232  // Find event in transition table and execute.
233  //
234  if( (pos = m_mapTransTbl.find(nEventId)) != m_mapTransTbl.end() )
235  {
236  pEvent = pos->second;
237 
238  // evaluate guard expression
239  if( pEvent->evalGuardExpr(this, nPrevStateId, nEventId) )
240  {
241  // execute action
242  nNextStateId = pEvent->execAction(this, nPrevStateId, nEventId);
243  }
244 
245  // evaluated to false
246  else
247  {
248  nNextStateId = m_nStateId;
249  }
250  }
251 
252  //
253  // No event in transition table, so execute default action.
254  //
255  else
256  {
257  nNextStateId = actionDefault(nPrevStateId, nEventId);
258  }
259 
260  // stay is this state
261  if( nNextStateId == StateIdThis )
262  {
263  nNextStateId = m_nStateId;
264  }
265 
266  return nNextStateId;
267 }
virtual int actionDefault(int nPrevStateId, int nEventId)
Execute &#39;default&#39; action.
Definition: State.h:327
virtual int execAction(State *pState, int nPrevStateId, int nEventId)
Execute action associated with the event.
Definition: State.cxx:107
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
int m_nStateId
state id
Definition: State.h:389
static const int StateIdThis
the current (this) state
Definition: State.h:169
string State::getActionName ( int  nEventId)

Get state action name.

Parameters
nEventIdEvent id.
Returns
String.

Definition at line 283 of file State.cxx.

284 {
285  TransTblMap_T::iterator pos;
286 
287  if( (pos = m_mapTransTbl.find(nEventId)) != m_mapTransTbl.end() )
288  {
289  return pos->second->getActionName();
290  }
291  else
292  {
293  return "";
294  }
295 }
TransTblMap_T m_mapTransTbl
state transition table
Definition: State.h:392
int rnr::State::getCurAction ( )
inline

Get the currently retrieved input action.

Returns
Current action.

Definition at line 128 of file State.orig.h.

References m_nCurAction.

129  {
130  return m_nCurAction;
131  }
int m_nCurAction
current state input action
Definition: State.orig.h:196
string State::getEventName ( int  nEventId)

Get state event name.

Parameters
nEventIdEvent id.
Returns
String.

Definition at line 269 of file State.cxx.

270 {
271  TransTblMap_T::iterator pos;
272 
273  if( (pos = m_mapTransTbl.find(nEventId)) != m_mapTransTbl.end() )
274  {
275  return pos->second->getEventName();
276  }
277  else
278  {
279  return "";
280  }
281 }
TransTblMap_T m_mapTransTbl
state transition table
Definition: State.h:392
std::string rnr::State::getRefTag ( ) const
inline

Get state reference tag.

Returns
String.

Definition at line 357 of file State.h.

References rnr::io::indent().

358  {
359  return m_strRefTag;
360  }
std::string m_strRefTag
state tag (short id string)
Definition: State.h:391
string State::getReservedStateName ( int  nStateId)
protectedvirtual

Get the reserved state name.

Parameters
nStateIdState id.
Returns
String.

Definition at line 352 of file State.cxx.

353 {
354  switch( nStateId )
355  {
356  case StateIdStart:
357  return "*Start";
358  case StateIdPrev:
359  return "Previous"; // if recording, then ...
360  case StateIdThis:
361  return m_strStateName;
362  case StateIdTerminate:
363  return "Terminate*";
364  case StateIdUndef:
365  return "#undef";
366  default:
367  return "";
368  }
369 }
static const int StateIdStart
start state
Definition: State.h:167
static const int StateIdUndef
undefined state
Definition: State.h:166
static const int StateIdTerminate
terminate state
Definition: State.h:170
static const int StateIdThis
the current (this) state
Definition: State.h:169
std::string m_strStateName
state name
Definition: State.h:390
static const int StateIdPrev
the previous state
Definition: State.h:168
int rnr::State::getStateId ( ) const
inline

Get state id.

Returns
State id.

Definition at line 337 of file State.h.

Referenced by rnr::StateMach::addState().

338  {
339  return m_nStateId;
340  }
int m_nStateId
state id
Definition: State.h:389
std::string rnr::State::getStateName ( ) const
inline

Get state name.

Returns
String.

Definition at line 347 of file State.h.

Referenced by rnr::StateMach::addState().

348  {
349  return m_strStateName;
350  }
std::string m_strStateName
state name
Definition: State.h:390
uint_t rnr::State::getTimeOut ( )
inline

Get recieve event time out.

Returns
Returns time out value.

Definition at line 259 of file State.h.

260  {
261  return m_usecTimeOut;
262  }
uint_t m_usecTimeOut
receive event time out (useconds)
Definition: State.h:395
bool rnr::State::hasFatal ( )
inline

Test if state has fatal condition.

Returns
Returns true or false.

Definition at line 138 of file State.orig.h.

References m_bFatal, and m_bModified.

139  {
140  return m_bFatal;
141  }
bool m_bFatal
fatal error
Definition: State.orig.h:197
void State::init ( int  nStateId,
const std::string &  strStateName = "",
const std::string &  strRefTag = "" 
)
protected

Initialize data.

Parameters
nStateIdState id. Must be state machine unique.
strStateNameState name.
strRefTagState reference id.

Definition at line 318 of file State.cxx.

322 {
323  m_nStateId = nStateId;
324  m_bHasQueuedEvent = false;
325  m_usecTimeOut = 0;
326 
327  if( strStateName.empty() )
328  {
329  stringstream ss;
330 
331  ss << "S" << m_nStateId;
332  ss >> m_strStateName;
333  }
334  else
335  {
336  m_strStateName = strStateName;
337  }
338 
339  if( strRefTag.empty() )
340  {
341  stringstream ss;
342 
343  ss << m_nStateId;
344  ss >> m_strRefTag;
345  }
346  else
347  {
348  m_strRefTag = strRefTag;
349  }
350 }
std::string m_strRefTag
state tag (short id string)
Definition: State.h:391
uint_t m_usecTimeOut
receive event time out (useconds)
Definition: State.h:395
int m_nStateId
state id
Definition: State.h:389
bool m_bHasQueuedEvent
[no] queued state event
Definition: State.h:394
std::string m_strStateName
state name
Definition: State.h:390
void rnr::State::maskHardButton ( bool  bEnDis)
inline

Enable/disable masking of hard button input.

Parameters
bEnDisEnable (true) or disable (false) reading hard button input.

Definition at line 163 of file State.orig.h.

References m_bHardBttnEnable.

164  {
165  m_bHardBttnEnable = bEnDis;
166  }
bool m_bHardBttnEnable
enable/disable hard button input
Definition: State.orig.h:199
void State::printState ( FILE *  fp = stdout,
int  indent = 0 
)
virtual

Print out state.

Parameters
fpOutput file pointer.
indentLeft indentation.

Definition at line 298 of file State.cxx.

299 {
300  TransTblMap_T::iterator iter;
301  StateEvent *pEvent;
302 
303  fprintf(fp, "%*sState: %s.%d\n",
304  indent, "", m_strStateName.c_str(), m_nStateId);
305 
306  for(iter = m_mapTransTbl.begin();
307  iter != m_mapTransTbl.end();
308  ++iter )
309  {
310  pEvent = iter->second;
311  fprintf(fp, "%*sEvent: %s.%d %s\n",
312  indent+2, "",
313  pEvent->getEventName().c_str(), pEvent->getEventId(),
314  pEvent->getActionName().c_str());
315  }
316 }
osManipIndent indent()
Left indent at current stream indentation level.
Definition: IOManip.cxx:115
TransTblMap_T m_mapTransTbl
state transition table
Definition: State.h:392
int m_nStateId
state id
Definition: State.h:389
std::string m_strStateName
state name
Definition: State.h:390
virtual void rnr::State::prompt ( Session session)
inlinevirtual

Prompt user, given the state.

Parameters
sessionSession data.

Definition at line 175 of file State.orig.h.

175 { }
virtual void rnr::State::queueNextEvent ( int  nEventId)
inlinevirtual

Queue the next event to be "recieved".

Subsequent call to receiveEvent() should retrieve this event rather than doing the state's standard receive operations.

Definition at line 282 of file State.h.

283  {
284  m_nQueuedEventId = nEventId;
285  m_bHasQueuedEvent = true;
286  }
int m_nQueuedEventId
queue state event
Definition: State.h:393
bool m_bHasQueuedEvent
[no] queued state event
Definition: State.h:394
int State::receiveEvent ( )
virtual

Receive next event.

Randomly chooses an event id listed in the transition table.

The receive event may set state internal variables used specifically by the state.

Returns
Returns event id.

Reimplemented in rnr::StateKb, and rnr::StateWin.

Definition at line 204 of file State.cxx.

References rnr::Random::randrange().

Referenced by rnr::StateMach::run().

205 {
206  if( m_mapTransTbl.size() > 0 )
207  {
208  TransTblMap_T::iterator pos;
209  Random rand;
210  int i;
211 
212  i = rand.randrange(0, (int)m_mapTransTbl.size()-1);
213 
214  // no '+' or '+=' operator - so crawl through map
215  for(pos = m_mapTransTbl.begin(); i > 0; --i, ++pos);
216 
217  return pos->second->getEventId();
218  }
219  else
220  {
221  return 0;
222  }
223 }
TransTblMap_T m_mapTransTbl
state transition table
Definition: State.h:392
int randrange(int nMin=0, int nMax=INT_MAX)
Generates a random integer uniformally distrubuted between [nMin, nMax].
Definition: Random.cxx:88
Random variable generators class.
Definition: Random.h:79
virtual int rnr::State::setNextAction ( int  nAction)
inlinevirtual

Set the next input action to process.

Subsequent call to getNextAction() will retrieve this action.

Returns
The to-be-retrieved next action.

Definition at line 115 of file State.orig.h.

References m_pMenu.

116  {
117  if( m_pMenu != NULL )
118  {
119  m_pMenu->SetCurrentAction(nAction);
120  }
121  }
Menu * m_pMenu
button menu
Definition: State.orig.h:194
uint_t rnr::State::setTimeOut ( uint_t  usecTimeOut)
inline

Set recieve event time out.

Parameters
usecTimeOutTime out in seconds. Set to 0 for blocking until event with no time out.
Returns
Returns last time out value.

Definition at line 247 of file State.h.

248  {
249  uint_t usecLast = m_usecTimeOut;
250  m_usecTimeOut = usecTimeOut;
251  return usecLast;
252  }
uint_t m_usecTimeOut
receive event time out (useconds)
Definition: State.h:395

The documentation for this class was generated from the following files: