On the blog we have already covered the basics of UML, but a short reminder is in order. It is a notation that helps you prepare diagrams showing a software model from different angles. It lets us show the structure as well as the flow of information and the interactions between parts of the system. In the linked article we also mentioned that UML diagrams are often wrongly equated with class diagrams. That is a mistake, because the notation has a much broader scope, and today we will prove it.
Ahead of us is a description of state diagrams, another example of models that describe the real world for IT purposes. And don't worry, this text will be much shorter than the previous one. Note also that although we are talking about a diagram, the topic is really about designing the flow of states of an object in a system, something that is done very often during the software analysis stage.
The simplest use
Imagine an online bookstore where users can buy books. Each item can be available in the store, then added to the cart and finally ordered. But that is not the end of the book's life cycle: the user can still follow the status of its shipping and delivery, waiting impatiently for the purchase. So a book has a state (or status) that changes in response to certain events. The set of possible states of an object, together with the transitions showing the moments at which the status changes, is a state machine. A state diagram is therefore nothing more than an illustration of a state machine, and an example of a UML diagram from the behavioral group.
Here is an illustration of the situation above:
Every state diagram has an initial point (a black circle) and at least one final point (a black circle with a ring around it). The "at least one" is there because, for aesthetic reasons, a diagram sometimes has several final points, all with the same meaning. The states themselves are shown as rectangles with rounded corners, and transitions lead from one to another, drawn as arrows labeled with the kind of transition. In the simplest case that is an action after which the transition to the given state occurs. Of course, you can go from one state to many, just as you can reach one state from many. The example above happens to be very simple.
Composite states and other options
A mechanism that allows a bit more in a state diagram is the so-called composite state. Put simply, it is a state machine contained in another state machine. It has its own initial point, but from every state we can exit to the outside, for example to the final point of the base machine. Composite states can be used for the following:
- simply improving the look of a diagram when there are many transitions or some patterns repeat,
- splitting states into groups, with transitions between them, and moving on to the next state only after those are finished,
- remembering which substate you were in when you return to a composite state.
The last point is especially interesting. Sometimes you need to return to a composite state, but to the substate the program was in when it left. For that we use history (you may also see it called "shallow"), marked with a circle with an "H" inside. There is also so-called deep history. Imagine that composite states can have substates that are themselves composite states and contain their own substates. When we want to show that the flow should go exactly to the right substate regardless of the nesting level, we just add an asterisk to the "H" to get "H*".
Let's look at an example for the flow of statuses in Feedybacky, at least in a slightly shortened version. On the way we can also show two more mechanisms that let us "complicate" a diagram.
In the diagram above we can see that once a ticket is started and accepted by the IT team, a series of questions and answers may follow. So we have a composite state for the group of "in progress" statuses, made up of three items: "question to the client", "question to the manager" and the actual "in progress". After accepting the ticket, the manager may have questions about it and then changes the status to the first one. To show in what situation this happens, we use a conditional statement, marked by a square rotated by 45 degrees. It is used to branch the flow and may look familiar to anyone who once learned flowcharts. Note that the condition is written in square brackets, to tell this note apart from an ordinary transition name. After going through the loop of questions to the client and then to the manager, the ticket can be considered on hold ("Paused"). We can see here that after the decision to resume, the flow goes to the last known substate in the composite "in progress" state, or the ticket is simply deleted. If, on the other hand, the ticket is actually being worked on and finally completed, we move to the "deployed" state. And here is another novelty: the extended description of a transition. It has the following form:
wyzwalacz [dozór] / akcja ^ wywołanie zdarzenia
In other words: trigger [guard] / action ^ event invocation. It can replace a conditional statement (which is the role it plays in the diagram above), but it can also point to further actions and events that occur during the transition. In the example, we have the client's decision about a fix: if they have remarks, we go back to the substate, specifically to the "question to the manager" status. If everything is fine, we take the "left side" and at the same time mark the ticket as completed and consider its history closed.
There are also situations in which an object can be in two or more states at once, as long as they belong to separate groups. For example, a book in a store can be in varying condition (undamaged, damaged and so on), but it can also be unpaid or paid (that is, have a state related to payment for the product). We call such situations parallel states and mark them with a rectangle divided into two areas separated by a dashed line. They resemble substates a bit, but they are something different.
Finally, the description of the states themselves can be extended with further directives, which specify how the object behaves in different situations. After each directive comes a slash, followed by the action or event to run (the latter is preceded by a caret (^)). The available options are:
- entry: the action is invoked on entering the state (the equivalent of a constructor).
- do: the action is invoked while the object stays in the state (in an endless loop).
- exit: the action is invoked on leaving the state (the equivalent of a destructor).
- event
nazwa_zdarzenia: the action is invoked when the given event occurs (nazwa_zdarzenia stands for "event name").
Below is an example of such a description. Note the use of the caret (^) at "state review". It is an event invocation, and here too the notation is consistent with what we mentioned earlier.
Summary
We have presented the second of the most commonly used UML diagrams, the state diagram, which lets you model the flow of an object's status in an IT system. And let's be honest: in most large applications there will be objects described by this property. As with the class diagram, documentation does not always use the full UML notation to describe states, but it can be useful for modeling something that would take many pages to describe.
Best regards and thank you, Jakub Rojek.


