14

I like to draw a UML sequence diagram to display the sequence of actions and interactions of different parts of my system (I think they called actors).

However, I need to include if then else statements here. Do I do something wrong? Can I use conditional if statements in my UML sequence diagram? How?

Saeed Neamati
  • 18,142
  • 23
  • 87
  • 125

3 Answers3

19

As in many types of UML diagrams, you can use guards to denote a condition. A guard is a boolean expression that you can write over a transition.

Here's a picture with an example: enter image description here

Notice the "[pastDueBalance = 0]" condition.

You can also use combined fragments for more complicated conditional logic. See this nice msdn article for further information.

Falcon
  • 19,248
  • 4
  • 78
  • 93
4

There are four possibilities I know of (with increasing power):

  1. The easiest is just using guards, which are simple, side-effect free boolean expressions.
  2. With combined fragments, you can group sets of messages together to show conditional flow in a sequence diagram (alternatives, options, loops)
  3. Much more powerful is the Object Constraint Language (OCL), which can additionally quantify (for all, exists) and offers a full fledged (DesignBy-)Contract language (that is nearly as expressive as full first order logic).
  4. The most powerful way is to express your constraint using a UML Action Language from Executable UML (xUML) that conforms to the UML Action Semantics.

My choice would be the least powerful one that is still sufficiently expressible for what you want to do. For instance, actions in 4. can have side-effects, which makes them much more fragile than OCL.

Olivier
  • 105
  • 3
DaveFar
  • 1,406
  • 12
  • 19
3

While sequence diagrams are perfect for your description, make sure to check Activity Diagrams.

When you want to show internal details (including detailed rules), Activity Diagrams is what you want to use. The plain activity diagrams focus on sequence of activities and rules and are poor in showing Actor responsibilities, nevertheless, you can use swim lanes to hi-light individual actor's responsibilities. Each lane would include activities an actor is responsible for.

These links discusses activity diagrams with swim lanes:

Ivan Machado
  • 103
  • 4
NoChance
  • 12,412
  • 1
  • 22
  • 39