调用TypeError未绑定方法ToTransition()时,必须将FSM实例作为第一个参数(改为获取str实例)

2024-04-27 18:23:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个有限状态机,应该随机选择一个状态进入,但是我得到了以下类型的错误

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

Tags: selfexecuteifinitdefclassprintenter
1条回答
网友
1楼 · 发布于 2024-04-27 18:23:38

这里:

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

param名称是“FMS”,但是您将self.FSM设置为FSM,然后将其解析为(全局)FSM类。在

如果你遵循Python命名惯例(CamelCase中的类名,下面所有的变量和方法名)或者至少是一些一致的命名约定,在这些约定中,一个类和一个实例不使用相同的名称。。。在

相关问题 更多 >