Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if-condition in uml state machine diagram

Considering this situation:

...when an order is placed there is a check for availability, if is passed the preparation will start else the order is refused.

If the customer decides to pay with a credit card before starting the preparation the price of the order is locked on the credit card.

When the preparation is completed the order is delivered and if more than 30 minutes from the placement of the order have passed there is a discount of 50%.

My doubt is how to model the if condition in a state machine diagram, I would model it in the following way but I'm not sure it's the right way:

How should I model if conditions in a state machine diagram?

like image 460
abc Avatar asked Jan 10 '23 09:01

abc


2 Answers

On a UML state machine diagram, conditionals are associated with transitions. The transition has a 3-part label in the form of "trigger-signature [guard]/activity". Guard is the conditional and must evaluate to true in order for the transition to be taken. All 3 parts of the transition label are optional.

From your problem description, I might define 3 states named "Waiting for Order", "Preparing Order", and "Delivering Order". There is a transition from "Waiting for Order" to "Preparing Order" and that transition could be labeled "order placed [order is available] /". I opted to leave off the activity because, from the problem description, I don't see any activity associated with this transition. You could maybe draw another transition labeled "order placed [order is unavailable] / refuse order". However, this transition would start from "Waiting for Order" and return to "Waiting for Order" because we don't change states when the order is refused. In this transition I included the refuse order activity because I assume there is some actual activity associated with refusing an order.

Alternately, I have seen transitions drawn which include a decision diamond where the arrow leading into the diamond is labeled with the trigger, one arrow out of the diamond is labeled with [guard] / activity and the other arrow out of the diamond is labeled with [else] / activity. I'm not sure whether this is technically correct UML though.

I think that the conditionals that you put in the entry activities for your Preparation and Delivery states are pretty good the way they are. Because those conditionals seem like they are associated with the activity that occurs upon entry into those states rather than any state transition.

like image 181
kkrambo Avatar answered Jan 12 '23 21:01

kkrambo


In an UML state diagram, an if-condition should be modeled as a choice element, represented by a diamond-shaped symbol. The outgoing transitions must be marked with the corresponding conditions ("guards" in UML terminology) in square brackets. A trigger (event) and a behaviour (action) may be additionally given as described by kkrambo.

Example: choice example

This and this gives some good advice on choices (and state diagrams in general).

like image 25
Gerd Avatar answered Jan 12 '23 21:01

Gerd