Skip to main content

State Machine

State-machines are a powerful framework for defining business processes. They are fundamental in understanding how we model work in our applications. One of the main principles of NS theory is separation of states which essentially states tasks should be transparent in their execution. Interaction and execution of tasks can be modeled very clearly using state-machines.

Fundementals

There are two basic components; States and Transitions. State belongs to a specific object and describes the current status of that object. Transitions define all changes in the status of an object we want to allow. In a software system however it is not always possible to have atomic transitions. This means the application produces side effects while transitioning from one state to the other. Consider the state-machine of a door below:

While opening the door it gets stuck halfway somehow, which means we are now in an undefined state! This can be resolved by either closing the door again or by defining a new state in which someone has to look at it and fix the issue. This problem is not unique to this example of a door, and in general every task which performs some level of work has this same problem. This is why we will use a slightly different definition for transitions.

Now our transition itself is also stateful, by indicating it is in progress through an interim state and indicating it failed through the failed state. In this example 'OpeningDoor' is our interim state and 'OpeningDoorFailed' is the failed state. By defining these additional states, we can at any point employ the correct strategy. Is the door opening? Then we should probably not interfere with it. Did we encounter a failure? Then we might revert to our begin state, 'Closed', or define another transition to resolve the issue.

Branching transitions

In the previous example 'OpenDoor' has only one successful outcome in 'Open'. It is however possible to have multiple outcomes, depending on anything that can happen within the executed task. We call these successful outcomes end states of which there should always be at least one. When a transition has multiple end states we call it branching. Note that the failed state is only representing technical failure while functional errors are represented as a successful outcome of a transition.