Activity Diagrams
Modelling Workflows with UML Activity Diagrams
An activity diagram is the UML diagram that shows the order in which actions happen in a process. That process might be a business workflow, the steps of a use case, or an algorithm whose parts can run in parallel. A class diagram shows structure, and a state diagram follows one object through its states. An activity diagram follows the work itself, including its choices and concurrent branches. You use one to agree a workflow with stakeholders, to spot missing cases before coding, and to plan tests.
Nodes, edges and tokens
UML defines what an activity diagram means in terms of tokens. A token is a marker that travels along the edges. An action runs when a token reaches it and passes the token on when it finishes. Each kind of node has its own token rule.
Node | Notation | Token rule |
|---|---|---|
Initial node | Filled circle | Places one token on its outgoing edge when the activity starts |
Action | Rounded rectangle | Runs when a token arrives, then offers a token to its outgoing edge |
Decision | Diamond, one edge in, guarded edges out | Sends the token along the one edge whose guard is true |
Merge | Diamond, several edges in, one out | Passes on each arriving token without waiting for others |
Fork | Bar, one edge in, several out | Copies the token onto every outgoing edge |
Join | Bar, several edges in, one out | Waits for a token on every incoming edge, then emits one |
Activity final | Bullseye | Ends the whole activity and removes every token |
Flow final | Circle containing an X | Consumes one token while other flows continue |
Each outgoing edge of a decision carries a guard, such as [in stock]. The guards on one decision must be mutually exclusive and together cover every case. The guard [else] is the simplest way to guarantee that coverage.
Concurrency means unconstrained order
A fork does not promise that its branches run at the same instant on different processors. It says that actions in different branches have no required order relative to each other, while each branch keeps its own internal order. Suppose a single worker performs branches containing n1,n2,…,nk actions, one action at a time. The number of valid orderings is the multinomial coefficient:
Example: In the diagram above, suppose the payment branch had two actions, Charge card then Send receipt, while Pack items stayed a single action. One worker could order the three actions in 3!/(2!1!)=3 ways: Pack items first, between the two payment actions, or last.
Activity partitions, often called swimlanes, show who performs each action. Object nodes, drawn as plain rectangles, show data such as an Order passing between actions. Neither changes the token rules.
Counting scenarios
Testers and reviewers often count the distinct scenarios through an acyclic diagram. A scenario is one combination of guard outcomes in a single run. Work from the innermost regions outward:
A decision adds because exactly one of its branches is taken. A fork multiplies because every branch runs, so each combination of the branches' own choices is a different scenario. A branch with no decisions counts as one scenario.
Example: In the order diagram, the stock decision gives 1+1=2 scenarios and the fork region gives 1×1=1, so the activity has 2×1=2 scenarios. Now suppose Pack items were followed by a two-way decision on gift wrapping. The fork region would then give 1×2=2 scenarios, and the activity would have 2×2=4.
What to remember
- Tokens give the diagram its meaning: trace one token at a time to check behaviour.
- Decision and merge handle alternatives; fork and join handle concurrency. Never close a decision with a join.
- Guards on a decision must be exclusive and complete; [else] guarantees completeness.
- An activity final ends everything, while a flow final ends only one flow.
- Decisions add scenarios; sequences and forks multiply them.
- One worker performing concurrent branches has N=(∑ni)!/∏ni! valid orderings.
Example: Scenarios and concurrency in an insurance-claim workflow
An insurer models claim handling with the activity diagram below. After Receive claim, a decision checks the policy. An invalid policy leads to Send rejection and an activity final node; a valid policy reaches a fork. Branch A runs Assess damage, then decides on the assessed cost in dollars: [under 1000] leads to Auto-approve, and [1000 or more] leads to Assign adjuster. Branch B runs Check fraud records, then a three-way decision: [flagged] leads to Escalate, [suspicious] leads to Request documents, and [clear] goes straight to the merge. Both branches meet at a join before Issue payment and the final node. A scenario is one combination of guard outcomes taken during a single run.
a) How many distinct scenarios can a single run of the activity follow?
Count from the innermost regions outward. Branch A's decision has two alternatives of one action each, so branch A contributes ________ scenarios.
Branch B's decision has three alternatives. The [clear] edge runs no action, but it is still one way through, so branch B contributes ________ scenarios.
Both branches of the fork always run, so their choices combine. The fork region gives 2 × 3 = ________ scenarios. The policy decision then adds the rejection branch, which is one scenario:
The activity has 7 scenarios. Adding every branch count, 1+2+3=6, would treat the fork as a choice; but a fork runs both branches, so it multiplies.
b) What is the minimum number of test runs that traverses every edge of the diagram at least once?
The rejection edge needs a run of its own, because a rejected claim never reaches the fork. That is one run.
Every valid run takes exactly one of branch B's three outgoing edges: [flagged], [suspicious] or [clear]. Covering all three therefore needs ________ runs with a valid policy. Branch A's two edges fit inside those three runs, and every valid run also covers the fork, join and Issue payment edges.
One minimal set is: an invalid policy; a valid policy, 600 dollars, flagged; a valid policy, 2400 dollars, suspicious; and a valid policy at any cost, clear. The minimum is 4 runs. Covering every action would need only 3, because the [clear] edge carries no action; edge coverage is the stricter criterion.
c) Take the scenario for a valid policy with an assessed cost of 2400 dollars and a [suspicious] fraud result. One clerk performs all the concurrent actions one at a time. In how many different orders can the clerk perform them?
Branch A runs Assess damage then Assign adjuster, which is ________ actions in a fixed order. Branch B runs Check fraud records then Request documents, also 2 actions in a fixed order. The fork allows any interleaving that keeps each branch's internal order.
The clerk performs ________ actions in all. Choose which two positions go to branch A; branch B fills the rest in its own order:
The clerk has 6 valid orders. As a check, writing A and B for the branches, the patterns are AABB, ABAB, ABBA, BAAB, BABA and BBAA. Receive claim always comes before the fork and Issue payment after the join, so neither changes the count.
d) During review, someone proposes rewriting branch A's guards as [cost<1000] and [cost>1000]. What happens to a claim assessed at exactly 1000 dollars, and how should the guards be fixed?
At a cost of exactly 1000 dollars, both guards evaluate to ________. The token at the decision has no edge it may take, so it stays there, and branch A never delivers a token to the join. The join waits indefinitely, so Issue payment never runs, even though branch B finishes normally. In other words, a gap in one decision's guards stalls the whole activity, not just one branch.
To fix this, restore complete coverage: write [cost≥1000] on the second edge, or replace it with [else]. The original guards, [under 1000] and [1000 or more], were already exclusive and complete, so the proposed rewrite should be rejected.
A team models its continuous-integration pipeline as a UML activity diagram. The initial node flows to the action Pull latest commit. The remaining flows are listed below in no particular order:
- Run unit tests flows to decision D2, whose outgoing edges carry the guards [coverage < 80%] to Generate coverage report and [coverage ≥ 80%] to merge node M.
- Deploy to staging flows to Run smoke tests, which flows to an activity final node.
- Pull latest commit flows to Build container image.
- Generate coverage report flows to merge node M.
- Build container image flows to decision D1, whose outgoing edges carry the guards [build failed] to Notify developer, which flows to its own activity final node, and [build succeeded] to Run unit tests.
- Merge node M flows to Deploy to staging.
In one run the build succeeds and the unit tests report 72% line coverage. Arrange the actions that execute in this run in the order they execute.
1.
Build container image
2.
Pull latest commit
3.
Run smoke tests
4.
Run unit tests
5.
Deploy to staging
6.
Generate coverage report
A stock-control activity diagram runs Check stock, then reaches a decision. The guard [in stock] leads to Ship from warehouse, and the guard [else] leads to Order from supplier. The two branches end on the incoming edges of a join bar, which is followed by Send invoice and an activity final node. What happens when the activity runs?