aboutsummaryrefslogtreecommitdiff
path: root/behaviors
diff options
context:
space:
mode:
authorGravatar eugue <eug.sun@gmail.com>2011-01-24 14:41:38 -0500
committerGravatar eugue <eug.sun@gmail.com>2011-01-24 14:41:38 -0500
commit00e836cfa4e2652d724972585d661143142af002 (patch)
treef612ea43024f13dc8c16f192735676e9976630fc /behaviors
parentd39f871cf9fcce13c6db315052a4951f692719f9 (diff)
merge with biginstall
Diffstat (limited to 'behaviors')
-rw-r--r--behaviors/Accelerate.xml2
-rw-r--r--behaviors/AddPixelEvent.py26
-rw-r--r--behaviors/BehaviorChain.py9
-rw-r--r--behaviors/ColorChangerBehavior.py2
-rw-r--r--behaviors/DebugBehavior.py2
-rw-r--r--behaviors/EchoBehavior.py2
-rw-r--r--behaviors/ModifyParam.py8
-rw-r--r--behaviors/MoveBehavior.py29
-rw-r--r--behaviors/PixelDecay.xml4
-rw-r--r--behaviors/RandomWalk.py14
-rw-r--r--behaviors/RecursiveDecay.py3
-rw-r--r--behaviors/RedShift.xml9
-rw-r--r--behaviors/ResponseMover.py15
-rw-r--r--behaviors/RestrictLocation.py36
-rw-r--r--behaviors/RunningBehavior.py1
-rw-r--r--behaviors/Square.py11
16 files changed, 157 insertions, 16 deletions
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 @@
<Args>
<ParamType>Sensor</ParamType>
<ParamName>StepSize</ParamName>
- <ParamOp>{val}*1.01</ParamOp>
+ <ParamOp>{val}*1.1</ParamOp>
</Args>
</Behavior>
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 @@
<Behavior>
- <Class>behaviors.DecayBehavior</Class>
+ <Class>behaviors.AddPixelEvent</Class>
<Args>
+ <Class>pixelevents.DecayEvent</Class>
<DecayType>Exponential</DecayType>
<Coefficient>.01</Coefficient>
<z-index>0</z-index>
- <RenderToScreen>False</RenderToScreen>
</Args>
</Behavior>
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 @@
+<Behavior>
+ <Class>behaviors.RestrictLocation</Class>
+ <Args>
+ <Action>(255,0,255)</Action>
+ <ParamName>Color</ParamName>
+ <LocationRestriction>{x}>100</LocationRestriction>
+ </Args>
+</Behavior>
+
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)