From 9e342041f05e88f8d1987a48fdcdc10c14ef095f Mon Sep 17 00:00:00 2001 From: eugue Date: Sat, 12 Feb 2011 20:07:15 -0500 Subject: Added SwitchBehavior and its unit test file. Could be used to control games. --- behaviors/SwitchBehavior.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 behaviors/SwitchBehavior.py (limited to 'behaviors') 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 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. + In Config file, include: + Behavior's ID + Default behavior's ID + """ + 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) + -- cgit v1.2.3 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(-) (limited to 'behaviors') 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 From 2c9cb0784184346917f9ad509f9b2c4056b60167 Mon Sep 17 00:00:00 2001 From: rcoh Date: Wed, 16 Feb 2011 20:07:15 -0500 Subject: remove email address. --- behaviors/Flasher.py | 2 +- config/FireflyDemo.xml | 2 +- docs/XmlConfiguration.tex | 4 +++- 3 files changed, 5 insertions(+), 3 deletions(-) (limited to 'behaviors') 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/config/FireflyDemo.xml b/config/FireflyDemo.xml index de639d9..06ae7ba 100644 --- a/config/FireflyDemo.xml +++ b/config/FireflyDemo.xml @@ -1,6 +1,6 @@ - + diff --git a/docs/XmlConfiguration.tex b/docs/XmlConfiguration.tex index a1cce8f..0ccd08d 100644 --- a/docs/XmlConfiguration.tex +++ b/docs/XmlConfiguration.tex @@ -17,7 +17,9 @@ As you will see, however, XML in SmootLight goes beyond simple configuration. XML in SmootLight allows us to declare a LightSystem (an inherently non-declarative thing) in the same way you might write a webpage in HTML. We will refer to the XML system here-on-in as - `SmootConf'. Without any further ado, lets start looking at + `SmootConf'. \textbf{The fastest way to get familiar with SmootConf is simply to look at + the XML files in the configuration file. However, if you want a more structured approach to + its feature and sublties this document should do the job.} Without any further ado, lets start looking at how this all works. \section{Declaring a class in SmootConf} The most common thing done is SmootConf is declaring a class -- Class declaration code will -- cgit v1.2.3