aboutsummaryrefslogtreecommitdiff
path: root/operationscore
diff options
context:
space:
mode:
authorGravatar rcoh <rcoh@mit.edu>2011-02-12 20:39:58 -0500
committerGravatar rcoh <rcoh@mit.edu>2011-02-12 20:39:58 -0500
commitc8316a35a1eb292f09db8b7ff36dd33b43dfe8b6 (patch)
tree4abd076114d333440c239af950f630cbbbcf329b /operationscore
parenta239c7accdc634459d2db014b8d8b6d5b78bab1b (diff)
Added an override of setLastOutput to square.py. Added some pre-built queries into the BQS.
Modified the default behavior or setLastOutput to make a deep copy of ouput to prevent accidental interference.
Diffstat (limited to 'operationscore')
-rw-r--r--operationscore/Behavior.py31
1 files changed, 28 insertions, 3 deletions
diff --git a/operationscore/Behavior.py b/operationscore/Behavior.py
index 7090a23..349c4cf 100644
--- a/operationscore/Behavior.py
+++ b/operationscore/Behavior.py
@@ -12,6 +12,7 @@ class Behavior(SmootCoreObject):
specifically designed to do this (like AddPixelEvent).
timeStep is called on every iteration of the LightInstallation
addInput is called on each individual input received, and the inputs queue"""
+
def init(self):
self.validateArgs('Behavior.params')
if type(self['Inputs']) != type([]):
@@ -21,34 +22,55 @@ class Behavior(SmootCoreObject):
self.outGoingQueue = []
self.lastState = None
self.behaviorInit()
+
def behaviorInit(self):
pass
+
def addMapper(fn):
def withmap(fn):
return self.addMapperToResponse(fn())
return withmap
+
def processResponse(self, sensorInputs, recursiveInputs):
raise Exception('ProcessResponse not defined!')
+
def addInput(self, sensorInput):
self.sensorResponseQueue.append(sensorInput)
+
#used for behavior chaining
+
def immediateProcessInput(self, sensorInputs, recursiveInputs=[]):
(outputs,recursions) = self.processResponse(sensorInputs, \
recursiveInputs)
return self.addMapperToResponse((outputs,recursions))
+
def addInputs(self, sensorInputs):
if type(sensorInputs) == type([]):
[self.addInput(sensorInput) for sensorInput in sensorInputs]
else:
self.addInput(sensorInputs)
- #private
+
+ @staticmethod
+ def deepCopyPacket(self, datapacket):
+ """Returns a deep copy of a behavior data packet (a list of dicts) so that modifying the
+ returned packet will not modify the incoming packet."""
+ ret = []
+ for d in datapacket:
+ d = dict(d)
+ ret.append[d]
+ return ret
+
def getLastOutput(self):
return self.lastState
+
def setLastOutput(self, output):
"""Override to modify state. For example: if you are using a behavior that does uses
strings for location specification, you will want to override this to point to a single
- location. Make sure you keep lastState as a [] of {}. (List of dicts)"""
- self.lastState = output
+ location. Make sure you keep lastState as a [] of {}. (List of dicts). Additonally,
+ ensure that you call Behavior.deepCopyPacket on the packet before hand to avoid inadvertent
+ down-stream modifications. Look at Square.py for an example of this."""
+ self.lastState = Behavior.deepCopyPacket(output)
+
def addMapperToResponse(self, responses):
if self['Mapper'] != None:
if type(responses) == type(tuple):
@@ -59,11 +81,14 @@ class Behavior(SmootCoreObject):
r['Mapper'] = self['Mapper']
return responses
return responses
+
def timeStep(self): #TODO: type checking. clean this up
(outputs, recursions) = self.processResponse(self.sensorResponseQueue, \
self.recursiveResponseQueue)
self.sensorResponseQueue = []
self.recursiveResponseQueue = recursions
self.setLastOutput(outputs)
+ if outputs != []:
+ pdb.set_trace()
main_log.debug(self['Id'] + ' Ouputs ' + str(outputs))
return self.addMapperToResponse(outputs)