aboutsummaryrefslogtreecommitdiff
path: root/behaviors
diff options
context:
space:
mode:
authorGravatar eugue <eug.sun@gmail.com>2011-02-12 20:07:15 -0500
committerGravatar eugue <eug.sun@gmail.com>2011-02-12 20:07:15 -0500
commit9e342041f05e88f8d1987a48fdcdc10c14ef095f (patch)
tree2513db9b09bc5d199dcf328cfba815df32af3dc0 /behaviors
parentff07b18748c64243c1c6bc62f489bfd03205d13a (diff)
Added SwitchBehavior and its unit test file. Could be used to control games.
Diffstat (limited to 'behaviors')
-rw-r--r--behaviors/SwitchBehavior.py24
1 files changed, 24 insertions, 0 deletions
diff --git a/behaviors/SwitchBehavior.py b/behaviors/SwitchBehavior.py
new file mode 100644
index 0000000..1fb755d
--- /dev/null
+++ b/behaviors/SwitchBehavior.py
@@ -0,0 +1,24 @@
+from operationscore.Behavior import *
+import util.ComponentRegistry as compReg
+
+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.
+ In Config file, include:
+ <Prefix>Behavior's ID<Prefix>
+ <DefaultBehavior>Default behavior's ID<DefaultBehavior>
+ """
+ def behaviorInit(self):
+ self.defaultBehavior = compReg.getComponent(self['DefaultBehavior'])
+ self.currBehavior = None
+
+ def processResponse(self, sInputs, rInputs):
+ dataStr = sInputs[-1]['Data']
+ if dataStr[0] in self.argDict:
+ self.currBehavior = compReg.getComponent(self[dataStr[0]])
+ sInputs[-1]['Data'] = sInputs[-1]['Data'][1:] # remove prefix
+ return self.currBehavior.processResponse(sInputs, rInputs)
+ else:
+ return self.defaultBehavior.processsResponse(sInputs, rInputs)
+