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


Python transitions.Machine方法代码示例

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


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

示例1: endpoint_factory

# 需要导入模块: import transitions [as 别名]
# 或者: from transitions import Machine [as 别名]
def endpoint_factory(hashed_val):
    endpoint = Endpoint(hashed_val)
    machine = Machine(
        model=endpoint,
        states=Endpoint.states,
        transitions=Endpoint.transitions,
        initial='unknown',
        send_event=True)
    machine.name = endpoint.name[:8]+' '
    endpoint.machine = machine
    copro_endpoint = Endpoint(hashed_val)
    copro_machine = Machine(
        model=copro_endpoint,
        states=Endpoint.copro_states,
        transitions=Endpoint.copro_transitions,
        initial='copro_unknown',
        send_event=True)
    copro_machine.name = endpoint.name[:8]+'_copro'
    endpoint.copro_machine = copro_machine
    return endpoint 
开发者ID:CyberReboot,项目名称:poseidon,代码行数:22,代码来源:endpoint.py

示例2: test_add_model_initial_state

# 需要导入模块: import transitions [as 别名]
# 或者: from transitions import Machine [as 别名]
def test_add_model_initial_state(self):
        states = ['A', 'B', 'C', 'D', 'E', 'F']
        machine = Machine(model=[], states=states, initial='A', name='Test Machine')
        machine.add_transition('advance', 'A', 'B')
        machine.add_transition('advance', 'B', 'C')
        machine.add_transition('advance', 'C', 'D')

        s1 = Dummy()
        s2 = Dummy()
        s3 = Dummy()

        machine.add_model(s1)
        machine.add_model(s2, initial='B')
        machine.add_model(s3, initial='C')

        s1.advance()
        s2.advance()
        s3.advance()

        self.assertTrue(s1.is_B())
        self.assertTrue(s2.is_C())
        self.assertTrue(s3.is_D()) 
开发者ID:pytransitions,项目名称:transitions,代码行数:24,代码来源:test_add_remove.py

示例3: test_add_model_no_initial_state

# 需要导入模块: import transitions [as 别名]
# 或者: from transitions import Machine [as 别名]
def test_add_model_no_initial_state(self):
        states = ['A', 'B', 'C', 'D', 'E', 'F']
        machine = Machine(model=[], states=states, name='Test Machine', initial=None)
        machine.add_transition('advance', 'A', 'B')
        machine.add_transition('advance', 'B', 'C')
        machine.add_transition('advance', 'C', 'D')

        s1 = Dummy()
        s2 = Dummy()
        s3 = Dummy()

        with self.assertRaises(ValueError):
            machine.add_model(s1)
        machine.add_model(s1, initial='A')
        machine.add_model(s2, initial='B')
        machine.add_model(s3, initial='C')

        s1.advance()
        s2.advance()
        s3.advance()

        self.assertTrue(s1.is_B())
        self.assertTrue(s2.is_C())
        self.assertTrue(s3.is_D()) 
开发者ID:pytransitions,项目名称:transitions,代码行数:26,代码来源:test_add_remove.py

示例4: test_transition_definitions

# 需要导入模块: import transitions [as 别名]
# 或者: from transitions import Machine [as 别名]
def test_transition_definitions(self):
        states = ['A', 'B', 'C', 'D']
        # Define with list of dictionaries
        transitions = [
            {'trigger': 'walk', 'source': 'A', 'dest': 'B'},
            {'trigger': 'run', 'source': 'B', 'dest': 'C'},
            {'trigger': 'sprint', 'source': 'C', 'dest': 'D'}
        ]
        m = Machine(states=states, transitions=transitions, initial='A')
        m.walk()
        self.assertEqual(m.state, 'B')
        # Define with list of lists
        transitions = [
            ['walk', 'A', 'B'],
            ['run', 'B', 'C'],
            ['sprint', 'C', 'D']
        ]
        m = Machine(states=states, transitions=transitions, initial='A')
        m.to_C()
        m.sprint()
        self.assertEqual(m.state, 'D') 
开发者ID:pytransitions,项目名称:transitions,代码行数:23,代码来源:test_core.py

示例5: test_pass_state_instances_instead_of_names

