【问题标题】:state do not change in a FSM in python using transitions library使用转换库在 python 中的 FSM 中状态不会改变
【发布时间】:2020-09-27 20:31:38
【问题描述】:

我正在尝试从 talk 复制代码:

class Video:
    ## Define the states
    PLAYING = "playing"
    PAUSED = "paused"
    STOPPED = "stopped"
    
    
    def __init__(self,source):
        self.source = source
        
        transitions = [
            
            {"trigger":"play","source":self.PAUSED, "dest":self.PLAYING},
            {"trigger":"play","source":self.STOPPED, "dest":self.PLAYING},
            ##
            {"trigger":"pause","source":self.PAUSED, "dest":self.PAUSED},
            ##
            {"trigger":"stop","source":self.PAUSED, "dest":self.STOPPED},
            {"trigger":"stop","source":self.PLAYING, "dest":self.STOPPED}
            
        ]
        
        self.machine = Machine( 
        model = self,
        transitions = transitions,
        initial = self.STOPPED)
        
        
    def pause(self):
        print ("pause")
        
    def play(self):
        print ("play")
        
    def stop(self):
        print ("stop")

但我称之为,它不起作用:

test = Video("some text")

返回警告:

2020-09-27 17:25:50,255 [11472] WARNING  transitions.core:828: [JupyterRequire] Model already contains an attribute 'play'. Skip binding.
2020-09-27 17:25:50,259 [11472] WARNING  transitions.core:828: [JupyterRequire] Model already contains an attribute 'pause'. Skip binding.
2020-09-27 17:25:50,260 [11472] WARNING  transitions.core:828: [JupyterRequire] Model already contains an attribute 'stop'. Skip binding.

但主要问题是状态没有改变:

这是原演讲的代码:

【问题讨论】:

    标签: python fsm


    【解决方案1】:

    我在这里发布修改后的代码,它会为您提供预期的输出。为代码格式化道歉,我真的很挣扎这里的标记编辑器。

    有问题的代码似乎缺少的东西是

    1. 您没有将状态列表传递给 FSM 机器。

    2. 此外,看起来,机器使用触发功能修改模型。您拥有相同命名的函数似乎覆盖了那些[1]。我认为触发任何函数的正确方法是使用“after”属性和函数名称。很可能以前也会有。

      from transitions import Machine
      
      class Video:
      
          ## Define the states
          PLAYING = "playing"
          PAUSED = "paused"
          STOPPED = "stopped"
      
          states = [PLAYING, PAUSED, STOPPED]
      
      
          def __init__(self,source):
              self.source = source
      
              transitions = [ 
      
                  {"trigger":"play","source":self.PAUSED, "dest":self.PLAYING, "after": "log_play"},
                  {"trigger":"play","source":self.STOPPED, "dest":self.PLAYING, "after": "log_play"},
                   ##  
                  {"trigger":"pause","source":self.PAUSED, "dest":self.PAUSED},
                  ##  
                  {"trigger":"stop","source":self.PAUSED, "dest":self.STOPPED},
                  {"trigger":"stop","source":self.PLAYING, "dest":self.STOPPED}
      
              ]   
      
              self.machine = Machine( 
                  model = self,
                  transitions = transitions,
                  states = self.states,
                  initial = self.STOPPED)
      
      
          def log_pause(self):
              print ("pause")
      
          def log_play(self):
              print ("play")
      
          def log_stop(self):
              print ("stop")
      

    参考:

    1. https://github.com/pytransitions/transitions/blob/master/transitions/core.py#L594

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-03
      • 2012-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-10
      • 2019-02-12
      相关资源
      最近更新 更多