Exemplo n.º 1
0
    def test_notify_child(self):
        m = StateMachine()

        composite_state = m.create_state('composite', CompositeState)
        target_state = m.create_state('target')
        composite_state.add_transition_to(target_state, 'tick')

        substate_source = composite_state.create_state('substate1')
        substate_target = composite_state.create_state('substate2')
        substate_source.add_transition_to(substate_target, 'tick')

        substate_source.on_end = MagicMock()
        substate_target.on_start = MagicMock()

        composite_state.initial_state = substate_source
        composite_state.initialise()

        m.initial_state = composite_state
        m.initialise()

        # Test, expecting the composite state's machine to transition
        e = Event('tick')
        m.notify(e)

        # Verify
        self.assertEqual(composite_state, m.current_state)
        self.assertEqual(substate_target, composite_state.current_state)
        self.assertEqual(1, substate_source.on_end.call_count)
        self.assertEqual(1, substate_target.on_start.call_count)
Exemplo n.º 2
0
    def setUp(self):

        chatbot = StateMachine('chatbot')
        active_state = chatbot.create_state('active', CompositeState)
        sleeping_state = chatbot.create_state('sleeping', State)

        chatbot.initial_state = active_state

        active_state.add_transition_to(sleeping_state, 'sunset')
        sleeping_state.add_transition_to(active_state, 'sunrise')

        happy_state = active_state.create_state('happy')
        sad_state = active_state.create_state('sad')

        happy_state.add_transition_to(sad_state, 'criticism')
        sad_state.add_transition_to(happy_state, 'praise')

        active_state.initial_state = happy_state
        active_state.initialise()

        chatbot.initialise()

        self.chatbot = chatbot
        self.active_state = active_state
        self.sleeping_state = sleeping_state
        self.happy_state = happy_state
        self.sad_state = sad_state

        self.sunrise = Event('sunrise')
        self.sunset = Event('sunset')

        self.criticism = Event('criticism')
        self.praise = Event('praise')
Exemplo n.º 3
0
    def setUp(self):

        chatbot = StateMachine('chatbot')
        active_state = chatbot.create_state('active', CompositeState)
        sleeping_state = chatbot.create_state('sleeping', State)
        final_state = chatbot.create_state('final', FinalState)

        chatbot.initial_state=active_state
        active_state.add_transition_to(sleeping_state, 'sunset')
        active_state.add_transition_to(final_state, 'bandwidth exceeded')

        sleeping_state.add_transition_to(active_state, 'sunrise')
        sleeping_state.add_transition_to(final_state, 'power off')

        def become_happy( event, state ):
            state.logger.info('Becoming Happy')

        def happy_message(event, state):
            if event.name=='msg':
                state.logger.info('Hello %s, I am happy', event.payload['from'])

        def become_sad(event, state):
            state.logger.info('Becoming Sad')

        def sad_message(event, state):
            if event.name=='msg':
                state.logger.info('Hello %s, I am sad', event.payload['from'])

        def sun_is_shining(event, state):
            return True

        def clouds_are_looming(event, state):
            return True

        def do_happy_dance(event, state):
            return True

        def hang_head_in_shame(event, state):
            return True

        happy_state = active_state.create_state('happy')
        happy_state.on_start = become_happy
        happy_state.on_run   = happy_message

        sad_state = active_state.create_state('sad')
        sad_state.on_start = become_sad
        sad_state.on_run = sad_message

        final_state = active_state.create_state( 'depressed', FinalState )

        happy_state.add_transition_to(sad_state, 'criticism', guard=clouds_are_looming, action=hang_head_in_shame)
        sad_state.add_transition_to(happy_state, 'praise', guard=sun_is_shining, action=do_happy_dance)
        sad_state.add_transition_to(final_state, 'excessive criticism')

        active_state.initial_state=happy_state
        active_state.initialise()

        self.chatbot = chatbot
