400 likes | 666 Views
Finite state machines. Finite State Machines (FSMs). Widely used specification technique Used to specify system behavior in response to events Both output and next state can be determined solely on the basis of understanding the current state and the event that caused the transition
E N D
Finite State Machines(FSMs) • Widely used specification technique • Used to specify system behavior in response to events • Both output and next state can be determined solely on the basis of understanding the current state and the event that caused the transition • H/W engineers have been using FSM for years
Can I interest you in our new long distance service? No thanks.
Can I interest you in our new long distance service? Get Lost!!
Can I interest you in our new long distance service? Get Lost!! Same stimulus event Different response
State Machines • Models how a system responds differently to events over time
State Machines • Models how a system responds differently to events over time button switch
States OFF ON
States Press OFF ON Press
Finite State Machines • State • the memory of previous events • Events • inputs to a system • Actions • output in some form • mechanical, electrical or software event
Finite State Machines • State • the memory of previous events • Events • inputs to a system • Actions • output in some form • mechanical, electrical or software event
Coke Machine • Events • coin insertion • Actions • coke delivery • coins returned • State • the memory of how much money has been deposited
FSM Diagram Event-1 State 1 State 2 Action-k
FSM Event-1 State 1 State 2 Action-k Event-1 Not all state transitions have actions associated with them
ScenarioQuarters Only, 75 cents • Customer • enter quarter • Customer • enter quarter • Customer • enter quarter • Coke Machine • deliver coke
Enumerate Events & Actions Events:E1: Deposit 25 cents Actions: A1: Deliver coke
Define States Start State E1 A B E1 E1/A1 C Events:E1: Deposit 25 cents Actions: A1: Deliver coke States: A: No money B: 25 cents entered C: 50 cents entered
Coke Machine Scenario 2Coin Return • Customer • enter quarter • Customer • enter quarter • Customer • press coin return (CR) • Coke Machine • return coins
Enumerate Events & Actions Events:E1: Deposit 25 centsE2: Coin Return (CR) Actions: A1: Deliver coke A2: Return coins
Define States Start State E1 A B E1 E1/A1 E2/A2 C Events:E1: Deposit 25 centsE2: Coin Return (CR) Actions: A1: Deliver coke A2: Return coins States: A: No money B: 25 cents entered C: 50 cents entered
Define States E2/A2 Start State E1 A B E1 E1/A1 E2/A2 C Events:E1: Deposit 25 centsE2: Coin Return (CR) Actions: A1: Deliver coke A2: Return coins States: A: No money B: 25 cents entered C: 50 cents entered
Transition Table • State E1 E2 • A B • B C A/coin return • C A/coke A/coin return
Transition Table No BlanksAllowed !! • State E1 E2 • A B • B C A/coin return • C A/coke A/coin return
Transition Table • State E1 E2 • A B A • B C A/coin return • C A/coke A/coin return
Telephone System FSMLocal 4-digit exchange Events d: digit dialed h: hang up Actions c1: connect to caller
Telephone System FSMLocal 4-digit exchange d d d d/c1 A B C D E Events d: digit dialed h: hang up Actions c1: connect to caller StatesA: start B: 1 digit dialed C: 2 digits dialed D: 3 digits dialed E: call in progress
Telephone System FSMLocal 4-digit exchange h d d d d/c1 A B C D E Events d: digit dialed h: hang up Actions c1: connect to caller StatesA: start B: 1 digit dialed C: 2 digits dialed D: 3 digits dialed E: call in progress
Telephone System FSMLocal 4-digit exchange h d d d d/c1 A B C D E Events d: digit dialed h: hang up Actions c1: connect to caller StatesA: start B: 1 digit dialed C: 2 digits dialed D: 3 digits dialed E: call in progress
Problems with FSMs • The state explosion problem • Confusing when too many states • Requirements: • “in all airborne states, when the yellow handle is pulled, the seat will be ejected” • maps event to large collection of states • “when the selection button is pressed, enter the selected mode” • implies a clustering or grouping of states
a A b B c b C c Harel’s State Charts • Allows grouping or clustering of states D a A b B c C c Clustering into new superstate D - reallyan abstraction. To be in D really means to be ineither A or C
10 C F A 5 5 B E Coke Machine 10/coke 10 5 D 5 5/coke
C F E Coke Machine CR 10/coke ouch! CR 10 10 5 D A 5 5 5 5/coke B CR
Money Entered No Money (A)
Money Entered 5 No Money (A) 10 CR
C Money Entered 5 10 5 B 5 No Money (A) 10 CR