Package SmootLight :: Package operationscore :: Module Behavior
[hide private]
[frames] | no frames]

Source Code for Module SmootLight.operationscore.Behavior

 1  import pdb 
 2  from operationscore.SmootCoreObject import * 
 3  from logger import main_log 
4 -class Behavior(SmootCoreObject):
5 """Abstract class for a behavior. On every time step, the behavior is passed the 6 inputs from all sensors it is bound to as well as any recursive inputs that it 7 spawned during the last time step. Inheriting classes MUST define 8 processResponse. processResponse should return a list of dictionaries which 9 define the properties of the light response, (outputs, recursions). They must give a location and 10 color. They may define a PixelEvent to more closely control the outgoing 11 data, however, this is normally handled by routing the event to a behavior 12 specifically designed to do this (like AddPixelEvent). 13 timeStep is called on every iteration of the LightInstallation 14 addInput is called on each individual input received, and the inputs queue""" 15
16 - def init(self):
17 self.validateArgs('Behavior.params') 18 if type(self['Inputs']) != type([]): 19 self['Inputs'] = [self['Inputs']] 20 self.recursiveResponseQueue = [] 21 self.sensorResponseQueue = [] 22 self.outGoingQueue = [] 23 self.lastState = None 24 self.behaviorInit()
25
26 - def behaviorInit(self):
27 pass
28
29 - def addMapper(fn):
30 def withmap(fn): 31 return self.addMapperToResponse(fn())
32 return withmap
33
34 - def processResponse(self, sensorInputs, recursiveInputs):
35 raise Exception('ProcessResponse not defined!')
36
37 - def addInput(self, sensorInput):
38 self.sensorResponseQueue.append(sensorInput)
39 40 #used for behavior chaining 41
42 - def immediateProcessInput(self, sensorInputs, recursiveInputs=[]):
43 (outputs,recursions) = self.processResponse(sensorInputs, \ 44 recursiveInputs) 45 return self.addMapperToResponse((outputs,recursions)) 46
47 - def addInputs(self, sensorInputs):
48 if type(sensorInputs) == type([]): 49 [self.addInput(sensorInput) for sensorInput in sensorInputs] 50 else: 51 self.addInput(sensorInputs)
52 53 @staticmethod
54 - def deepCopyPacket(datapacket):
55 """Returns a deep copy of a behavior data packet (a list of dicts) so that modifying the 56 returned packet will not modify the incoming packet.""" 57 ret = [] 58 for d in datapacket: 59 d = dict(d) 60 ret.append(d) 61 return ret
62
63 - def getLastOutput(self):
64 return self.lastState
65
66 - def setLastOutput(self, output):
67 """Override to modify state. For example: if you are using a behavior that does uses 68 strings for location specification, you will want to override this to point to a single 69 location. Make sure you keep lastState as a [] of {}. (List of dicts). Additonally, 70 ensure that you call Behavior.deepCopyPacket on the packet before hand to avoid inadvertent 71 down-stream modifications. Look at Square.py for an example of this.""" 72 self.lastState = Behavior.deepCopyPacket(output)
73
74 - def addMapperToResponse(self, responses):
75 if self['Mapper'] != None: 76 if type(responses) == type(tuple): 77 (out, recurs) = responses 78 return (self.addMapperToResponse(out), self.addMapperToResponse(recurs)) 79 if type(responses) == type([]): 80 for r in responses: 81 r['Mapper'] = self['Mapper'] 82 return responses 83 return responses
84
85 - def timeStep(self): #TODO: type checking. clean this up
86 (outputs, recursions) = self.processResponse(self.sensorResponseQueue, \ 87 self.recursiveResponseQueue) 88 self.sensorResponseQueue = [] 89 self.recursiveResponseQueue = recursions 90 self.setLastOutput(outputs) 91 main_log.debug(self['Id'] + ' Ouputs ' + str(outputs)) 92 return self.addMapperToResponse(outputs) 93