aboutsummaryrefslogtreecommitdiff
path: root/behaviors
diff options
context:
space:
mode:
authorGravatar Russell Cohen <rcoh@mit.edu>2011-01-20 14:57:43 -0500
committerGravatar Russell Cohen <rcoh@mit.edu>2011-01-20 14:57:43 -0500
commitf45b5e262c394cf00ef88f7fca1eab1b4de0fec9 (patch)
tree0f513d55c859773e27f1f615f3160ef1f66124ce /behaviors
parent0af38d1882291523d0d290a638e88be793f27678 (diff)
Rewrite of Behavior parent class to suck less. Lots of bug fixes. Added
'RestrictLocation' which allows events to fire based on their location.
Diffstat (limited to 'behaviors')
-rw-r--r--behaviors/BehaviorChain.py4
-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/RandomWalk.py1
-rw-r--r--behaviors/RecursiveDecay.py3
-rw-r--r--behaviors/RedShift.xml9
-rw-r--r--behaviors/RestrictLocation.py28
9 files changed, 46 insertions, 13 deletions
diff --git a/behaviors/BehaviorChain.py b/behaviors/BehaviorChain.py
index e15bd23..39f4402 100644
--- a/behaviors/BehaviorChain.py
+++ b/behaviors/BehaviorChain.py
@@ -29,8 +29,8 @@ 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
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/RandomWalk.py b/behaviors/RandomWalk.py
index dfe6716..fd6c2c8 100644
--- a/behaviors/RandomWalk.py
+++ b/behaviors/RandomWalk.py
@@ -3,6 +3,7 @@ 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 = []
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/RestrictLocation.py b/behaviors/RestrictLocation.py
index 649500f..febc9ed 100644
--- a/behaviors/RestrictLocation.py
+++ b/behaviors/RestrictLocation.py
@@ -1,12 +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']}
-
- def processInput(
+ 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, [])
+
+