# 需要导入模块: import transitions [as 别名]
# 或者: from transitions import Machine [as 别名]
def test_pass_state_instances_instead_of_names(self):
        state_A = State('A')
        state_B = State('B')
        states = [state_A, state_B]
        m = Machine(states=states, initial=state_A)
        assert m.state == 'A'
        m.add_transition('advance', state_A, state_B)
        m.advance()
        assert m.state == 'B'
        state_B2 = State('B', on_enter='this_passes')
        with self.assertRaises(ValueError):
            m.add_transition('advance2', state_A, state_B2)
        m2 = Machine(states=states, initial=state_A.name)
        assert m.initial == m2.initial
        with self.assertRaises(ValueError):
            Machine(states=states, initial=State('A')) 
开发者ID:pytransitions,项目名称:transitions,代码行数:18,代码来源:test_core.py

示例6: test_inheritance

# 需要导入模块: import transitions [as 别名]
# 或者: from transitions import Machine [as 别名]
def test_inheritance(self):
        states = ['A', 'B', 'C', 'D', 'E']
        s = InheritedStuff(states=states, initial='A')
        s.add_transition('advance', 'A', 'B', conditions='this_passes')
        s.add_transition('advance', 'B', 'C')
        s.add_transition('advance', 'C', 'D')
        s.advance()
        self.assertEqual(s.state, 'B')
        self.assertFalse(s.is_A())
        self.assertTrue(s.is_B())
        s.advance()
        self.assertEqual(s.state, 'C')

        class NewMachine(Machine):
            def __init__(self, *args, **kwargs):
                super(NewMachine, self).__init__(*args, **kwargs)

        n = NewMachine(states=states, transitions=[['advance', 'A', 'B']], initial='A')
        self.assertTrue(n.is_A())
        n.advance()
        self.assertTrue(n.is_B())
        with self.assertRaises(ValueError):
            NewMachine(state=['A', 'B']) 
开发者ID:pytransitions,项目名称:transitions,代码行数:25,代码来源:test_core.py

示例7: test_send_event_data_callbacks

# 需要导入模块: import transitions [as 别名]
# 或者: from transitions import Machine [as 别名]
def test_send_event_data_callbacks(self):
        states = ['A', 'B', 'C', 'D', 'E']
        s = Stuff()
        # First pass positional and keyword args directly to the callback
        m = Machine(model=s, states=states, initial='A', send_event=False,
                    auto_transitions=True)
        m.add_transition(
            trigger='advance', source='A', dest='B', before='set_message')
        s.advance(message='Hallo. My name is Inigo Montoya.')
        self.assertTrue(s.message.startswith('Hallo.'))
        s.to_A()
        s.advance('Test as positional argument')
        self.assertTrue(s.message.startswith('Test as'))
        # Now wrap arguments in an EventData instance
        m.send_event = True
        m.add_transition(
            trigger='advance', source='B', dest='C', before='extract_message')
        s.advance(message='You killed my father. Prepare to die.')
        self.assertTrue(s.message.startswith('You')) 
开发者ID:pytransitions,项目名称:transitions,代码行数:21,代码来源:test_core.py

示例8: test_string_callbacks

# 需要导入模块: import transitions [as 别名]
# 或者: from transitions import Machine [as 别名]
def test_string_callbacks(self):

        m = Machine(states=['A', 'B'],
                    before_state_change='before_state_change',
                    after_state_change='after_state_change', send_event=True,
                    initial='A', auto_transitions=True)

        m.before_state_change = MagicMock()
        m.after_state_change = MagicMock()

        m.to_B()

        self.assertTrue(m.before_state_change[0].called)
        self.assertTrue(m.after_state_change[0].called)

        # after_state_change should have been called with EventData
        event_data = m.after_state_change[0].call_args[0][0]
        self.assertIsInstance(event_data, EventData)
        self.assertTrue(event_data.result) 
开发者ID:pytransitions,项目名称:transitions,代码行数:21,代码来源:test_core.py

示例9: test_state_callbacks

# 需要导入模块: import transitions [as 别名]
# 或者: from transitions import Machine [as 别名]
def test_state_callbacks(self):

        class Model:
            def on_enter_A(self):
                pass

            def on_exit_A(self):
                pass

            def on_enter_B(self):
                pass

            def on_exit_B(self):
                pass

        states = [State(name='A', on_enter='on_enter_A', on_exit='on_exit_A'),
                  State(name='B', on_enter='on_enter_B', on_exit='on_exit_B')]

        machine = Machine(Model(), states=states)
        state_a = machine.get_state('A')
        state_b = machine.get_state('B')
        self.assertEqual(len(state_a.on_enter), 1)
        self.assertEqual(len(state_a.on_exit), 1)
        self.assertEqual(len(state_b.on_enter), 1)
        self.assertEqual(len(state_b.on_exit), 1) 
