Lecture 7

The Models are the Code Executable UML
Lecture 7 - Getting a Lifecycle
Paul Krause
Executable UML
Lecture 7 - Getting a Lifecycle
 Active Classes vs Passive Classes
 Some basics of finite automata
 How to use Statecharts properly
 Completing the State model with State Transition
Tables
Executable UML
Active vs Passive Classes
 Active Classes
 The behaviour of instances of active classes (“active
objects”) varies over time
 e.g. Instances of a Thread class can be started,
paused, continued, stopped
 This behaviour can be captured in a Statechart
 Passive Classes
 Passive objects have the same behaviour at all times
 Operations on a class are sufficient to define its
behaviour
 Information classes, e.g. Address, are typically of this
kind
Executable UML
State
 A condition or situation during the life of an object
during which it satisfies some condition, performs
some action, or waits for some event.
 The
Unified Modelling Reference Manual
Executable UML
An Abstract Machine
Chewing Gum
Quiet
Talkative
Liquorice
Wine Gum
Happy
Executable UML
Finite Automata with Output
0/0, 1/1
0/1
no
carry
0/1
start
1/0
1/0
Executable UML
owe
carry
Moore = Mealy
 Mealy machine
 Actions are on the transitions (and only on the
transitions)
 Moore machine
 Actions are on entry into new states (and only on
entry)
 Both are computationally sufficient
 Anything that can be expressed as a Moore
machine can be expressed as a Mealy machine,
and vice-versa
Executable UML
UML Statechart Notation
guard
condition
actions
event name
StateA
entry/ actions
exit/ actions
Executable UML
e1 [cond] / action1;action2
StateB
entry/ actions
exit/ actions
What Happens?
initially x = 0 & y = 0
StateA
e1 [y < 10] / x = 5
exit/ x = 10
StateC
exit/ y = 10
Executable UML
StateB
entry/ action
exit/ action
e1 [x < 10] / y = 5
StateD
entry/ action
exit/ action
Good Practice for Statecharts
 Express the behaviour of Active Classes using a
Statechart for each active class
 Restrict usage to a subset of the full Statechart
notation for clarity:
 Actions
are specified on entry into a state (not on exit,
or on the transition itself)
 An individual event can only cause a single transition
out of a given state
 This usage corresponds to expressing behaviour
as a “Moore” machine
Executable UML
State-dependent behaviour
Stopped
entry/
trainStopped
trainMadeSafe
Stopped for
Evacuation
fireDetected
entry/
open all doors
sound alarm
trainStarted
trainStopped
Moving
entry/
Executable UML
fireDetected
Emergency stop
in progress
entry/
apply emergency brake
Including state-independent behaviour
Updating fuel
during emergency
trainMadeSafe
Updating fuel
while stopped
entry/
this.fuelLevel = fuelLevel
fireDetected
entry/
this.fuelLevel = fuelLevel
fuelLevelUpdated
Stopped
entry/
trainMadeSafe
Stopped for
Evacuation
fireDetected
entry/
open all doors
sound alarm
trainStarted
trainStopped
trainStopped
Updating fuel during
emergency stop
trainStarted
trainStopped
Moving
fireDetected
entry/
trainStopped
fuelLevelUpdated
Updating fuel
while moving
entry/
this.fuelLevel = fuelLevel
Executable UML
Emergency stop
in progress
entry/
apply emergency brake
fireDetected
entry/
this.fuelLevel = fuelLevel
fuelLevelUpdated
Including state-independent behaviour
Updating fuel
during emergency
trainMadeSafe
Updating fuel
while stopped
entry/
this.fuelLevel = fuelLevel
fireDetected
entry/
this.fuelLevel = fuelLevel
fuelLevelUpdated
Stopped
entry/
trainMadeSafe
Stopped for
Evacuation
fireDetected
entry/
open all doors
sound alarm
trainStarted
trainStopped
trainStopped
Updating fuel during
emergency stop
trainStarted
trainStopped
Moving
fireDetected
entry/
trainStopped
fuelLevelUpdated
Updating fuel
while moving
entry/
this.fuelLevel = fuelLevel
Executable UML
Emergency stop
in progress
entry/
apply emergency brake
fireDetected
entry/
this.fuelLevel = fuelLevel
fuelLevelUpdated
State-independent behaviour
Executable UML
Ideal Class representation
Name Compartment
Aircraft
Attribute Compartment
registrationNo
airframeNo
altitude
latitude
longitude
Operation Compartment
createAircraft
getPosition
Signal Compartment
Executable UML
takeOffClearanceGranted
landingClearanceGranted
readyForDeparture
aircraftClearedRunway
State model for Aircraft Class
Flying
aircraftClearedRunway
Taking Off
landingClearanceGranted
Landing
takeOffClearanceGranted
Waiting for Clearance
to Take-Off
aircraftClearedRunway
On Ground
readyForDeparture
In most UML tools, this is the place
where the signal events associated with
a class are recorded.
Executable UML
Microwave Oven - being more systematic
 When the door is closed and
