I checked to see what the next talk was going to be at my local Java Users Group (the Denver JUG) and saw that Eitan Suez was going to be presenting on The State Machine Compiler. My first reaction was, “What the heck is that?”
So I spent a few minutes reading about state machines and realized again why going to JUG meetings is great: JUG Serendipity. By attending JUG meetings, I learn about development tools and technologies I normally would never hear about.
Eitan will talk next Wednesday about the State Machine Compiler, a Java open-source (Mozilla Public License) project to automate creating classes that implement the Gang of Four State pattern. SMC allows you to define a “state machine” mapping file that defines the states your object can be in, what transitions trigger movement between states, and actions that should occur upon state transition. Then, in your Java code, you use a generated state Context object to change states when needed. The generated code handles the work of invoking methods to perform the actions and track the state transition.
By keeping the state definitions and transition logic in a separate file, you remove all the ugly switch and if-then-else state transition logic from your Java code. The down side is you have to learn a new notation to represent the state machine. Fortunately, the syntax defined by SMC isn’t overly complex. And it’s not XML!
SMC apparently was written originally by Robert C. Martin, then picked up and expanded by Charles W. Rapp. (Apparently not the same Charles Rapp agency that manages Nipsey Russell.) Uncle Bob still has a Java version of SMC available, but without source code.