How can I show goto statements in Sequence Diagrams.
For example in the diagram below, once "sleep till hold period" is expired I want to take control back to the "is_item_inventory_onhold_state(item_id)" statement. How can I show this is a diagram?
I am using https://sequencediagram.org/ to create these diagrams.
Code written to generate the diagram above:
title Item Executor
loop for each item in a list
Client->ItemExecutor: execute(item)
ItemExecutor -> ItemStateService:is_item_inventory_onhold_state(item_id)
alt True - Item state is on hold
ItemStateService -->ItemExecutor: True
ItemExecutor ->ItemExecutor: sleep till hold period
goto ItemExecutor -> ItemStateService:is_item_inventory_onhold_state(item_id)
else False - Item is not in Held State
ItemStateService -->ItemExecutor:False
ItemExecutor ->ItemExecutor: do_something()
end
ItemExecutor ->Client : Acknowledge
end
alt is used to describe alternative scenarios of a workflow. Only one of the options will be executed. opt is used to describe optional step in workflow. For example, for online shop purchase sequence diagram you may use opt to describe how user can add gift wrapping if she wishes.
Guards are conditions that need to be used throughout UML diagrams to control flow. Remember that a guard could only be assigned to a single message. To draw a guard on a sequence diagram, you placed the guard element above the message line being guarded and in front of the message name, as shown below.
A sequence diagram is a Unified Modeling Language (UML) diagram that illustrates the sequence of messages between objects in an interaction. A sequence diagram consists of a group of objects that are represented by lifelines, and the messages that they exchange over time during the interaction.
Goto is not supported in sequence diagrams (for good reasons). Use a combination of a loop
and a break
operator instead. See this diagram:
sequencediagram.org/Item Executor
sequencediagram.org/Item Executor (with Execution Specifications)
Some remarks on this diagram
break-fragment
leaves the immediately enclosing fragment. Therefore it must be contained directly in the loop-fragment
. If it is within an alt-fragment, only this fragment is left.alt
and opt
fragments don't use a guard
. The fragment that happens is choosen by the occurrence of the reply
message with a certain return-value
. If you want to use a guard, you have to assign the return-value to a local variable. This would then happen above the alt-fragment (see diagram below).execution specifications
(sometimes referred to as "activations") are only shown, where they help the readability. Contrary to popular belief they are not mandatory.guard
condition. If you want to avoid spelling out the iterator, you can use a - semantic free - comment
attached to the loop. Misusing the boolean guard for this makes no sense. If you want a formal definition, you have to add you own stereotype «for each loop»
ItemExecutor
and ItemStateService
are class-names. They need a preceeding colon to distinguish them from role-names. Of course if the role- and class-names are identical, your diagram can be correct.reply
message for the execute
message. As such it would carry the same name (which is omitted here).execution specifications
to coincide with the send events
of the reply-messages
, which would have been correct.Example with guards for the alt
and break
fragments (excerpt):
sequencediagram.org/Item Executor (with Execution Specifications and guards)
You do not need a 'goto', like we don't use them in language since long time
Add a second loop and go out with a combined fragment 'break'
A goto itself is not shown. You just show what operations are sent. You might add a note at the place where the goto happens. However, I think that the usage of gotos should not be encouraged in any way. Instead it should probably some exception handling.
According to your comments you can use a break
fragment like this:
It will break the outer loop, so the is_item...
is repeated after sleep...
.
Note As per comment from @AxelScheithauer the break
will just leave the enclosing fragment. However, I'd regard this a specification flaw. A break is "usually" used with loop control flows (plus a case control flow; but UML does not have a fragment for that). It's probably the best to name the break
fragment so it's clear it will affect the outer loop fragment (as shown in my edited picture).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With