开发者ID:pytransitions,项目名称:transitions,代码行数:27,代码来源:test_core.py

示例10: test_pickle

# 需要导入模块: import transitions [as 别名]
# 或者: from transitions import Machine [as 别名]
def test_pickle(self):
        import sys
        if sys.version_info < (3, 4):
            import dill as pickle
        else:
            import pickle

        states = ['A', 'B', 'C', 'D']
        # Define with list of dictionaries
        transitions = [
            {'trigger': 'walk', 'source': 'A', 'dest': 'B'},
            {'trigger': 'run', 'source': 'B', 'dest': 'C'},
            {'trigger': 'sprint', 'source': 'C', 'dest': 'D'}
        ]
        m = Machine(states=states, transitions=transitions, initial='A')
        m.walk()
        dump = pickle.dumps(m)
        self.assertIsNotNone(dump)
        m2 = pickle.loads(dump)
        self.assertEqual(m.state, m2.state)
        m2.run() 
开发者ID:pytransitions,项目名称:transitions,代码行数:23,代码来源:test_core.py

示例11: test_queued

# 需要导入模块: import transitions [as 别名]
# 或者: from transitions import Machine [as 别名]
def test_queued(self):
        states = ['A', 'B', 'C', 'D']
        # Define with list of dictionaries

        def change_state(machine):
            self.assertEqual(machine.state, 'A')
            if machine.has_queue:
                machine.run(machine=machine)
                self.assertEqual(machine.state, 'A')
            else:
                with self.assertRaises(MachineError):
                    machine.run(machine=machine)

        transitions = [
            {'trigger': 'walk', 'source': 'A', 'dest': 'B', 'before': change_state},
            {'trigger': 'run', 'source': 'B', 'dest': 'C'},
            {'trigger': 'sprint', 'source': 'C', 'dest': 'D'}
        ]

        m = Machine(states=states, transitions=transitions, initial='A')
        m.walk(machine=m)
        self.assertEqual(m.state, 'B')
        m = Machine(states=states, transitions=transitions, initial='A', queued=True)
        m.walk(machine=m)
        self.assertEqual(m.state, 'C') 
开发者ID:pytransitions,项目名称:transitions,代码行数:27,代码来源:test_core.py

示例12: test_queued_errors

# 需要导入模块: import transitions [as 别名]
# 或者: from transitions import Machine [as 别名]
def test_queued_errors(self):
        def before_change(machine):
            if machine.has_queue:
                machine.to_A(machine)
            machine._queued = False

        def after_change(machine):
            machine.to_C(machine)

        def failed_transition(machine):
            raise ValueError('Something was wrong')

        states = ['A', 'B', 'C']
        transitions = [{'trigger': 'do', 'source': '*', 'dest': 'C', 'before': failed_transition}]
        m = Machine(states=states, transitions=transitions, queued=True,
                    before_state_change=before_change, after_state_change=after_change)
        with self.assertRaises(MachineError):
            m.to_B(machine=m)

        with self.assertRaises(ValueError):
            m.do(machine=m) 
开发者ID:pytransitions,项目名称:transitions,代码行数:23,代码来源:test_core.py

示例13: test_multiple_models

# 需要导入模块: import transitions [as 别名]
# 或者: from transitions import Machine [as 别名]
def test_multiple_models(self):
        s1, s2 = Stuff(), Stuff()
        states = ['A', 'B', 'C']

        m = Machine(model=[s1, s2], states=states,
                    initial=states[0])
        self.assertEqual(len(m.models), 2)
        self.assertEqual(len(m.model), 2)
        m.add_transition('advance', 'A', 'B')
        s1.advance()
        self.assertEqual(s1.state, 'B')
        self.assertEqual(s2.state, 'A')
        m = Machine(model=s1, states=states,
                    initial=states[0])
        # for backwards compatibility model should return a model instance
        # rather than a list
        self.assertNotIsInstance(m.model, list) 
开发者ID:pytransitions,项目名称:transitions,代码行数:19,代码来源:test_core.py

示例14: test_ordered_transition_callback

# 需要导入模块: import transitions [as 别名]
# 或者: from transitions import Machine [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:pytransitions,项目名称:transitions,代码行数:20,代码来源:test_core.py

示例15: test_ordered_transition_condition

# 需要导入模块: import transitions [as 别名]
# 或者: from transitions import Machine [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:pytransitions,项目名称:transitions,代码行数:18,代码来源:test_core.py


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