61 #include "rnr/rnrconfig.h" 78 #define LOGTRANSITION(nCurrStateId, nEventId, nNextStateId) \ 81 if( LOGABLE(LOG_LEVEL_DIAG3) ) \ 83 logTransition(nCurrStateId, nEventId, nNextStateId); \ 92 StateMach::StateMach(
int nStateMachId,
const string &strStateMachName) :
93 m_strStateMachName(strStateMachName)
95 m_nStateMachId = nStateMachId;
96 m_nStartStateId = State::StateIdStart;
97 m_nCurrStateId = State::StateIdUndef;
98 m_nMarkStateId = State::StateIdUndef;
99 m_bIsRecording =
false;
102 StateMach::StateMach(
int nStateMachId,
103 const string &strStateMachName,
105 State *listStates[]) :
106 m_strStateMachName(strStateMachName)
110 m_nStateMachId = nStateMachId;
111 m_nStartStateId = nStartStateId;
112 m_nCurrStateId = State::StateIdUndef;
113 m_nMarkStateId = State::StateIdUndef;
114 m_bIsRecording =
false;
116 for(i=0; listStates[i] != NULL; ++i)
118 addState(listStates[i]);
122 StateMach::~StateMach()
124 StateTblMap_T::iterator iter;
139 va_start(ap, pState);
141 for(i=0; pState != NULL; pState = va_arg(ap,
State *), ++i)
153 StateTblMap_T::iterator pos;
159 LOGWARN(
"%s.%d: State [%s.%d] already exists - replacing.",
161 pos->second->getStateName().c_str(), pos->second->getStateId());
168 LOGDIAG3(
"%s.%d: [%s.%d] state added.",
176 StateTblMap_T::iterator pos;
186 LOGERROR(
"%s.%d: No state found in state table with state id=%d.",
194 StateTblMap_T::iterator pos;
210 pState = pos->second;
257 LOGERROR(
"%s.%d: No state found in state table with state id=%d.",
305 StateTblMap_T::iterator iter;
307 fprintf(fp,
"%*sState Machine: %s.%d\n",
312 iter->second->printState(fp, indent+2);
318 static const char *sUndef =
"#undef";
320 StateTblMap_T::iterator pos;
328 strCurr = pos->second->getStateName();
329 strEvent = pos->second->getEventName(nEventId);
330 strAction = pos->second->getActionName(nEventId);
341 strNext = pos->second->getStateName();
348 LOGDIAG3(
"%s.%d: [%s.%d] -- %s(%s.%d) --> [%s.%d]",
350 strCurr.c_str(), nCurrStateId,
351 strAction.c_str(), strEvent.c_str(), nEventId,
352 strNext.c_str(), nNextStateId);
std::string m_strStateMachName
state machine name
int m_nMarkStateId
mark state id of last run cycle
void pushStateId(int nStateid)
Push state id onto stack of state ids.
virtual void actionEnterState(int nPrevStateId, int nEventId)
Execute 'enter state' action.
std::vector< int > m_stackStateIds
stack of previous state ids
virtual int receiveEvent()
Receive next event.
osManipIndent indent()
Left indent at current stream indentation level.
bool deleteState(int nStateId)
Delete state from this state machine.
int getStateId() const
Get state id.
Finite State Machine interface.
int m_nCurrStateId
current state id
virtual int dispatchEvent(int nPrevStateId, int nEventId)
Dispatch received event by executing associated action and transitioning to the next state...
virtual void printStateMach(FILE *fp=stdout, int indent=0)
Print out state machine.
virtual void run()
Run the state machine from the start.
int m_nStartStateId
starting state id
bool m_bIsRecording
is [not] recording work flow
int addStates(State *pState,...)
Add a list of states to this state machine.
static const int StateIdUndef
undefined state
static const int StateIdTerminate
terminate state
std::string getStateName() const
Get state name.
int m_nStateMachId
state machine id
State base class interface.
StateTblMap_T m_mapStateTbl
state table
static const int StateIdThis
the current (this) state
void addState(State *pState)
Add state to this state machine.
#define LOGTRANSITION(nCurrStateId, nEventId, nNextStateId)
Log state transition at diagnostic level 3.
virtual void logTransition(int nCurrStateId, int nEventId, int nNextStateId)
Log state transition.
static const size_t NO_LIMIT
no limit on length of recording
virtual void actionExitState(int nNextStateId, int nEventId)
Execute 'exit state' action.
int popStateId()
Pop state id from stack of state ids.
size_t m_uMaxRecording
max recording length
static const int StateIdPrev
the previous state