The previous article introduced the basic knowledge of UML and detailed the knowledge points and drawing methods of class diagrams in UML.
In this article, we will continue to introduce another type of diagram in UML - the state diagram.
The role of the state diagram is to assist in compiling and decoding state machine code, and it is also practical in embedded software development.
1 Introduction to UML Statechart
A state diagram, showing a state machine, is a UML activity diagram.
UML has many different types of diagrams, including:
-
Static diagrams: use case diagrams, class diagrams, package diagrams
-
Dynamic diagrams: activity diagrams, state diagrams, sequence diagrams, collaboration diagrams
State machines are used to model the dynamic behavior of model elements, or in other words, to model the event-driven aspects of a system.
2 Basic elements of state diagram
2.1 Status
The state is an important part of the state machine, describing the results of the execution of the dynamic behavior of the object in which the state machine is located.
2.1.1 Initial/Final/Selected State
These three are pseudostates :
-
Initial state: a solid circle
-
Final state: represented by a solid dot in a circle
-
Selection state: represented by a diamond, and the text in brackets above the conditional branch represents the guard conditions .
2.1.2 General Status
A rounded rectangle is used in UML to represent a general state.
Components of the state:
-
Name: A string of characters used to identify different states. It can be anonymous and is usually placed on top of the state icon.
-
Entry/Exit Action: Indicates the action performed when entering/exiting this state. The action can be an atomic action or an action sequence.
-
Internal transition: An internal transition does not cause a state change. The triggering of this transition does not cause the entry/exit actions of the state to be executed. Syntax: Event/Action Expression
2.1.3 Composite State
A composite state, or combined state, is a state that contains substates.
As shown in the figure below, "Compound State 2" is a compound state, and the internal "State 2-1" and "State 2-2" are sub-states.
It can also be expressed in the following form, drawing the contents of the composite state separately.
Note: The letter H in the circle in the figure represents the historical state . It is also a pseudo state. Its purpose is to remember the sub-state when exiting from the combined state. When entering the combined state again, you can directly enter this sub-state instead of starting from the initial state of the combined state again.
2.2 Conversion
Transition is the association between two states. It means that the object performs certain actions in the first state (Source State) and enters the next state (Target State) when a certain thing happens and a certain condition is met.
2.2.1 General conversion
In general, the state transition is the transition between two different states, as shown in the following diagram:
The components of the conversion are:
-
Source State: The source state is the state affected by the transition. When an object is in the source state, it will activate a transition when it receives a trigger event or meets the monitoring condition.
-
Target State: After the conversion is completed, the state of the object changes. The state of the object at this time is the target state of the conversion.
-
Trigger: An event that causes a conversion. The event here can be a signal, call, time period, or a change in state.
-
Guard Condition: A Boolean expression enclosed in square brackets that is placed after the trigger event. Guard Conditions can reference object attribute values and trigger event parameters.
-
Effect: The result after the object state is transferred
2.2.2 Self-conversion
Self-Transitions means that a state can have a transition back to its own state.
2.2.3 Local Conversion and External Conversion
For transitions between composite states, it is important to note the difference between local transitions and external transitions.
In the above figure, the left side is the local transformation and the right side is the external transformation.
-
In the above two cases, the source state contains the target state. The difference between the two transitions in this case is that the local transition will not exit from the source state (no exit action will be performed), while the external transition will exit and re-enter the source state.
-
In the following two cases, the target state contains the source state. The difference between the two transitions in this case is that the local transition will not enter the target state again (no entry action will be performed), while the external transition will exit and re-enter the target state.
3 A UML state diagram example
3.1 Draw UML state diagram using Visio
Create a new Visio file. When you open it, you will be prompted to create a certain type of diagram. Here, select "Software and Database -> UML State Machine"
After clicking OK, you will enter the editing interface of the UML state diagram. On the left, you can see the basic elements for editing the UML state diagram. Drag these elements to the editing panel on the right, and you can see the basic structure of these elements:
3.2 Example explanation
Below is an example of a UML state diagram:
This is a UML state diagram of a fully automatic washing machine, which mainly includes three states: "Settings", "Working", and "Pause".
-
When you turn on the machine, it will first enter the "Settings" state, where you can set the washing mode and water volume, etc. After triggering the start, it will enter the "Working" state.
-
"Working" is a composite state, which includes 4 sub-states: "Add Water", "Wash", "Drain", and "Spin". After entering this state, it starts from the "Add Water" sub-state, then executes the "Wash" and "Drain" sub-states. After draining, it jumps to the "Add Water" or "Spin" sub-state depending on whether the number of washes is sufficient.
-
When in the "Working" state, no matter which sub-state it is in, as long as you press the Pause button, it will enter the "Pause" state. Press Continue again to enter the "Working" state and continue execution. Note that when you continue, it points to the historical state (an H in a circle), which means that it continues to point to the sub-state when it was paused.
4 Conclusion
This article introduces the basic knowledge of UML state diagrams, and uses Visio to draw a UML state diagram example of a fully automatic washing machine to introduce how to draw UML state diagrams and the meaning they express.