这里使用python实现一个简单的状态框架,代码需要在python3.2环境下运行
代码如下:
from time import sleep
from random import randint, shuffle
class statemachine(object):
''' usage: create an instance of statemachine, use set_starting_state(state) to give it an
initial state to work with, then call tick() on each second (or whatever your desired
time interval might be. '''
def set_starting_state(self, state):
''' the entry state for the state machine. '''
state.enter()
self.state = state
def tick(self):
''' calls the current state's do_work() and checks for a transition '''
next_state = self.state.check_transitions()
if next_state is none:
# stick with this state
self.state.do_work()
else:
# next state found, transition to it
self.state.exit()
next_state.enter()
self.state = next_state
class basestate(object):
''' usage: subclass basestate and override the enter(), do_work(), and exit() methods.
enter() -- setup for your state should occur here. this likely includes adding
transitions or initializing member variables.
do_work() -- meat and potatoes of your state. there may be some logic here that will
cause a transition to trigger.
exit() -- any cleanup or final actions should occur here. this is called just
before transition to the next state.
'''
def add_transition(self, condition, next_state):
''' adds a new transition to the state. the condition param must contain a callable
object. when the condition evaluates to true, the next_state param is set as
the active state. '''
# enforce transition validity
assert(callable(condition))
assert(hasattr(next_state, enter))
assert(callable(next_state.enter))
assert(hasattr(next_state, do_work))
assert(callable(next_state.do_work))
assert(hasattr(next_state, exit))
assert(callable(next_state.exit))
# add transition
if not hasattr(self, transitions):
self.transitions = []
self.transitions.append((condition, next_state))
def check_transitions(self):
''' returns the first state thats condition evaluates true (condition order is randomized) '''
if hasattr(self, transitions):
shuffle(self.transitions)
for transition in self.transitions:
condition, state = transition
if condition():
return state
def enter(self):
pass
def do_work(self):
pass
def exit(self):
pass
##################################################################################################
############################### example usage of state machine ###################################
##################################################################################################
class walkingstate(basestate):
def enter(self):
print(walkingstate: enter())
def condition(): return randint(1, 5) == 5
self.add_transition(condition, joggingstate())
self.add_transition(condition, runningstate())
def do_work(self):
print(walking...)
def exit(self):
print(walkingstate: exit())
class joggingstate(basestate):
def enter(self):
print(joggingstate: enter())
self.stamina = randint(5, 15)
def condition(): return self.stamina self.add_transition(condition, walkingstate())
def do_work(self):
self.stamina -= 1
print(jogging ({0})....format(self.stamina))
def exit(self):
print(joggingstate: exit())
class runningstate(basestate):
def enter(self):
print(runningstate: enter())
self.stamina = randint(5, 15)
def walk_condition(): return self.stamina self.add_transition(walk_condition, walkingstate())
def trip_condition(): return randint(1, 10) == 10
self.add_transition(trip_condition, trippingstate())
def do_work(self):
self.stamina -= 2
print(running ({0})....format(self.stamina))
def exit(self):
print(runningstate: exit())
class trippingstate(basestate):
def enter(self):
print(trippingstate: enter())
self.tripped = false
def condition(): return self.tripped
self.add_transition(condition, walkingstate())
def do_work(self):
print(tripped!)
self.tripped = true
def exit(self):
print(trippingstate: exit())
if __name__ == __main__:
state = walkingstate()
state_machine = statemachine()
state_machine.set_starting_state(state)
while true:
state_machine.tick()
sleep(1)
希望本文所述对大家的python程序设计有所帮助。