aboutsummaryrefslogtreecommitdiff
path: root/util/BehaviorQuerySystem.py
diff options
context:
space:
mode:
authorGravatar rcoh <rcoh@mit.edu>2011-02-12 15:59:59 -0500
committerGravatar rcoh <rcoh@mit.edu>2011-02-12 15:59:59 -0500
commit4d62e1152241854ab864142d827d735d84405078 (patch)
tree2589c6a37721e1b8362c8e8c6a580c8c185b2f86 /util/BehaviorQuerySystem.py
parentb45b9079c5decd720d8275378bb0d6dc172c6234 (diff)
Modified the test code to automatically pick up new tests. Just add your test file to
tests/__init__.py. Fixed a bug in ZigZagLayout. Added the BehaviorQuerySystem to allow querying on behaviors. Added the "ModulateColor" behavior which will continually shift a color along the HSV plane. Added TestBQS to test the behavior query system. Modified Behavior to have a getLastOutput and setLastOutput method.
Diffstat (limited to 'util/BehaviorQuerySystem.py')
-rw-r--r--util/BehaviorQuerySystem.py39
1 files changed, 39 insertions, 0 deletions
diff --git a/util/BehaviorQuerySystem.py b/util/BehaviorQuerySystem.py
new file mode 100644
index 0000000..5399435
--- /dev/null
+++ b/util/BehaviorQuerySystem.py
@@ -0,0 +1,39 @@
+import types
+"""The behavior query system is a module that allows querying behaviors based on lambda-function
+predicates."""
+def initBQS():
+ global behaviorList, initialized
+ behaviorList = []
+ initialized = True
+
+def addBehavior(behavior):
+ behaviorList.append(behavior)
+
+def query(predicateList):
+ """BehaviorQuerySystem.query takes a list of predicates (functions with signature:
+ (behavior,output)), and
+ optionally a behavior to be compared to."""
+ #want to do queries wrt: behavior itself, the behavior packet, the querying behavior
+ if isinstance(predicateList, types.FunctionType):
+ predicateList = [predicateList]
+ elif not isinstance(prediateList, list):
+ raise Exception('Predicate list must be a function or list of functions')
+ global behaviorList, initialized
+ ret = []
+ if not initialized:
+ initBQS()
+
+ for behavior in behaviorList: #Consider every behavior
+ lastOutput = behavior.getLastOutput()
+ for output in lastOutput: #Look at every element it has output
+ validOutput = True
+ for pred in predicateList: #Evaluate every predicate. A predicate is a lambda function that
+ #takes a dict and returns a bool.
+ if not pred(output):
+ validOutput = False
+ break
+ if validOutput:
+ ret.append(output)
+ return ret
+
+