Exemplo n.º 1
0
def runMachine(start):
    eventHub = core.EventHub()
    qeventHub = core.QueuedEventHub(eventHub)

    subsystems = core.SubsystemList()
    subsystems.append(qeventHub)
    machine = state.Machine(deps=subsystems)

    sequence = raw_input('Enter sequence: ')

    machine.start(start)
    assert machine.currentState() is not None, 'State machine not started'
    for x in sequence:
        eventType = globals()[x.upper()]
        qeventHub.publish(eventType, core.Event())
        qeventHub.publishEvents()

        assert machine.currentState(
        ) is not None, 'State machine ended prematurely'

    # State machine finished, send finish event
    qeventHub.publish(FINISH, core.Event())
    qeventHub.publishEvents()

    assert machine.currentState() is None, 'State machine has not ended'
    machine.stop()
Exemplo n.º 2
0
    def testConfig(self):
        cfg = { 
            'param' : 5,
            'States' : {
                'ram.test.ai.state.StateTestConfig' : {
                    'val' : 10,
                    'other' : 'job'
                    },
                'ram.test.ai.state.ConfigState' : {
                    'one' : 20,
                    'three' : 'apples'
                    }
                }
            }
        machine = state.Machine(cfg = cfg)
        machine.start(StateTestConfig)
        current = machine.currentState()
        self.assertEqual(10, current.getConfig('val'))
        self.assertEqual('job', current.getConfig('other'))

        # Test default config value loading
        machine.stop()
        self.assertRaises(KeyError, machine.start, ConfigState)

        cfg = { 
            'param' : 5,
            'States' : {
                'ram.test.ai.state.StateTestConfig' : {
                    'val' : 10,
                    'other' : 'job'
                    },
                'ram.test.ai.state.ConfigState' : {
                    'one' : 20,
                    'three' : 'apples',
                    'four' : None
                    }
                }
            }
        machine.stop()
        machine = state.Machine(cfg = cfg)
        machine.start(ConfigState)
        current = machine.currentState()
        self.assertEqual(current._one, 20)
        self.assertEqual(current._two, 10)
        self.assertEqual(current._three, 'apples')
        self.assertEqual(current._four, None)
Exemplo n.º 3
0
 def testSubsystemPassing(self):
     eventHub = core.EventHub("EventHub")
     qeventHub = core.QueuedEventHub(eventHub, "QueuedEventHub")
     machine = state.Machine(deps = [eventHub, qeventHub])
     
     machine.start(Start)
     startState = machine.currentState()
     
     # Check for subsystems
     self.assertEquals(eventHub, startState.eventHub)
     self.assertEquals(qeventHub, startState.queuedEventHub)
Exemplo n.º 4
0
    def testStop(self):
        # No States
        machine = state.Machine()
        machine.stop()

        # Normal
        startState = self.machine.currentState()

        # Stop the machine and make sure events have no effect
        self.machine.stop()

        self.assertRaises(Exception, self.machine.injectEvent,
                          self._makeEvent("Start", value = 1))
        cstate = self.machine.currentState()

        self.assertNotEquals(End, type(cstate))
        self.assertNotEquals(startState, cstate)
Exemplo n.º 5
0
    def testPublish(self):
        self.called = False
        self.sender = None
        self.type = None

        def reciever(event):
            self.called = True
            self.type = event.type
            self.sender = event.sender

        machine = state.Machine()
        machine.start(Start)

        machine.subscribe("TestEvent", reciever)
        machine.currentState().publish("TestEvent", core.Event())

        self.assert_(self.called)
        self.assertEqual("TestEvent", self.type)
        self.assertEqual(machine, self.sender)
Exemplo n.º 6
0
    def setUp(self, extraDeps=None, cfg=None):
        # Handle case where the timer is still mocked for some reason, we make
        # sure to un mock it
        if AITestCase.__timerMocked:
            self.mockTimer()

        # Replace the Timer class with the Mock one
        self.mockTimer()

        if extraDeps is None:
            extraDeps = []

        if cfg is None:
            cfg = {}

        self.eventHub = core.EventHub()
        self.qeventHub = core.QueuedEventHub(self.eventHub)
        self.timerManager = timer.TimerManager(deps=[self.eventHub])
        self.estimator = MockEstimator(cfg=cfg.get('StateEstimator', {}))
        self.controller = MockController(eventHub=self.eventHub,
                                         estimator=self.estimator)
        self.vehicle = MockVehicle(cfg=cfg.get('Vehicle', {}))
        self.visionSystem = MockVisionSystem()

        aCfg = cfg.get('Ai', {})
        self.ai = AI(aCfg)

        deps = [
            self.controller, self.timerManager, self.eventHub, self.qeventHub,
            self.vehicle, self.visionSystem, self.ai, self.estimator
        ]

        mCfg = cfg.get('MotionManager', {})
        self.motionManager = motion.basic.MotionManager(mCfg, deps)
        deps.append(self.motionManager)

        deps.extend(extraDeps)
        sCfg = cfg.get('StateMachine', {})
        self.machine = state.Machine(cfg=sCfg, deps=deps)
Exemplo n.º 7
0
 def testEventHub(self):
     eventHub = core.EventHub()
     qeventHub = core.QueuedEventHub(eventHub)
     mockEventSource = MockEventSource(eventHub)
     
     machine = state.Machine(deps = [eventHub, qeventHub])
     machine.start(Start)
     
     # Send an event, and make sure it does get through until the queue
     # releases it
     mockEventSource.sendEvent(MockEventSource.ANOTHER_EVT, value = 20)
     startState = machine.currentState()
     self.assertEquals(Start, type(startState))
     
     # Release events and make sure we have transition properly
     qeventHub.publishEvents()
     self.assertEquals(QueueTestState, type(machine.currentState()))
     self.assert_(startState.anotherEvtEvent)
     self.assertEquals(20, startState.anotherEvtEvent.value)
     
     # Fire off another event and make sure we haven't gone anywhere
     mockEventSource.sendEvent(MockEventSource.THING_UPDATED, value = 34)
     qeventHub.publishEvents()
     self.assertEquals(QueueTestState, type(machine.currentState()))
Exemplo n.º 8
0
 def testAiSet(self):
     ai = aisys.AI()
     machine = state.Machine(deps = [ai])
     self.assertEquals(machine, ai.mainStateMachine)
Exemplo n.º 9
0
 def setUp(self):
     self.machine = state.Machine()
     self.machine.start(Start)