State diagrams, like those shown so far, are helpful in understanding the potential changes within a system. However, without more detail these diagrams fail to explain the behavior of the system. It is possible to add
Liquid Cooled below 0 degrees C Cooled below 100 degrees C Heated above
0 degrees C 100 degrees CHeated above
Ice Water Vapor
system behavior by actions within the states. The correct state diagram notation for such actions divides states into two compartments: the top compartment names the state and the bottom compartment describes actions associated with the state. Each state can include up to three differ- ent actions of the following types:
• entry—This defines an action that is performed upon each transition into the state.
• exit—This defines an action that is performed upon each transition out of the state.
• do—This defines an action that is performed continuously while in the state. Welcome Out of Service Verifying Invalid Warning After 1 min. of inactivity After 1 min. of inactivity After 30 sec. of inactivity After 5 sec. After 10 sec. Tank is full
Insert nozzle and engage lever Credit card validated
Disengage & return nozzle to pump
Disengage & return nozzle to pump
Disengage & return nozzle to pump Credit card fails to validate
Swipe credit card owner turns pump on owner turns off pump
owner turn pump on
Ready
Pumping
Finished
Receipt
Figure 6.13 adds behaviors to the gasoline pump state diagram. The transitions and associated events in Figure 6.13 are the same as in Figure 6.12. The only difference is that actions have been added.
An entry action is appropriate whenever the action needs to occur upon a transition into the state. Entering the Verifying state causes the custom- er’s credit card to be checked for validity and entering the Receipt state cause the customer’s receipt to be printed. Exit actions occur whenever a transition is made from a state. Sometimes an equivalent state diagram can be drawn by substituting an exit action of one state with an entry action to a preceding state. For example, the exit action of the Pumping state could be removed by making it into an entry action in both the Finished and Receipt states. Also, note that entry and exit actions are to be performed every time the associated transition is made. Therefore, the alarm bell from the Warning state must ring again for every passing min- ute of inactivity.
A second example of using entry, exit, and do actions is given in Figure 6.14. This state diagram describes the user authentication system of an ATM. The user of the ATM begins by activating the machine perhaps
Warning Invalid Out of Service Welcome Verifying Ready Pumping Finished Receipt
entry/ring bell for 2 sec. do/show Please remove
nozzle screen entry/start gas flow
do/gas flow exit/stop gas flow do/show ready to fill screen
entry/check card do/show welcome screen do/turn light off
do/show Invalid Credit Card message entry/print receipt After 1 min. of inactivity After 30 sec. of inactivity Disengage & return nozzle to pump
Disengage & return nozzle to pump Disengage &
return nozzle to pump After 1 min.
of inactivity Credit card validated Credit card fails to validate Swipe credit card
Owner turns pump on
After 10 sec. Owner turn pump on
Owner turn off pump
Insert nozzle and engage lever
Tank is full After 5 sec.
by pressing a button on touching the touch screen. While in the Initial state, the ATM is displaying a screen that welcomes the user and asks for a credit card to be swiped. The card swiping is an event causing a transition into the Awaiting PIN state. Immediately before making this transition, the Initial state’s exit action is performed. The do action of the Awaiting PIN state explains that the user will be requested to enter a PIN at this time. Once the PIN is entered, a transition into the Checking state is made. If the PIN is valid then the next transition is to the Validated state, which serves as the end of this process.
User activates ATM
Initial
Awaiting PIN
Checking
Validated ERROR
do/show welcome screen exit/attemptCount assigned zero
do/show screen to request PIN exit/attemptCount increased by one
entry/system checks card and PIN
do/show transaction screen entry/destroy credit carddo/show See Card Provider message
User swipes credit card
User enters PIN
PIN is valid PIN is invalidand attemptCount = 3
PIN is invalid and attemptCount < 3
Invalid PINs are handled in a different way. This algorithm makes use of a variable, called attemptCount. The attemptCount variable is assigned an initial value of zero by the exit action of the Initial state. Each exit from the Awaiting PIN state causes this variable to increase by one. In other words, while in the Checking state the attemptCount variable will store the number of times the user has attempted to enter a PIN. This particular ATM allows the user to attempt the PIN three times and upon a third fail- ure it destroys the credit card. Using variables, like attemptCount, in state machines helps to keep track of data in addition to the states.