How to reference earlier activity in PlantUML UML Activity Diagram

Viewed 8922

I am trying to make an activity diagram with PlantUML (new beta syntax).

So far I came up with (simplified):

@startuml
start
:A;
if (Q1) then (yes)
  :B;
  
  if (Q2) then (yes)
    :D;
  else (no)
    :E;
  endif
  
else (no)
  :C;
endif
stop
@enduml

plant uml diagram

It means, do A, if yes on first question do B otherwise C. After B ask question 2, if yes do D if no do E.

Instead of pointing to E when the answer on question 2 is no I want to go to activity C, however I don't know how to reference it. If I put :C; there (instead of :E; it is just interpreted as a new activity (however it's flow should continue from C there). I assume there is a way to draw a flow like this with PlantUML but I don't see it yet.

What would be the best way to reference an already defined activity?

4 Answers

My current solution (been using plantUML for a week or so) is something like this:

@startuml
start
:A;
if (Q1) then (yes)
  :B;

  if (Q2) then (yes)
    :D;
  else (no)
    :E;
    :call C>
  endif
else (no)
  :call C>
endif
stop
partition C {
  :do 1st C thing;
  :do 2nd C thing;
}
@enduml

Plant uml diagram

I moved to graphviz for this exact reason. Plantuml gives some easy syntax for some types of diagrams but for moving in multiple directions it gets challenging.

I try to use plantuml for flow diagrams but when I get close to state machines I move to graphviz. So a graphviz solution to your problem would look like the following.

Original drawing:

digraph drawing1 {
  A -> B [label="yes"]
  A -> C [label="no"]
  B -> D [label="yes"]
  B -> E [label="no"]
}

PlantUML graphviz diagram

Make B goto C when no.

digraph drawing1 {
  A -> B [label="yes"]
  A -> C [label="no"]
  B -> D [label="yes"]
  B -> C [label="no"]
}

PlantUML Graphviz diagram

If you want to make nodes B and C line up with each other you can use the following code change.

digraph drawing1 {
  A -> B [label="yes"]
  A -> C [label="no"]
  B -> D [label="yes"]
  B -> C [label="no"]
  {rank=same B C}
}

PlantUML Graphviz diagram

I gave up on solving similar problems to yours with plantuml.


In Windows, once you install graphviz and want to generate a png output you go to your directory with a file that contains you digraph code; let's call the file test.gv.

Then run the following command to generate the output test.png.

dot test.gv -Tpng -o test.png

This is what I found worked...

|Main|
start
  :SomeClass constructor called;
  :buildTreeListOpts called;
if (hasOptions()) then (yes)
else (no)
    :require DataLoader;
    :call getData;
|#Cyan|DataLoader|
 :getData;
if (hasDataInLocalStorage()) then (yes)
else (no)
|DataLoader|
    :XHR fetch options;
    :callback;
endif
|Main|
endif
  :finalize;
stop

PlantUML diagram

Keep practicing on their live demo until you find something that works. http://plantuml.com/

Good luck.

There was a similar question on the PlantUML forums in January, but basically the solution was to duplicate the common activities in such cases. So in your case it would give:

@startuml
start
:A;
if (Q1) then (yes)
  :B;

  if (Q2) then (yes)
    :D;
  else (no)
    :E;
    :C;
  endif

else (no)
  :C;
endif
stop
@enduml

PlantUML Graphviz diagram

Currently it only seems possible to use a workaround for this.

Related