def test_state_machine_init_method(): """Basic testing for the State Machine init method """ # Setup state_machine = StateMachine() # Setup def decision(): return 'state2' def decision2(): return 'state3' state1 = State(name="state1", routine_function=fun_routine, decision_function=decision, entry_function=fun_entry, exit_function=fun_exit, is_async=False) state2 = State(name="state2", routine_function=fun_routine, decision_function=decision2, entry_function=fun_entry, exit_function=fun_exit, is_async=False) state3 = State(name="state3", routine_function=fun_routine, decision_function=decision, is_async=False) state_machine.add_states(state3, state2, state1) state_machine.initial_state = "state1"
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')
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)
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
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
def test_state_machine_complete(): """Testing the complete state_machine application """ class MyClass: def __init__(self): self.state1_entry_variable = False self.state1_exit_variable = False self.state1_routine_variable = False self.state2_entry_variable = False self.state2_exit_variable = False self.state2_routine_variable = False self.state3_entry_variable = False self.state3_exit_variable = False self.state3_routine_variable = False def func_exit_state1(self): self.state1_exit_variable = True def func_entry_state1(self): self.state1_entry_variable = True def func_routine_state1(self): self.state1_routine_variable = True def func_decision_state1(self): return 'state2' def func_exit_state2(self): self.state2_exit_variable = True def func_entry_state2(self): self.state2_entry_variable = True def func_routine_state2(self): self.state2_routine_variable = True def func_decision_state2(self): return 'state3' def func_exit_state3(self): self.state3_exit_variable = True def func_entry_state3(self): self.state3_entry_variable = True def func_routine_state3(self): self.state3_routine_variable = True def func_decision_state3(self): return 'state1' my_class = MyClass() state1 = State(name="state1", routine_function=my_class.func_routine_state1, decision_function=my_class.func_decision_state1, entry_function=my_class.func_entry_state1, exit_function=my_class.func_exit_state1, is_async=False) state2 = State(name="state2", routine_function=my_class.func_routine_state2, decision_function=my_class.func_decision_state2, entry_function=my_class.func_entry_state2, exit_function=my_class.func_exit_state2, is_async=False) state3 = State(name="state3", routine_function=my_class.func_routine_state3, decision_function=my_class.func_decision_state3, entry_function=my_class.func_entry_state3, exit_function=my_class.func_exit_state3, is_async=False) state_machine = StateMachine() state_machine.add_states(state1, state2, state3) state_machine.initial_state = 'state1' # Execute for i in range(3): state_machine.run() # Verify if all my_class attribute are true for key in my_class.__dict__: # Verify if all attributes were setted if getattr(my_class, key) != True: assert True assert True