<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Just a Thought...: Using Java Enums for Finite State Machines</title>
    <link>http://bloritsch.d-haven.net/articles/2007/07/23/using-java-enums-for-finite-state-machines</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Random thoughts</description>
    <item>
      <title>Using Java Enums for Finite State Machines</title>
      <description>&lt;p&gt;&lt;a href="http://en.wikipedia.org/wiki/Finite_state_machine"&gt;Finite state machines&lt;/a&gt;  are useful design constructs for a number of situations, although they seem to be fewer and farther between these days.  Currently, the only place I tend to use them is when I have to write a parser by hand.  Sure there are &lt;span class="caps"&gt;BNF&lt;/span&gt; parser generators around, but not all parsing requirements fit those restrictions.  I have to parse legacy message formats which predate &lt;span class="caps"&gt;BNF&lt;/span&gt; parser theory (i.e. from the 1950s), so this is a useful tool to ensure that the message is properly formatted and all the information is pulled out properly.&lt;/p&gt;


	&lt;p&gt;According to Design Patterns by the GoF, the way to do an object oriented finite state machine is to use objects to represent each state, each with a common interface.  My first introduction was a C++ program that was written with the old C mindset.  That meant that the states were represented by an &lt;code&gt;enum&lt;/code&gt; and all actions were taken with large switch or if/else hierarchies.  I can see how using objects can clean things up, because the conditional logic would be decided by the state object.  Of course, the state pattern in the GoF book required keeping the state in an external object and neglected to dictate how to change the state properly.&lt;/p&gt;


	&lt;h4&gt;The Problem We are Solving&lt;/h4&gt;


	&lt;p&gt;For my purposes, I have to populate a message object with all the information from a text message.  That includes things like security markings, addresses, tags, captions, etc.  The message format is distinct enough so that each line means something.  Of course, there is a proper order of processing, but I can parse one line at a time.  That simplifies things a whole bunch.  I can have the parser work with an interface that looks like this:&lt;/p&gt;


&lt;pre&gt;
public interface Partial {
    public State parse(Message message, String line)
        throws ParseException;
}
&lt;/pre&gt;

	&lt;p&gt;In Java 7 we will likely be able to use partials for this approach, which will clean up a lot of the code clutter.  In that case the interface would look like this:&lt;/p&gt;


&lt;pre&gt;
{Message, String =&amp;gt; State throws ParseException}
&lt;/pre&gt;

	&lt;p&gt;Either approach you take will allow you to write the enumeration delegating the actual processing to anonymous classes.  I&amp;#8217;m sure you&amp;#8217;re thinking, &amp;#8220;My God! This guy&amp;#8217;s off his rocker!  I thought he liked &lt;em&gt;elegant&lt;/em&gt; code!&amp;#8221;  Trust me, you&amp;#8217;ll see the elegance in a minute.  You aren&amp;#8217;t going to do this all the time, but when the situation calls for it, you&amp;#8217;ll appreciate it.  The real benefit comes from testing.&lt;/p&gt;


	&lt;h4&gt;Enums and Finite State Machines&lt;/h4&gt;


	&lt;p&gt;Java enums are objects which is really useful.  I&amp;#8217;ve used this fact to associate sort order &lt;span class="caps"&gt;SQL&lt;/span&gt; snippets with an enum for the different sorting algorithms supported in a system, along with other uses.  I decided to do an experiment with rewriting a parser we have.  The parser works for the most part, but it leaves out some important information we want to support, and more importantly it is difficult to change.  It needs some major rework beyond the scope of a bunch of refactorings.  That is why I chose to rewrite it.&lt;/p&gt;


	&lt;p&gt;We have to start writing the State instances, and it really helps to have a starting point.  For this blog, I&amp;#8217;ll only split a message into header and body sections.  There&amp;#8217;s a whole lot more going on, but I just want to show how things work.  The marker that splits the message header from the body will be a line that has &amp;#8220;BODY&amp;#8221; on it with nothing else.  First, let&amp;#8217;s look at our State enum.  The important thing here is that we are not&lt;/p&gt;


