TypeError - 未绑定方法 ToTransition() 必须以 FSM 实例作为第一个参数调用(取而代之的是 str 实例)

TypeError - unbound method ToTransition() must be called with FSM instance as first argument (got str instance instead)

我有一个有限状态机,它应该随机 select 进入一个状态,但是我收到以下类型错误;

unbound method ToTransition() must be called with FSM instance as first argument (got str instance instead)

我不确定为什么会这样。启动时,它可以调用第一个状态,但随后失败。如有任何建议,我们将不胜感激。

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()

这里:

class State(object):
    def __init__(self, FMS):
        self.FSM = FSM

参数名称是 "FMS" 但您将 self.FSM 设置为 FSM,然后解析为(全局)FSM class。

如果您遵循 Python 命名约定(classes 名称采用 CamelCase,变量和方法名称采用 all_lower)或至少遵循一些一致的命名约定,则不会发生这种情况您不对 class 和实例使用相同的确切名称...