1 from operationscore.Behavior import *
2 import util.ComponentRegistry as compReg
3 from behaviors.ModifyParam import *
4 import util.Geo as Geo
5 import util.Strings as Strings
6 import random
7 import pdb
9 """RestrictLocation is a Behavior which does an action -- A ModifyParam, actually, when a certain
10 location based condition is met. It takes arguments as follows:
11
12 <Action> -- Operation to perform, using ModifyParam syntax. Use {val} to reference the variable
13 specified by ParamName.
14 <ParamName> -- the name of the parameter to modify.
15 <LocationRestriction> -- either a tuple of (xmin,ymin,xmax,ymax) or a python-correct conditional. Use {x} and
16 {y} to reference x and y. Use < and > to get < and > in XML. EG:
17 <LocationRestriction>{x}<0 or {x}>800</LocationRestriction>"""
18
20 action = self['Action']
21 modifyParamArgs = {'ParamType': 'Sensor',
22 'ParamName':self['ParamName'],'ParamOp':self['Action']}
23 self.locBounds = self['LocationRestriction']
24 self.paramModifier = ModifyParam(modifyParamArgs)
25 if isinstance(self.locBounds, str):
26 self.locBounds = self.locBounds.replace('{x}', 'l[0]')
27 self.locBounds = self.locBounds.replace('{y}', 'l[1]')
28 self.locEval = eval('lambda l:'+self.locBounds)
29 elif isinstance(self.locBounds, tuple):
30 if len(self.locBounds) != 4:
31 raise Exception('Must be in form (xmin,yin,xmax,ymax)')
32 else:
33 self.locEval = lambda l:Geo.pointWithinBoundingBox(l,\
34 self.LocBounds)
36 ret = []
37 for data in sensorInputs:
38 if self.locEval(data['Location']):
39 (dataOut, recur) = self.paramModifier.immediateProcessInput([data], [])
40
41 ret += dataOut
42 else:
43 ret.append(data)
44 return (ret, [])
45