&lt;pre&gt;
public enum State implements Partial {
    HEADER(null),
    BODY(null);

    private final Partial partial;

    private State(Partial parser) {
        partial = parser;
    }

    public State parse(Message message, String line)
            throws ParseException {
        return partial.parse(message, line);
    }
}
&lt;/pre&gt;

	&lt;p&gt;The implementations of &lt;code&gt;HEADER&lt;/code&gt; and &lt;code&gt;BODY&lt;/code&gt; are &lt;code&gt;null&lt;/code&gt; right now because we will get into it a bit later.  First, you&amp;#8217;ll notice a couple things about the enum.  We are passing something that does work into the constructor, which also means that our enums can do work.  For consistency sake we used the same interface for the enum as we did for the interface we pass into the constructors.  If we were to use the closures spec, we wouldn&amp;#8217;t have an interface to implement, so the method we provided would be how we access the blocks passed into the constructor.  The spirit of the design is the same, it&amp;#8217;s just that there is less extra code to type.  Just so you can see what it looks like (assuming I have a better understanding of the spec), here you go:&lt;/p&gt;


&lt;pre&gt;
public enum State {
    HEADER(null),
    BODY(null);

    private final {Message,String=&amp;gt;State throws ParseException}
           partial;

    private State(
            {Message,String=&amp;gt;State throws ParseException} parser) {
        partial = parser;
    }

    public State parse(Message message, String line)
            throws ParseException {
        return partial.invoke(message, line);
    }
}
&lt;/pre&gt;

	&lt;p&gt;In either case, the base design is identical.  The only way to have the functionality of enum values change based on the value is to use the delegate approach.  In short, we are passing in an object that does the work that is specific to that state in the constructor and calling it later when we call the &lt;code&gt;parse&lt;/code&gt; method.  It&amp;#8217;s also important to note that enums are singletons by definition.  There is one and only one State.HEADER enum value in the system, as there is one and only one State.BODY enum value in the system.  That means the implementation has to be re-entrant.  As long as you don&amp;#8217;t attempt to keep any state in the objects you should be fine.&lt;/p&gt;


	&lt;p&gt;For the rest of the article, I&amp;#8217;ll be focusing on the anonymous class approach (i.e. the first version).  I&amp;#8217;m assuming you can do the translation into closures later.  Besides, I&amp;#8217;m not sure if it would be legal to use the control invocation syntax for the constructor or not.  This is a question for Neal Grafter, would this be legal syntax for the constructor of an object (it would be better if that&amp;#8217;s the case)?:&lt;/p&gt;


&lt;pre&gt;
HEADER(Message message, String line:) {
    HEADER
}
&lt;/pre&gt;

	&lt;h4&gt;The State Implementations&lt;/h4&gt;


	&lt;p&gt;The implementation of the state is very simple.  We are providing an anonymous class (or closure declaration).  The header is going to add the line of text to the header provided until we hit the &amp;#8220;BODY&amp;#8221; line.  We aren&amp;#8217;t going to copy that line at all.  The important thing is that we can easily test these conditions in isolation from any other state.  Let&amp;#8217;s write some tests to make sure our implementation does what it is supposed to do (we are skipping the boiler plate JUnit code):&lt;/p&gt;


&lt;pre&gt;
public void testCopiesLineToMessageHeader_and_ReturnsHEADERstate()
        throws ParseException {
    String line = "test line";
    Message message = new Message();

    State state = State.HEADER.parse(message, line);

    assertEquals( State.HEADER, state );
    assertEquals( line, message.getHeader() );
}
&lt;/pre&gt;

	&lt;p&gt;Currently our implementation compiles but only throws &lt;code&gt;NullPointerExceptions&lt;/code&gt; because we haven&amp;#8217;t given it anything to do yet.  Let&amp;#8217;s at least get this to pass.  We have to rewrite the &lt;span class="caps"&gt;HEADER&lt;/span&gt; constructor in the enum:&lt;/p&gt;


