state machines. what are they? sometimes called a “finite state machine” depicted as a “state...

28
State Machines

Upload: cecil-hodge

Post on 17-Dec-2015

217 views

Category:

Documents


4 download

TRANSCRIPT

State Machines

What are they?

• Sometimes called a “Finite State Machine” • Depicted as a “state diagram”• It’s a tool for specifying a system’s behavior• Used in many disciplines

– Language parsing– Software design– Systems design– Hardware design– Giving directions and other “every day life things”

Example

• Consider a subway turnstile system– The gate is either locked or unlocked– If locked, the patron inserts a coin to

unlock it– If unlocked, the patron passes through

• This “wordy” definition is somewhat awkward although it does get the message across

A Better Description Through Symbols

• Subway turnstile as a state machine

Locked Unlocked

Insert coin

Pass thru

States

TransitionsTriggers (events)

/Unlock gate

/Lock gate

Actions (outputs)

Example

• Subway turnstile as a state machine– There 2 “states” that the gate can be in

• Unlocked• Locked• These are represented as circles in the diagram

– There are 2 events that can change the state of the system • Insert coin• Pass thru• These are represented as state-to-state lines with text designators

(before the “/” character)– There are 2 outputs/actions from the system

• Lock gate• Unlock gate• These are represented as state-to-state lines with text designators

(after the “/” character)

Symbology (notation)

• The start state

Locked Unlocked

Insert coin/Unlock gate

Pass thru/Lock gate

Symbology (notation)

• System is always in exactly one state– The solid circle marks the start state– When the system starts up this is the first state

visited– The link out of the start state has no event

associated with it– There are no links into the start state– This is called an unconditional transition and

always happens

Symbology (notation)

Locked Unlocked

Insert coin/Unlock gate

Pass thru/Lock gate

Close down

Close down

• The final (end) state

Symbology (notation)

• The system may have zero or more final states– The solid circle with the ring around it– Anytime the system transitions into one of

these states, this diagram is completed• The system may shut down• The system may transition to another state

machine (up in the hierarchy – more later)

Better than words?

• In this case the “wordy” version wasn’t too bad

• But what if we need to add more, unforeseen stuff?

Example

• Let’s add some more options– If the gate is locked and someone passes

through, set off an alarm– If the gate us unlocked and someone

inserts money, thank them for the “donation”

• The “wordiness” just doubled

Example

locked unlocked

Coin/unlock

Pass/lock

Coin/”thank you”Pass/sound alarm

close

close

• But the picture still isn’t overly complicated

Additions

• Changing things “midstream” isn’t even that hard– Instead of sounding an alarm on a dead-

beat perhaps we might want to do more

Example

locked unlocked

Coin/unlock

Pass/lock

Coin/”thank you”Pass/sound alarm

close

closeviolation reset

• Addition of states/transitions/actions

Additions

• We could go on and on, but we won’t

Implementation

• The software implementation is basically a switch statement in Java (or the equivalent if/else if/else)

switch (state) {

case locked:…

case unlocked:…

case violation:…

}

Implementation

• The implementation in hardware is through sequential circuits with flip-flops (memory elements) and gates

And so on…

• There are a lot more features related to state machines– They can be “Hierarchical”

• One “super state” can contain many “sub states”

Hierarchical

A BEnters state C at start state

Enters state C at sub-state B

C

Parallel

• Two state machines may run in parallel, interacting with one another

Parallel

A

B

C

D

event_1/action_1

action_1/action_2

And so on…

• They can contain “conditional connectors”– Can put “if statements” on the transitions

Conditional connectors

A B

C

event case x

case y

And so on…

• Guarded transitions– Like a conditional transition but with just

one “case”

Guarded Transitions

A Bevent [case x]

And so on…

• States can have– Entry actions

• These are things that occur as soon as the state is entered

– Transition actions• These are things that occur on the transition to/from the

state

– Exit actions• These are things that occur as a state is being exited

Summary

• They can be as simple or as complicated as necessary.

• But, if they get too complicated they are not serving their stated purpose and should be broken up into multiple state machines

Assignment

• Create a state-diagram describing the operation of the “4 Button Digital Watch”– Be neat!

• If you must draw it by hand, make sure it is clean, clear, and readable