From d8ba03006e2ea1400e80caded738e79c186e8de3 Mon Sep 17 00:00:00 2001 From: eugue Date: Mon, 14 Feb 2011 17:13:33 -0500 Subject: change SwitchBehavior to take in a JSON dict to avoid weird XML parsing. Also added a public function to manually set current behavior. --- behaviors/SwitchBehavior.py | 28 ++++++++++++++++++++-------- tests/TestSwitchBehavior.py | 12 +++++++++--- 2 files changed, 29 insertions(+), 11 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 s mapping to 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: - Behavior's ID - Default behavior's ID + JSON format dict with prefix keys and behavior ID values + Default behavior's ID + An example config excerpt: + + behaviors.SwitchBehavior + + switch + {'@':'game1', '#':'game2', '$':'game3'} + game1 + + """ 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 diff --git a/tests/TestSwitchBehavior.py b/tests/TestSwitchBehavior.py index f130431..774dbbc 100644 --- a/tests/TestSwitchBehavior.py +++ b/tests/TestSwitchBehavior.py @@ -15,21 +15,27 @@ class TestSwitchBehavior(unittest.TestCase): compReg.registerComponent(self.behavior1) compReg.registerComponent(self.behavior2) - self.switchBehavior = SwitchBehavior({'Id': 'switch', '1': 'behavior1', '2': 'behavior2', 'DefaultBehavior': 'behavior1'}) + self.switchBehavior = SwitchBehavior({'Id': 'switch', 'PrefixToBehavior': '{"@": "behavior1", "#": "behavior2"}', 'DefaultBehavior': 'behavior1'}) compReg.registerComponent(self.switchBehavior) def tearDown(self): pass def test_switch_to_behavior1(self): - inputs = [{'Data': '1something', 'Location': 'someloc'}] + inputs = [{'Data': '@something', 'Location': 'someloc'}] returned = self.switchBehavior.processResponse(inputs, []) assert returned[0][0]['Location'] == 'someloc' def test_switch_to_behavior2(self): - inputs = [{'Data': '2something'}] + inputs = [{'Data': '#something'}] returned = self.switchBehavior.processResponse(inputs, []) assert returned[0] == [] + def test_default_behavior(self): + inputs = [{'Data': 'something', 'Location': 'someloc'}] + returned = self.switchBehavior.processResponse(inputs, []) + assert returned[0][0]['Location'] == 'someloc' + + if __name__ == '__main__': unittest.main() -- cgit v1.2.3