1 import pdb
2 from operationscore.SmootCoreObject import *
3 from logger import main_log
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
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
28
32 return withmap
33
35 raise Exception('ProcessResponse not defined!')
36
39
40
41
43 (outputs,recursions) = self.processResponse(sensorInputs, \
44 recursiveInputs)
45 return self.addMapperToResponse((outputs,recursions))
46
52
53 @staticmethod
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
65
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
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
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