diff options
Diffstat (limited to 'util')
-rw-r--r-- | util/BehaviorQuerySystem.py | 39 | ||||
-rw-r--r-- | util/ColorOps.py | 4 | ||||
-rw-r--r-- | util/ComponentRegistry.py | 25 | ||||
-rw-r--r-- | util/Config.py | 15 | ||||
-rw-r--r-- | util/Geo.py | 12 |
5 files changed, 83 insertions, 12 deletions
diff --git a/util/BehaviorQuerySystem.py b/util/BehaviorQuerySystem.py new file mode 100644 index 0000000..643b95c --- /dev/null +++ b/util/BehaviorQuerySystem.py @@ -0,0 +1,39 @@ +import types +"""The behavior query system is a module that allows querying behaviors based on lambda-function +predicates.""" +def initBQS(): + global behaviorList, initialized + behaviorList = [] + initialized = True + +def addBehavior(behavior): + behaviorList.append(behavior) + +def query(predicateList): + """BehaviorQuerySystem.query takes a list of predicates (functions with signature: + (behavior,output)), and + optionally a behavior to be compared to.""" + #want to do queries wrt: behavior itself, the behavior packet, the querying behavior + if isinstance(predicateList, types.FunctionType): + predicateList = [predicateList] + elif not isinstance(predicateList, list): + raise Exception('Predicate list must be a function or list of functions') + global behaviorList, initialized + ret = [] + if not initialized: + initBQS() + + for behavior in behaviorList: #Consider every behavior + lastOutput = behavior.getLastOutput() + for output in lastOutput: #Look at every element it has output + validOutput = True + for pred in predicateList: #Evaluate every predicate. A predicate is a lambda function that + #takes a dict and returns a bool. + if not pred(output): + validOutput = False + break + if validOutput: + ret.append(output) + return ret + + diff --git a/util/ColorOps.py b/util/ColorOps.py index 4b1162a..796a902 100644 --- a/util/ColorOps.py +++ b/util/ColorOps.py @@ -40,3 +40,7 @@ def randomBrightColor(): hue, sat, val = colorsys.hsv_to_rgb(hue, sat, val) ret = [hue, sat, val] return floatToIntColor(ret) + +class Color(object): + def __init__(self, r,g,b): + self.rep = [r,g,b] diff --git a/util/ComponentRegistry.py b/util/ComponentRegistry.py index 776cd17..be913df 100644 --- a/util/ComponentRegistry.py +++ b/util/ComponentRegistry.py @@ -11,16 +11,19 @@ def clearRegistry(): initRegistry() def removeComponent(cid): - globals()['Registry'].pop(cid) + global Registry + Registry.pop(cid) def getComponent(cid): - return globals()['Registry'][cid] + global Registry + return Registry[cid] #Registry of all components of the light system #TODO: pick a graceful failure behavior and implement it def registerComponent(component, cid=None): + global Registry if cid != None: - globals()['Registry'][cid] = component + Registry[cid] = component else: try: cid = component['Id'] @@ -28,22 +31,26 @@ def registerComponent(component, cid=None): cid = getNewId() component['Id'] = cid main_log.debug(cid + 'automatically assigned') - globals()['Registry'][cid] = component + Registry[cid] = component return cid def verifyUniqueId(cid): - return not cid in globals()['Registry'] + global Registry + return not cid in Registry def removeComponent(cid): - globals()['Registry'].pop(cid) + global Registry + Registry.pop(cid) def getComponent(cid): - return globals()['Registry'][cid] + global Registry + return Registry[cid] def getNewId(): - trialKey = len(globals()['Registry']) + global Registry + trialKey = len(Registry) trialId = hashlib.md5(str(trialKey)).hexdigest() - while trialId in globals()['Registry']: + while trialId in Registry: trialKey += 1 trialId = hashlib.md5(str(trialKey)).hexdigest() return trialId diff --git a/util/Config.py b/util/Config.py index 6fdb0d4..25018a8 100644 --- a/util/Config.py +++ b/util/Config.py @@ -1,4 +1,5 @@ from xml.etree.ElementTree import * +import re import sys import xml import pdb @@ -112,8 +113,20 @@ def pullArgsFromItem(parentNode): return args def attemptEval(val): + """Runs an eval if possible, or converts into a lambda expression if indicated. Otherwise, + leaves as a string.""" try: - val = eval(val) + if '${' in val and '}$' in val: #TODO: this could be a little cleaner + dictVal = re.sub("'\$\{(.+?)\}\$'", "b['\\1']", val) #replace expressions '${blah}$' with b['blah'] + dictVal = re.sub("\$\{(.+?)\}\$", "a['\\1']", dictVal) #replace all expressions like {blah} with a['blah'] + if "'${" and "}$'" in val: #nested lambda madness + lambdaVal = eval('lambda a: lambda b: ' + dictVal) + else: + lambdaVal = eval('lambda a:'+dictVal) #TODO: nested lambdas + return lambdaVal #convert referential objects to lambda expressions which can be + #resolved dynamically. + else: + val = eval(val) except (NameError, SyntaxError): val = str(val) return val diff --git a/util/Geo.py b/util/Geo.py index 0dde80b..211e89d 100644 --- a/util/Geo.py +++ b/util/Geo.py @@ -2,8 +2,9 @@ import math from bisect import * import random -def pointWithinBoundingBox(point, bb): #this could be in 4 lines, but I'm lazy. - return sum([(point[i % 2] <= bb[i]) == (i>1) for i in range(4)]) == 4 +def pointWithinBoundingBox(point, bb): + """Returns whether or not a point (x,y) is within a bounding box (xmin, ymin, xmax, ymax)""" + return all([(point[i % 2] <= bb[i]) == (i>1) for i in range(4)]) def addLocations(l1,l2): return tuple([l1[i]+l2[i] for i in range(len(l1))]) @@ -32,3 +33,10 @@ def windtrail(x,y,height,center,width): b=center c=width return a*((math.exp(-((x-b))/(c)))**2)*(math.exp(-((y))/(0.2*c)))**2 + +class Location(object): + def __init__(self,x=0,y=0): + self.x = x + self.y = y + def __add__(self, b): + return Location(self.x+b.x, self.y+b.y) |