executable uml the models are the code - executable uml lecture 7 - getting a lifecycle paul krause
TRANSCRIPT
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
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
e1 [cond] / action1;action2
event name
guardcondition actions
StateA
entry/ actionsexit/ actions
StateB
entry/ actionsexit/ actions
Executable UML
What Happens?
e1 [y < 10] / x = 5StateA
exit/ x = 10
StateB
entry/ actionexit/ action
e1 [x < 10] / y = 5StateC
exit/ y = 10
StateD
entry/ actionexit/ action
initially x = 0 & y = 0
Executable UML
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/
Moving
entry/
Emergency stopin progressentry/
apply emergency brake
Stopped forEvacuationentry/open all doorssound alarm
trainStopped
fireDetected
trainStartedtrainStopped
fireDetected
trainMadeSafe
Executable UML
Including state-independent behaviour
Stopped
entry/
Moving
entry/
Emergency stopin progressentry/
apply emergency brake
Stopped forEvacuationentry/open all doorssound alarm
trainStopped
fireDetected
trainStartedtrainStopped
fireDetected
trainMadeSafe
Updating fuelwhile stopped
entry/
this.fuelLevel = fuelLevel
Updating fuelduring emergency
entry/
this.fuelLevel = fuelLevel
Updating fuel duringemergency stop
entry/
this.fuelLevel = fuelLevel
Updating fuelwhile moving
entry/
this.fuelLevel = fuelLevel
trainMadeSafe
trainStopped fuelLevelUpdated
fireDetected
fireDetected
fuelLevelUpdated
trainStopped
fuelLevelUpdated
trainStarted
Executable UML
Including state-independent behaviour
Stopped
entry/
Moving
entry/
Emergency stopin progressentry/
apply emergency brake
Stopped forEvacuationentry/open all doorssound alarm
trainStopped
fireDetected
trainStartedtrainStopped
fireDetected
trainMadeSafe
Updating fuelwhile stopped
entry/
this.fuelLevel = fuelLevel
Updating fuelduring emergency
entry/
this.fuelLevel = fuelLevel
Updating fuel duringemergency stop
entry/
this.fuelLevel = fuelLevel
Updating fuelwhile moving
entry/
this.fuelLevel = fuelLevel
trainMadeSafe
trainStopped fuelLevelUpdated
fireDetected
fireDetected
fuelLevelUpdated
trainStopped
fuelLevelUpdated
trainStarted
Executable UML
Ideal Class representation
Aircraft
registrationNoairframeNoaltitudelatitudelongitude
createAircraftgetPosition
takeOffClearanceGrantedlandingClearanceGrantedreadyForDepartureaircraftClearedRunway
Name Compartment
Attribute Compartment
Operation Compartment
Signal Compartment
Executable UML
State model for Aircraft Class
Flying
Landing
On Ground
Waiting for Clearanceto Take-Off
Taking OfflandingClearanceGranted
aircraftClearedRunway
readyForDeparture
takeOffClearanceGranted
aircraftClearedRunway
In most UML tools, this is the placewhere the signal events associated witha 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
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
MicrowaveOven
manufacturerserialNumber {I}lightOndoorOpenpowerTubeEnergizedcookingTimeRemaining
Executable UML
Events and Actions
Signals or Events
doorOpened
doorClosed
buttonPressed
timerTimesOut (counts down to zero)
Actions
Turn on the light
Turn off the light
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
4. CookingInterruptedentry/turn off lightde-energize power tubeclear timer
5. Cooking Complete
entry/turn off lightde-energize power tubeclear timer
3. Cooking
entry/turn on lightenergize power tubeset timer for 1 minute
2. Door Open
entry/turn on light
1. Ready to Cook
entry/turn off light
doorClosed
doorOpened
doorOpened
buttonPressed
timerTimesOut
doorOpened
doorClosed
The numbering does notimply any sequencing
Executable UML
The Statechart is not a complete model
4. CookingInterruptedentry/turn off lightde-energize power tubeclear timer
5. Cooking Complete
entry/turn off lightde-energize power tubeclear timer
3. Cooking
entry/turn on lightenergize power tubeset timer for 1 minute
2. Door Open
entry/turn on light
1. Ready to Cook
entry/turn off light
doorClosed
doorOpened
doorOpened
buttonPressed
timerTimesOut
doorOpened
doorClosed
What happens if wepress the button whencooking is complete?
What happens if wepress the button while
cooking?
What happens if wepress the button when
the door is open?
Can a door closed eventbe generated when
the door is already closed?
Executable UML
State Transition TablebuttonPressed
doorOpened
doorClosed
timerTimesOut
Ready to Cook
Cooking Door Open
Cooking Cooking Interrupted
Cooking Complete
Cooking Complete
Door Open
Cooking Interrupted
Door Closed
Door Open
Door Closed
Executable UML
State Transition TablebuttonPressed
doorOpened
doorClosed
timerTimesOut
Ready to Cook
Cooking Door Open
Cooking Cooking Interrupted
Cooking Complete
Cooking Complete
Cooking Door Open
Cooking Interrupted
Door Closed
Door Open
Door Closed
Executable UML
New Transition
4. CookingInterruptedentry/turn off lightde-energize power tubeclear timer
5. Cooking Complete
entry/turn off lightde-energize power tubeclear timer
3. Cooking
entry/turn on lightenergize power tubeset timer for 1 minute
2. Door Open
entry/turn on light
1. Ready to Cook
entry/turn off light
doorClosed
doorOpened
doorOpened
buttonPressed
timerTimesOut
doorOpened
doorClosed
buttonPressed
Executable UML
State Transition TablebuttonPressed
doorOpened
doorClosed
timerTimesOut
Ready to Cook
Cooking Door Open
Cooking Cooking? Cooking Interrupted
Cooking Complete
Cooking Complete
Cooking Door Open
Cooking Interrupted
Door Closed
Door Open
Door Closed
Executable UML
Is this correct?
4. CookingInterruptedentry/turn off lightde-energize power tubeclear timer
5. Cooking Complete
entry/turn off lightde-energize power tubeclear timer
3. Cooking
entry/turn on lightenergize power tubeset timer for 1 minute
2. Door Open
entry/turn on light
1. Ready to Cook
entry/turn off light
doorClosed
doorOpened
doorOpened
buttonPressed
timerTimesOut
doorOpened
doorClosed
buttonPressed
buttonPressed
Executable UML
Discovering new states and events
4. CookingInterruptedentry/turn off lightde-energize power tubeclear timer
5. Cooking Completeentry/turn off lightde-energize power tubeclear timer
3. Cooking
entry/turn on lightenergize power tubeset timer for 1 minute
2. Door Open
entry/turn on light
1. Ready to Cook
entry/turn off light
doorClosed
doorOpened
doorOpened
buttonPressed
timerTimesOut
doorOpened
doorClosed
buttonPressed
6. CookingExtendedentry/add 1 min. to timer
buttonPressed
timerTimesOut
doorOpened
buttonPressed
Executable UML
State Transition TablebuttonPressed
doorOpened
doorClosed
timerTimesOut
Ready to Cook
Cooking Door Open
Cooking Cooking Extended
Cooking Interrupted
Cooking Complete
Cooking Complete
Cooking Door Open
Cooking Interrupted
Door Closed
Door Open
Door Closed
Cooking Extended
Cooking Extended
Cooking Interrupted
Cooking Complete
Executable UML
Event ignored
4. CookingInterruptedentry/turn off lightde-energize power tubeclear timer
5. Cooking Completeentry/turn off lightde-energize power tubeclear timer
3. Cooking
entry/turn on lightenergize power tubeset timer for 1 minute
2. Door Open
entry/turn on light
1. Ready to Cook
entry/turn off light
doorClosed
doorOpened
doorOpened
buttonPressed
timerTimesOut
doorOpened
doorClosed
buttonPressed
6. CookingExtendedentry/add 1 min. to timer
buttonPressed
timerTimesOut
doorOpened
buttonPressed
buttonPressed event willhave no effect
buttonPressed eventwill have no effect
timerTimesOut eventwill have no effect
Executable UML
State Transition TablebuttonPressed
doorOpened
doorClosed
timerTimesOut
Ready to Cook
Cooking Door Open Event Ignored
Cooking Cooking Extended
Cooking Interrupted
Cooking Complete
Cooking Complete
Cooking Door Open
Cooking Interrupted
Event Ignored Door Closed
Event Ignored
Door Open
Event Ignored Door Closed
Cooking Extended
Cooking Extended
Cooking Interrupted
Cooking Complete
Executable UML
Can’t Happen
4. CookingInterruptedentry/turn off lightde-energize power tubeclear timer
5. Cooking Completeentry/turn off lightde-energize power tubeclear timer
3. Cooking
entry/turn on lightenergize power tubeset timer for 1 minute
2. Door Open
entry/turn on light
1. Ready to Cook
entry/turn off light
doorClosed
doorOpened
doorOpened
buttonPressed
timerTimesOut
doorOpened
doorClosed
buttonPressed
6. CookingExtendedentry/add 1 min. to timer
buttonPressed
timerTimesOut
doorOpened
buttonPressed
another doorOpen eventcan’t happen here!
another doorClosed eventcan’t happen here!
Executable UML
State Transition TablebuttonPressed
doorOpened
doorClosed
timerTimesOut
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 Interrupted
Event Ignored 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
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