def __init__(self): """Set up the states""" self.tcp_established = State( name='TCPEstablished', enter_action=lambda: print('Entered TCPEstablished state'), transitions=[ Transition(event=Event.open, target=lambda: self.tcp_listen), ]) self.tcp_listen = State( name='TCPListen', enter_action=lambda event: print('Entered TCPListen state'), transitions=[ Transition(event=Event.acknowledge, action=self.send_ack, target=lambda: self.tcp_listen), Transition(event=Event.close, target=lambda: self.tcp_closed), ]) self.tcp_closed = State( name='TCPClosed', enter_action=lambda event: print('Entered TCPClosed state'), ) states = [ self.tcp_established, self.tcp_listen, self.tcp_closed ] super().__init__(states)
def test_is_triggered_true_with_trigger(self): current_state = MagicMock() source = MagicMock() target = MagicMock() event = Event('test_event') t = Transition(source, target, trigger='test_event') r = t.is_triggered(event) self.assertTrue( r )
def test_is_triggered_true_with_trigger(self): current_state = MagicMock() source = MagicMock() target = MagicMock() event = Event('test_event') t = Transition(source, target, trigger='test_event') r = t.is_triggered(event) self.assertTrue(r)
def test_is_triggered_false_with_guard(self): current_state = MagicMock() source = MagicMock() target = MagicMock() event = MagicMock() guard = lambda event, current_state:False t = Transition(source, target, guard=guard) r = t.is_triggered(event) self.assertFalse( r )
def test_is_triggered_false_with_guard(self): current_state = MagicMock() source = MagicMock() target = MagicMock() event = MagicMock() guard = lambda event, current_state: False t = Transition(source, target, guard=guard) r = t.is_triggered(event) self.assertFalse(r)
def test__init__(self): source = MagicMock() target = MagicMock() trigger = MagicMock() guard = MagicMock() action = MagicMock() t = Transition(source, target, trigger, guard, action) self.assertEqual(source, t.source) self.assertEqual(target, t.target) self.assertEqual(trigger, t.trigger) self.assertEqual(guard, t.guard) self.assertEqual(action, t.action)
def __init__(self): """Set up the vending state machine""" c = VendingMachine.cent # Unicode US cent sign self.change = 0 self.start_state = State( name='start', enter_action=lambda: self.display('\nTo dispense, enter 25' + c), transitions=[ Transition(event=Coin.nickel, target=lambda: self.got_5), Transition(event=Coin.dime, target=lambda: self.got_10), Transition(event=Coin.quarter, target=lambda: self.dispensing) ]) self.got_5 = State( name='got_5', enter_action=lambda event: self.display('You entered: 5' + c), transitions=[ Transition(event=Coin.nickel, target=lambda: self.got_10), Transition(event=Coin.dime, target=lambda: self.got_15), Transition(event=Coin.quarter, target=lambda: self.dispensing, action=lambda _: self.set_change(5)) ]) self.got_10 = State( name='got_10', enter_action=lambda event: self.display('You entered: 10' + c), transitions=[ Transition(event=Coin.nickel, target=lambda: self.got_15), Transition(event=Coin.dime, target=lambda: self.got_20), Transition(event=Coin.quarter, target=lambda: self.dispensing, action=lambda _: self.set_change(10)) ]) self.got_15 = State( name='got_15', enter_action=lambda event: self.display('You entered: 15' + c), transitions=[ Transition(event=Coin.nickel, target=lambda: self.got_20), Transition(event=Coin.dime, target=lambda: self.dispensing), Transition(event=Coin.quarter, target=lambda: self.dispensing, action=lambda _: self.set_change(15)) ]) self.got_20 = State( name='got_20', enter_action=lambda event: self.display('You entered: 20' + c), transitions=[ Transition(event=Coin.nickel, target=lambda: self.dispensing), Transition(event=Coin.dime, target=lambda: self.dispensing, action=lambda _: self.set_change(5)), Transition(event=Coin.quarter, target=lambda: self.dispensing, action=lambda _: self.set_change(20)) ]) self.dispensing = State( name='dispensing', enter_action=lambda event: self.dispense(event), transitions=None) super().__init__(states=[ self.start_state, self.got_5, self.got_10, self.got_15, self.got_20, self.dispensing ])
def init_states(self): # TOGGLE_LANDING and Flying and not in landing zone: -> Landing self.FLYING.add_transition( event=enums.Events.TOGGLE_LANDING, transition=Transition( target_state=self.LANDING, guards=[lambda boid: not boid.in_landing_zone])) # When start landing, cannot be below landing line self.LANDING.enter_actions.append(not_below_landing_zone) # TOGGLE_LANDING and flying and in landing zone: -> LANDED self.FLYING.add_transition( event=enums.Events.TOGGLE_LANDING, transition=Transition(target_state=self.LANDED, guards=[lambda boid: boid.in_landing_zone])) # TOGGLE_LANDING and Landing: -> Flying self.LANDING.add_transition( event=enums.Events.TOGGLE_LANDING, transition=Transition(target_state=self.FLYING)) # TOGGLE_LANDING and Landed: -> Flying self.LANDED.add_transition( event=enums.Events.TOGGLE_LANDING, transition=Transition(target_state=self.FLYING)) # SHOUT, Flying and leader is landing or landed self.FLYING.add_transition( event=enums.Events.SHOUT, transition=Transition(target_state=self.LANDING, guards=[ lambda boid: boid.game.leader.state in [self.LANDING, self.LANDED], ])) # LANDING_ZONE_ENTERED and Landing: -> Landed self.LANDING.add_transition( event=enums.Events.LANDING_ZONE_ENTERED, transition=Transition( target_state=self.LANDED, guards=[lambda boid: not boid.game.time_keeper.is_sunset])) # When just landed, go to top of landing zone self.LANDED.enter_actions.append(to_top_of_landing_zone) # Landed and sunset starts: -> Sleeping self.LANDED.add_transition( event=enums.Events.START_OF_SUNSET, transition=Transition(target_state=self.SLEEPING)) # Landing, sunset started, and landing zone entered: -> Sleeping self.LANDING.add_transition( event=enums.Events.LANDING_ZONE_ENTERED, transition=Transition( target_state=self.SLEEPING, guards=[lambda boid: boid.game.time_keeper.is_sunset])) # Sleeping and sunrise starts: -> Flying self.SLEEPING.add_transition(event=enums.Events.START_OF_SUNRISE, transition=Transition( target_state=self.FLYING, action=on_start_of_sunrise)) # Landing or flying when night false -> Dying self.FLYING.add_transition( event=enums.Events.START_OF_NIGHTTIME, transition=Transition(target_state=self.DYING)) self.LANDING.add_transition( event=enums.Events.START_OF_NIGHTTIME, transition=Transition(target_state=self.DYING)) self.DYING.enter_actions.append(start_death_clock) transition = Transition(target_state=self.HUNGRY) for state in [self.FLYING, self.LANDED, self.LANDING]: state.add_transition(event=enums.Events.GOT_HUNGRY, transition=transition) # TODO: Make sure this list is complete - probably better way of doing this transition = Transition(target_state=self.DYING) for state in [ self.FLYING, self.LANDING, self.LANDING, self.SLEEPING, self.HUNGRY, ]: state.add_transition(event=enums.Events.STARVING, transition=transition) # Start feeding, from hungry self.HUNGRY.add_transition( event=enums.Events.START_FEEDING, transition=Transition(target_state=self.FEEDING)) # Done feeding, replete, back to flying self.FEEDING.add_transition( event=enums.Events.REPLETE, transition=Transition(target_state=self.FLYING))