Package org.agilewiki.jactor.simpleMachine

You can think of a State Machine as a kind of computer.

See: Description

Package org.agilewiki.jactor.simpleMachine Description

You can think of a State Machine as a kind of computer. The three main parts of a computer are the program, the program counter and the memory.

The program is an array of operations which are executed by the state machine. The program counter is an index which identifies the next operation to be executed. The execution cycle of a state machine is a loop with 3 steps:

  1. Fetch the operation identified by the program counter.
  2. Increment the program counter by 1. And
  3. Execute the operation that was fetched.
  4. When you reach the end of the array, the state machine exits.

    An operation can change the value of the program counter. This is how a goto works. But to make things just a little friendlier, we add a table called labels. As we create the program, we can capture the index of the next location in the program array and save it with a string for a key in the labels table. This way a goto operation can be given the name of a location, which it looks up in the labels table, rather than an index into the array of operations.

    The memory of the state machine is another table called results. This table also uses strings as keys. And an operation can easily access and update the results table.

    Why use a state machine? Because it is simpler to program a state machine to execute a series of operations than it is to code it directly, when each operation has a callback which is invoked when the operation completes. The state machine handles all the callbacks, allowing you to focus on the operations to be performed.

    The nested class JLPCActor.SMBuilder is a concrete implementation of _SMBuilder and is used to define, instantiate and execute state machines.

        class ReturnF1 extends JLPCActor {
    
            ReturnF1(Mailbox mailbox) {
                super(mailbox);
            }
    
            public void processRequest(Object unwrappedRequest, RP rp)
                    throws Exception {
                SMBuilder smb = new SMBuilder();
                smb._return(new ObjectFunc() {
                    public Object get(StateMachine sm) {
                        return "Hello world!";
                    }
                });
                smb.call(rp);
            }
        }
    
                Result:
                Hello world!
     

Copyright © 2012. All Rights Reserved.