Definition
A Finite State Machine is a model of computation, based on a hypothetical machine made of one or more states.
- only one single state of this machine can be active at the same time
- the machine has to transition from one state to another in to perform different actions
A Finite State Machine is any device storing the state of something at a given time. The state will change based on inputs, providing the resulting output for the implemented changes.
A FSM is characterized as follows:
- We have a fixed set of states that the machine can be in
- The machine can only be in one state at a time
- A sequence of inputs is sent to the machine
- Every state has a set of transitions and every transition is associated with an input and pointing to a state
const machine = {
state: 'OFF',
transitions: {
OFF: {
press() {
this.state = 'ON'
}
},
ON: {
press() {
this.state = 'BLINK';
},
},
BLINK: {
press() {
this.state = 'OFF';
},
},
},
dispatch(actionName) {
const action = this.transitions[this.state][actionName];
if (action) {
action.call(this);
} else {
console.log('invalid action');
}
},
};
const flashlight = Object.create(machine);
console.log(flashlight.state); // OFF
flashlight.dispatch('press');
console.log(flashlight.state); // ON
flashlight.dispatch('press');
console.log(flashlight.state); // BLINK
References
- medium.com
- dev.to
- Used by (Deepa, Thilagam, et al., 2018) to model the behavior of an unknown web application under test. Elegant and clear mathematical formulation of FSM
- Used by (Li, Yan, et al., 2012), in an extended way, considering also data constraints between transitions