umlsequence-diagramplantumlpuml

How to change formatting of conditional blocks in PlantUML sequence diagram


I have what looks like this:

enter image description here

the code that's generating the conditional block is:

alt Condition1
  Object3 -> Object4: Message5
else Condition2
  Object3 -> Object5: Message6
end

I have 2 questions:

  1. is there a way to change the "alt" text in the display in the top left corner of the box? Seems odd since "alt" has an unclear meaning

  2. Is there a way to center the "[Condition 2]" text? Seems odd that it's formatted to the left whereas [Condition 1] is centered.


Solution

  • PlantUML sequence diagram try to use standard UML as much as possible. The grey pentagon corresponds in UML notation to the interaction operator. alt is the UML keyword for an alternative (i.e. in programming languages constructs like if..else or switch ..case ). There is absolutely nothing weird about alt in the UML world. You'll soon get used to to it ;-)

    You can also use other UML interaction operators, such as opt, loop, break, par but each has a specific meaning in UML and a slightly different syntax, although plantuml is less restrictive in this regard.

    But you will be pleased to discover that PlantUML is not so strict and allows some flexibility, if you prefix your grey box title with the keyword group:

    group if Condition1
      Object3 -> Object4: Message5
    else Condition2
      Object3 -> Object5: Message6
    end
    

    enter image description here

    Regarding the position of the condition, you touch a point, because planUML aligns it as left to the box as possible. According to UML the condition should be positioned across a specific lifeline and not necessarily at the left. Here the UML specification extract:

    An InteractionConstraint is shown in square brackets covering the lifeline where the first event occurrence will occur, positioned above that event, in the containing Interaction or InteractionOperand.

    For this there is no solution in the current plantUML version. I have in the past already tried out everything, even manual blank padding using quotes (which are unfortunately displayed) and unicode hard spaces (ā€‚), which works but is included between the brackets and looks therefore uggly.