aboutsummaryrefslogtreecommitdiff
path: root/behaviors/SwitchBehavior.py
diff options
context:
space:
mode:
authorGravatar eugue <eug.sun@gmail.com>2011-02-14 17:13:33 -0500
committerGravatar eugue <eug.sun@gmail.com>2011-02-14 17:13:33 -0500
commitd8ba03006e2ea1400e80caded738e79c186e8de3 (patch)
treee02130d04c210f25c1b6fa08cfa72a3dce7d586d /behaviors/SwitchBehavior.py
parent1e8b07e52c8b15b9bf4d9dc357f56bc8bbccc718 (diff)
change SwitchBehavior to take in a JSON dict to avoid weird XML parsing. Also added a public function to manually set current behavior.
Diffstat (limited to 'behaviors/SwitchBehavior.py')
-rw-r--r--behaviors/SwitchBehavior.py28
1 files changed, 20 insertions, 8 deletions
diff --git a/behaviors/SwitchBehavior.py b/behaviors/SwitchBehavior.py
index 1fb755d..1377b42 100644
--- a/behaviors/SwitchBehavior.py
+++ b/behaviors/SwitchBehavior.py
@@ -1,24 +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 Args in the form of <Prefix>s mapping to <Behavior ID>s. The behavior detects the prefix on the data and use the corresponding Behavior to process the data and return the outputs.
+ 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:
- <Prefix>Behavior's ID<Prefix>
- <DefaultBehavior>Default behavior's ID<DefaultBehavior>
+ <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.argDict:
- self.currBehavior = compReg.getComponent(self[dataStr[0]])
+ 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)
- else:
- return self.defaultBehavior.processsResponse(sInputs, rInputs)
+ return self.currBehavior.processResponse(sInputs, rInputs)
+ def setBehavior(self, behavior):
+ self.currBehavior = behavior