Synonym: Finite state automaton.
FSMs are often used in real-time systems to control devices such as toaster ovens, elevators, and nuclear power plants. More importantly for JOCKEY, however, FSMs are also widely used to parse text according to the rules of a formal grammar.
Turing machines (a theoretical concept) are a generalization of finite state machines. FSMs are not quite as powerful as Turing machines (partly because they don't have infinite memory). But they're pretty close. They're especially useful for solving complex problems for which conventional techniques tie themselves in knots.
FSMs reflect a rather formal, abstract approach which may seem a bit alien to a programmer in the trenches. Once you catch on, however, you find that FSMs are not only powerful, but also easy to code (at least in simple but still useful cases).
In particular, we will discuss deterministic FSMs. Non-deterministic FSMs are more concise, but they are harder to translate into code.
An FSM consists of three components:
The diagram may also reflect actions other than state transitions. One approach is to label the arrows not only with events but also with actions, at least in an abbreviated form.
When you're working out the design, doing a lot of scribbling, you needn't be finicky about the notation. Leave things out to save time, or invent your own peculiar notation. As your design approaches completion, a more formal approach helps you make sure that you haven't missed anything.
The following transition diagram fits the bill. The initial state is labelled '0'.
This diagram does not reflect the fact that a data name can be no
longer than 30 characters. We can apply this limit outside the FSM
itself, after collecting all the characters.
Another approach is to devise a larger FSM which checks the length after each character:
This diagram uses a slightly different notation: the initial state is
a solid dot instead of a circle. Furthermore the initial state does not
read any characters; it leads directly to another state. This change
eliminates some duplicated transitions.
The colored states, numbered 3, 4, 5, and 7, do not read characters either. They simply check the length of the character string. The resulting events, reflecting whether the string is too long, are internally generated.
While there are more complex examples in my own programs, they are highly specific to the JOCKEY application. It would be awkward to explain them to a general audience, and no one would have the patience to read the explanation anyway.
Then test and re-test. If you isolate your FSM in a subprogram (hint, hint) you can write a test driver for it, and subject it to test cases that would be hard to find in real data.
At the coding level there are three broad kinds of FSMs:
The first two may be combined into a hybrid, when convenient. The third kind is now available for COBOL through a tool called Libero, from iMatix. Libero runs under Unix, MS-DOS, Windows, OS/2, and VMS. An MVS version is not yet available.In the first place, most COBOL programmers know nothing about FSMs, and will find the whole thing completely baffling. Comments in the code should explain what is going on, at least briefly. They might contain a reference to this Web page or to other reference material.
In the second place, even for someone who understands FSMs, COBOL is not a very lucid way to represent one. To understand how the program works, you first have to figure out how it implements the FSM; then you have to painstakingly reconstruct the transition diagram. Only then can you even start looking for a way to fix a bug or otherwise change the program's behavior.
In the absence of automated tools, one solution to this problem is to include the transition diagram as part of the documentation. This solution requires that your documentation be based either on paper (where even a crude hand-drawn diagram would be welcome) or on some storage medium that supports graphics (such as Web pages).
A better solution -- when it works -- is to make maintenance unnecessary. Isolate each FSM (or perhaps a collection of related FSMs) in a subroutine that does nothing but implement the FSM. Make sure it works. With any luck, no one will ever have to look at it again.
Further reading