29 protected ref FSMStateBase
m_State;
30 protected FSMStateBase m_OwnerState;
31 protected ref FSMStateBase m_InitialState;
33 protected bool m_HasCompletions =
false;
35 void HFSMBase (FSMStateBase ownerState = NULL)
37 m_OwnerState = ownerState;
43 FSMStateBase GetCurrentState ()
50 FSMStateBase GetOwnerState ()
62 FSMStateBase curr = state;
66 curr = curr.GetParentState();
68 return path.Count() > 0;
75 void SetInitialState (FSMStateBase initial_state)
77 m_InitialState = initial_state;
84 m_Transitions.Insert(t);
86 if (t.m_event == NULL)
88 Print(
"Warning (performance): FSM " +
this +
" has completion transition for src=" + t.m_srcState +
" ---NULL----|> dst=" + t.m_dstState);
89 m_HasCompletions =
true;
97 void Start (FSMEventBase initial_event = NULL,
bool useExistingState =
false)
101 fsmDebugPrint(
"[hfsm] " + this.
ToString() +
"::Start(" + initial_event.ToString() +
"), init_state=" + m_InitialState.ToString());
104 if (!useExistingState)
107 m_State.OnEntry(initial_event);
109 if (m_HasCompletions)
110 ProcessCompletionTransitions();
119 void Terminate (FSMEventBase terminal_event = NULL)
128 m_State.OnExit(terminal_event);
132 void Abort (FSMEventBase abort_event = NULL)
158 fsmDebugPrint(
"[hfsm] (local abort) state=" + t.m_srcState.ToString() +
"-------- ABORT event=" + e.ToString() +
"[G=" + t.m_guard.ToString() +
"]/A=" + t.m_action.ToString() +
" --------|> dst=" + t.m_dstState.ToString());
164 t.m_action.Action(e);
166 auto tmp = t.m_srcState.GetParentState();
167 if (tmp == t.m_dstState.GetParentState())
171 if (t.m_dstState != NULL)
180 fsmDebugPrint(
"[hfsm] abort & terminating fsm: state=" + t.m_srcState.ToString() +
" event=" + e.ToString());
201 FSMStateBase FindAbortDestinationState (FSMEventBase e)
206 fsmDebugPrint(
"[hfsm] SUB! " + GetOwnerState().
Type().
ToString() +
"::FindAbortDestinationState(" + e.Type().ToString() +
")");
208 fsmDebugPrint(
"[hfsm] root::FindAbortDestinationState(" + e.Type().ToString() +
")");
215 FSMStateBase abort_dst = a.FindAbortDestinationState(e);
224 int i = FindFirstUnguardedTransition(e);
229 fsmDebugPrint(
"[hfsm] abort event has no transition: src=" +
m_State.ToString() +
" e=" + e.Type().ToString());
254 fsmDebugPrint(
"[hfsm] root::ProcessAbortEvent(" + e.Type().ToString() +
")");
262 FSMStateBase abort_dst = a.ProcessAbortEvent(e, subfsm_res);
283 if (GetOwnerState() == abort_dst.GetParentState())
287 fsmDebugPrint(
"[hfsm] aborted sub machine=" +
m_State.ToString() +
" & abort destination reached.");
318 int i = FindFirstUnguardedTransition(e);
323 fsmDebugPrint(
"[hfsm] abort event has no transition: src=" +
m_State.ToString() +
" e=" + e.Type().ToString());
343 fsmDebugPrint(
"[hfsm] aborted sub machine=" +
m_State.ToString() +
" will fall-through to dst=" + t.m_dstState);
379 fsmDebugPrint(
"[hfsm] root::ProcessEvent(" + e.Type().ToString() +
" =" + e.DumpToString());
383 if (m_HasCompletions)
384 ProcessCompletionTransitions();
410 int i = FindFirstUnguardedTransition(e);
415 fsmDebugPrint(
"[hfsm] event has no transition: src=" +
m_State.ToString() +
" e=" + e.Type().ToString());
422 if (row.m_dstState != NULL)
425 if (row.m_srcState.GetParentState() == row.m_dstState.GetParentState())
426 res = LocalTransition(i, e);
428 Error(
"cross-hierarchy transition or misconfigured transition detected!");
434 if (row.m_srcState.GetParentState() == GetOwnerState())
435 res = LocalTransition(i, e);
437 Error(
"cross-hierarchy transition or misconfigured transition detected!");
443 protected int FindFirstUnguardedTransition (FSMEventBase e)
445 FSMStateBase curr_state =
m_State;
447 int count = m_Transitions.Count();
448 for (
int i = 0; i < count; ++i)
451 if ((t.m_srcState == curr_state) && (t.m_event != NULL) && (t.m_event.Type() == e.Type()))
454 bool hasGuard = t.m_guard != NULL;
455 if (!hasGuard || (hasGuard && t.m_guard.GuardCondition(e)))
465 FSMStateBase FindTransitionState(FSMStateBase s, FSMEventBase e)
467 FSMStateBase curr_state = s;
469 int count = m_Transitions.Count();
470 for (
int i = 0; i < count; ++i)
473 if ((t.m_srcState == curr_state) && (t.m_event != NULL) && (t.m_event.Type() == e.Type()))
481 FSMStateBase FindGuardedTransitionState(FSMStateBase s, FSMEventBase e)
483 FSMStateBase curr_state = s;
485 int count = m_Transitions.Count();
486 for (
int i = 0; i < count; ++i)
489 if ((t.m_srcState == curr_state) && (t.m_event != NULL) && (t.m_event.Type() == e.Type()))
491 bool hasGuard = t.m_guard != NULL;
492 if (!hasGuard || (hasGuard && t.m_guard.GuardCondition(e)))
501 protected int FindFirstCompletionTransition ()
505 FSMStateBase curr_state =
m_State;
507 int count = m_Transitions.Count();
508 for (
int i = 0; i < count; ++i)
513 if ((t.m_srcState.Type() == curr_state.Type()) && (t.m_event == NULL))
515 bool hasGuard = t.m_guard != NULL;
516 if (!hasGuard || (hasGuard && t.m_guard.GuardCondition(NULL)))
537 fsmDebugPrint(
"[hfsm] (local) state=" + t.m_srcState.ToString() +
"-------- event=" + e.ToString() +
"[G=" + t.m_guard.ToString() +
"]/A=" + t.m_action.ToString() +
" --------|> dst=" + t.m_dstState.ToString());
543 t.m_action.Action(e);
547 if (t.m_dstState != NULL)
552 GetOwnerState().OnSubMachineChanged(t.m_srcState, t.m_dstState);
555 m_State.OnStateChanged(t.m_srcState, t.m_dstState);
563 fsmDebugPrint(
"[hfsm] terminating fsm: state=" + t.m_srcState.ToString() +
" event=" + e.ToString());
567 GetOwnerState().OnSubMachineChanged(t.m_srcState, NULL);
586 int completionIdx = FindFirstCompletionTransition();
587 while (completionIdx != -1)
591 if (row.m_dstState != NULL)
594 if (row.m_srcState.GetParentState() == row.m_dstState.GetParentState())
595 res = LocalTransition(completionIdx, NULL);
597 Error(
"cross-hierarchy transition or misconfigured transition detected!");
603 if (row.m_srcState.GetParentState() == GetOwnerState())
604 res = LocalTransition(completionIdx, NULL);
606 Error(
"cross-hierarchy transition or misconfigured transition detected!");
610 completionIdx = FindFirstCompletionTransition();