You are on page 1of 6

5

State Modeling

5.1 Figure A5.1 shows a state diagram for an extension ladder. You could also include states
for the ladder fully extended and fully contracted.

ExtensionLadder

raise Unlocked lower


lower
Locked Locked raise raise
rope slack rope taut
raise
[slack gone] Passing
lower rung up

Figure A5.1 State diagram for an extension ladder.

5.2 In Figure A5.2 the event A refers to pressing the A button. In this diagram, releasing the
button is unimportant and is not shown (although you must obviously release the button
before you can press it again). Note that a new button event cannot be generated while
any button is pressed. You can consider this a constraint on the input events themselves
and need not show it in the state diagram (although it would not be wrong to do so).

46
Chapter 5 Answers to Exercises 47

DigitalWatch
A

Display time A Set hours A Set minutes


do/ show hours do/ show hours do/ show minutes
and minutes

B / advance hour B / advance minute

Figure A5.2 State diagram for a simple digital watch

5.3 Figure A5.3 elaborates the state diagram in the exercise.

AnsweringMachine

caller hangs up
caller hangs up
Announcing Recording
Hung up do/ record
do/ play announcement announcement
call detected message
/answer call
complete

Figure A5.3 State diagram for a telephone answering machine

5.4 Figure A5.4 extends the state diagram from the previous answer to answer after five
rings. The number of rings are kept in an internal counter that is reset on each new call
and incremented on every ring.

AnsweringMachine
caller hangs up

caller hangs up Recording announcement complete


do/ record message

caller hangs up ring [#rings>=5]


/ answer call Announcing
Hung up Ringing
do/ play announcement
call detected
/ #rings:=0 ring [#rings<5] / #rings++
caller hangs up
caller answers
Talking

Figure A5.4 State diagram for a machine that answers after five rings
48 Chapter 5 / State Modeling

5.5 Figure A5.5 elaborates the state diagram in the exercise.

DataTransferProtocol
data read by host
Data not available Data available
do/ indicate data not available do/ indicate data available
new data ready
reset Data lost new data ready
do/ indicate data lost

Figure A5.5 State diagram of a data transfer protocol

5.6 Figure A5.6 shows the completed state diagram for the motor control.

MotorControl
on is no longer asserted

Starting
on is asserted motor is running Running
Off do/ apply power to run do/ apply power
winding and apply to run winding
power to start winding
motor is
overheated
on is no longer asserted motor is overheated
Too Hot
reset [motor is not overheated]

Figure A5.6 State diagram for a motor control

5.7 Figure A5.7 shows another approach for motor control.

MotorControl
stop down

start is down Starting


and stop is up motor is running Running
Off do/ apply power to run do/ apply power
winding and apply to run winding
power to start winding
motor is
overheated
stop down motor is overheated
Too Hot
reset [motor is not overheated]

Figure A5.7 An alternate approach to motor control


Chapter 5 Answers to Exercises 49

5.8 Figure A5.8 shows a state diagram for a diagram editor. This is only a small part of a
complete editor. In the real editor there are more ways to pick items and you can pick
more than one item (next exercise).
Some events can be ignored. In state Nothing selected event left down with cursor on
no object has no effect and therefore is not shown. Similarly, left drag (that is moving
the cursor while the left button is down) has no effect in the Nothing selected state.
In the real editor, the right button is used to pop up a menu dependent on the selection
state. There would be many selection substates, one for each kind of item (or combina-
tion of items) that can be selected. Similarly, there would be transitions to create new
items.

DiagramEditor
left down [cursor on object]
/selection:=pick object
left drag/move selected object
Object selected
left down [cursor on object]
/selection:=pick object left down [cursor on no object]
/selection:=null
Nothing selected

Figure A5.8 State diagram for a diagram editor

5.9 Figure A5.9 extends the diagram editor for selecting multiple objects.

DiagramEditor
left shift down [cursor on object]
left down [cursor on object] /selection+=pick object
/selection:=pick object
left drag/move selected objects
Object selected
left down [cursor on object]
/selection:=pick object left down [cursor on no object]
/selection:=null
Nothing selected

Figure A5.9 State diagram for a diagram editor with selection of multiple objects

5.10 Figure A5.10 extends Figure E5.4 to represent the observed jamming behavior properly.

5.11 Figure A5.11 shows a state diagram. Note that even simple state diagrams can lead to
complex behavior. A change event occurs whenever the candle is taken out of its holder
50 Chapter 5 / State Modeling

CopyMachine

OutOfPaper

paperAdded paperTrayBecomesEmpty AND NOT allCopiesMade

MakingCopies PaperJam Unjammed


do/ printCopies paperPath do/ flashWarningLight paperPath
Becomes Cleared
Blocked
allCopiesMade powerTurnedOff / turnReadyLightOff
startPressed
powerTurnedOn /
internal Warming
numCopies:=1,
increment / Test isAutoContrast:=TRUE,
numCopies++ Ready Finished do/ internalTesting, size:=normal
flashReadyLight
decrement entry / turnReady Off
[numCopies>1] LightOn
/ numCopies-- powerTurnedOff / turnReadyLightOff

changeSize / changeContrast [NOT isAutoContrast] /


size:=newValue contrast:=newValue

toggleAutoContrast /
isAutoContrast:=NOT isAutoContrast

Figure A5.10 State diagram for copy machine with observed behavior for jamming

Bookcase
[at north, east, south, or west]

change Rotating change Stopping


Stopped
do/ rotate bookcase do/ rotate bookcase

[at north]

Figure A5.11 State diagram for bookcase control

or whenever it is put back. The condition at north is satisfied whenever the bookcase is
behind the wall. The condition at north, east, south, or west is satisfied whenever the
bookcase is facing front, back, or to the side.
When you first discovered the bookcase, it was in the Stopped state pointing south.
When your friend removed the candle, a change event drove the bookcase into the Ro-
Chapter 5 Answers to Exercises 51

tating state. When the bookcase was pointing north, the condition at north put the book-
case back into the Stopped state. When your friend reinserted the candle, another change
event put the bookcase into the Rotating state until it again pointed north. Pulling the
candle out generated another change event and would have caused the bookcase to ro-
tate a full turn if you had not blocked it with your body. Forcing the bookcase back is
outside the scope of the control and does not have to be explained.
When you put the candle back again another change event was generated, putting the
bookcase into the Rotating state once again. Taking the candle back out resulted in yet
another change event, putting the bookcase into the Stopping state. After 1/4 turn, the
condition at north, east, south or west was satisfied, putting the bookcase into the
Stopped state.
What you should have done at first to gain entry was to take the candle out and quick-
ly put it back before the bookcase completed 1/4 turn.

You might also like