1 import types
2 """The behavior query system is a module that allows querying behaviors based on lambda-function
3 predicates."""
8
10 """Add a behavior to the behavior registry."""
11 behaviorList.append(behavior)
12
14 """BehaviorQuerySystem.query takes a list of predicates (functions with signature:
15 (behavior,output)), and
16 optionally a behavior to be compared to."""
17
18 if isinstance(predicateList, types.FunctionType):
19 predicateList = [predicateList]
20 elif not isinstance(predicateList, list):
21 raise Exception('Predicate list must be a function or list of functions')
22 global behaviorList, initialized
23 ret = []
24 if not initialized:
25 initBQS()
26
27 for behavior in behaviorList:
28 lastOutput = behavior.getLastOutput()
29 for output in lastOutput:
30 validOutput = True
31 for pred in predicateList:
32
33 if not pred(output):
34 validOutput = False
35 break
36 if validOutput:
37 ret.append(output)
38 return ret
39
41 """Returns a lambda function that checks if for behaviors within maxDist of loc. Can be passed
42 in as an arg to query."""
43 return lambda args:geo.dist(args['Location'], loc) <= maxDist
44
46 """A premade method to do the common task of finding behavior near a location."""
47 return query(getDistLambda(loc, maxDist))
48