Exemplo n.º 4
0
    def test_add_transition_to(self):
        m   = StateMachine()
        ps  = m.create_state('ps', PseudoState)
        tgt1 = m.create_state('tgt1')

        g1 = lambda event, state: True
        ps.add_transition_to(tgt1, guard=g1)

        self.assertEqual(1, len(ps.transitions))
        self.assertEqual(tgt1, ps.transitions[0].target)
Exemplo n.º 5
0
    def test_add_transition_to(self):
        m = StateMachine()
        ps = m.create_state('ps', PseudoState)
        tgt1 = m.create_state('tgt1')

        g1 = lambda event, state: True
        ps.add_transition_to(tgt1, guard=g1)

        self.assertEqual(1, len(ps.transitions))
        self.assertEqual(tgt1, ps.transitions[0].target)
Exemplo n.º 6
0
    def test_to_str(self):

        m = StateMachine()
        s = m.create_state(name='test state')

        s2 = m.create_state(name='s2')
        s.add_transition_to(s2, 'trigger')

        r = s.transitions[0].to_str()

        self.assertEqual('trigger', r)
Exemplo n.º 7
0
    def test_to_str(self):

        m = StateMachine()
        s = m.create_state(name='test state')

        s2 = m.create_state(name='s2')
        s.add_transition_to( s2, 'trigger' )

        r = s.transitions[0].to_str()

        self.assertEqual('trigger', r)
Exemplo n.º 8
0
    def test_transitions(self):

        m = StateMachine()
        s = m.create_state(name='test state')

        s2 = m.create_state(name='s2')
        s3 = m.create_state(name='s3')

        s.add_transition_to(s2, 'ignore')
        s.add_transition_to(s3, 'trigger')

        transitions = s.transitions

        self.assertIsInstance(transitions, list)
        self.assertEqual(2, len(transitions))
Exemplo n.º 9
0
    def test_transition_to_default(self):

        m = StateMachine()
        s = m.create_state(name='test state')

        s2 = m.create_state(name='should get here')
        s3 = m.create_state(name='should not get here')

        s.add_transition_to(s3, 'test event')
        s.add_transition_to(s2)

        event = Event('any event')
        t = s.next_transition(event)

        self.assertEqual(s2, t.target)
Exemplo n.º 10
0
    def test_next_transition(self):

        m = StateMachine()
        s = m.create_state(name='test state')

        s2 = m.create_state(name='s2')
        s3 = m.create_state(name='s3')

        s.add_transition_to(s2, 'ignore')
        s.add_transition_to(s3, 'trigger')

        event = Event('trigger')
        result = s.next_transition(event)

        self.assertEqual(s3, result.target)
Exemplo n.º 11
0
    def test_transition_to_default(self):

        m = StateMachine()
        s = m.create_state(name='test state')

        s2 = m.create_state(name='should get here')
        s3 = m.create_state(name='should not get here')

        s.add_transition_to(s3, 'test event')
        s.add_transition_to(s2)

        event  = Event('any event')
        t      = s.next_transition(event)

        self.assertEqual(s2, t.target)
Exemplo n.º 12
0
    def test_next_transition(self):

        m = StateMachine()
        s = m.create_state(name='test state')

        s2 = m.create_state(name='s2')
        s3 = m.create_state(name='s3')

        s.add_transition_to( s2, 'ignore' )
        s.add_transition_to( s3, 'trigger' )

        event  = Event('trigger')
        result = s.next_transition( event )

        self.assertEqual(s3, result.target )
Exemplo n.º 13
0
    def test_transitions(self):

        m = StateMachine()
        s = m.create_state(name='test state')

        s2 = m.create_state(name='s2')
        s3 = m.create_state(name='s3')

        s.add_transition_to( s2, 'ignore' )
        s.add_transition_to( s3, 'trigger' )

        transitions = s.transitions

        self.assertIsInstance( transitions, list )
        self.assertEqual(2, len( transitions ))
Exemplo n.º 14
0
    def test_next_transition(self):

        m   = StateMachine()
        ps  = m.create_state('ps', PseudoState)
        tgt1 = m.create_state('tgt1')
        tgt2 = m.create_state('tgt2')

        g1 = lambda event, state: True
        ps.add_transition_to(tgt1, guard=g1)

        g2 = lambda event, state: False
        ps.add_transition_to(tgt2, guard=g2)

        e = Event()
        t = ps.next_transition(e)
        self.assertEqual(tgt1, t.target)