&lt;pre&gt;
HEADER(new Partial() {
    public State parse(Message message, String line)
            throws ParseException {

        message.addLineToHeader( line );
        return HEADER;
    }
});
&lt;/pre&gt;

	&lt;p&gt;That&amp;#8217;s all well and good, but we need to make sure that we switch to the &lt;span class="caps"&gt;BODY&lt;/span&gt; state eventually.  So let&amp;#8217;s add a new test:&lt;/p&gt;


&lt;pre&gt;
public testBodyLineReturnsBodyState_and_DoesNotWriteToMessage()
        throws ParseException {
    String line = "BODY" 
    Message message = new Message();

    State state = State.HEADER.parse(message, line);

    assertEquals( State.BODY, state );
    assertEmpty( message.getHeader() );
}
&lt;/pre&gt;

	&lt;p&gt;Now, all we have to do is make this pass in the &lt;span class="caps"&gt;HEADER&lt;/span&gt; enum:&lt;/p&gt;


&lt;pre&gt;
HEADER(new Partial() {
    public State parse(Message message, String line)
            throws ParseException {
        if ( line.equals("BODY") ) return BODY;

        message.addLineToHeader( line );

        return HEADER;
    }
});
&lt;/pre&gt;

	&lt;p&gt;Now, if you are using Java closures, you simply cannot have multiple exit points.  It&amp;#8217;s easy enough to alter the logic.  Some people don&amp;#8217;t like what I did as a matter of principle.  That&amp;#8217;s OK.  We know it works and its tested.  We can refactor it later to our heart&amp;#8217;s content.  For the purpose of brevity, it&amp;#8217;s up to you to do the same thing with the &lt;code&gt;State.BODY&lt;/code&gt; implementation.&lt;/p&gt;


	&lt;h4&gt;Using our Finite State Machine&lt;/h4&gt;


	&lt;p&gt;Using the Finite State Machine we just created (granted it has only two states) is really easy.  We know that we need a message object, and we need to iterate over the lines to a message.  We&amp;#8217;ll assume that you got it from some Reader.  Here is a method that does the hard work for you:&lt;/p&gt;


&lt;pre&gt;
public Message parseMessage(Reader in)
        throws ParseException, IOException
{
    Message message = new Message();
    State state = State.HEADER;
    BufferedReader reader = new BufferedReader(in);

    String line = null;

    while((line = reader.readLine()) != null) {
        state = state.parse(message, line);
    }

    reader.close();

    return message;
}
&lt;/pre&gt;

	&lt;p&gt;I left all the error handling code as an exercise for you, dear reader.  If you want to provide accurate line numbers for your &lt;code&gt;ParseException&lt;/code&gt; objects, you can surround the thing in a try/catch and use the following construct:&lt;/p&gt;


