A Finite-State Machine is a system with explicitly defined states and transitions between the states with the following syntax
Mostly the state name is documented within a state; the arrow of a transition can also be used for documentation.
This example shows a similar state machine with three states that runs from the initial state to a final state (left to right). This example shows the static structure of the state machine, so there is no runtime information within the illustration
State machines are an useful tool to describe regular expressions and we would like to motivate this concept for explaining the functional principle:
The main goal is to create a system which can check strings that matchs the following criteria: The strings starts with an arbitrary sequence of the letter
a
orb
(the sequence can be empty). After the initial sequence follows a positive number which can be any digit. The end of the digit sequence is a sequence of the letterx
with two letters at minimum. All letters within this string can be lower- or upper-case. Some valid example sequences:ab1x
,aaabbb169Xx
,AaAabbBB972xXxXXXX
Most programming languages define such regular expression in a perl notation or posix notation. We use for the example the posix notation which is defined as:
(a|A|b|B)* [0-9]+ (x|X){2,}
(a|A|b|B)*
defines the different letters and the |
defines the or-Operator. At the end the *
-operator defines $\geq 0$ elements.[0-9]+
defines all elements between 0 and 9 and the +
-operator sets the number of elements $\geq 1$(x|X){2,}
defines similar to the first both letter cases and the {2,}
defines the number of elements with $\geq 2$Based on this definition, it is possible to define a state machine which can check if the string matchs the given structure. During runtime, the string is read character by character and based on the state machine a transition from start state to final state will be found. If the state machine terminates in the error state or on any other state, the string does not match. For example:
Based on the static information of the state machine, it can be extended to a petri net, which allows describing agent behaviour during runtime.