From cf1048df72b845ef7fefd5ec5709f7d1b2c4df79 Mon Sep 17 00:00:00 2001 From: rcoh Date: Sat, 12 Feb 2011 16:34:25 -0500 Subject: Added some new tests to BehaviorQuerySystem.py which demonstrate how to use it. Fixed a bug in BQS. --- tests/TestBQS.py | 30 ++++++++++++++++++++++++++---- util/BehaviorQuerySystem.py | 2 +- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/tests/TestBQS.py b/tests/TestBQS.py index 8dc90b2..7316c31 100644 --- a/tests/TestBQS.py +++ b/tests/TestBQS.py @@ -1,18 +1,40 @@ import unittest import util.BehaviorQuerySystem as bqs from behaviors.ColorChangerBehavior import * +import util.Geo as geo class TestBQS(unittest.TestCase): def setUp(self): bqs.initBQS() b = ColorChangerBehavior({'Id': 'color','ColorList':[(255,0,0)]}) + c = ColorChangerBehavior({'Id': 'color2', 'ColorList':[(0,0,255)]}) bqs.addBehavior(b) - b.addInput({'Location':(5,5)}) + bqs.addBehavior(c) + b.addInput({'Location':(3,4)}) + c.addInput({'Location':(5,12)}) b.timeStep() + c.timeStep() def tearDown(self): bqs.initBQS() - - def test_color_predicate(self): + def test_simple_query(self): validQuery = lambda args:args['Color']==(255,0,0) invalidQuery = lambda args:args['Color']==(254,0,0) - assert bqs.query(validQuery) == [{'Color':(255,0,0), 'Location':(5,5)}] + assert bqs.query(validQuery) == [{'Color':(255,0,0), 'Location':(3,4)}] assert bqs.query(invalidQuery) == [] + def test_dist_query(self): + validDist = lambda args:geo.dist(args['Location'], (0,0)) <= 5 + invalidDist = lambda args:geo.dist(args['Location'], (0,0)) <= 2 + doubleDist = lambda args:geo.dist(args['Location'], (0,0)) <= 20 + + assert bqs.query(validDist) == [{'Color':(255,0,0), 'Location':(3,4)}] + assert bqs.query(invalidDist) == [] + assert bqs.query(doubleDist) == [{'Color':(255,0,0), 'Location':(3,4)}, {'Color':(0,0,255),\ + 'Location':(5,12)}] + def test_complex_queries(self): + + validQuery = lambda args:args['Color']==(255,0,0) + doubleDist = lambda args:geo.dist(args['Location'], (0,0)) <= 20 + + twoPartPredicate = lambda args:doubleDist(args) and validQuery(args) + assert bqs.query(twoPartPredicate) == [{'Color':(255,0,0), 'Location':(3,4)}] + assert bqs.query([validQuery, doubleDist]) == [{'Color':(255,0,0), 'Location':(3,4)}] + diff --git a/util/BehaviorQuerySystem.py b/util/BehaviorQuerySystem.py index 5399435..643b95c 100644 --- a/util/BehaviorQuerySystem.py +++ b/util/BehaviorQuerySystem.py @@ -16,7 +16,7 @@ def query(predicateList): #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): + elif not isinstance(predicateList, list): raise Exception('Predicate list must be a function or list of functions') global behaviorList, initialized ret = [] -- cgit v1.2.3