freeCodeCamp
The finite state machine (FSM) is a software design pattern where a given model transitions to other behavioral states through external input.
A FSM is defined by its states, its initial state and the transitions.
The power of FSM comes from the ability to clearly define different behaviors in different conditions. Usually FSM is used with looping behavioral scripts which constantly evaluate the current situation in a loop or with events.
To help form an image of how this might be applied, a coffee machine will be used as an example of a finite state machine. We will also cover a state diagram to visualise the FSM and provide coding examples.
This diagram shows three possible states for the coffee machine:
The lines between these states show which transitions are possible between states and in which direction. These transitions have conditions for when the FSM needs to change between states.
In every state there is defined behavior which will only be executed when the object is in that state. For instance, during PoweredOff the coffee machine won’t brew coffee before it’s powered on, during the Open state it will wait either until there’s enough cash inserted, until the power down command is given, or until it runs out of coffee. During this Open state it can do routines such as cleaning which won’t happen in other states.
Every FSM has an initial state, this means which state it starts in when it is created and has to be defined when constructed or instantiated. Of course it’s possible to directly change state if conditions are met.
Every state either constantly evaluates if it should transition to another state or will transition to another state based on a triggered event.
There are two types of finite automaton, Deterministic (DFA) and Nondeterministic (NFA). Both of them accept regular languages and operate more or less in the same way described above however with some differences.
A DFA accepts or rejects a string of symbols and only produces one unique computation or automaton for each input string. Deterministic refers to the uniqueness of the computation. A Finite State Machine is called a DFA if it obeys the following rules:
An NFA does not need to obey these restrictions, meaning that each DFA is also an NFA. And since they both only recognize regular languages, each NFA can be converted into an equivalent DFA using the powerset construction algorithm.
So what sort of rules can we expect to find in NFAs but not DFAs ?