diff options
author | rcoh <rcoh@mit.edu> | 2010-12-08 16:39:50 -0500 |
---|---|---|
committer | rcoh <rcoh@mit.edu> | 2010-12-08 16:39:50 -0500 |
commit | b335b746523ffd59db1402b097a802b3fd99eaac (patch) | |
tree | 74333be1820f3d2666358c3b009beb14bf929256 /behaviors | |
parent | 353ab16db64c86122c0fcb9e1852b85c14b354b8 (diff) |
Code for the demo. Everything works afaik. Contains a couple more optimizations. Contains modify param behavior. Improved support for recursive hooks. Modifications to SmootCoreObject to get us closer to a fully multi-threaded system. This should be the last commit directly to master. All further commits should be on subranches and get merged.
Diffstat (limited to 'behaviors')
-rw-r--r-- | behaviors/BehaviorChain.py | 11 | ||||
-rw-r--r-- | behaviors/ColorChangerBehavior.py | 5 | ||||
-rw-r--r-- | behaviors/ModifyParam.py | 27 | ||||
-rw-r--r-- | behaviors/RecursiveDecay.py | 4 | ||||
-rw-r--r-- | behaviors/RunningBehavior.py | 6 |
5 files changed, 43 insertions, 10 deletions
diff --git a/behaviors/BehaviorChain.py b/behaviors/BehaviorChain.py index 8b5cb1d..fe50573 100644 --- a/behaviors/BehaviorChain.py +++ b/behaviors/BehaviorChain.py @@ -20,8 +20,11 @@ class BehaviorChain(Behavior): if behaviorId in self.hooks: #process recursive hook if there is one hookBehavior = Util.getComponentById(self.hooks[behaviorId]) - (response, recurrence) = \ - hookBehavior.immediateProcessInput(response, - recurrence) - self.feedback[behaviorId] = recurrence +#we feed its recurrence in as input to the behavior. + (recurrence, hookRecurrence) = \ + hookBehavior.immediateProcessInput(recurrence, \ + []) + if hookRecurrence != []: + print 'Hook recurrences are not currently supported. Implement it yourself or bug russell' + self.feedback[behaviorId] = recurrence return response diff --git a/behaviors/ColorChangerBehavior.py b/behaviors/ColorChangerBehavior.py index 12ef6f4..a3b1739 100644 --- a/behaviors/ColorChangerBehavior.py +++ b/behaviors/ColorChangerBehavior.py @@ -7,8 +7,9 @@ class ColorChangerBehavior(Behavior): for sensory in sensorInputs: newDict = dict(sensory) #don't run into shallow copy issues if self['ColorList'] != None: - newDict['Color'] = Util.chooseRandomColor(self['ColorList']) + newDict['Color'] = Util.chooseRandomColor(self['ColorList']) #TODO: this doesn't work. else: newDict['Color'] = Util.randomColor() +#newDict['Color'] = (255,0,0) ret.append(newDict) - return ret + return (ret, recursiveInputs) diff --git a/behaviors/ModifyParam.py b/behaviors/ModifyParam.py new file mode 100644 index 0000000..38b8cd5 --- /dev/null +++ b/behaviors/ModifyParam.py @@ -0,0 +1,27 @@ +from operationscore.Behavior import * +import Util +import pdb +#Class to perform a given operation on some element of an argDict. Designed to be used a recursive hook, but can serve sensor-based functions as well. Specify ParamType (Sensor or Recurse), ParamName, and ParamOp, (a valid python statement with the old value represented as {val}) +class ModifyParam(Behavior): + def processResponse(self, sensorInputs, recursiveInputs): + paramType = self['ParamType'] + paramName = self['ParamName'] + paramOp = self['ParamOp'] + if paramType == 'Sensor': + searchSet = sensorInputs + elif paramType == 'Recurse': + searchSet = recursiveInputs + else: + raise Exception('Unknown Param Type') + for behaviorInput in searchSet: + if paramName in behaviorInput: + try: + 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': + return (sensorInputs, searchSet) + diff --git a/behaviors/RecursiveDecay.py b/behaviors/RecursiveDecay.py index b119b85..ee38bc4 100644 --- a/behaviors/RecursiveDecay.py +++ b/behaviors/RecursiveDecay.py @@ -2,11 +2,11 @@ from operationscore.Behavior import * class RecursiveDecay(Behavior): def processResponse(self, sensorInputs, recursiveInputs): ret = [] - for response in recursiveInputs: + for response in sensorInputs: if not 'ResponsesLeft' in response: response['ResponsesLeft'] = self['InitialResponseCount'] else: response['ResponsesLeft'] -= 1 if response['ResponsesLeft'] > 0: ret.append(response) - return (sensorInputs, ret) #no direct ouput + return (ret, recursiveInputs) #no direct ouput diff --git a/behaviors/RunningBehavior.py b/behaviors/RunningBehavior.py index 255ce69..3d82d29 100644 --- a/behaviors/RunningBehavior.py +++ b/behaviors/RunningBehavior.py @@ -10,11 +10,13 @@ class RunningBehavior(Behavior): outDict = dict(recurInput) if not 'Dir' in outDict: outDict['Dir'] = 1 #to the right + if not 'StepSize' in outDict: + outDict['StepSize'] = self['StepSize'] outDict['Location']= Util.addLocations(outDict['Location'], - (self['StepSize']*outDict['Dir'],0)) + (outDict['StepSize']*outDict['Dir'],0)) if not Util.pointWithinBoundingBox(outDict['Location'], \ Util.getScreen().getSize()): - outDict['Dir'] *= -1 + outDict['Dir'] *= -1 ret.append(outDict) ret += newResponses return (ret, ret) |