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()
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)
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)
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)
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)
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)
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()))
def testAiSet(self): ai = aisys.AI() machine = state.Machine(deps = [ai]) self.assertEquals(machine, ai.mainStateMachine)
def setUp(self): self.machine = state.Machine() self.machine.start(Start)