the button pressed, the oven
cooks for 1 minute
MicrowaveOven
manufacturer
serialNumber {I}
lightOn
doorOpen
powerTubeEnergized
cookingTimeRemaining
Executable UML
 The light is on when cooking or
when the door is open
 Further button presses increase
the cooking time by 1 minute
each
 When the timer times out, the
power tube and light are turned
off and a warning beep is
emitted
Events and Actions
Signals or Events
Actions
 doorOpened
 Turn on the light
 doorClosed
 Turn off the light
 buttonPressed
 timerTimesOut (counts
down to zero)
 Energize the power tube
 De-energize the power
tube
 Set the timer for 1 min.
 Add 1 min. to the timer
 Clear the timer
 Sound the beeper
Executable UML
First version of Statechart
The numbering does not
imply any sequencing
doorClosed
doorClosed
2. Door Open
1. Ready to Cook
entry/
turn on light
entry/
turn off light
4. Cooking
Interrupted
entry/
turn off light
de-energize power tube
clear timer
buttonPressed
doorOpened
3. Cooking
doorOpened
entry/
turn on light
energize power tube
set timer for 1 minute
5. Cooking Complete
doorOpened
Executable UML
entry/
turn off light
de-energize power tube
clear timer
timerTimesOut
The Statechart is not a complete model
doorClosed
Can a door closed event
doorClosed be generated when
the door is already closed?
2. Door Open
What happens if we
entry/
turn
on light
press
the button when
the door is open?
1. Ready to Cook
4. Cooking
Interrupted
entry/
turn off light
de-energize power tube
clear timer
buttonPressed
entry/
turn off light
doorOpened
doorOpened
What happens if we
3. Cooking
press the button while
entry/
cooking?
turn on light
energize power tube
set timer for 1 minute
What
happensComplete
if we
5. Cooking
the button when
doorOpened press
entry/
cooking
is complete?
turn off light
de-energize power tube
clear timer
Executable UML
timerTimesOut
State Transition Table
Ready to
Cook
buttonPressed
doorOpened
Cooking
Door Open
Cooking
Cooking
Interrupted
Cooking
Complete
Door Open
doorClosed
Cooking
Complete
Cooking
Interrupted
Door Closed
Door Open
Door Closed
Executable UML
timerTimesOut
State Transition Table
Ready to
Cook
buttonPressed
doorOpened
Cooking
Door Open
Cooking
Cooking
Complete
doorClosed
Cooking
Interrupted
Cooking
Cooking
Complete
Door Open
Cooking
Interrupted
Door Closed
Door Open
Door Closed
Executable UML
timerTimesOut
New Transition
4. Cooking
Interrupted
doorClosed
entry/
turn off light
de-energize power tube
clear timer
doorClosed
2. Door Open
1. Ready to Cook
entry/
turn on light
entry/
turn off light
buttonPressed
doorOpened
3. Cooking
doorOpened
buttonPressed
entry/
turn on light
energize power tube
set timer for 1 minute
5. Cooking Complete
doorOpened
Executable UML
entry/
turn off light
de-energize power tube
clear timer
timerTimesOut
State Transition Table
buttonPressed
doorOpened
Ready to
Cook
Cooking
Door Open
Cooking
Cooking?
Cooking
Interrupted
Cooking
Complete
Cooking
Door Open
doorClosed
Cooking
Complete
Cooking
Interrupted
Door Closed
Door Open
Door Closed
Executable UML
timerTimesOut
Is this correct?
4. Cooking
Interrupted
doorClosed
entry/
turn off light
de-energize power tube
clear timer
doorClosed
2. Door Open
1. Ready to Cook
entry/
turn on light
entry/
turn off light
buttonPressed
doorOpened
3. Cooking
doorOpened
buttonPressed
entry/
turn on light
energize power tube
set timer for 1 minute
5. Cooking Complete
doorOpened
Executable UML
entry/
turn off light
de-energize power tube
clear timer
buttonPressed
timerTimesOut
Discovering new states and events
doorClosed
doorClosed
2. Door Open
1. Ready to Cook
entry/
turn on light
entry/
turn off light
4. Cooking
Interrupted
entry/
turn off light
de-energize power tube
clear timer
doorOpened
buttonPressed
buttonPressed
3. Cooking
doorOpened
doorOpened
buttonPressed
entry/
turn on light
energize power tube
set timer for 1 minute
6. Cooking
Extended
entry/
add 1 min. to timer
5. Cooking Complete
doorOpened
Executable UML
entry/
turn off light
de-energize power tube
clear timer
timerTimesOut
timerTimesOut
buttonPressed
State Transition Table
buttonPressed
doorOpened
Ready to
Cook
Cooking
Door Open
Cooking
Cooking
Extended
Cooking
Interrupted
Cooking
Complete
Cooking
Door Open
doorClosed
Cooking
Complete
Cooking
Interrupted
Door Closed
Door Open
Door Closed
Cooking
Extended
Cooking
Extended
Executable UML
Cooking
Interrupted
timerTimesOut
Cooking
Complete
Event ignored
buttonPressed
event will
4. Cooking
have
no effect
Interrupted
doorClosed
doorClosed
buttonPressed event
2.
Door
will
haveOpen
no effect 1. Ready to Cook
entry/
turn on light
entry/
turn off light
timerTimesOut
event
de-energize power
tube
clearhave
timer no effect
will
doorOpened
buttonPressed
buttonPressed
entry/
turn off light
3. Cooking
doorOpened
doorOpened
buttonPressed
entry/
turn on light
energize power tube
set timer for 1 minute
6. Cooking
Extended
entry/
add 1 min. to timer
5. Cooking Complete
doorOpened
Executable UML
entry/
turn off light
de-energize power tube
clear timer
timerTimesOut
timerTimesOut
buttonPressed
State Transition Table
buttonPressed
doorOpened
doorClosed
Ready to
Cook
Cooking
Door Open
Event Ignored
Cooking
Cooking
Extended
Cooking
Interrupted
Cooking
Complete
Cooking
Complete
Cooking
Door Open
Cooking
Event Ignored
Interrupted
Door Closed
Door Open
Event Ignored
Door Closed
Cooking
Extended
Cooking
Extended
Executable UML
Cooking
Interrupted
timerTimesOut
Event Ignored
Cooking
Complete
Can’t Happen
another doorOpen
event
4. Cooking
can’t happen
here!
Interrupted
doorClosed
doorClosed
2. Door Open
entry/
turn on light
entry/
turn off light
de-energize power tube
clear timer
1. Ready to Cook
doorOpened
another doorClosed event
entry/
can’t happen here! buttonPressed
buttonPressed
turn off light
3. Cooking
doorOpened
doorOpened
buttonPressed
entry/
turn on light
energize power tube
set timer for 1 minute
6. Cooking
Extended
entry/
add 1 min. to timer
5. Cooking Complete
doorOpened
Executable UML
entry/
turn off light
de-energize power tube
clear timer
timerTimesOut
timerTimesOut
buttonPressed
State Transition Table
buttonPressed
doorOpened
doorClosed
Ready to
Cook
Cooking
Door Open
Can’t Happen Event Ignored
Cooking
Cooking
Extended
Cooking
Interrupted
Can’t Happen Cooking
Complete
Cooking
Complete
Cooking
Door Open
Can’t Happen Can’t Happen
Cooking
Event Ignored
Interrupted
Can’t Happen
Door Closed
Event Ignored
Door Open
Event Ignored
Can’t Happen
Door Closed
Can’t Happen
Cooking
Extended
Cooking
Extended
Cooking
Interrupted
Can’t Happen Cooking
Complete
Executable UML
timerTimesOut
Practical benefits for Testing
 Writing out the State Transition table helps
identify missing transitions
 We
found one major fault in the first Philips DVD player
in this way, even before executing the test cases
 Each cell in the table is a potential test case.
However, remember:
 You
do need to execute “Event Ignored” tests
 You do not need to execute “Can’t Happen” tests
 in fact you will be physically unable to!
Executable UML
Summary
 We have seen how Statecharts can be used to
model the lifecycles of class instances
 Restricting usage of full UML to a precise subset
adds clarity to the models, but
 Statecharts are an incomplete view of the full
state behaviour
 Use
State Transition Tables to help identify missing
transitions and states
 Base your test cases on the State Transition Tables and
not on the Statechart
 See Chapter 9 of Mellor and Balcer
Executable UML