Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Goto in Sequence Diagram?

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?

enter image description here

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 
like image 721
Lokesh Agrawal Avatar asked Feb 06 '19 09:02

Lokesh Agrawal


People also ask

What is OPT and ALT in sequence diagram?

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.

How Guard condition is represented in sequence diagram?

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.

What is a sequence diagram in UML?

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.


3 Answers

Goto is not supported in sequence diagrams (for good reasons). Use a combination of a loop and a break operator instead. See this diagram: enter image description here sequencediagram.org/Item Executor

sequencediagram.org/Item Executor (with Execution Specifications)

Some remarks on this diagram

  • the 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.
  • Both 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).
  • return values are shown with a preceeding colon. The message name would preceed this, but when it is obvious it can be omitted (as it is here).
  • execution specifications (sometimes referred to as "activations") are only shown, where they help the readability. Contrary to popular belief they are not mandatory.
  • UML doesn't know for each loops. Therefore I added iterator operations. The term "for each item in a list" is not a 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»
  • I assume 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.
  • The "Acknowledge" message is just the reply message for the execute message. As such it would carry the same name (which is omitted here).
  • In the version with Execution Specifications, the drawing tool didn't allow the end of the 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): with guards sequencediagram.org/Item Executor (with Execution Specifications and guards)

like image 95
Axel Scheithauer Avatar answered Oct 17 '22 09:10

Axel Scheithauer


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'

like image 36
bruno Avatar answered Oct 17 '22 08:10

bruno


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:

enter image description here

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).

like image 2
qwerty_so Avatar answered Oct 17 '22 08:10

qwerty_so