diff options
author | dxiao <dxiao@mit.edu> | 2011-02-20 13:27:26 -0500 |
---|---|---|
committer | dxiao <dxiao@mit.edu> | 2011-02-20 13:27:26 -0500 |
commit | 9ad7728d6303f5ee1be1f105b87cea872d84a5c4 (patch) | |
tree | 8ad677d209759ee8dfa445363cd97c681badd2b5 /behaviors | |
parent | 037a4faa064cbd4637a78f2742afa746bbc8aaad (diff) | |
parent | 58ec94a477f5edef0bf75a60252af96adec34d8d (diff) |
Merge branch 'master' into conner5
Diffstat (limited to 'behaviors')
-rw-r--r-- | behaviors/Flasher.py | 2 | ||||
-rw-r--r-- | behaviors/SwitchBehavior.py | 36 |
2 files changed, 37 insertions, 1 deletions
diff --git a/behaviors/Flasher.py b/behaviors/Flasher.py index 1d79d41..395d898 100644 --- a/behaviors/Flasher.py +++ b/behaviors/Flasher.py @@ -4,7 +4,7 @@ import util.ColorOps as colorops import pdb class Flasher(Behavior): """Implements a pulsing/flashing behavior. - Jim Salem: jsalem@gmail.com + Jim Salem Args: Factor - The speed of flashing. Must be b/w 0 and 1. Default is .95 diff --git a/behaviors/SwitchBehavior.py b/behaviors/SwitchBehavior.py new file mode 100644 index 0000000..1377b42 --- /dev/null +++ b/behaviors/SwitchBehavior.py @@ -0,0 +1,36 @@ +from operationscore.Behavior import * +import util.ComponentRegistry as compReg +import json + +class SwitchBehavior(Behavior): + """ + SwitchBehavior is a behavior that transform into different behaviors base on the input data. + The behavior expects a JSON formatted argument 'PrefixToBehavior' that maps prefixes to behaviors. The behavior detects the prefix on the data and use the corresponding Behavior to process the data and return the outputs. + In Config file, include: + <PrefixToBehavior>JSON format dict with prefix keys and behavior ID values</PrefixToBehavior> + <DefaultBehavior>Default behavior's ID</DefaultBehavior> + An example config excerpt: + <Behavior> + <Class>behaviors.SwitchBehavior</Class> + <Args> + <Id>switch</Id> + <PrefixToBehavior>{'@':'game1', '#':'game2', '$':'game3'}</PrefixToBehavior> + <DefaultBehavior>game1</DefaultBehavior> + </Args> + </Behavior> + """ + def behaviorInit(self): + self.defaultBehavior = compReg.getComponent(self['DefaultBehavior']) + self.prefixDict = json.loads(self['PrefixToBehavior']) + self.currBehavior = None + self.setBehavior(self.defaultBehavior) + + def processResponse(self, sInputs, rInputs): + dataStr = sInputs[-1]['Data'] + if dataStr[0] in self.prefixDict: + self.setBehavior(compReg.getComponent(self.prefixDict[dataStr[0]])) + sInputs[-1]['Data'] = sInputs[-1]['Data'][1:] # remove prefix + return self.currBehavior.processResponse(sInputs, rInputs) + + def setBehavior(self, behavior): + self.currBehavior = behavior |