91.411: Software Engineering I
Chapter 3: Software Design – Part 4
Activity Diagrams and State Diagrams
Sarita Bassil, Ph.D.
Outline
1. Activity Diagrams
A. Introduction B. Notation
C. Examples
91.411 - Soft. Eng. I S. Bassil - Spring 2013 2
2. State Diagrams
A. Introduction B. Main concepts C. Actions
D. Guards
E. Kinds of transitions
F. Composite state
Outline
1. Activity Diagrams
A. Introduction B. Notation
C. Examples
2. State Diagrams
A. Introduction B. Main concepts C. Actions
D. Guards
E. Kinds of transitions
F. Composite state
1A. Activity Diagrams – Introduction
• Activity diagrams are used to describe the sequence of activities.
– At the analysis
1level, we use activity diagrams to describe use cases.
• Allows for describing the activities of a use case at a high level of abstraction.
– At the design level, we use activity diagrams to describe algorithms.
• Activity diagrams allow for expressing sequential and concurrent activities.
1 The first step within the software development process, i.e., the step of requirements analysis and specification.
91.411 - Soft. Eng. I S. Bassil - Spring 2013 4
Outline
1. Activity Diagrams
A. Introduction B. Notation C. Examples
2. State Diagrams
A. Introduction B. Main concepts C. Actions
D. Guards
E. Kinds of transitions
F. Composite state
1B. Activity Diagrams – Notation
Elements of activity diagrams
• Activity
• Transition (not labeled)
It is caused by the completion of the previous activity.
• Synchronization bar (join and fork)
It describes the coordination of activities.
• Decision diamond It shows decisions.
• Start and stop markers
• Conditions
Used on transition.
91.411 - Soft. Eng. I 6
activityName
[ condition ]
S. Bassil - Spring 2013
Outline
1. Activity Diagrams
A. Introduction B. Notation
C. Examples
2. State Diagrams
A. Introduction B. Main concepts C. Actions
D. Guards
E. Kinds of transitions
F. Composite state
1C. Activity Diagrams - Examples
Activity diagram of the library case study
find book on shelf
wait in queue [borrower]
[returner]
record borrowing record return put book back on shelf [borrowing]
[returning]
prepare for next member
member librarian
1C. Activity Diagrams - Examples
Use case: Withdraw money from an ATM machine
• Principal actor:
– Client
• Pre-conditions:
– The ATM contains money bills, it is waiting for an operation, it is not down, it is not being maintained, it is ready to accept a bank card.
• Post-conditions:
– The ATM already returned the bank card, if money has been withdrawn, the account balance is equal to the initial balance minus the withdrawn amount, if not, the initial account balance is still the same.
• Trigger:
– A client inserts its bank card into the ATM machine.
1C. Activity Diagrams - Examples
Use case: Withdraw money from an ATM machine
•Main flow:
1. The client inserts his/her bank card
2. The system reads the card and verifies its validity
3. The system asks the user to enter his/her confidential code (PIN) 4. The client enters his PIN
5. The system verifies that the code corresponds to the card 6. The client picks the withdraw operation
7. The system asks for the amount to withdraw 8. The client enters the amount
9. The system verifies that the amount is less than or equal to the balance 10.The system updates the balance
11.The system delivers money bills, printouts receipt and ejects card
91.411 - Soft. Eng. I S. Bassil - Spring 2013 10
1C. Activity Diagrams - Examples
Use case: Withdraw money from an ATM machine
•Alternative flows:
2. (A) Invalid card: Within step (2) if the card is found invalid, the system displays the error message: “invalid card”, swallows the card, and the use case is terminated.
5. (A) Incorrect PIN and first/second attempt: Within step (5), if the entered PIN is invalid and first or second attempt, the system displays the
error message: “incorrect PIN”, and go back to step 3 in main flow.
5. (B) Incorrect PIN and third attempt: Within step (5), if the entered PIN is invalid and third attempt, the system displays the error message:
“incorrect PIN”, swallows the card, and the use case is terminated.
9. (A) balance < amount to be withdrawn: Within step (9), if the entered amount is greater than the available balance, the system displays the warning message: “amount > available balance”, and go back to step 7 in main flow.
1C. Activity Diagrams - Examples
Use case: Withdraw money from an ATM machine
Activity diagram to be done in class.
91.411 - Soft. Eng. I S. Bassil - Spring 2013 12
Outline
1. Activity Diagrams
A. Introduction B. Notation
C. Examples
2. State Diagrams
A. Introduction B. Main concepts C. Actions
D. Guards
E. Kinds of transitions F. Composite state G. Representation of
valid behaviors
2A. State Diagrams - Introduction
•A state diagram allows for describing the behavior of a class object taking into account the received messages.
•Can be used as well for describing the execution of an operation, the enactment of a use case, etc.
14
Locked
Available unlock Sold
allocated_season_ticket
lock buy
State diagram for a show ticket to be sold on-line
elapsed_time
Outline
1. Activity Diagrams
A. Introduction B. Notation
C. Examples
2. State Diagrams
A. Introduction
B. Main concepts C. Actions
D. Guards
E. Kinds of transitions F. Composite state G. Representation of
valid behaviors
2B. State Diagrams – Main concepts
Elements of state diagrams
• State
• Transition (labeled)
• Start marker
91.411 - Soft. Eng. I 16
stateName event / action
An event is something
done to the object, such as it is being sent a message.
An action is something that the object does, such as it sends a message.
S. Bassil - Spring 2013
2B. State Diagrams – Main concepts
State
• Describes a specific moment of an instance life.
• An instance can’t be in more than one state at a time.
• A state is described by a name (usually).
• All class instances that are in a specific state react similarly to events.
• Instances that are in a specific state, at a specific time
– Have similar attributes’ values.– Are waiting for a particular event – Execute a particular activity
• In UML, there exists different state types: simple, concurrent, composite, etc.
stateName
2B. State Diagrams – Main concepts
Transition
• An (output) transition defines an instance’s response to a specific event. The instance being in a specific state, initially.
• The transitions are labeled using an event and (optionally) an action.
– Event: everything that happens and might affect an instance (trigger of the transition).
– Action: operation realized when a transition is realized.
91.411 - Soft. Eng. I 18
event / action
on loan on the shelf
return() / book.returned(self) borrow() / book.borrowed(self)
State diagram of class Copy
S. Bassil - Spring 2013
2B. State Diagrams – Main concepts
Events types (an event can be parameterized) Event
Type Description Syntax
Call / signal event
Reception of a synchronous message (for
which the sender is waiting for a response – the message requests that an operation be
performed) – as seen in the previous example.
op(p1:type, p2:type, )
Change event
Occurs when a condition changes from false to
true. when(expression)
Time event
Absolute time reached or a length of time elapsed, can be the elapsed time since the state was entered.
after(time)
2B. State Diagrams – Main concepts
Actions types (actions can take arguments)
91.411 - Soft. Eng. I 20
Action Type Description Syntax
Assignment Assigns a value to a
variable. target := expression
Call / message sending
(Synchronous) invocation of an object’s operation, can return a value.
opname(arg1, arg2, )
object.opname(arg1, arg2, ) Creation Creation of a new instance. new Cname(arg1, arg2, ) Destruction Destruction of an instance. object.destroy()
Miscellaneous Action described in another
language. [ description]
Sequence Sequence of actions. action1; action2;
S. Bassil - Spring 2013
Outline
1. Activity Diagrams
A. Introduction B. Notation
C. Examples
2. State Diagrams
A. Introduction B. Main concepts C. Actions
D. Guards
E. Kinds of transitions F. Composite state G. Representation of
valid behaviors
2C. State Diagrams – Input/output actions
• Some actions can be attached to a state instead of a transition.
– Input action: executed each time we enter a state.
• Notation: entry / action
– Output action: executed each time we exit a state.
• Notation: exit / action
91.411 - Soft. Eng. I 22
on loan
exit/
book.returned(self)
on the shelf
exit/
book.borrowed(self)
return() borrow()
State diagram of class Copy, with exit actions.
S. Bassil - Spring 2013
2C. State Diagrams – Input/output actions
STATE_2 entry / a2()
exit / b2()
STATE_1
entry / a1() exit / b1()
eventB / c2() eventA / c1()
What order do you think the actions will be executed in?
Outline
1. Activity Diagrams
A. Introduction B. Notation
C. Examples
91.411 - Soft. Eng. I 24
2. State Diagrams
A. Introduction B. Main concepts C. Actions
D. Guards
E. Kinds of transitions F. Composite state G. Representation of
valid behaviors
S. Bassil - Spring 2013
2D. State Diagrams – Guards
• Sometimes the occurrence of the same event in the same state may or may not cause a change of state, depending on the exact values of the
object’s attributes.
– The transition is conditional upon the evaluation of a guard
• If guard is true, the transition fires.
• If guard is false, the transition does not fire.
• Notation: event [guard]
2D. State Diagrams – Guards
91.411 - Soft. Eng. I 26
Not borrowable
returned()
borrowed()[last copy]
State diagram for class Book .
Borrowable returned()
borrowed() [not last copy]
This transition is important: returned() is an expected message, and it is
understood by the object in this state.
S. Bassil - Spring 2013
• Guard
– Conditional expression.
– Evaluated only when the event is triggered.
– May contain instances’ attributes, or parameters from the associated event.
• When the same event is associated with many
transitions, a guard (condition) can be added to specify the context and determine the transition to do.
• Guards associated with the same event, on the output transitions of a specific state, must be exclusive.
2D. State Diagrams – Guards
Outline
1. Activity Diagrams
A. Introduction B. Notation
C. Examples
91.411 - Soft. Eng. I 28
2. State Diagrams
A. Introduction B. Main concepts C. Actions
D. Guards
E. Kinds of transitions F. Composite state
G. Representation of valid behaviors
S. Bassil - Spring 2013
• External transition
– Standard transition that generates a state change, and all the corresponding actions (input/output actions, and actions related to the transition).
• Internal transition
– Transition that does not generate a state change, and does trigger only the actions associated with this transition (not the input/output actions, cf. the figure on the next slide).
• Completion transition
– Transition not activated by an event; it is implicitly activated when we reach the end of the activities’ flow.
2E. State Diagrams – Kinds of transitions
2E. State Diagrams – Kinds of transitions
Enter password
internal transitions
external transition
Outline
1. Activity Diagrams
A. Introduction B. Notation
C. Examples
2. State Diagrams
A. Introduction B. Main concepts C. Actions
D. Guards
E. Kinds of transitions F. Composite state G. Representation of
valid behaviors
• “Specialized” state composed of many sub- states.
– Sequential sub-states.
– Concurrent sub-states.
91.411 - Soft. Eng. I 32
2F. State Diagrams – Composite state
S. Bassil - Spring 2013
2F. State Diagrams – Composite state (sequential)
Show ticket purchase
Exit / eject_card() [failed_identification]
[succeeded_identification] / Initialize_selection() Identification
Selection choose(seat) / add_to_selection(seat)
Confirmation
Sale inactive
insert_card
click_cancel click -restart
click -buy
click-confirm
• When a composite state is activated, one of its sub-states is necessarily activated.
• Entry to and exit from a composite state.
– An input transition to a composite state is implicitly taken to its initial state.
– A transition towards the final state of a composite
state activates implicitly a completion transition exiting the composite state.
91.411 - Soft. Eng. I 34
2F. State Diagrams – Composite state
S. Bassil - Spring 2013
• When a transition enters/exists by passing through one or more imbricated composite states, all input/output actions are executed.
– Input actions of the most external state is executed first.
– Output actions of the most internal state is executed first.
2F. State Diagrams – Composite state
91.411 - Soft. Eng. I 36
2F. State Diagrams – Composite state (concurrent)
State diagram of a course
Unfinished course
Semester project
Final exam
terminated_project
success
failure
Succeeded course
Failed course
grade >= 60
grade < 60 terminated_lab terminated_lab
S. Bassil - Spring 2013
2F. State Diagrams – Composite state (concurrent)
State diagram of a course (another possible notation)
Unfinished course
Semester project
Final exam
terminated_project
failure
Succeeded course
Failed course
grade >= 60
grade < 60 terminated_lab
Reference to a state sub-
diagram
38
Wait for order
execute command
help command
entry / display help screen exit / clean help screen
question / show answer quit
Outline
1. Activity Diagrams
A. Introduction B. Notation
C. Examples
2. State Diagrams
A. Introduction B. Main concepts C. Actions
D. Guards
E. Kinds of transitions F. Composite state G. Representation of
valid behaviors
• A state diagram should represent all and only the valid states and transitions of a class
instances.
• If a state diagram, for a specific state S, doesn’t have a transition labeled by an event X, this
means that the instance should not react to this event in this state.
91.411 - Soft. Eng. I 40
2G. State Diagrams – Representation of valid behaviors
S. Bassil - Spring 2013