0% found this document useful (0 votes)
104 views

Finite State Machines or Finite Automata

Events and states a state machine is a conceptual machine that is useful to describe various objects and devices. A condition is a Boolean function such as: temperature 32o time >= T_Time a condition has duration. When e occurs in state S1, a transition is made to either S2 or S3.

Uploaded by

Sandeep Svsn
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
104 views

Finite State Machines or Finite Automata

Events and states a state machine is a conceptual machine that is useful to describe various objects and devices. A condition is a Boolean function such as: temperature 32o time >= T_Time a condition has duration. When e occurs in state S1, a transition is made to either S2 or S3.

Uploaded by

Sandeep Svsn
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

1

Finite-state machines (F.S.M.) or Finite automata

Lecture Notes

Copyright 2003 Bo Sandn, Colorado Technical University

State diagrams: Definitions


event Something that happens to the object instantaneously, at a point in time. (Implemented as an operation on an object.) In general, the attribute values and links held by an object at a given point in time. In particular, the value of the state variable. Note that state also has the intuitive meaning in the problem environment. state transition state diagram A change of state (in response to an event) Pattern of events, states and state transitions for a given class Same as state diagram Same as state diagram (a state diagram is a representation of a state machine) Same as state machine

state

state transition diagram state machine

finite automaton

Events and states


A state machine is a conceptual machine that is useful to describe various objects and devices. The object can exist in a number of states. It changes state only because of some event. An event is instantaneous. Examples of events: The user presses button X A mouse click (at a certain part of the screen) Flight 123 leaves Chicago The temperature goes below 32 The driver hits the brakes The engine stops 40 seconds have gone since event X Bag B arrives at the claim area The returning of a library book Examples of states: Flight 123 has arrived (as displayed on an airport monitor) The temperature is below 32 The engine is off The engine is on Bag B is in the claim area Screen X is being displayed (as on an ATM) Web page Y is being displayed Book Z is out on loan

State diagram of a library book

Published

Buy

Acquired

Classify

Available

Discard

Checkout / Return set due [normal] date

Return [overdue] / collect fine

Out Return / collect fine

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.)

A condition is used as a guard on a transition. Notation: event [condition]

e [temp > 32] S1 S2

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]

North/south may turn left

time-out

time-out

East/west may turn left

time-out [cars in E/W left lanes]

East/west may go straight

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.

Default entry state

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

caller off hook Caller active Waiting for dial

local number [callee busy]

local number [callee idle]

Busy signal

Ringing

Waiting for long-dist. number

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

Actions and activities


An action is the objects instantaneous response to an event (in addition to the state transition).

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

Notations for actions and activities

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

. Toaster state diagram with actions associated with transitions.

Idle

Lower/ on / start timer

Release / off

Time up / off / pop Cooled off

Heating

Overheat / off / pop

Cooling

16

Toaster state diagram. Actions and activities associated with states

Idle

Release Lower

Time up / pop Cooled off

Heating entry / start timer do / Heat

Overheat

Cooling entry / pop

17

Implementation of a class that is modeled as a state machine

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

Implementation of member function E2


void E2 ( ) { switch (State) { case S1; A21 ( ); State = S2; break; case S2: break; case S3: A22 ( ); State = S2; break; } } // Perform action a22 // Perform action a21

Similar for e1 and e3.

19

Implementation with a single member function


enum event_type {e1, e2, e3};

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

Release Top Bottom

You might also like