Book a Demo

Author Topic: how do you model an if-else assignment in activity diagrams?  (Read 6287 times)

mse

  • EA User
  • **
  • Posts: 308
  • Karma: +1/-0
    • View Profile
how do you model an if-else assignment in activity diagrams?
« on: January 22, 2016, 08:30:17 pm »
This might seem like an easy question, but when trying to model the following in an activity diagram, it seems the diagram is more confusing than the program text:

if x < y
  temp = y
else
  temp = x

theReturn = temp * inValue

return theReturn


The variable 'y' comes from a ReadStructuralFeatureAction which is from an object called foo (in)
x is an ActivityParameter (in)
theReturn is an ActivityParameter (out)
inValue is an ActivityParameter (in)
« Last Edit: January 22, 2016, 08:59:06 pm by mse »

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: how do you model an if-else assignment in activity diagrams?
« Reply #1 on: January 22, 2016, 09:42:35 pm »
You can use a decision to decide that. But honestly, not all things are better readable in graphical format. Just use plain text in an action to describe that the return value is calculated as stated.

q.

Uffe

  • EA Practitioner
  • ***
  • Posts: 1859
  • Karma: +133/-14
  • Flutes: 1; Clarinets: 1; Saxes: 5 and counting
    • View Profile
Re: how do you model an if-else assignment in activity diagrams?
« Reply #2 on: January 22, 2016, 10:50:27 pm »
It's not an easy question to answer because there are many different ways to model the same thing, even within an activity diagram, and it depends on what subset of the notation you're using and what the model is for; if it's for simulation or code generation that's one thing, but if it's intended for a human reader it's another.

If it is for human consumption there is a very useful EA feature you can use for situations like this, namely Show Diagram in Frame.

What you do is you create a composite Activity for the little code snippet you've described, draw the branch and assignment in the composite diagram, and then in the parent diagram (which contains the composite Activity) right-click the Activity and select New Child Diagram -- Show Composite Diagram in Frame.

This will replace the regular presentation of the Activity with the composite diagram. But since it is still an Activity, you can select to display its parameters. Move those to the appropriate places around the border and you end up with something that's easy to read and understand.

In this case, I would draw the following in the composite diagram:
  • A Decision with no name
  • An Atomic Action "temp = y" with a ControlFlow from the Decision with a constraint [x < y]
  • An Atomic Action "temp = x" with a ControlFlow from the Decision with a constraint [x >= y]
  • A Merge with no name, with one ControlFlow from each Atomic Action above, no constraints
  • An Atomic Action "theReturn = temp * inValue"

As you can see I'm omitting initial and final nodes, I'm not using WriteVariable Actions, just keeping it simple. The size of the resulting composite diagram is a factor, so you could even consider omitting the Merge if you like.

This approach works well for small snippets like the one you've described, but less well for large complex operations because the composite diagrams get too large.

HTH,


/Uffe
My theories are always correct, just apply them to the right reality.

minastaros2

  • EA Novice
  • *
  • Posts: 16
  • Karma: +2/-0
    • View Profile
Re: how do you model an if-else assignment in activity diagrams?
« Reply #3 on: January 27, 2016, 02:23:51 am »
Assumed that I do not draw all the object flows which might arise from x and inValue beeing ActivityParameters, that could do as the result calculation:

The important thing is that the reader grasps what is going on.
It might be different if you intended to automatically generate some code...