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


Python Machine.add_transition方法代码示例

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


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

示例1: test_repr

# 需要导入模块: from transitions import Machine [as 别名]
# 或者: from transitions.Machine import add_transition [as 别名]
    def test_repr(self):
        def a_condition(event_data):
            self.assertRegex(
                str(event_data.transition.conditions),
                r"\[<Condition\(<function TestTransitions.test_repr.<locals>"
                r".a_condition at [^>]+>\)@\d+>\]")
            return True

        # No transition has been assigned to EventData yet
        def check_prepare_repr(event_data):
            self.assertRegex(
                str(event_data),
                r"<EventData\('<State\('A'\)@\d+>', "
                r"None\)@\d+>")

        def check_before_repr(event_data):
            self.assertRegex(
                str(event_data),
                r"<EventData\('<State\('A'\)@\d+>', "
                r"<Transition\('A', 'B'\)@\d+>\)@\d+>")
            m.checked = True

        m = Machine(states=['A', 'B'],
                    prepare_event=check_prepare_repr,
                    before_state_change=check_before_repr, send_event=True,
                    initial='A')
        m.add_transition('do_strcheck', 'A', 'B', conditions=a_condition)

        self.assertTrue(m.do_strcheck())
        self.assertIn('checked', vars(m))
开发者ID:jaquinonesg,项目名称:SMA_exp,代码行数:32,代码来源:test_core.py

示例2: test_use_machine_as_model

# 需要导入模块: from transitions import Machine [as 别名]
# 或者: from transitions.Machine import add_transition [as 别名]
 def test_use_machine_as_model(self):
     states = ['A', 'B', 'C', 'D']
     m = Machine(states=states, initial='A')
     m.add_transition('move', 'A', 'B')
     m.add_transition('move_to_C', 'B', 'C')
     m.move()
     self.assertEquals(m.state, 'B')
开发者ID:TheMysteriousX,项目名称:transitions,代码行数:9,代码来源:test_core.py

示例3: test_before_after_callback_addition_callable

# 需要导入模块: from transitions import Machine [as 别名]
# 或者: from transitions.Machine import add_transition [as 别名]
 def test_before_after_callback_addition_callable(self):
     m = Machine(Stuff(), states=['A', 'B', 'C'], initial='A')
     m.add_transition('move', 'A', 'B')
     trans = m.events['move'].transitions['A'][0]
     trans.add_callback('after', m.model.increase_level)
     m.model.move()
     self.assertEquals(m.model.level, 2)
开发者ID:TheMysteriousX,项目名称:transitions,代码行数:9,代码来源:test_core.py

示例4: test_pass_state_instances_instead_of_names

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

示例5: test_garbage_collection

