我有一个有限状态机,应该随机选择要输入的状态,但是出现以下类型错误;
未绑定方法ToTransition()必须以FSM实例作为第一个参数来调用(取而代之的是str实例)
我不确定为什么会这样。启动时,它可以调用第一个状态,但随后失败。任何建议将不胜感激。
from random import randint
from time import clock
##========================================
##TRANSITIONS
class Transition(object):
def __init__(self, toState):
self.toState = toState
def Execute(self):
print ("Transitioning...")
##=======================================
##STATES
class State(object):
def __init__(self, FMS):
self.FSM = FSM
self.timer = 0
self.startTime = 0
def Enter(self):
self.timer = randint(0,5)
self.startTime = int(clock())
def Execute(self):
pass
def Exit(self):
pass
class CleanDishes(State):
def __init__(self, FSM):
super(CleanDishes, self).__init__(FSM)
def Enter(self):
print ("Preparing to clean dishes")
super(CleanDishes, self).Enter()
def Execute(self):
print ("Cleaning Dishes.")
if(self.startTime + self.timer <= clock()):
if not(randint(1,3) %2):
self.FSM.ToTransition("toVacuum")
else:
self.FSM.ToTransition("toSleep")
def Exit(self):
print ("Finished the Dishes.")
class Vacuum(State):
def __init__(self, FSM):
super(Vacuum, self).__init__(FSM)
def Enter(self):
print ("Preparing to Vacumm")
super(Vacuum, self).Enter()
def Execute(self):
print ("Vacuuming.")
if(self.startTime + self.timer <= clock()):
if not(randint(1, 3) %2):
self.FSM.ToTransition('toSleep')
else:
self.FSM.ToTransition('toCleanDishes')
def Exit(self):
print ("Finished Vacuuming.")
class Sleep(State):
def __init__(self, FSM):
super(Sleep, self).__init__(FSM)
def Enter(self):
print ("falling asleep")
super(Sleep, self).Enter()
def Execute(self):
print ("Sleeping.")
if(self.startTime + self.timer <= clock()):
if not(randint(1,3) %2):
self.FSM.ToTransition("toVacuum")
else:
self.FSM.ToTransition("toCleanDishes")
def Exit(self):
print ("Waking Up.")
##======================================
##FSM
class FSM(object):
def __init__(self, character):
self.char = character
self.states = {}
self.transitions = {}
self.curState = None
self.prevState = None
self.trans = None
def AddTransition(self, transName, transition):
self.transitions[transName] = transition
def AddState(self, stateName, state):
self.states[stateName] = state
def SetState(self, stateName):
self.prevState = self.curState
self.curState = self.states[stateName]
def ToTransition(self, toTrans):
self.trans = self.transitions[toTrans]
def Execute(self):
if(self.trans):
self.curState.Exit()
self.trans.Execute()
self.SetState(self.trans.toState)
self.curState.Enter()
self.trans = None
self.curState.Execute()
##===================================
##IMPLEMENTATION
Char = type("Char", (object,), {})
class RobotMaid(Char):
def __init__(self):
self.FSM = FSM(self)
##STATES
self.FSM.AddState("Sleep", Sleep(self.FSM))
self.FSM.AddState("CleanDishes", CleanDishes(self.FSM))
self.FSM.AddState("Vacuum", Vacuum(self.FSM))
##TRANSISITONS
self.FSM.AddTransition("toSleep", Transition(Sleep))
self.FSM.AddTransition("toVacuum", Transition(Vacuum))
self.FSM.AddTransition("toCleanDishes", Transition(CleanDishes))
self.FSM.SetState("Vacuum")
def Execute(self):
self.FSM.Execute()
if __name__ == '__main__':
r = RobotMaid()
for i in range(20):
startTime = clock()
timeInterval = 1
while(startTime + timeInterval > clock()):
pass
r.Execute()
答案 0 :(得分:2)
这里:
class State(object):
def __init__(self, FMS):
self.FSM = FSM
参数名称为“ FMS”,但是您将self.FSM
设置为FSM
,然后将其解析为(全局)FSM
类。
如果您遵循Python命名约定(CamelCase中的类名,all_lower中的变量和方法名)或至少某些一致的命名约定(如果您对类和实例...