Exemplo n.º 15
0
 def test_is_child_final_false(self):
     m=StateMachine()
     s=m.create_state('final', CompositeState)
     c=s.create_state('s')
     s.set_state(c)
     e=Event()
     self.assertFalse( guards.is_child_final(e, s))
Exemplo n.º 16
0
 def test_is_child_final_true(self):
     m = StateMachine()
     s = m.create_state('final', CompositeState)
     f = s.create_state('sfinal', FinalState)
     s.set_state(f)
     e = Event()
     self.assertTrue(guards.is_child_final(e, s))
Exemplo n.º 17
0
 def test_is_child_final_false(self):
     m = StateMachine()
     s = m.create_state('final', CompositeState)
     c = s.create_state('s')
     s.set_state(c)
     e = Event()
     self.assertFalse(guards.is_child_final(e, s))
Exemplo n.º 18
0
 def test_is_child_final_true(self):
     m=StateMachine()
     s=m.create_state('final', CompositeState)
     f=s.create_state('sfinal', FinalState)
     s.set_state(f)
     e=Event()
     self.assertTrue( guards.is_child_final(e, s))
Exemplo n.º 19
0
    def test_next_transition(self):

        m = StateMachine()
        ps = m.create_state('ps', PseudoState)
        tgt1 = m.create_state('tgt1')
        tgt2 = m.create_state('tgt2')

        g1 = lambda event, state: True
        ps.add_transition_to(tgt1, guard=g1)

        g2 = lambda event, state: False
        ps.add_transition_to(tgt2, guard=g2)

        e = Event()
        t = ps.next_transition(e)
        self.assertEqual(tgt1, t.target)
Exemplo n.º 20
0
    def test_to_str2(self):
        def guard_fn(event, state):
            return True

        def action_fn(event, state):
            pass

        m = StateMachine()
        s = m.create_state(name='test state')

        s2 = m.create_state(name='s2')
        s.add_transition_to(s2, 'trigger', guard=guard_fn, action=action_fn)

        r = s.transitions[0].to_str()

        self.assertEqual('trigger[guard_fn]/action_fn', r)
Exemplo n.º 21
0
    def test_add_transition_to(self):
        m  = StateMachine()
        s1 = m.create_state(name='s1')
        s2 = m.create_state(name='s2')

        s1.add_transition_to(s2, 'test event...')

        self.assertEqual( s2, s1.transitions_by_trigger['test event...'][0].target )
Exemplo n.º 22
0
    def test_is_final_state_using_subclass(self):
        class AnotherFinalState(FinalState):
            pass

        m = StateMachine()
        s = m.create_state('final', AnotherFinalState)
        e = Event()
        self.assertTrue(guards.is_final_state(e, s))
Exemplo n.º 23
0
    def test_to_str2(self):

        def guard_fn(event, state):
            return True

        def action_fn(event, state):
            pass

        m = StateMachine()
        s = m.create_state(name='test state')

        s2 = m.create_state(name='s2')
        s.add_transition_to( s2, 'trigger', guard=guard_fn, action=action_fn )

        r = s.transitions[0].to_str()

        self.assertEqual('trigger[guard_fn]/action_fn', r)
Exemplo n.º 24
0
    def test_add_transition_to(self):
        m = StateMachine()
        s1 = m.create_state(name='s1')
        s2 = m.create_state(name='s2')

        s1.add_transition_to(s2, 'test event...')

        self.assertEqual(s2,
                         s1.transitions_by_trigger['test event...'][0].target)
Exemplo n.º 25
0
    def test_start_stores_event(self):

        m = StateMachine()
        s = m.create_state(name='test state')

        e = Event()
        s.start(e)

        self.assertEqual(e, s.start_event)
Exemplo n.º 26
0
    def test_start_stores_event(self):

        m = StateMachine()
        s = m.create_state(name='test state')

        e = Event()
        s.start(e)

        self.assertEqual(e, s.start_event)
