当前位置: 首页>>代码示例>>Python>>正文


Python Machine.add_ordered_transitions方法代码示例

本文整理汇总了Python中transitions.Machine.add_ordered_transitions方法的典型用法代码示例。如果您正苦于以下问题:Python Machine.add_ordered_transitions方法的具体用法?Python Machine.add_ordered_transitions怎么用?Python Machine.add_ordered_transitions使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在transitions.Machine的用法示例。


在下文中一共展示了Machine.add_ordered_transitions方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_ordered_transition_condition

# 需要导入模块: from transitions import Machine [as 别名]
# 或者: from transitions.Machine import add_ordered_transitions [as 别名]
    def test_ordered_transition_condition(self):
        class Model:
            def __init__(self):
                self.blocker = False

            def check_blocker(self):
                return self.blocker

        model = Model()
        states = ['beginning', 'middle', 'end']
        m = Machine(model, states, initial='beginning')
        m.add_ordered_transitions(conditions=[None, None, 'check_blocker'])
        model.to_end()
        self.assertFalse(model.next_state())
        model.blocker = True
        self.assertTrue(model.next_state())
开发者ID:jaquinonesg,项目名称:SMA_exp,代码行数:18,代码来源:test_core.py

示例2: test_ordered_transition_callback

# 需要导入模块: from transitions import Machine [as 别名]
# 或者: from transitions.Machine import add_ordered_transitions [as 别名]
    def test_ordered_transition_callback(self):
        class Model:
            def __init__(self):
                self.flag = False

            def make_true(self):
                self.flag = True

        model = Model()
        states = ['beginning', 'middle', 'end']
        transits = [None, None, 'make_true']
        m = Machine(model, states, initial='beginning')
        m.add_ordered_transitions(before=transits)
        model.next_state()
        self.assertFalse(model.flag)
        model.next_state()
        model.next_state()
        self.assertTrue(model.flag)
开发者ID:jaquinonesg,项目名称:SMA_exp,代码行数:20,代码来源:test_core.py

示例3: test_ordered_transitions

# 需要导入模块: from transitions import Machine [as 别名]
# 或者: from transitions.Machine import add_ordered_transitions [as 别名]
    def test_ordered_transitions(self):
        states = ['beginning', 'middle', 'end']
        m = Machine(None, states)
        m.add_ordered_transitions()
        self.assertEquals(m.state, 'initial')
        m.next_state()
        self.assertEquals(m.state, 'beginning')
        m.next_state()
        m.next_state()
        self.assertEquals(m.state, 'end')
        m.next_state()
        self.assertEquals(m.state, 'initial')

        # Include initial state in loop
        m = Machine(None, states)
        m.add_ordered_transitions(loop_includes_initial=False)
        m.to_end()
        m.next_state()
        self.assertEquals(m.state, 'beginning')

        # Test user-determined sequence and trigger name
        m = Machine(None, states, initial='beginning')
        m.add_ordered_transitions(['end', 'beginning'], trigger='advance')
        m.advance()
        self.assertEquals(m.state, 'end')
        m.advance()
        self.assertEquals(m.state, 'beginning')

        # Via init argument
        m = Machine(
            None, states, initial='beginning', ordered_transitions=True)
        m.next_state()
        self.assertEquals(m.state, 'middle')
开发者ID:TheMysteriousX,项目名称:transitions,代码行数:35,代码来源:test_core.py

示例4: test_ordered_transitions

# 需要导入模块: from transitions import Machine [as 别名]
# 或者: from transitions.Machine import add_ordered_transitions [as 别名]
    def test_ordered_transitions(self):
        states = ["beginning", "middle", "end"]
        m = Machine(None, states)
        m.add_ordered_transitions()
        self.assertEquals(m.state, "initial")
        m.next_state()
        self.assertEquals(m.state, "beginning")
        m.next_state()
        m.next_state()
        self.assertEquals(m.state, "end")
        m.next_state()
        self.assertEquals(m.state, "initial")

        # Include initial state in loop
        m = Machine(None, states)
        m.add_ordered_transitions(loop_includes_initial=False)
        m.to_end()
        m.next_state()
        self.assertEquals(m.state, "beginning")

        # Test user-determined sequence and trigger name
        m = Machine(None, states, initial="beginning")
        m.add_ordered_transitions(["end", "beginning"], trigger="advance")
        m.advance()
        self.assertEquals(m.state, "end")
        m.advance()
        self.assertEquals(m.state, "beginning")

        # Via init argument
        m = Machine(None, states, initial="beginning", ordered_transitions=True)
        m.next_state()
        self.assertEquals(m.state, "middle")
开发者ID:khigia,项目名称:transitions,代码行数:34,代码来源:test_core.py

示例5: test_ordered_transition_error