&lt;pre&gt;
catch(ParseException pe) {
    // Rewrite the line number where the problem occurred.
    ParseException npe =
        new ParseException(pe.getMessage(), lineNumber);
    npe.setStackTrace(pe.getStackTrace());
    throw npe;
}
&lt;/pre&gt;

	&lt;p&gt;You have to keep track of the line number yourself.  The &lt;code&gt;lineNumber&lt;/code&gt; variable was incremented in the &lt;code&gt;while&lt;/code&gt; loop in my code.  I also wrapped the IOException in a ParseException keeping track of the line number so the signature was simplified.  Of course, you&amp;#8217;ll want close the reader in a &lt;code&gt;finally&lt;/code&gt; clause.&lt;/p&gt;


	&lt;h4&gt;In Conclusion&lt;/h4&gt;


	&lt;p&gt;You aren&amp;#8217;t going to write a finite state machine every day, but when you do you&amp;#8217;ll want to keep things as simple as possible.  The approach I outlined is very handy in the sense that you can completely test each state in isolation from the others.  Because it is its own object, you don&amp;#8217;t have to worry about testing the whole of the application to ensure your states are doing what they were designed to do.  I typically have each State instance tested in its own TestCase object.  This makes it particularly clear what each state is supposed to do, and how/when transitions take place.&lt;/p&gt;


	&lt;p&gt;I find that &lt;span class="caps"&gt;FSM&lt;/span&gt; are much easier to understand when you think about one state at at time.  Trying to keep the whole &amp;#8220;if this, then that, or is it the other thing&amp;#8221; reasoning can be avoided that way.  Doing it the old procedural approach is really not very useful these days.  Don&amp;#8217;t query data and keep control.  Ask your objects to do work for you.  Delegate.&lt;/p&gt;</description>
      <pubDate>Mon, 23 Jul 2007 12:57:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:fc2ed591-736e-4de4-9969-358e9cea3c65</guid>
      <author>Berin Loritsch</author>
      <link>http://bloritsch.d-haven.net/articles/2007/07/23/using-java-enums-for-finite-state-machines</link>
      <category>finitestatemachine</category>
      <category>fsm</category>
      <category>design</category>
      <category>code</category>
      <category>java</category>
      <category>delegate</category>
      <category>test</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>"Using Java Enums for Finite State Machines" by simpson.cl@gmail.com</title>
      <description>&lt;p&gt;An enum allows you to define a body containing methods and instance variables for each enum constant so you do not have to use the anonymous class syntax. The final result is still an anonymous class and it is still a singleton but, I find the syntax a little cleaner. I typically declare an abstract method in the enum and override it in the enum constants. You can also provide an abstract base method in the enum body and only override it in the enum constants that require additional behavior. Using an enum constant body the example above would be:&lt;/p&gt;

&lt;pre&gt;
public enum State {
HEADER() {
    @Override
    public State parse(Message message, String line)
            throws ParseException {
        if ( line.equals("BODY") ) return BODY;

        message.addLineToHeader( line );

        return HEADER;
    }
},
OTHER_ENUM_CONSTANTS ...;

private State() {}

abstract public State parse(Message message, String line)
            throws ParseException;
}
&lt;/pre&gt;</description>
      <pubDate>Sun, 29 Jul 2007 16:28:45 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:e1e312cf-ad21-4128-9a0c-36e01de1c544</guid>
      <link>http://bloritsch.d-haven.net/articles/2007/07/23/using-java-enums-for-finite-state-machines#comment-18</link>
    </item>
    <item>
      <title>"Using Java Enums for Finite State Machines" by Berin Loritsch</title>
      <description>&lt;p&gt;It makes much more sense if you knew more about the format of the text I have to parse.  Suffice it to say, there are several contexts that need to be parsed differently.  The original version of the parser used a string to represent the context and a large if/then/else structure based on the context value.  An enum was a natural evolution, and then making the enums take care of the conditionals further cleaned up the code.&lt;/p&gt;

&lt;p&gt;The use of enums also compliments the states in the FSM.  If you think of it as one enum value per state, you can have a fairly straightforward mapping from your state transition design to the implementation.&lt;/p&gt;

&lt;p&gt;Sure you can do the same thing with generic objects, but for this particular application it seemed to be a better fit to use the enum.&lt;/p&gt;</description>
      <pubDate>Fri, 27 Jul 2007 16:32:31 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:01f5af05-34bb-461c-aadd-d15818dd03a8</guid>
      <link>http://bloritsch.d-haven.net/articles/2007/07/23/using-java-enums-for-finite-state-machines#comment-16</link>
    </item>
    <item>
      <title>"Using Java Enums for Finite State Machines" by Ron</title>
      <description>&lt;p&gt;Hi, I think I&amp;#8217;m just being dense, but why do this using enums and not just regular classes?  I think I don&amp;#8217;t see any enum-ish idioms here, so I&amp;#8217;m not getting it.
Thanks very much,
Ron&lt;/p&gt;</description>
      <pubDate>Fri, 27 Jul 2007 16:16:37 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:b43829a1-986a-4b2e-9368-9b525f9e093d</guid>
      <link>http://bloritsch.d-haven.net/articles/2007/07/23/using-java-enums-for-finite-state-machines#comment-15</link>
    </item>
  </channel>
</rss>