Exemplo n.º 27
0
    def test_is_final_state_using_subclass(self):

        class AnotherFinalState(FinalState):
            pass

        m = StateMachine()
        s = m.create_state('final', AnotherFinalState)
        e = Event()
        self.assertTrue( guards.is_final_state(e, s) )
Exemplo n.º 28
0
    def test_add_transition_to_without_params(self):
        m = StateMachine()
        m.logger.info(m.states)
        s1 = m.create_state(name='s1')
        s2 = m.create_state(name='s2')

        s1.add_transition_to(s2)

        self.assertEqual(0, len(s1.transitions_by_trigger))
        self.assertEqual(s2, s1.default_transition.target)
Exemplo n.º 29
0
    def test_add_transition_to_without_params(self):
        m  = StateMachine()
        m.logger.info(m.states)
        s1 = m.create_state(name='s1')
        s2 = m.create_state(name='s2')

        s1.add_transition_to(s2)

        self.assertEqual( 0, len( s1.transitions_by_trigger ) )
        self.assertEqual( s2, s1.default_transition.target )
Exemplo n.º 30
0
    def test_notify_observers(self):
        """
        Test that a substate still notifies observers
        """
        # SETUP
        m = StateMachine('Observed')
        composite_state = m.create_state('composite', CompositeState)
        sub_state = composite_state.create_state('substate')

        listener = StateMachine('Listener')
        listener.notify = MagicMock()

        m.register_observer('tick', listener)

        # TEST
        e = Event('tick')
        sub_state.notify_observers(e)
        m.flush()

        self.assertEqual(1, listener.notify.call_count)
        listener.notify.assert_called_with(e)
Exemplo n.º 31
0
    def setUp(self):

        chatbot = StateMachine('chatbot')
        active_state = chatbot.create_state('active', CompositeState)
        sleeping_state = chatbot.create_state('sleeping', State)
        final_state = chatbot.create_state('final', FinalState)

        chatbot.initial_state = active_state
        active_state.add_transition_to(sleeping_state, 'sunset')
        active_state.add_transition_to(final_state, 'bandwidth exceeded')

        sleeping_state.add_transition_to(active_state, 'sunrise')
        sleeping_state.add_transition_to(final_state, 'power off')

        def become_happy(event, state):
            state.logger.info('Becoming Happy')

        def happy_message(event, state):
            if event.name == 'msg':
                state.logger.info('Hello %s, I am happy',
                                  event.payload['from'])

        def become_sad(event, state):
            state.logger.info('Becoming Sad')

        def sad_message(event, state):
            if event.name == 'msg':
                state.logger.info('Hello %s, I am sad', event.payload['from'])

        def sun_is_shining(event, state):
            return True

        def clouds_are_looming(event, state):
            return True

        def do_happy_dance(event, state):
            return True

        def hang_head_in_shame(event, state):
            return True

        happy_state = active_state.create_state('happy')
        happy_state.on_start = become_happy
        happy_state.on_run = happy_message

        sad_state = active_state.create_state('sad')
        sad_state.on_start = become_sad
        sad_state.on_run = sad_message

        final_state = active_state.create_state('depressed', FinalState)

        happy_state.add_transition_to(sad_state,
                                      'criticism',
                                      guard=clouds_are_looming,
                                      action=hang_head_in_shame)
        sad_state.add_transition_to(happy_state,
                                    'praise',
                                    guard=sun_is_shining,
                                    action=do_happy_dance)
        sad_state.add_transition_to(final_state, 'excessive criticism')

        active_state.initial_state = happy_state
        active_state.initialise()

        self.chatbot = chatbot
Exemplo n.º 32
0
 def test_is_final_state(self):
     m=StateMachine()
     s=m.create_state('final', FinalState)
     e=Event()
     self.assertTrue( guards.is_final_state(e, s))
Exemplo n.º 33
0
 def test_is_final_state(self):
     m = StateMachine()
     s = m.create_state('final', FinalState)
     e = Event()
     self.assertTrue(guards.is_final_state(e, s))