In a UML Sequence Diagram - If a flow should stop if a condition is met midway, how would it be best represented with alternate / optional fragments?
i.e. -
If the stop condition is not met then the flow is continued for several more steps.
Should the alternate fragment cover all of the steps since the stop condition, making all steps past it part of the alternate fragment, or is there a notation to handle the stop inside a small alternate fragment (confined only to the condition)?
There are three options for this situation. Each of them I illustrate with a diagram showing how the respective combined fragment should be used. The actual behaviour is hidden with interaction references (normalFlow for a flow that should normally be executed and breakFlow for any flow that should happen in case of a required break).
The first solution is the most convenient one - it exactly covers your case and you can also use the positive version of a break guard. However each of them provide you a valid possibility.
Break combined fragment
When a break combined fragment is met and its guard condition is true, only this fragment is still executed and then the execution of the interaction (flow) stops. If the condition is not met, the combined fragment is omitted and the normal flow continues. This is exactly the case you describe. In this case you would put the messages that shouldn't be executed in case of a break condition after the break combined fragment.
Opt combined fragment
When an opt combined fragment is met it executes only if a guard condition is true. The rest of a flow continues regardless of the condition.
You can put the part of the flow that is continued only if the break condition is not met inside the opt combined fragment. The opt fragment should have a guard that is opposite to the condition at which the flow should stop. If any additional actions should happen in case of a break, they should be put after the opt combined fragment.
Alt combined fragment
When an alt combined fragment is met its guard conditions are evaluated and only the eventual one fragment which guard evaluates to true is executed. There might be also a situation when none of the guards evaluate to true and no fragment is executed in such case. Whatever flow is after the combined fragment is executed normally anyway.
In this case you would preferably put two fragments, one with the correct operation guard and the second one with a condition that should cause a break. Put the normal flow in the first fragment and whatever should happen in case of a break in the second fragment.
You could shortcut the whole thing by using an opt fragment:
One could start arguing that this is syntactically incorrect, but it transports the message (I guess). And that is what counts.
From my personal experience: use fragments as few as possible. Rather split your scenarios to focus on the certain important aspects. A SD is a snapshot of the system at a place where you want an overview over what's going on and not a detailed roadmap with each possible cat and dog trail.
Related
I have a UML book and I am reading about nested fragments. It shows an example of a nested fragment. But what I dont get.. why does it say "If the condition "cancelation not sucessful" is true when entering this fragment (i.e. cancelation was unsuccesful) the interaction within this fragment is no longer executed".
What I have learned before is that a condition should be true before the interaction will be executed? But in this case it says the opposite of it.. (because they say it should be false to execute the interaction)
See for the image: https://ibb.co/CmstLcX
I think this is simply a typo in the book. The diagram makes sense, but the text describes nonsense. While the condition is true, the messages in the loop will happen up to three times.
Maybe the author got confused, because the message immediately before the loop is Cancellation. I assume the loop guard is referring to the success of the Order cancellation message, not to this Cancellation.
By the way, reply messages need to have the name of the original message. Most people get this wrong (and some textbooks). I'll grant that the text on the reply message is often meant to be the return value. As such it should be separated with a colon from the name of the message. In your case it is probably not necessary to repeat the message name, even though techically required. However, the colon is mandatory.
If it is the return value, all reply messages in the loops return the value Acceptance. I wonder, how the guard can then evaluate to true after the first time. Maybe it is only showing the scenario for this case. This is perfectly Ok. A sequence diagram almost never shows all possible scenarios. However, then the loop doesn't make sense. I guess the author didn't mean to return a specific value.
Or maybe it is the assignment target. In this case, it should look like this: Acceptance=Order Cancellation. Then the Acceptance attribute of the Dispatcher Workstation would be filled with whatever gets returned by the Order Cancellation message. Of course, then I would expect this attribute to be used in the guard, like this [not Acceptance].
A third possiblity is, that the author didn't mean synchroneous communication and just wanted to send signals. The Acceptance Signal could well contain an attribute Cancellation not successful. Then of course, no filled arrows and no dashed lines.
I can even think of a fourth possibility. Maybe the author wanted to show the name of an out parameter of the called operation. But this would officially look like this: Order Cancellation(Acceptance). Again the name of the message could be omitted, but the round brackets are needed to make the intention clear.
I think the diagram leaves a lot more questions open than you asked.
It only says "If the cancellation was not successful then" (do exception handling). This is pretty straight forward. if not false is the same as if true. Since it is within a Break fragment, this will be performed (with what ever is inside) and as a result will break the fragment where it is contained (usually some loop). Having a break just stand alone seems a bit odd (not to say wrong).
UML 2.5 p. 581:
17.6.3.9 Break
The interactionOperator break designates that the CombinedFragment represents a breaking scenario in the sense that the operand is a scenario that is performed instead of the remainder of the enclosing InteractionFragment. A break operator with a guard is chosen when the guard is true and the rest of the enclosing Interaction Fragment is ignored. When the guard of the break operand is false, the break operand is ignored and the rest of the enclosing InteractionFragment is chosen. The choice between a break operand without a guard and the rest of the enclosing InteractionFragment is done non-deterministically.
A CombinedFragment with interactionOperator break should cover all Lifelines of the enclosing InteractionFragment.
Except for that: you should not take fragments too serious. Graphical programming is nonsense. You make only spare use of any such constructs and only if they help understanding certain behavior. Do not get tempted to re-document existing code this way. Code is much more dense and better to read. YMMV
Main Idea: To convert an activity diagram into a sequence diagram.
Problem: I want that if "Action 2" fails then, in that case all the flows get triggered again from "Action 1".
(i.e Action 1 --> Action 2 --> then the verification again. and this should happen until Action 2 is Successful)
Basically, the flow works like a GOTO function and then from there triggers all the steps again.
I have tried to use a ref block but again it seems that it might be ambiguous and someone might read it only as the "execution of Action 1" and not the steps after it.
Therefore I wanted some hints to make this sort of scenario clear. Any suggestions would be appreciated.
Attaching the image for better clarity of the problem.
The activity and sequence diagrams are not supposed to be equivalent. Both set a different focus (flow vs interactions) and as a consequence you might have to do the mapping by identifying higher-level constructs.
Here you have clearly a loop:
You could reformulate in pseudocode as: REPEAT action 1 and action 2 UNTIL action 2 passes, which is a loop that has the test in the end. You could further rewrite this loop into: Last did not passed; WHILE last did not pass, DO action 1 and action 2, or WHILE true DO action 1 and action 2 and BREAk if it succeeds.
Now you can use a loop fragment to achieve this behavior in a sequence diagram. This fragment would enclose action1, action 2, and the test for success, and after the loop, would come the interaction for the success case. The nesting makes it very explicit what gets repeated and what not. For example, some variant of:
The only solution I can think of is creating a variable before the loop so it can enter the first time. However, I don't consider this as optimal.
In an interaction (shown through a sequence diagram) the natural way to support a loop is to use a combined fragment with the operand loop.
As said in formal/2017-12-05 §17.6.3.17 Loop (from page 584) :
The Guard may include a lower and an upper number of iterations of the loop as well as a Boolean expression. The semantics is such that a loop will iterate minimum the ‘minint’ number of times (given by the iteration expression in the
guard) and at most the ‘maxint’ number of times. After the minimum number of iterations have executed and the Boolean expression is false the loop will terminate.
Contrarily to a while in case of a do-while the test is at the end of the loop, an other way to say is the test (without side effect) is done at the beginning of the loop but its result is not taken into account the first time, and this is exactly the semantic of the combined fragment loop with ‘minint’ valuing 1 (but 0 in case of a while) and ‘maxint’ valuing * (means unlimited, see §17.6.4.9 page 586) => the notation for the loop operand is loop(1,*) and the Boolean expression is the test of the while.
The constraint in the loop fragment allows to write anything. Rather than using the contents of a variable you should express the number in clear text to explain the reason for the loop. In any case: graphical programming shall be avoided. Use it only to express some complex structures and not for each bit.
I am designing UML Activity diagram in ArgoUML now.
I know that if I want to design condition like:
if(condition) {
doTrueAction();
} else {
doFalseAction();
}
It could be done in UML activity diagram like following:
But what if we have another condition inside the output of the previous decision? Like this:
if(condition) {
if(condition2) {
condition2TrueAction();
} else {
condition2FalseAction();
}
}else{
conditionFalseAction();
}
As you see conditionTrueOutput is being output and condition at the same time here. It seems to me that design is broken.
Edit: Or I should use fork element instead of decision (diamond) element?
I want to know how to design this correctly. Is there any rule?
There's a wrong assumption in your thoughts. A Decision has no output. It just has conditional control flows leaving it. Each can be guarded by a certain condition. This guard is written in square braces:
Note that I did not write the (implicit) [true] guard after the first test (rather by sloppiness when editing, but actually it can be omitted).
Edit My first though that your diagram has another flaw in that the actions have no control flow going out was incorrect. Actually Superstructures state that a missing outgoing control flow is equivalent to an FlowFinal after processing the Action. So your diagram is correct here.
Regarding your edit: as the comment says, this is for parallel continuation. Nested conditions need to be like shown above.
Edit2 As Ister noted in the comments, a Decision can have more than just the if and else control flow going out, but an arbitrary number greater than 1. The UML 2.5 states on p. 388:
If exactly one target of an unblocked outgoing edge accepts the token, then the token traverses the corresponding edge and all other offers are withdrawn. If multiple targets accept the token simultaneously, then the token traverses only one of the edges corresponding to the accepting targets, but which one is not determined by this specification.
In order to avoid non-deterministic behavior, the modeler should arrange that at most one guard evaluate to true for each incoming token. If it can be ensured that only one guard will evaluate to true, a conforming implementation is not required to evaluate the guards on all outgoing edges once one has been found to evaluate to true.
For use only with DecisionNodes, a predefined guard “else” (represented as an Expression with “else” as its operator and no operands) may be used for at most one outgoing edge. This guard evaluates to true only if the token is not accepted by any other outgoing edge from the DecisionNode.
It seems that your design is coherent but it is not a UML diagram. I suppose that you are mixing the activity diagram with the sequence diagram.
Your diagram looks like a sequence diagram but it tries to represent information that are related to the activity diagrams.
I suggest you this web site to investigate on the differences between the two diagrams, I hope it can help you.
Instead, by trying to use your design, what I suggest you is to insert another condition (e.g. SecondConditionAction) before the two condotion2.
What is the difference between the alt and opt fragments in UML sequence diagrams?
alt is used to describe alternative scenarios of a workflow. Only one of the options will be executed.
opt is used to describe an optional step in the workflow.
For example, for an online shop purchase sequence diagram you may use opt to describe how the user can add gift wrapping if she wishes. alt may be used to describe two variants of payment: using credit card or wire money transfer.
In the UML specification the meaning is described in section 17.12.15. opt and alt are two different operator types and here's how the specification describes them:
alt:
The InteractionOperatorKind alt designates that the CombinedFragment represents a choice of behavior. At
most one of the operands will be chosen. The chosen operand must have an explicit or implicit guard
expression that evaluates to true at this point in the interaction. An implicit true guard is implied if the operand
has no guard.
opt:
The InteractionOperatorKind opt designates that the CombinedFragment represents a choice of behavior where
either the (sole) operand happens or nothing happens. An option is semantically equivalent to an alternative
CombinedFragment where there is one operand with non-empty content and the second operand is empty.
Alt is alternative flow (SWITCH or if. IF with two paths) Opt is IF with one flow. If you use Opt, code will be executed or not !
UML - sequence diagram alt vs opt
alt - if else
opr - single condition
Alt (alternative) is indeed similar to "if" but neither is limited to 2 options only, both can actually have many "else", many options, as needed. For example: //if (a=b) then a++; else if (a>b) then a--; else b++. In any case, if Alt is part of a code that is being called, Alt will execute.
Whereas Opt (Optional) not necessarily will be executed even if it is contained in the middle of a sequence or code that is being called. Typically, to execute an Opt sequence requires external interaction from user that is making a decision where many options were presented (like choosing an online paying method). In this particular case, as opposed to an Alt sequence, if the paying methods were "credit card", "pay pal" or "prepaid", the code will have 3 Opt sequences - with only one flow each -, but one and only one of the Opt sequences will actually execute.
Hope this helps!!
Best,
SD
They are basically the same.
alt is more used for several choices, like a switch sentence group in C programming language. While opt is more used for only two choices, like a if sentence.
But, don't get surprised, if you see both concepts used interchangeably.