Exercise-006 Testing History state
There are 2 types of history states. A shallow history and deep history. ‘H+’ is a deep history.
When you first reset the board, the state will be s211. Initially, the history of s1 is absent. It doesn’t have any history because it was never entered. The history was absent.
Let’s first analyze when there was no history.
Let’s discuss the history state. I made these changes:- I just introduced s12, and let me also define ENTRY action and EXIT action for s12. I will call s12-ENTRY and s12-EXIT.
First, let’s reset the board. When you reset the board, the state machine will settle s211. It never entered S1. That’s why history for s1 is absent. There is no history for s1. Here, I send the event F. Let’s take no history case.
You sent the event F when the state was s211. What happens?
First, F is not handled by s211, so it is propagated to the next level→ s21. s21 is also not handled, propagated to the next state→ s2. s2 handles F. Action associated with F is executed first→ s2-F.
After that, EXIT action of s211, EXIT action of s21, EXIT action of s2. Three EXIT actions. s211-EXIT, s21-EXIT, s2-EXIT. Then, there is a transition to the history state. History of s1. But the history of s1 is absent. There is no history for s1; that’s why history is ignored. When the history is ignored, the default path is taken. The default path points to S11.
What’s happening here?
ENTRY of s1, so s1-ENTRY. After that, ENTRY of s11. Like that, when history is absent.
Let’s verify this output. Go back to the IDE and compile it. Because I have made some changes. I send ‘F’ here. It is in s11-ENTRY.
Now, the state machine is in s11. Let’s send ‘I’ to this.
When you send ‘I,’ now ‘I’ is handled by s11 itself. It goes to s12. s12-ENTRY action is executed. The state is s12, as shown in Figure 1.
Now, the state is s12; execute F, to come back to s211.
We are in s211. Now send F. When you send F here, it will go to the history state of s1, and history is already there because it had previously visited s1. History was recorded in s1. And the history of s1 was s12. When the state was s12, you sent F here, and it came back to s2 or s211; the history was recorded as ‘last state of s1 was s12’.
You are in s211 and send F. What happens?
When you send F, s211 doesn’t handle F, so propagated to the next level s21, propagated to the next level s2. s2 handles it.
First, this is executed, followed by the EXIT actions. s211-EXIT, s21-EXIT, s2-EXIT. And the transition comes to the history state. History is already there, so it goes to s21. It won’t take this default path.
The line looks like this: the first ENTRY action of s1 and ENTRY action of s12. Let’s check that.(Figure 4)
We are in s211; we had already visited s12, send F, and you see that it comes to s12. It will not go to s11. It will go to s12 because that was the history recorded of s1. That’s about history state, and we will use this history state in our exercise later.
You are in s11, and you receive the event E. what happens?
E is not handled by s11, propagate to the next level → s1. E is not handled here and propagates to the next level →s. s handles E. For ‘s’, E is a local transition. First, the action associated with the transition is executed, and then, there is a transition to s11. What happens? All the EXIT actions have to be executed first. Because the s11 is getting re-entered. Re-entered what does that mean? It has to EXIT first to re-enter. EXIT of s11 (s11-EXIT), EXIT of s1 (s1-EXIT).
And then ENTRY action. ENTRY action is from the outermost state. That’s why s1-ENTRY, s11-ENTRY, like that.
Let’s assume that we are in state s11, and the foo variable value is initially 0. What happens if event D is sent here?
First of all, S11 handles D. The action associated with D is executed, but there is no action defined for D because you don’t see that ‘/’ mark (Figure 6). That means no action is defined. Here the Guard evaluated. The foo value is 0 initially, Guard fails. If the Guard fails here, that means s11 couldn’t handle D. When it couldn’t handle D, the D is propagated to the next level, ‘s1’. s1 handles D, and the foo variable value is 0. Not of 0 is 1, Guard evaluates to true, foo value becomes 1, and s1-D is printed. That’s why s1-D gets printed first.
When the guard evaluates to true, there is a transition. Transition is to s. The state machine is here now ‘s,’ which is now guided by this initial pseudo-state, s-INIT is executed.
Now it is getting entered into s11. When it is re-entered into s11, its EXIT action must be executed first from the innermost state to the outermost state. s11-EXIT is executed. s1-EXIT next.
And then, it will get re-entered again from the initial transition, s-INIT is executed. s1-ENTRY, s11-ENTRY, like that. And remember that, when s1-D happens, the foo value is set to 1.
The state is s11 again, the foo value is 1, and the event D is sent s11. Again s11 handles D. Only if this guard evaluates to true, but foo value is already set to 1, so guard evaluates to true. That means s11 successfully handled that event D. That’s why the action associated with that transition is executed s11-D, and then a local transition to s1. s1 was not entered from the outside. This is a local transition for s1. That’s why, after s11-D, the control comes to s1, and now it has to re-enter into s11. But, before re-entering, its EXIT action should be executed first. s11-EXIT is executed, and it re-enters here; s1-INIT and s11-ENTRY.
That’s about different transition execution sequences and event propagation. If you have any doubts, don’t worry, you’ll get cleared, so when you start drawing these state machines and implement a couple of exercises, everything will start making sense.
FastBit Embedded Brain Academy Courses
Click here: https://fastbitlab.com/course1