38
Finite state machines

Finite state machines. Finite State Machines (FSMs) Widely used specification technique Used to specify system behavior in response to events Both output

Embed Size (px)

DESCRIPTION

Can I interest you in our new long distance service?

Citation preview

Page 1: Finite state machines. Finite State Machines (FSMs) Widely used specification technique Used to specify system behavior in response to events Both output

Finite state machines

Page 2: Finite state machines. Finite State Machines (FSMs) Widely used specification technique Used to specify system behavior in response to events Both output

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

Page 3: Finite state machines. Finite State Machines (FSMs) Widely used specification technique Used to specify system behavior in response to events Both output

Can I interest you in our new long distance service?

Page 4: Finite state machines. Finite State Machines (FSMs) Widely used specification technique Used to specify system behavior in response to events Both output

Can I interest you in our new long distance service?

No thanks.

Page 5: Finite state machines. Finite State Machines (FSMs) Widely used specification technique Used to specify system behavior in response to events Both output

Can I interest you in our new long distance service?

Page 6: Finite state machines. Finite State Machines (FSMs) Widely used specification technique Used to specify system behavior in response to events Both output

Can I interest you in our new long distance service?

Get Lost!!

Page 7: Finite state machines. Finite State Machines (FSMs) Widely used specification technique Used to specify system behavior in response to events Both output

Can I interest you in our new long distance service?

Get Lost!!

Same stimulus event

Different response

Page 8: Finite state machines. Finite State Machines (FSMs) Widely used specification technique Used to specify system behavior in response to events Both output

State Machines• Models how a system responds

differently to events over time

Page 9: Finite state machines. Finite State Machines (FSMs) Widely used specification technique Used to specify system behavior in response to events Both output

State Machines

• Models how a system responds differently to events over time

button switch

Page 10: Finite state machines. Finite State Machines (FSMs) Widely used specification technique Used to specify system behavior in response to events Both output

States

ON OFF

Page 11: Finite state machines. Finite State Machines (FSMs) Widely used specification technique Used to specify system behavior in response to events Both output

States

ON OFF

Press

Press

Page 12: Finite state machines. Finite State Machines (FSMs) Widely used specification technique Used to specify system behavior in response to events Both output

Finite State Machines• State

– the memory of previous events• Events

– inputs to a system• Actions

– output in some form•mechanical, electrical or software

event

Page 13: Finite state machines. Finite State Machines (FSMs) Widely used specification technique Used to specify system behavior in response to events Both output

Finite State Machines• State

– the memory of previous events• Events

– inputs to a system• Actions

– output in some form•mechanical, electrical or software

event

Page 14: Finite state machines. Finite State Machines (FSMs) Widely used specification technique Used to specify system behavior in response to events Both output

Coke Machine• Events

– coin insertion• Actions

– coke delivery– coins returned

• State– the memory of how much money

has been deposited

Page 15: Finite state machines. Finite State Machines (FSMs) Widely used specification technique Used to specify system behavior in response to events Both output

FSM Diagram

State 1 State 2Event-1

Action-k

Page 16: Finite state machines. Finite State Machines (FSMs) Widely used specification technique Used to specify system behavior in response to events Both output

FSM

State 1 State 2Event-1

Action-k

Event-1

Not all state transitions have actions associated with them

Page 17: Finite state machines. Finite State Machines (FSMs) Widely used specification technique Used to specify system behavior in response to events Both output

ScenarioQuarters Only, 75 cents• Customer

– enter quarter• Customer

– enter quarter• Customer

– enter quarter• Coke Machine

– deliver coke

Page 18: Finite state machines. Finite State Machines (FSMs) Widely used specification technique Used to specify system behavior in response to events Both output

Enumerate Events & Actions

Events:E1: Deposit 25 cents

Actions:A1: Deliver coke

Page 19: Finite state machines. Finite State Machines (FSMs) Widely used specification technique Used to specify system behavior in response to events Both output

Define States

A B

C

Start State

E1

E1E1/A1

States:A: No moneyB: 25 cents enteredC: 50 cents entered

Events:E1: Deposit 25 cents

Actions:A1: Deliver coke

Page 20: Finite state machines. Finite State Machines (FSMs) Widely used specification technique Used to specify system behavior in response to events Both output

State Transition TableCurrentState

EventE1

A B

B C

C A/deliver coke

Page 21: Finite state machines. Finite State Machines (FSMs) Widely used specification technique Used to specify system behavior in response to events Both output

Coke Machine Scenario 2Coin Return

• Customer– enter quarter

• Customer– enter quarter

• Customer– press coin return (CR)

• Coke Machine– return coins

