From 7386cbc7ce48e3996d92d84cff3d1a4dab8f538d Mon Sep 17 00:00:00 2001 From: rcoh Date: Mon, 20 Dec 2010 14:19:09 -0500 Subject: A little bit more util stuff. Migrated the component registry to the ComponentRegistry module. Almost done. --- LightInstallation.py | 16 +++++++++++----- Util.py | 39 --------------------------------------- behaviors/BehaviorChain.py | 5 +++-- behaviors/RunningBehavior.py | 3 ++- config/Outdoor.xml | 2 +- util/ComponentRegistry.py | 17 +++++++++++++++++ 6 files changed, 34 insertions(+), 48 deletions(-) create mode 100644 util/ComponentRegistry.py diff --git a/LightInstallation.py b/LightInstallation.py index 5f12abf..e6e5a26 100644 --- a/LightInstallation.py +++ b/LightInstallation.py @@ -5,6 +5,7 @@ import pdb, sys, time, Util, thread from pygame.locals import * import util.TimeOps as clock import util.Config as configGetter +import util.ComponentRegistry as compReg #Python class to instantiate and drive a Screen through different patterns, #and effects. class LightInstallation: @@ -21,9 +22,10 @@ class LightInstallation: #input #give Util a pointer to our componentRegistry and screen so that everyone can use #it - Util.setComponentDict(self.componentDict) + #Util.setComponentDict(self.componentDict) self.screen = Screen() - Util.setScreen(self.screen) + #Util.setScreen(self.screen) + compReg.registerComponent(self.screen, 'Screen') #TODO: move to constants file config = configGetter.loadConfigFile(configFileName) #read configs from xml rendererConfig = config.find('RendererConfiguration') @@ -70,7 +72,8 @@ class LightInstallation: cid = component['Id'] if cid == None: raise Exception('Components must have Ids!') - self.componentDict[cid] = component + #self.componentDict[cid] = component + compReg.registerComponent(component) def initializeComponent(self, config): components = [] if config != None: @@ -134,8 +137,11 @@ class LightInstallation: def processResponse(self,inputDict, responseDict): inputId = inputDict['Id'] boundBehaviorIds = self.inputBehaviorRegistry[inputId] - [self.componentDict[b].addInput(responseDict) for b in boundBehaviorIds] - + #TODO: fix this, it crashes because inputs get run before beahviors exist + try: + [compReg.getComponent(b).addInput(responseDict) for b in boundBehaviorIds] + except: + print 'Behaviors not initialized yet. WAIT!' def main(argv): print argv if len(argv) == 1: diff --git a/Util.py b/Util.py index 2973e33..8aa4d8a 100644 --- a/Util.py +++ b/Util.py @@ -20,17 +20,6 @@ def pointWithinBoundingBox(point, bb): #this could be in 4 lines, but I'm lazy. print pointWithinBoundingBox((118,21), (10,8,298,42)) def addLocations(l1,l2): return tuple([l1[i]+l2[i] for i in range(len(l1))]) -def setScreen(screen): - globals()["screen"] = screen -def getScreen(): - return screen -def setComponentDict(componentDictRef): - globals()["componentDict"] = componentDictRef -def getComponentById(cid): - if cid in componentDict: - return componentDict[cid] - else: - return None def addPixelEventIfMissing(responseDict): if not 'PixelEvent' in responseDict: if 'Color' in responseDict: @@ -52,34 +41,6 @@ def find_le(a, x): def find_ge(a, x): 'Find leftmost value greater than x' return bisect_left(a, x) -#Given a dictionary of connections, returns their topological ordering -- (the -#order in which they can be visited such that all parents have been visited -#before their children. Returns the order or None if no such ordering exists -#(the graph contains a cycle). -def topologicalSort(adjacencyDict): - def dfsVisit(vertex): - gray[vertex] = 1 - for child in adjacencyDict[vertex]: - if not child in visited: - if child in gray: #We have a cycle. No topological ordering - #exists! - raise Exception('Cycle!') - dfsVisit(child) - orderedList.insert(0, vertex) - visited[vertex] = 1 - orderedList = [] - visited = {} - gray = {} - for vertex in adjacencyDict: - try: - if not vertex in visited: - dfsVisit(vertex) - except: - return None #cycle - return orderedList -def topoTest(): - adj = {'a':['d','c'], 'b':['c'], 'c':['e'], 'd':['e'], 'e':[]} - print topologicalOrdering(adj) def testXMLParse(fileName): #pdb.set_trace() config = ElementTree() diff --git a/behaviors/BehaviorChain.py b/behaviors/BehaviorChain.py index fe50573..65f5c9d 100644 --- a/behaviors/BehaviorChain.py +++ b/behaviors/BehaviorChain.py @@ -1,4 +1,5 @@ from operationscore.Behavior import * +import util.ComponentRegistry as compReg import Util import pdb class BehaviorChain(Behavior): @@ -10,7 +11,7 @@ class BehaviorChain(Behavior): def processResponse(self, sensorInputs, recursiveInputs): response = sensorInputs for behaviorId in self['ChainedBehaviors']: - behavior = Util.getComponentById(behaviorId) + behavior = compReg.getComponent(behaviorId) if behaviorId in self.feedback: recurrence = self.feedback[behaviorId] else: @@ -19,7 +20,7 @@ class BehaviorChain(Behavior): recurrence) if behaviorId in self.hooks: #process recursive hook if there is one - hookBehavior = Util.getComponentById(self.hooks[behaviorId]) + hookBehavior = compReg.getComponent(self.hooks[behaviorId]) #we feed its recurrence in as input to the behavior. (recurrence, hookRecurrence) = \ hookBehavior.immediateProcessInput(recurrence, \ diff --git a/behaviors/RunningBehavior.py b/behaviors/RunningBehavior.py index 3d82d29..4f51898 100644 --- a/behaviors/RunningBehavior.py +++ b/behaviors/RunningBehavior.py @@ -1,4 +1,5 @@ from operationscore.Behavior import * +import util.ComponentRegistry as compReg import pdb import Util class RunningBehavior(Behavior): @@ -15,7 +16,7 @@ class RunningBehavior(Behavior): outDict['Location']= Util.addLocations(outDict['Location'], (outDict['StepSize']*outDict['Dir'],0)) if not Util.pointWithinBoundingBox(outDict['Location'], \ - Util.getScreen().getSize()): + compReg.getComponent('Screen').getSize()): outDict['Dir'] *= -1 ret.append(outDict) ret += newResponses diff --git a/config/Outdoor.xml b/config/Outdoor.xml index 7f054f0..10274b2 100644 --- a/config/Outdoor.xml +++ b/config/Outdoor.xml @@ -120,7 +120,7 @@ inputs.PygameInput followmouse - 10 + 50 True diff --git a/util/ComponentRegistry.py b/util/ComponentRegistry.py new file mode 100644 index 0000000..f8fe00d --- /dev/null +++ b/util/ComponentRegistry.py @@ -0,0 +1,17 @@ +import pdb +#Registry of all components of the light system +#TODO: pick a graceful failure behavior and implement it +registry = {} +def registerComponent(component, cid=None): + if cid != None: + registry[cid] = component + else: + try: + cid = component['Id'] + registry[cid] = component + except: + raise Exception('Must specify Id, component did not store it') +def removeComponent(cid): + registry.pop(cid) +def getComponent(cid): + return registry[cid] -- cgit v1.2.3