# 需要导入模块: from transitions import Machine [as 别名]
# 或者: from transitions.Machine import add_transition [as 别名]
    def test_garbage_collection(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()

        s2_collected = threading.Event()
        s2_proxy = weakref.proxy(s2, lambda _: s2_collected.set())

        machine.add_model([s1, s2])

        self.assertTrue(s1.is_A())
        self.assertTrue(s2.is_A())
        s1.advance()

        self.assertTrue(s1.is_B())
        self.assertTrue(s2.is_A())

        self.assertFalse(s2_collected.is_set())
        machine.remove_model(s2)
        del s2
        gc.collect()
        self.assertTrue(s2_collected.is_set())

        s3 = Dummy()
        machine.add_model(s3)
        s3.advance()
        s3.advance()
        self.assertTrue(s3.is_C())
开发者ID:jaquinonesg,项目名称:SMA_exp,代码行数:36,代码来源:test_add_remove.py

示例6: test_before_after_callback_addition

# 需要导入模块: from transitions import Machine [as 别名]
# 或者: from transitions.Machine import add_transition [as 别名]
 def test_before_after_callback_addition(self):
     m = Machine(Stuff(), states=["A", "B", "C"], initial="A")
     m.add_transition("move", "A", "B")
     trans = m.events["move"].transitions["A"][0]
     trans.add_callback("after", "increase_level")
     m.model.move()
     self.assertEquals(m.model.level, 2)
开发者ID:khigia,项目名称:transitions,代码行数:9,代码来源:test_core.py

示例7: ShoppingList

# 需要导入模块: from transitions import Machine [as 别名]
# 或者: from transitions.Machine import add_transition [as 别名]
class ShoppingList(object):
    states = ['todo', 'progress', 'finished']

    """
    @:param str name
    """
    def __init__(self, name):
        self.name = name
        self.items = {}
        self.machine = Machine(model=self, states=ShoppingList.states, initial='todo')
        self.machine.add_transition('done', ['todo', 'progress'], 'finished', conditions='is_done')
        self.machine.add_transition('shopping', 'todo', 'progress')

    """
    @:param ListItem item
    """
    def add(self, item):
        self.items[item.name] = item

    """
    @:param str name
    @:return ListItem
    """
    def get(self, name):
        return self.items.get(name)

    """
    @:param str name
    """
    def remove(self, name):
        del self.items[name]

    """
    Look in the items of this shopping list to see if the item was
    found, and if it was, reduce the quantity required by the given
    amount

    @:param str name
    @:param int quantity
    """
    def update(self, name, quantity=1):
        item = self.get(name)

        if item is not None:
            item.found(quantity)

        if self.is_done():
            self.to_finished()

    """
    Determines if the shopping is done by making sure all the list
    items have been fully acquired
    """
    def is_done(self):
        items = [item for item in self.items.values() if item.state is not 'acquired']

        return len(items) is 0
开发者ID:brendo,项目名称:shopomatic,代码行数:59,代码来源:shopping_list.py

示例8: test_before_after_transition_listeners

# 需要导入模块: from transitions import Machine [as 别名]
# 或者: from transitions.Machine import add_transition [as 别名]
    def test_before_after_transition_listeners(self):
        m = Machine(Stuff(), states=["A", "B", "C"], initial="A")
        m.add_transition("move", "A", "B")
        m.add_transition("move", "B", "C")

        m.before_move("increase_level")
        m.model.move()
        self.assertEquals(m.model.level, 2)
        m.model.move()
        self.assertEquals(m.model.level, 3)
开发者ID:khigia,项目名称:transitions,代码行数:12,代码来源:test_core.py

示例9: __init__

# 需要导入模块: from transitions import Machine [as 别名]
# 或者: from transitions.Machine import add_transition [as 别名]
class UnSubscription:
    states = ["new", "unsubscribed", "acknowledged"]

    def __init__(self, packet_id, topics):
        self.topics = topics
        self.packet_id = packet_id
        self._init_states()

    def _init_states(self):
        self.machine = Machine(model=self, states=UnSubscription.states, initial="new")
        self.machine.add_transition(trigger="unsubscribe", source="new", dest="unsubscribed")
        self.machine.add_transition(trigger="acknowledge", source="unsubscribed", dest="acknowledged")
开发者ID:gitter-badger,项目名称:hbmqtt,代码行数:14,代码来源:protocol.py

示例10: test_get_transitions

# 需要导入模块: from transitions import Machine [as 别名]
# 或者: from transitions.Machine import add_transition [as 别名]
 def test_get_transitions(self):
     states = ['A', 'B', 'C', 'D']
     m = Machine('self', states, initial='a', auto_transitions=False)
     m.add_transition('go', ['A', 'B', 'C'], 'D')
     m.add_transition('run', 'A', 'D')
     self.assertEqual(
         {(t.source, t.dest) for t in m.get_transitions('go')},
         {('A', 'D'), ('B', 'D'), ('C', 'D')})
     self.assertEqual(
         [(t.source, t.dest)
          for t in m.get_transitions(source='A', dest='D')],
         [('A', 'D'), ('A', 'D')])
开发者ID:tyarkoni,项目名称:transitions,代码行数:14,代码来源:test_core.py

示例11: test_send_event_data_conditions

# 需要导入模块: from transitions import Machine [as 别名]
# 或者: from transitions.Machine import add_transition [as 别名]
 def test_send_event_data_conditions(self):
     states = ["A", "B", "C", "D"]
     s = Stuff()
     # First pass positional and keyword args directly to the condition
     m = Machine(model=s, states=states, initial="A", send_event=False)
     m.add_transition(trigger="advance", source="A", dest="B", conditions="this_fails_by_default")
     s.advance(boolean=True)
     self.assertEquals(s.state, "B")
     # Now wrap arguments in an EventData instance
     m.send_event = True
     m.add_transition(trigger="advance", source="B", dest="C", conditions="extract_boolean")
     s.advance(boolean=False)
     self.assertEquals(s.state, "B")
开发者ID:khigia,项目名称:transitions,代码行数:15,代码来源:test_core.py

示例12: Mincer

# 需要导入模块: from transitions import Machine [as 别名]
# 或者: from transitions.Machine import add_transition [as 别名]
class Mincer(object):

    def __init__(self, valid_states, valid_transitions, initial):
        self.processings = 0
        self.machine = Machine(model=self, states=valid_states, initial=initial)
        for i in valid_transitions:
			self.machine.add_transition(trigger=i['trigger'], \
				source=i['source'], dest=i['dest'], after=i['after'])

    def increase_processings(self):
        self.processings += 1

    def noop(self):
        pass
开发者ID:BartGo,项目名称:transition-drafts,代码行数:16,代码来源:main.py

示例13: Matter

# 需要导入模块: from transitions import Machine [as 别名]
# 或者: from transitions.Machine import add_transition [as 别名]
class Matter(object):
    def __init__(self): 
        self.machine = Machine(self, ['solid', 'liquid'], send_event=True, initial='solid')
        self.machine.add_transition('melt', 'solid', 'liquid', prepare='set_environment', conditions='is_greater_than_melt')
        self.temp = 0
        self.pressure = 101.325
    def set_environment(self, event):
        self.temp = event.kwargs.get('temp')
        self.pressure = event.kwargs.get('pressure')
    def print_temperature(self): print("Current temperature is %d degrees celsius." % self.temp)
    def print_pressure(self): print("Current pressure is %.2f kPa." % self.pressure)
    def is_greater_than_melt(self,event): 
        print (self.temp)
        return (self.temp > 100)
开发者ID:DuaneNielsen,项目名称:poloniex,代码行数:16,代码来源:fsm_params.py

示例14: test_send_event_data_conditions

# 需要导入模块: from transitions import Machine [as 别名]
# 或者: from transitions.Machine import add_transition [as 别名]
 def test_send_event_data_conditions(self):
     states = ['A', 'B', 'C', 'D']
     s = Stuff()
     # First pass positional and keyword args directly to the condition
     m = Machine(model=s, states=states, initial='A', send_event=False)
     m.add_transition(
         trigger='advance', source='A', dest='B', conditions='this_fails_by_default')
     s.advance(boolean=True)
     self.assertEquals(s.state, 'B')
     # Now wrap arguments in an EventData instance
     m.send_event = True
     m.add_transition(
         trigger='advance', source='B', dest='C', conditions='extract_boolean')
     s.advance(boolean=False)
     self.assertEquals(s.state, 'B')
开发者ID:acjc,项目名称:transitions,代码行数:17,代码来源:test_transitions.py

示例15: test_send_event_data

# 需要导入模块: from transitions import Machine [as 别名]
# 或者: from transitions.Machine import add_transition [as 别名]
 def test_send_event_data(self):
     states = ['A', 'B', 'C', 'D']
     s = Stuff()
     # First pass positional and keyword args directly to the callback
     m = Machine(model=s, states=states, initial='A', send_event=False)
     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.'))
     # 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:barseghyanartur,项目名称:transitions,代码行数:17,代码来源:test_transitions.py


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