python实现简单状态框架的方法

本文实例讲述了python实现简单状态框架的方法。分享给大家供大家参考。具体分析如下:

这里使用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

Posted in 未分类