# 需要导入模块: from transitions import Machine [as 别名]
# 或者: from transitions.Machine import add_ordered_transitions [as 别名]
 def test_ordered_transition_error(self):
     m = Machine(states=['A'], initial='A')
     with self.assertRaises(ValueError):
         m.add_ordered_transitions()
     m.add_state('B')
     m.add_ordered_transitions()
     m.add_state('C')
     with self.assertRaises(ValueError):
         m.add_ordered_transitions(['C'])
开发者ID:jaquinonesg,项目名称:SMA_exp,代码行数:11,代码来源:test_core.py

示例6: Order

# 需要导入模块: from transitions import Machine [as 别名]
# 或者: from transitions.Machine import add_ordered_transitions [as 别名]
class Order(SignalModule, Thread):

    states = ['init',
              'starting_trigger',
              'waiting_for_trigger_callback',
              'stopping_trigger',
              'start_order_listener',
              'waiting_for_order_listener_callback',
              'analysing_order']

    def __init__(self):
        super(SignalModule, self).__init__()
        Thread.__init__(self, name=Order)
        Utils.print_info('Starting order signal')
        # load settings and brain from singleton
        sl = SettingLoader()
        self.settings = sl.settings
        self.brain = BrainLoader().brain

        # keep in memory the order to process
        self.order_to_process = None

        # get the player instance
        self.player_instance = PlayerLauncher.get_player(settings=self.settings)

        # save an instance of the trigger
        self.trigger_instance = None
        self.trigger_callback_called = False
        self.skip_trigger = False  # keep the status of the trigger, if true we can skip it in the statue machine

        # save the current order listener
        self.order_listener = None
        self.order_listener_callback_called = False

        # Initialize the state machine
        self.machine = Machine(model=self, states=Order.states, initial='init', queued=True)

        # define transitions
        self.machine.add_transition('start_trigger', ['init', 'analysing_order'], 'starting_trigger')
        self.machine.add_transition('wait_trigger_callback', 'starting_trigger', 'waiting_for_trigger_callback')
        self.machine.add_transition('stop_trigger', 'waiting_for_trigger_callback', 'stopping_trigger')
        self.machine.add_transition('wait_for_order', 'stopping_trigger', 'waiting_for_order_listener_callback')
        self.machine.add_transition('analyse_order', 'waiting_for_order_listener_callback', 'analysing_order')
        self.machine.add_transition('start_order_listener', 'analysing_order', 'start_order_listener')

        self.machine.add_ordered_transitions()

        # add method which are called when changing state
        self.machine.on_enter_starting_trigger('start_trigger_process')
        self.machine.on_enter_waiting_for_trigger_callback('waiting_for_trigger_callback_thread')
        self.machine.on_enter_stopping_trigger('stop_trigger_process')
        self.machine.on_enter_start_order_listener('start_order_listener_thread')
        self.machine.on_enter_waiting_for_order_listener_callback('waiting_for_order_listener_callback_thread')
        self.machine.on_enter_analysing_order('analysing_order_thread')

    def run(self):
        # run hook on_start
        HookManager.on_start()
        self.start_trigger()

    def start_trigger_process(self):
        """
        This function will start the trigger thread that listen for the hotword
        """
        logger.debug("[Order] Entering state: %s" % self.state)
        HookManager.on_waiting_for_trigger()
        self.trigger_instance = TriggerLauncher.get_trigger(settings=self.settings, callback=self.trigger_callback)
        self.trigger_callback_called = False
        self.trigger_instance.daemon = True
        # Wait that the kalliope trigger is pronounced by the user
        self.trigger_instance.start()
        self.next_state()

    def waiting_for_trigger_callback_thread(self):
        """
        Method to print in debug that the main process is waiting for a trigger detection
        """
        logger.debug("[Order] Entering state: %s" % self.state)
        if self.settings.options.deaf:  # the user asked to deaf inside the deaf neuron
            Utils.print_info("Kalliope is deaf")
            self.trigger_instance.pause()
        else:
            Utils.print_info("Waiting for trigger detection")
        # this loop is used to keep the main thread alive
        while not self.trigger_callback_called:
            sleep(0.1)
        # if here, then the trigger has been called
        HookManager.on_triggered()
        self.next_state()

    def waiting_for_order_listener_callback_thread(self):
        """
        Method to print in debug that the main process is waiting for an order to analyse
        """
        logger.debug("[Order] Entering state: %s" % self.state)
        # this loop is used to keep the main thread alive
        while not self.order_listener_callback_called:
            sleep(0.1)
        # TODO on end listening here
        self.next_state()
#.........这里部分代码省略.........
开发者ID:igorstarki,项目名称:kalliope,代码行数:103,代码来源:order.py


注:本文中的transitions.Machine.add_ordered_transitions方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。