AST Transformations
[ 210 ]
}
state: "ON"
state: "OFF"
event: "toggle"
when: "ON"
prev = "OFF"
when: "OFF"
next * "OFF"
This will provide the following output:
ErrorState.groovy: 9: assignment to next is only allowed
@ line 9, column 9.
prev = "OFF"
^
ErrorState.groovy: 11: assignment operator is only allowed
@ line 11, column 9.
next * "OFF"
^
Building the new AST nodes
Once the StateMachineParser class has done its magic, we should have a valid
representation of the state machine in the StateMachineModel object. From this
model, we need to build a new implementation of the state machine pattern. This
will involve adding nodes to the AST for the various elements of the pattern.
We will need to generate a client class to implement the state machine itself. We
will need a state context class, and we will need a concrete state class for each of the
states. We also need to build methods to implement each of the events across all the
state classes and the client class and to handle all the individual state transitions that
are declared for each event.
We handle all of this in the StateMachineBuilder class. The StateMachineBuilder
class is initialized with the new StateMachineModel object we built during parsing.
It has a method buildStatePattern that does the work of building the pattern
classes, but this is subsequently broken down into smaller steps. We will look at
each step in detail:
class StatePatternBuilder {
def model
http://www.ebook3000.com