Package SmootLight :: Package behaviors :: Module BehaviorChain
[hide private]
[frames] | no frames]

Source Code for Module SmootLight.behaviors.BehaviorChain

 1  from operationscore.Behavior import * 
 2  import util.ComponentRegistry as compReg 
 3  from logger import main_log 
 4  import pdb 
5 -class BehaviorChain(Behavior):
6 """BehaviorChain is a class which chains together multiple behavior. BehaviorChain is in itself a 7 behavior, and behaves and can be used accordingly. BehaviorChain also supports recursive hooks to 8 be set on its constituent behaviors. ChainedBehaviors should be specified in <Args> as follows: 9 10 <ChainedBehaviors> 11 <Id>behavior1Id</Id> 12 <Id>behavior2Id</Id> 13 </ChainedBehaviors> 14 15 Behaviors may also be appended programmatically via the appendBehavior method. 16 17 Recursive hooks should be specified with Python dict syntax as follows: 18 19 <RecursiveHooks>{'behavior1Id':'hookid'}</RecursiveHooks> 20 21 Behavior Chain manages all recurrences that its constituents propogate. At this point, it does not 22 support recurrences in its hooks.""" 23
24 - def behaviorInit(self):
25 self.feedback = {} #dictionary to allow feedback of recursives 26 self.hooks = self['RecursiveHooks'] 27 if self.hooks == None: 28 self.hooks = {}
29
30 - def processResponse(self, sensorInputs, recursiveInputs):
31 response = sensorInputs 32 for behaviorId in self['ChainedBehaviors']: 33 behavior = compReg.getComponent(behaviorId) 34 if behaviorId in self.feedback: 35 recurrence = self.feedback[behaviorId] 36 else: 37 recurrence = [] 38 (response,recurrence) = behavior.immediateProcessInput(response,\ 39 recurrence) 40 41 if behaviorId in self.hooks: #process recursive hook if there is one 42 hookBehavior = compReg.getComponent(self.hooks[behaviorId]) 43 #we feed its recurrence in as input to the behavior. 44 (recurrence, hookRecurrence) = \ 45 hookBehavior.immediateProcessInput(recurrence, \ 46 []) 47 if hookRecurrence != []: 48 main_log.warn('Hook recurrences are not currently supported.') 49 self.feedback[behaviorId] = recurrence 50 return (response, [])
51
52 - def appendBehavior(behavior):
53 bid = compReg.registerComponent(behavior) #register behavior (will make 54 #a new id if there isn't one) 55 self['ChainedBehaviors'].append(bid)
56