Where the classic finite state machine required one large function to define all states of a FSM, the current finite state machine system creates these states implicitly. To prevent confusion, it may be best to create this finite state machine as its own class.
from direct.fsm import FSM
from direct.fsm import State
class NewFSM (FSM.FSM):
|
Like the classic finite state machine, this machine requires enter and exit functions. The name of the state is derived from the names of the entrance and exit functions, and is usually capitalized. There is also a third function, a filter function that may be called for each state. This filter function is called when a request for transition is made, and it checks the current state’s filter to see if it is indeed a legal transition. If there is no filter function for a state, then a default filter command is called, which will change the state. However, if there is a filter function provided for the state, then the default filter command will simply reject any transition.
The traffic light finite state machine is provided as an example of how to write for the new finite state machine system.
from direct.fsm import FSM
from direct.fsm import State
class NewStyle(FSM.FSM):
def enterRed(self, oldState, newState):
print "enterRed(self, '%s', '%s')" % (oldState, newState)
def filterRed(self, request, args):
print "filterRed(self, '%s', %s)" % (request, args)
if request == 'advance':
return 'Green'
return self.defaultFilter(request, args)
def exitRed(self, oldState, newState):
print "exitRed(self, '%s', '%s')" % (oldState, newState)
def enterYellow(self, oldState, newState):
print "enterYellow(self, '%s', '%s')" % (oldState, newState)
def filterYellow(self, request, args):
print "filterYellow(self, '%s', %s)" % (request, args)
if request == 'advance':
return 'Red'
return self.defaultFilter(request, args)
def exitYellow(self, oldState, newState):
print "exitYellow(self, '%s', '%s')" % (oldState, newState)
def enterGreen(self, oldState, newState):
print "enterGreen(self, '%s', '%s')" % (oldState, newState)
def filterGreen(self, request, args):
print "filterGreen(self, '%s', %s)" % (request, args)
if request == 'advance':
return 'Yellow'
return self.defaultFilter(request, args)
def exitGreen(self, oldState, newState):
print "exitGreen(self, '%s', '%s')" % (oldState, newState)
|
The new finite state machine uses the filter functions to determine the new state. Instead of having to provide the actual name of the state, this example used the string “advance.” With this, the filter function called knew which state to move to. Otherwise, it returned the default filter. Since there is an existing filter for the state, the default filter simply sends a message saying that the request has been denied.
Just like the classic finite state machine, the standard one has a request command. It is also able to force a transition.
request.(‘<String>’)
forceTransition(‘<State Name>’)
|
|