From aaf8bdbee7fdc1d4721f43307fc824c373c69ec4 Mon Sep 17 00:00:00 2001 From: Russell Cohen Date: Mon, 10 Jan 2011 23:24:02 -0500 Subject: some improvements to behavior chain. A "Square" Behavior --- LightInstallation.py | 8 ++------ TestAll.py | 4 ++++ behaviors/BehaviorChain.py | 5 +++++ behaviors/Square.py | 11 +++++++++++ config/Outdoor.xml | 9 ++++++++- pixelmappers/SimpleMapper.py | 1 + tests/TestComponentRegistry.py | 12 ++++++++++-- util/ComponentRegistry.py | 10 ++++++++-- 8 files changed, 49 insertions(+), 11 deletions(-) create mode 100644 behaviors/Square.py diff --git a/LightInstallation.py b/LightInstallation.py index aaedcf2..2726005 100644 --- a/LightInstallation.py +++ b/LightInstallation.py @@ -79,12 +79,8 @@ class LightInstallation: self.renderers = self.initializeComponent(rendererConfig) def registerComponents(self, components): for component in components: - cid = component['Id'] - if cid == None: #TODO: determine if componenent is critical, and if so, die - main_log.error('Components must be registered with Ids. Component not registered') - else: - compReg.registerComponent(component) - main_log.debug(cid + ' registered') + cid = compReg.registerComponent(component) + main_log.debug(cid + ' registered') def initializeComponent(self, config): components = [] if config != None: diff --git a/TestAll.py b/TestAll.py index 9921c7e..23b34ea 100644 --- a/TestAll.py +++ b/TestAll.py @@ -1,5 +1,9 @@ import unittest from unittest import TestLoader import tests.TestConfigLoaders +import tests.TestComponentRegistry testSuite = TestLoader().loadTestsFromModule(tests.TestConfigLoaders) unittest.TextTestRunner(verbosity=2).run(testSuite) + +testSuite = TestLoader().loadTestsFromModule(tests.TestComponentRegistry) +unittest.TextTestRunner(verbosity=2).run(testSuite) diff --git a/behaviors/BehaviorChain.py b/behaviors/BehaviorChain.py index 0170fa8..e15bd23 100644 --- a/behaviors/BehaviorChain.py +++ b/behaviors/BehaviorChain.py @@ -32,3 +32,8 @@ yourself or bug russell') self.feedback[behaviorId] = recurrence return response + def appendBehavior(behavior): + bid = compReg.registerComponent(behavior) #register behavior (will make + #a new id if there isn't one) + self['ChainedBehaviors'].append(bid) + diff --git a/behaviors/Square.py b/behaviors/Square.py new file mode 100644 index 0000000..a6e9401 --- /dev/null +++ b/behaviors/Square.py @@ -0,0 +1,11 @@ +from operationscore.Behavior import * +class Square(Behavior): + def processResponse(self, sensorInputs, recursiveInputs): + for sensory in sensorInputs:#TODO: consider replicating the dict + xLoc = sensory['Location'][0] + yLoc = sensory['Location'][1] + width = self['Width'] + sensory['Location'] =\ + '{x}<'+str(xLoc+width)+',{x}>'+str(xLoc-width)+\ + ',{y}<'+str(yLoc+width)+',{y}>'+str(yLoc-width) + return (sensorInputs, recursiveInputs) diff --git a/config/Outdoor.xml b/config/Outdoor.xml index f0995b1..053f31e 100644 --- a/config/Outdoor.xml +++ b/config/Outdoor.xml @@ -84,6 +84,13 @@ pixelsleft + + behaviors.Square + + square + 10 + + behaviors/LoopAndDie.xml @@ -130,7 +137,7 @@ echo - pixelsleft + square decay True diff --git a/pixelmappers/SimpleMapper.py b/pixelmappers/SimpleMapper.py index 2df24e0..31a48cc 100644 --- a/pixelmappers/SimpleMapper.py +++ b/pixelmappers/SimpleMapper.py @@ -32,6 +32,7 @@ class SimpleMapper(PixelMapper): if pixelValid: ret.append((pixel, 1)) except Exception as exp: + pdb.set_trace() raise Exception('Bad event condition') return ret diff --git a/tests/TestComponentRegistry.py b/tests/TestComponentRegistry.py index 1acd0c8..5ada524 100644 --- a/tests/TestComponentRegistry.py +++ b/tests/TestComponentRegistry.py @@ -1,11 +1,19 @@ import unittest import util.ComponentRegistry as compReg +from operationscore.SmootCoreObject import SmootCoreObject class TestComponentRegistry(unittest.TestCase): def setUp(self): compReg.initRegistry() def tearDown(self): compReg.clearRegistry() - def test_register_component(self): - comp = SmootCoreObject({'Id': obj1}) + def test_register_component_id_specified(self): + comp = SmootCoreObject({'Id': 'obj1'}) compReg.registerComponent(comp) + newcomp = compReg.getComponent('obj1') + assert comp == newcomp + def test_register_new_id(self): + comp = SmootCoreObject({}) + cid =compReg.registerComponent(comp) + newcomp = compReg.getComponent(cid) + assert comp == newcomp diff --git a/util/ComponentRegistry.py b/util/ComponentRegistry.py index 0518f56..43c4795 100644 --- a/util/ComponentRegistry.py +++ b/util/ComponentRegistry.py @@ -4,14 +4,18 @@ from logger import main_log #TODO: make component registry a singleton def initRegistry(): #TODO: don't overwrite existing registry - globals()['Registry'] = {} + if not 'Registry' in globals(): + globals()['Registry'] = {} def clearRegistry(): initRegistry() + def removeComponent(cid): globals()['Registry'].pop(cid) + def getComponent(cid): return globals()['Registry'][cid] + #Registry of all components of the light system #TODO: pick a graceful failure behavior and implement it def registerComponent(component, cid=None): @@ -26,11 +30,13 @@ def registerComponent(component, cid=None): main_log.debug(cid + 'automatically assigned') globals()['Registry'][cid] = component return cid -#def registerDefault( + def removeComponent(cid): globals()['Registry'].pop(cid) + def getComponent(cid): return globals()['Registry'][cid] + def getNewId(): trialKey = len(globals()['Registry']) trialId = hashlib.md5(str(trialKey)).hexdigest() -- cgit v1.2.3