Example #1
0
 def _get_state_handlers(self):
     return {
         'init': State(),
         'initialising': MovingState(),
         'idle': State(),
         'car_moving': MovingState(),
         'sample_dropped': SampleDroppedState(),
     }
Example #2
0
    def _get_state_handlers(self):
        def do_eat():
            self.energy = min(100, self.energy + 25)
            self.clean = max(0, self.clean - 5)
            self.tired = min(100, self.tired + 5)

        def do_clean():
            self.clean = min(100, self.clean + 45)
            self.tired = min(100, self.tired + 10)
            self.bored = min(100, self.bored + 5)

        def do_sleep():
            self.tired = min(100, self.tired - 75)
            self.energy = max(0, self.energy - 5)
            self.clean = max(0, self.clean - 5)

        def do_play():
            self.bored = max(0, self.bored - 30)
            self.energy = max(0, self.energy - 25)
            self.tired = min(100, self.tired + 15)
            self.clean = max(0, self.clean - 15)

        return {
            'unnamed': State(),
            'happy': DecayState(bored=0.1,
                                tired=0.05,
                                energy=-0.05,
                                clean=-0.1),
            'unhappy': DecayState(bored=0.05,
                                  tired=0.1,
                                  energy=-0.1,
                                  clean=-0.1),
            'action_received': State(),
            'action_completed': State(),
            'eating': ActionState(action=do_eat),
            'cleaning': ActionState(action=do_clean),
            'sleeping': ActionState(action=do_sleep),
            'playing': ActionState(action=do_play),
            'dying': DyingState(bored=0.01,
                                tired=0.01,
                                energy=-0.01,
                                clean=-0.01),
            'dead': State()
        }
Example #3
0
    def test_State_receives_Context(self):
        state = State()
        context = object()
        StateMachine({
            'initial': 'foo',
            'states': {
                'foo': state
            }
        },
                     context=context)

        self.assertEqual(state._context, context)
Example #4
0
    def test_can_specify_state_handlers_as_State(self, *_):
        foo = State()
        bar = State()
        sm = StateMachine({
            'initial': 'foo',
            'states': {
                'foo': foo,
                'bar': bar,
            },
            'transitions': {
                ('foo', 'bar'): lambda: True
            }
        })

        # First cycle enters and executes initial state, but forces delta T to zero
        sm.process(1.0)
        foo.on_entry.assert_called_once_with(0)
        foo.in_state.assert_called_once_with(0)

        foo.on_entry.reset_mock()
        foo.in_state.reset_mock()

        # Second cycle transitions due to lambda: True above
        sm.process(2.0)
        foo.on_exit.assert_called_once_with(2.0)
        bar.on_entry.assert_called_once_with(2.0)
        bar.in_state.assert_called_once_with(2.0)

        foo.on_exit.reset_mock()
        bar.on_entry.reset_mock()
        bar.in_state.reset_mock()

        # Third cycle only does an in_state in bar
        sm.process(3.0)
        bar.in_state.assert_called_once_with(3.0)
        bar.on_entry.assert_not_called()
        bar.on_exit.assert_not_called()
Example #5
0
 def _get_state_handlers(self):
     return {'idle': State(), 'moving': DefaultMovingState()}
Example #6
0
 def _get_state_handlers(self):
     return {
         'idle': State(),
     }
Example #7
0
 def _get_state_handlers(self):
     return {"idle": State(), "moving": DefaultMovingState()}
    def test_State_receives_Context(self):
        state = State()
        context = object()
        StateMachine({"initial": "foo", "states": {"foo": state}}, context=context)

        self.assertEqual(state._context, context)