nus-cs2103-AY2021S1 / forum

20 stars 2 forks source link

Alternative Paths in Activity Diagram #516

Closed richardcom closed 3 years ago

richardcom commented 3 years ago

Hi, for the question in the practice exam(screenshot 3), though A is the most appropriate option for this quesion, but I am a little confused. I think that maybe A could be satisfied under some conditions. My doubt originates from the concepts of alternative paths in activity diagram.

image

It is mentioned that ONLY 1 Path can be True at any time. But I wonder if it is possible that neither paths is true at any time, and the application is terminated at the branch node. Thus, F, G1, G2 will not be executed and results in Option A.

Moreover, can I please clarify if the same applies to alternative path in sequence diagram? Does one of the paths must be taken at any time?

image

image

GeNiaaz commented 3 years ago

It is mentioned that ONLY 1 Path can be True at any time. But I wonder if it is possible that neither paths is true at any time, and the application is terminated at the branch node. Thus, F, G1, G2 will not be executed and results in Option A.

Maybe instead think of it like a section of code that has to be executed, example:

...
if (x >= 3) {
    do something
} else {
    do smth else
}
...

can you think of a scenario where this code would not run? Possibly if the entire code block is skipped entirely due to a condition elsewhere? Sure that is possible, but then the condition to require it being skipped would have to be included in another condition before this and would show up in the diagram too. I hope you can see where I'm going with this: if it was not executed, it would be indicted in the diagram by another fork / branch.

Moreover, can I please clarify if the same applies to alternative path in sequence diagram? Does one of the paths must be taken at any time?

The alt notation is designed specifically for conditional clauses, so yes, one of them MUST BE taken at any time. But bear in mind that again, an earlier conditional may have rendered this path unused.

To address your initial query

But in this case such as in Qn15, since the fork has to be executed, thus both branches of the fork have to be executed too.

richardcom commented 3 years ago

Hi thanks for the reply! But I was wondering if we have some code like below(a guard condition)(which may not completely comply with the coding standard for missing the final else block, but may be practical sometimes haha), but if we want to represent the code in the activity diagram or sequence diagram, it might results in a condition where x = -1, then maybe no paths can be taken.

...
if (x >= 3) {
    throw some execption or maybe do something
} else if (x >= 2) {
     throw some execption or maybe do something
}

the main happy path continues
...
GeNiaaz commented 3 years ago

Ahh, in this case, an optional path would be taken then (sequence diagram). As for activity diagram, maybe one of the conditions in the branch can be the condition for the happy path (ie, x<=1), and the other condition can fulfil the >=2. And from there, you can have another branch that expressed >=3 for the first if clause.

You can check out the week's quiz for this chapter (week 8 quiz 2 if I'm not wrong), there was one question that tested this concept on optional path for a part of code which had an if and omitted the else

damithc commented 3 years ago

Hi thanks for the reply! But I was wondering if we have some code like below(a guard condition)(which may not completely comply with the coding standard for missing the final else block, but may be practical sometimes haha), but if we want to represent the code in the activity diagram or sequence diagram, it might results in a condition where x = -1, then maybe no paths can be taken.

...
if (x >= 3) {
    throw some execption or maybe do something
} else if (x >= 2) {
     throw some execption or maybe do something
}

the main happy path continues
...

In an activity diagram, this will be a three-way split, with an implicit [else] branch. No, an activity diagram cannot get stuck at a branch node. That means exactly one alternative must be true for every branch node. Perhaps is should add the exactly one phrase to the textbook to make it clear.

Wincenttjoi commented 3 years ago

Hi thanks for the reply! But I was wondering if we have some code like below(a guard condition)(which may not completely comply with the coding standard for missing the final else block, but may be practical sometimes haha), but if we want to represent the code in the activity diagram or sequence diagram, it might results in a condition where x = -1, then maybe no paths can be taken.

...
if (x >= 3) {
    throw some execption or maybe do something
} else if (x >= 2) {
     throw some execption or maybe do something
}

the main happy path continues
...

I think in this case you might need to infer the conditions to convert them into activity diagram. It may look like this: image

Please correct me if im wrong because im not 100% sure either.

richardcom commented 3 years ago

Thank you @damithc @GeNiaaz @Wincenttjoi ! So sorry to disturb you Prof @damithc , may I please further clarify on the exercise below, so based on the above explanations, if we change the right branch condition to x > 5, is it still considered incorrect since this graph might terminate if x = 4.

image

damithc commented 3 years ago

Thank you @damithc @GeNiaaz @Wincenttjoi ! So sorry to disturb you Prof @damithc , may I please further clarify on the exercise below, so based on the above explanations, if we change the right branch condition to x > 5, is it still considered incorrect since this graph might terminate if x = 4.

image

Yes.

richardcom commented 3 years ago

Thanks a lot Prof Damith @damithc ! : )