aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Russell Cohen <rcoh@mit.edu>2011-01-10 23:24:02 -0500
committerGravatar Russell Cohen <rcoh@mit.edu>2011-01-10 23:24:02 -0500
commitaaf8bdbee7fdc1d4721f43307fc824c373c69ec4 (patch)
treeee22f0ac45dede7fa8263cfc91f2730b19d07da9
parenta1969e4c4bf61bc6e73c6a59fbef96e8ce361611 (diff)
some improvements to behavior chain. A "Square" Behavior
-rw-r--r--LightInstallation.py8
-rw-r--r--TestAll.py4
-rw-r--r--behaviors/BehaviorChain.py5
-rw-r--r--behaviors/Square.py11
-rw-r--r--config/Outdoor.xml9
-rw-r--r--pixelmappers/SimpleMapper.py1
-rw-r--r--tests/TestComponentRegistry.py12
-rw-r--r--util/ComponentRegistry.py10
8 files changed, 49 insertions, 11 deletions
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 @@
<Id>pixelsleft</Id>
</Args>
</Behavior>
+ <Behavior>
+ <Class>behaviors.Square</Class>
+ <Args>
+ <Id>square</Id>
+ <Width>10</Width>
+ </Args>
+ </Behavior>
<Behavior Id="recursivedecay">
<InheritsFrom>behaviors/LoopAndDie.xml</InheritsFrom>
<Args>
@@ -130,7 +137,7 @@
</Inputs>
<ChainedBehaviors>
<Id>echo</Id>
- <Id>pixelsleft</Id>
+ <Id>square</Id>
<Id>decay</Id>
</ChainedBehaviors>
<RenderToScreen>True</RenderToScreen>
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()