Finite State Machines or Finite Automata
Finite State Machines or Finite Automata
Lecture Notes
state
finite automaton
Published
Buy
Acquired
Classify
Available
Discard
Renew / set due date Overdue check [long overdue] / send notice
Long overdue
Conditions
A condition is a Boolean function such as: temperature < 32 time T_Time A condition has duration. (time = T_Time is an event.)
e [temp 32]
S3
When e occurs in state S1, a transition is made to either S2 or S3 depending on the condition. The condition is usually based on the state of another object than the one described by the state diagram. If the condition is based on attributes of the object being described by the state diagram then it is often equivalent to a state.
Conditional transitions
time-out [cars in N/S left lanes] North/south may go straight time-out [no cars in N/S left lanes] time-out [no cars in E/W left lanes]
time-out
time-out
Superstate
i S1 S3 S2
i
U is a superstate. It is an abbreviated notation: Whenever the event i occurs in a substate of U, a transition to S3 occurs S1 i S3
S2 A superstate is just a way to group states. The object modeled by the state diagram is in U exactly when it is in one of the (sub)state, S1 and S2. There can be any number of substates in a given superstate. Superstates can also be nested within each other.
V
r
S21
S4
S1
p k
S22
A transition "to a superstate" relies on a default entry state indicated by an arrow from nowhere, inside the superstate. When event i occurs in state S1, a transition to S21 occurs. Substates are visible to states outside the superstate. In state S4, the event k causes a transition to S22. There can also be transitions from substates to states outside the superstate (as from S21 to S4)
m S3
Telephony example
Idle
caller on hook
Busy signal
Ringing
From any state in the superstate Caller active, the event caller on hook causes a transition back to Idle. (After Davis, Software Requirements, 2nd Ed., Prentice-Hall 1993)
10
S21
Here, the event t causes a transition from any substate of V to S22 p From substate S23, the event x is a transition to the superstate V. It causes a transition to substate S21, which is the default entry state in V.
t S22
S23
11
S1
on-hook / disconnect
S2
The event on-hook causes the action disconnect and a transition from S1 to S2. An activity is an operation that takes time. Example: sound alarm sound ring tone record message Another way to indicate that an activity takes place is by means of actions that mark its beginning and end: start/stop alarm start/stop ring tone start/stop recording message An activity is associated with a state.
12
State Name ________________________ event1 / action1 entry / action3 do / activity1 exit / action4 event5 / action5 event2 / action2
action3 is taken whenever the state is entered. It's taken after action1 activity1 is performed throughout the state action4 is taken whenever there is a transition from the state. It would be taken before action2 If event5 happens in this state, there is no transition but action5 is taken
13
Whole-house fan example The control system of a so-called whole-house fan is connected to a button and to the fan motor. The fan is initially off. By pressing the button once you start the fan going at high speed. By pressing the button once more, you set the fan going at low speed. By pressing it again, you turn the fan off. The button causes an interrupt to the controller. The motor commands are High, Low and Stop.
Off
press/High press/Stop
On High
press/Low
On Low
What if the fan instead has three buttons, High, Low and Off, that can be pressed in any order?
14
Pop-up toaster
That day is not far away when a toaster is more intelligent than some members of the family. German industrialist
A toaster works as follows: It starts in the Idle state. The event Lower takes it to the Heating state. (This is when a bar is lowered into the toaster, usually with pieces of bread on it.) The heat is then turned on and a timer is started. The toasting time is set according to a knob where the user can select the degree of toasting. From Heating, the event Release causes a transition to Idle. This is when the bar is manually released. The heat is then turned off. From Heating, the event Time up causes a transition to Idle. The heat is turned off and the bread is popped out of the toaster. From Heating, the event Overheat causes a transition to Cooling. The heat is turned off and the bread is popped. From Cooling, the event Cooled off causes a transition to Idle.
Represent this as a state transition diagram: a) with all actions associated with state transitions (on the arrows) b) using entry and exit actions and do activities as much as possible.
15
Idle
Release / off
Heating
Cooling
16
Idle
Release Lower
Overheat
17
S1
e2 / a21
S2 S2 e3 / a3 e2 / a22
e1 / a1 S3
enum state_type {S1, S2, S3}; Attribute: state_type State = S1 Operations signaling events: void E1 ( ); void E2 ( ); void E3 ( ); Operations to perform actions: void A1 ( ); void A21 ( ); void A22 ( ); void A3 ( ); // Initial state: S1
18
19
void Event (event_type e) { switch (State) { case S1: switch (e) { case e1: A1 ( ); // Perform action a1 State = S3; break; case e2: A21 ( ); State = S2; break; } break; case S2: switch (e) { case e3: A3 ( ); State = S3; } break; case S3: switch (e) { case e2: A22 ( ); State = S2; } } }
20
Orthogonal decomposition:
"and" states
An aircraft can independently be moving horizontally and vertically In_flight Horizontal Vertical
Turning left
Ascending
Flying straight
Flying level
Turning right
Descending
21
What is the series of states entered and actions taken as the following sequence of events occur in this state diagram? (Remember that each event can cause more than one action.) 1: e1 4: e2 7: e3 2: e9 5: e1 8: e9 3: e5 6: e4 9: e8
S1
e3 / a3
S11
e4 / a4
S12
entry / a7 exit / a13
S111
e2 / a2 entry / a8
e1 / a1
S0
e1 / a1
e9 / a1
e9 / a9
S112
entry / a11 exit / a10
e5 / a5
S13
entry / a6 exit / a12
e8 / a8 entry / a16
22
Car window [Sandn 1993, page 210-11] A power car window is manipulated by means of a control lever that can be held in the positions up and down and springs back to a neutral position when released. Normally, the window moves down while the control is held in the down position and moves up when the control is in the up position until the window reaches its fully open or fully closed position, respectively. But if the control is held down for more than S seconds, the window enters an automatic mode and continues to move down even after the control is released. In the automatic mode, the window stops at its current position if the control is moved to up. The following events are caused by the control and by the window reaching its top or bottom position: Down Up The control is moved to the down position. Effect: The window starts moving downward (if possible) The control is moved to the up position. Effect: The window starts moving upward (if possible) In automatic mode, it stops. The control is released from the down (up) position. Effect: The window stops. (No effect in the automatic mode.) The window reaches its fully closed position. The window reaches its fully open position.
fsm 2003-05-20