aboutsummaryrefslogtreecommitdiff
path: root/util/BehaviorQuerySystem.py
blob: 688eecb1e388c19615d03c8bf926ad5b59a3fe35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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):
    """Add a behavior to the behavior registry."""
    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 

def getDistLambda(loc, maxDist):
    """Returns a lambda function that checks if for behaviors within maxDist of loc.  Can be passed
    in as an arg to query."""
    return lambda args:geo.dist(args['Location'], loc) <= maxDist

def getBehaviorsNear(loc, maxdist):
    """A premade method to do the common task of finding behavior near a location."""
    return query(getDistLambda(loc, maxDist))