From 00e836cfa4e2652d724972585d661143142af002 Mon Sep 17 00:00:00 2001 From: eugue Date: Mon, 24 Jan 2011 14:41:38 -0500 Subject: merge with biginstall --- behaviors/Accelerate.xml | 2 +- behaviors/AddPixelEvent.py | 26 ++++++++++++++++++++++++-- behaviors/BehaviorChain.py | 9 +++++++-- behaviors/ColorChangerBehavior.py | 2 +- behaviors/DebugBehavior.py | 2 +- behaviors/EchoBehavior.py | 2 +- behaviors/ModifyParam.py | 8 +++----- behaviors/MoveBehavior.py | 29 +++++++++++++++++++++++++++++ behaviors/PixelDecay.xml | 4 ++-- behaviors/RandomWalk.py | 14 ++++++++++++++ behaviors/RecursiveDecay.py | 3 ++- behaviors/RedShift.xml | 9 +++++++++ behaviors/ResponseMover.py | 15 +++++++++++++++ behaviors/RestrictLocation.py | 36 ++++++++++++++++++++++++++++++++++++ behaviors/RunningBehavior.py | 1 + behaviors/Square.py | 11 +++++++++++ 16 files changed, 157 insertions(+), 16 deletions(-) create mode 100644 behaviors/MoveBehavior.py create mode 100644 behaviors/RedShift.xml create mode 100644 behaviors/ResponseMover.py create mode 100644 behaviors/RestrictLocation.py create mode 100644 behaviors/Square.py (limited to 'behaviors') diff --git a/behaviors/Accelerate.xml b/behaviors/Accelerate.xml index c78195b..f9de077 100644 --- a/behaviors/Accelerate.xml +++ b/behaviors/Accelerate.xml @@ -3,6 +3,6 @@ Sensor StepSize - {val}*1.01 + {val}*1.1 diff --git a/behaviors/AddPixelEvent.py b/behaviors/AddPixelEvent.py index bf3cfff..7f134e1 100644 --- a/behaviors/AddPixelEvent.py +++ b/behaviors/AddPixelEvent.py @@ -1,4 +1,26 @@ from operationscore.Behavior import * +import util.Strings as Strings +from logger import main_log class AddPixelEvent(Behavior): - def initBehavior(self): - className = self['Class'] + def behaviorInit(self): + [module, className] = self['Class'].split('.') + try: + exec('from ' + module+'.'+className + ' import *', globals()) + except Exception as inst: + main_log.error('Error importing ' + module+'.'+className+ '. Component not\ + initialized.') + main_log.error(str(inst)) + self.eventGenerator = eval('lambda args:'+className+'(args)') + + #^lambda function to do generate new event (takes args) + + def processResponse(self, sensors, recurses): + ret = [] + for sensory in sensors: + outDict = {} + outDict[Strings.LOCATION] = sensory[Strings.LOCATION] + settingsDict = dict(self.argDict) + settingsDict['Color'] = sensory['Color'] + outDict['PixelEvent'] = self.eventGenerator(settingsDict) + ret.append(outDict) + return (ret, recurses) diff --git a/behaviors/BehaviorChain.py b/behaviors/BehaviorChain.py index 0170fa8..39f4402 100644 --- a/behaviors/BehaviorChain.py +++ b/behaviors/BehaviorChain.py @@ -29,6 +29,11 @@ class BehaviorChain(Behavior): if hookRecurrence != []: main_log.warn('Hook recurrences are not currently supported. Implement it\ yourself or bug russell') - self.feedback[behaviorId] = recurrence - return response + self.feedback[behaviorId] = recurrence + return (response, []) + + def appendBehavior(behavior): + bid = compReg.registerComponent(behavior) #register behavior (will make + #a new id if there isn't one) + self['ChainedBehaviors'].append(bid) diff --git a/behaviors/ColorChangerBehavior.py b/behaviors/ColorChangerBehavior.py index e1827eb..2a8d974 100644 --- a/behaviors/ColorChangerBehavior.py +++ b/behaviors/ColorChangerBehavior.py @@ -11,4 +11,4 @@ class ColorChangerBehavior(Behavior): else: newDict['Color'] = color.randomColor() ret.append(newDict) - return (ret, recursiveInputs) + return (ret, []) diff --git a/behaviors/DebugBehavior.py b/behaviors/DebugBehavior.py index 9bf3ea8..34f4106 100644 --- a/behaviors/DebugBehavior.py +++ b/behaviors/DebugBehavior.py @@ -5,4 +5,4 @@ class DebugBehavior(Behavior): def processResponse(self, sensorInputs, recursiveInputs): if sensorInputs != []: main_log.debug('Sensor Inputs: ' + str(sensorInputs)) - return [] + return ([], []) diff --git a/behaviors/EchoBehavior.py b/behaviors/EchoBehavior.py index be0ed14..589c42b 100644 --- a/behaviors/EchoBehavior.py +++ b/behaviors/EchoBehavior.py @@ -9,4 +9,4 @@ class EchoBehavior(Behavior): outDict[Strings.LOCATION] = sensory[Strings.LOCATION] outDict['Color'] = (255,0,0) ret.append(outDict) - return ret + return (ret, []) diff --git a/behaviors/ModifyParam.py b/behaviors/ModifyParam.py index 3701013..f589e05 100644 --- a/behaviors/ModifyParam.py +++ b/behaviors/ModifyParam.py @@ -5,7 +5,7 @@ class ModifyParam(Behavior): def processResponse(self, sensorInputs, recursiveInputs): paramType = self['ParamType'] paramName = self['ParamName'] - paramOp = self['ParamOp'] + paramOp = str(self['ParamOp']) if paramType == 'Sensor': searchSet = sensorInputs elif paramType == 'Recurse': @@ -13,12 +13,10 @@ class ModifyParam(Behavior): else: raise Exception('Unknown Param Type') for behaviorInput in searchSet: - if paramName in behaviorInput: - try: + if paramName in behaviorInput: #TODO: copy -> modify instead of just + #copying paramOp = paramOp.replace('{val}', 'behaviorInput[paramName]') #convert the {val} marker to something we can execute behaviorInput[paramName] = eval(paramOp) - except: - raise Exception('Bad operation. Use things like \'{val}*5\', \'{val}+5\', exp({val}) etc.') if paramType == 'Sensor': #return accordingly return (searchSet, recursiveInputs) if paramType == 'Recurse': diff --git a/behaviors/MoveBehavior.py b/behaviors/MoveBehavior.py new file mode 100644 index 0000000..9ae98b9 --- /dev/null +++ b/behaviors/MoveBehavior.py @@ -0,0 +1,29 @@ +from operationscore.Behavior import * +import util.ComponentRegistry as compReg +import util.Geo as Geo +import util.Strings as Strings + +class MoveBehavior(Behavior): + def processResponse(self, sensorInputs, recursiveInputs): + print 'processing' + print sensorInputs + if recursiveInputs: + currRecLocs = recursiveInputs + else: + currRecLocs = [{'Location' : (5, 5)), 'Color' : [255, 255, 255]}] + + if sensorInputs: # if input exists, change location + ret = [] + for currRecLoc in currRecLocs: + currDict = dict(currRecLoc) + for sensorInput in sensorInputs: + currDict['Location'][0] += sensorInput['x'] * self['XStep'] + currDict['Location'][1] += sensorInput['y'] * self['YStep'] + #currDict['Color'] = sensorInput['color'] + ret.append(currDict) + #print ret + return (ret, ret) + + else: # if not, return current recursive location. + #print currRecLocs + return (currRecLocs, currRecLocs) diff --git a/behaviors/PixelDecay.xml b/behaviors/PixelDecay.xml index f9eee0d..c19a1a8 100644 --- a/behaviors/PixelDecay.xml +++ b/behaviors/PixelDecay.xml @@ -1,9 +1,9 @@ - behaviors.DecayBehavior + behaviors.AddPixelEvent + pixelevents.DecayEvent Exponential .01 0 - False diff --git a/behaviors/RandomWalk.py b/behaviors/RandomWalk.py index 8254430..fd6c2c8 100644 --- a/behaviors/RandomWalk.py +++ b/behaviors/RandomWalk.py @@ -1,5 +1,19 @@ from operationscore.Behavior import * import util.ComponentRegistry as compReg +import util.Geo as Geo +import util.Strings as Strings +import random +import pdb class RandomWalk(Behavior): def processResponse(self, sensors, recursives): + ret = [] + s = self['StepSize'] + for sensory in sensors: + step = [random.randint(-s,s), random.randint(-s,s)] + outdict = dict(sensory) + outdict[Strings.LOCATION] = Geo.addLocations(step, outdict[Strings.LOCATION]) + ret.append(outdict) + return (ret,recursives) + + diff --git a/behaviors/RecursiveDecay.py b/behaviors/RecursiveDecay.py index ee38bc4..218813d 100644 --- a/behaviors/RecursiveDecay.py +++ b/behaviors/RecursiveDecay.py @@ -1,4 +1,5 @@ from operationscore.Behavior import * +import pdb class RecursiveDecay(Behavior): def processResponse(self, sensorInputs, recursiveInputs): ret = [] @@ -9,4 +10,4 @@ class RecursiveDecay(Behavior): response['ResponsesLeft'] -= 1 if response['ResponsesLeft'] > 0: ret.append(response) - return (ret, recursiveInputs) #no direct ouput + return (ret, []) #no direct ouput diff --git a/behaviors/RedShift.xml b/behaviors/RedShift.xml new file mode 100644 index 0000000..a99e2ba --- /dev/null +++ b/behaviors/RedShift.xml @@ -0,0 +1,9 @@ + + behaviors.RestrictLocation + + (255,0,255) + Color + {x}>100 + + + diff --git a/behaviors/ResponseMover.py b/behaviors/ResponseMover.py new file mode 100644 index 0000000..e1faccb --- /dev/null +++ b/behaviors/ResponseMover.py @@ -0,0 +1,15 @@ +import pdb +from operationscore.Behavior import * +import util.ComponentRegistry as compReg +#ResponseMover is a scaffold for behaviors that spawn 'walkers' which act autonomously on input. +#Add a recursive hook to control the movement. +class ResponseMover(Behavior): + def processResponse(self, sensorInputs, recursiveInputs): + newResponses = sensorInputs + ret = [] + for recurInput in recursiveInputs: + outDict = dict(recurInput) + ret.append(outDict) + ret += newResponses + return (ret, ret) + diff --git a/behaviors/RestrictLocation.py b/behaviors/RestrictLocation.py new file mode 100644 index 0000000..febc9ed --- /dev/null +++ b/behaviors/RestrictLocation.py @@ -0,0 +1,36 @@ +from operationscore.Behavior import * +import util.ComponentRegistry as compReg +from behaviors.ModifyParam import * +import util.Geo as Geo +import util.Strings as Strings +import random +import pdb +class RestrictLocation(Behavior): + def behaviorInit(self): + action = self['Action'] + modifyParamArgs = {'ParamType': 'Sensor', + 'ParamName':self['ParamName'],'ParamOp':self['Action']} + self.locBounds = self['LocationRestriction'] + self.paramModifier = ModifyParam(modifyParamArgs) + if isinstance(self.locBounds, str): + self.locBounds = self.locBounds.replace('{x}', 'l[0]') + self.locBounds = self.locBounds.replace('{y}', 'l[1]') + self.locEval = eval('lambda l:'+self.locBounds) + elif isinstance(self.locBounds, tuple): + if len(self.locBounds) != 4: + raise Exception('Must be in form (xmin,yin,xmax,ymax)') + else: + self.locEval = lambda l:Geo.pointWithinBoundingBox(l,\ + self.LocBounds) + def processResponse(self, sensorInputs, recursiveInputs): + ret = [] + for data in sensorInputs: + if not self.locEval(data['Location']): + (dataOut, recur) = self.paramModifier.immediateProcessInput([data], []) + #behaviors expect lists ^[] + ret += dataOut + else: + ret.append(data) + return (ret, []) + + diff --git a/behaviors/RunningBehavior.py b/behaviors/RunningBehavior.py index 92db69b..5eb33f7 100644 --- a/behaviors/RunningBehavior.py +++ b/behaviors/RunningBehavior.py @@ -15,6 +15,7 @@ class RunningBehavior(Behavior): outDict['StepSize'] = self['StepSize'] outDict['Location']= Geo.addLocations(outDict['Location'], (outDict['StepSize']*outDict['Dir'],0)) + if not Geo.pointWithinBoundingBox(outDict['Location'], \ compReg.getComponent('Screen').getSize()): outDict['Dir'] *= -1 diff --git a/behaviors/Square.py b/behaviors/Square.py new file mode 100644 index 0000000..a6e9401 --- /dev/null +++ b/behaviors/Square.py @@ -0,0 +1,11 @@ +from operationscore.Behavior import * +class Square(Behavior): + def processResponse(self, sensorInputs, recursiveInputs): + for sensory in sensorInputs:#TODO: consider replicating the dict + xLoc = sensory['Location'][0] + yLoc = sensory['Location'][1] + width = self['Width'] + sensory['Location'] =\ + '{x}<'+str(xLoc+width)+',{x}>'+str(xLoc-width)+\ + ',{y}<'+str(yLoc+width)+',{y}>'+str(yLoc-width) + return (sensorInputs, recursiveInputs) -- cgit v1.2.3