Page 22: Finite state machines. Finite State Machines (FSMs) Widely used specification technique Used to specify system behavior in response to events Both output

Enumerate Events & Actions

Events:E1: Deposit 25 centsE2: Coin Return (CR)Actions:A1: Deliver cokeA2: Return coins

Page 23: Finite state machines. Finite State Machines (FSMs) Widely used specification technique Used to specify system behavior in response to events Both output

Define States

A B

C

Start State

E1

E1E1/A1

States:A: No moneyB: 25 cents enteredC: 50 cents entered

Events:E1: Deposit 25 centsE2: Coin Return (CR)Actions:A1: Deliver cokeA2: Return coins

E2/A2

Page 24: Finite state machines. Finite State Machines (FSMs) Widely used specification technique Used to specify system behavior in response to events Both output

Define States

A B

C

Start State

E1

E1E1/A1

States:A: No moneyB: 25 cents enteredC: 50 cents entered

Events:E1: Deposit 25 centsE2: Coin Return (CR)Actions:A1: Deliver cokeA2: Return coins

E2/A2

E2/A2

Page 25: Finite state machines. Finite State Machines (FSMs) Widely used specification technique Used to specify system behavior in response to events Both output

Transition Table• State E1 E2• A B• B C A/coin

return• C A/coke A/coin

return

Page 26: Finite state machines. Finite State Machines (FSMs) Widely used specification technique Used to specify system behavior in response to events Both output

Transition TableTransition Table

State E1 E2 A B B C A/coin return C A/coke A/coin return

No BlanksAllowed !!

Page 27: Finite state machines. Finite State Machines (FSMs) Widely used specification technique Used to specify system behavior in response to events Both output

Transition Table

• State E1 E2• A B A• B C A/coin return• C A/coke A/coin return

Page 28: Finite state machines. Finite State Machines (FSMs) Widely used specification technique Used to specify system behavior in response to events Both output

Telephone System FSMLocal 4-digit exchange

Eventsd: digit dialedh: hang up

Actionsc1: connect to caller

Page 29: Finite state machines. Finite State Machines (FSMs) Widely used specification technique Used to specify system behavior in response to events Both output

Telephone System FSMLocal 4-digit exchange

A B C D Ed d d d/c1

StatesA: startB: 1 digit dialedC: 2 digits dialedD: 3 digits dialedE: call in progress

Eventsd: digit dialedh: hang up

Actionsc1: connect to caller

Page 30: Finite state machines. Finite State Machines (FSMs) Widely used specification technique Used to specify system behavior in response to events Both output

Telephone System FSMLocal 4-digit exchange

A B C D Ed d d d/c1

h

StatesA: startB: 1 digit dialedC: 2 digits dialedD: 3 digits dialedE: call in progress

Eventsd: digit dialedh: hang up

Actionsc1: connect to caller

Page 31: Finite state machines. Finite State Machines (FSMs) Widely used specification technique Used to specify system behavior in response to events Both output

Telephone System FSMLocal 4-digit exchange

A B C D Ed d d d/c1

h

StatesA: startB: 1 digit dialedC: 2 digits dialedD: 3 digits dialedE: call in progress

Eventsd: digit dialedh: hang up

Actionsc1: connect to caller

Page 32: Finite state machines. Finite State Machines (FSMs) Widely used specification technique Used to specify system behavior in response to events Both output

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

Page 33: Finite state machines. Finite State Machines (FSMs) Widely used specification technique Used to specify system behavior in response to events Both output

Harel’s State Charts• Allows grouping or clustering of states

ab

cb

c

A

C

B

a

b

c

c

A

C

B

D

Clustering into new superstate D - reallyan abstraction. To be in D really means to be ineither A or C

Page 34: Finite state machines. Finite State Machines (FSMs) Widely used specification technique Used to specify system behavior in response to events Both output

Coke Machine

E

D F10

A

B

5

10C

5

5

10/coke

5/coke5

Page 35: Finite state machines. Finite State Machines (FSMs) Widely used specification technique Used to specify system behavior in response to events Both output

Coke Machine

A

B E

D F5

10C

5

10 5

10/coke

5/coke5

CR

CR

CR ouch!

Page 36: Finite state machines. Finite State Machines (FSMs) Widely used specification technique Used to specify system behavior in response to events Both output

No Money

(A)

Money Entered

Page 37: Finite state machines. Finite State Machines (FSMs) Widely used specification technique Used to specify system behavior in response to events Both output

No Money

(A)

Money Entered

5

10

CR

Page 38: Finite state machines. Finite State Machines (FSMs) Widely used specification technique Used to specify system behavior in response to events Both output

No Money

(A)

Money Entered

5

10

CR

B

5 10C5