diff options
author | eugue <eug.sun@gmail.com> | 2011-02-12 20:09:41 -0500 |
---|---|---|
committer | eugue <eug.sun@gmail.com> | 2011-02-12 20:09:41 -0500 |
commit | 1e8b07e52c8b15b9bf4d9dc357f56bc8bbccc718 (patch) | |
tree | cb80c07023d8f4781d95e23f989976093f7c9d5d /util/BehaviorQuerySystem.py | |
parent | 9e342041f05e88f8d1987a48fdcdc10c14ef095f (diff) | |
parent | a239c7accdc634459d2db014b8d8b6d5b78bab1b (diff) |
Merge branch 'master' of github.com:rcoh/SmootLight into behavior-factory
Diffstat (limited to 'util/BehaviorQuerySystem.py')
-rw-r--r-- | util/BehaviorQuerySystem.py | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/util/BehaviorQuerySystem.py b/util/BehaviorQuerySystem.py new file mode 100644 index 0000000..643b95c --- /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(predicateList, 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 + + |