You may know code refactorings from your favorite IDE; they help restructuring your code in order to keep it clean and readable. Similarly, YAKINDU Statechart Tools provide refactorings for your state machine. With these refactorings you can easily group states into a composite state, create subdiagrams or rename your variables and events. This blog post shows the three probably most useful refactorings, group states, extract subdiagram and rename, and explains how they work together.
Group States into Composite
In many cases, the system you model with your state machine consists of several logical parts. Having a state machine with all states lying flat on the same hierarchy level does not reflect the system’s logical structure. Furthermore, one often has the same transitions going in or out of a group of states. Modeling each transition individually is cumbersome, time intensive and reduces the readability of the state machine.
Let’s take a coffee machine as an example where the user can cancel the preparation process at any time. Without any grouping each state of the preparation process has the same outgoing transition to reflect the cancellation possibility. Grouping these states into one composite state PrepeareDrink allows to express this aspect with only one transition.
To perform this refactoring, you must select all states while holding the [Ctrl] or [Cmd] key to enable multi-selection; right-click on one of them, and select Refactor → Group States Into Composite from the context menu. This refactoring is only possible if all selected states belong to the same region.
Grouping states into composite states allows to logically structure your state machine by introducing different hierarchy levels. However, having many composite states while presenting all details of their inner behavior makes it hard to grasp the top level structure of the system.
To improve readability, you can extract the internal details of a composite state into an own subdiagram. The composite state then appears like a normal state, but has a decorator in the lower-right corner indicating the existence of a subdiagram. Hovering over this decorator shows a preview of the subdiagram’s content. Clicking on the decorator opens the subdiagram in a separate editor tab. The breadcrumb bar at the top allows easy navigation throughout the hierachy levels.
You can find this refactoring in the context menu of a composite state under Refactor → Extract Subdiagram. The counter refactoring for including the contents of a subdiagram inside its container state again is called Inline Subdiagram.
Using composite states and subdiagrams are powerful features to structure your state machine in a more readable manner. Especially in combination with named entry and exit nodes, composite states unleash their expressiveness as you can precisely define which inner state should be entered when the composite state is reached.
Take a look at the following example. When the user input is coffee the PrepareDrink state is entered by entering its inner state Coffee. This is configured via named entry points and transition priorities. If you want to know more about this feature you should read our blog post or our documentation.
Another useful refactoring is the Rename refactoring. This refactoring helps you to rename a variable, operation, event or interface throughout the whole state machine. It automatically finds all references to these elements and adjusts them. To initiate renaming, right-click on the name of a variable, event or interface in the diagram editor, in the definition section, or in a text field in the Properties view, then select Refactoring → Rename…
Besides the presented refactorings, YAKINDU Statechart Tools provides refatorings for folding and unfolding transition actions into state’s entry and exit actions and vice versa. For more information, please refer to our documentation.