Package mosp_examples :: Module action_wiggler
[hide private]
[frames] | no frames]

Source Code for Module mosp_examples.action_wiggler

 1  #!/bin/env python 
 2   
 3  """ Action example: infect action is enabled is disabled again 
 4      - random movement 
 5      - zombie infect (distance 20 meters) 
 6      - all zombies spontaneously get healed at tick 1000  
 7          - infection actions are stopped 
 8      - output to visual player, which is executed as child process 
 9  """ 
10   
11  import sys 
12  sys.path.append("..") 
13   
14  from mosp.core import Simulation, Person, action, start_action, stop_action 
15  from mosp.geo import osm 
16  from mosp.impl import movement 
17  from mosp.monitors import ChildprocessPlayerChamplainMonitor, SocketPlayerMonitor 
18   
19  __author__ = "B. Henne" 
20  __contact__ = "henne@dcsec.uni-hannover.de" 
21  __copyright__ = "(c) 2011, DCSec, Leibniz Universitaet Hannover, Germany" 
22  __license__ = "GPLv3" 
23 24 25 -class StopActionsWiggler(Person):
26 """Implements a zombie stopping its actions at tick 1000. 27 @author: B. Henne""" 28
29 - def __init__(self, *args, **kwargs):
30 """Init the zombie, that will be healed.""" 31 super(StopActionsWiggler, self).__init__(*args, **kwargs) 32 self.p_infected = False 33 if kwargs.get('infected'): 34 self.infect()
35 36 next_target = movement.person_next_target_random 37
38 - def infect(self):
39 """The infection routine itself. 40 41 If not infected, person gets infected. Log is printed to stderr.""" 42 if self.p_infected == False: 43 self.p_infected = True 44 self.p_color = 1 45 self.p_color_rgba = (1.0, 0.1, 0.1, 1.0) 46 start_action(self.infect_other, delay=0) 47 sys.stderr.write('t=%s %s infected - started %s\n' % (self.sim.now(), self.name, 'self.infect_other'))
48 # elif (self.sim.now() > 0) and ((self.sim.now() % 1000) == 0) and (self.name != 'p0'): 49 # self.color = 2 50 # stop_action(self.infect_other) # is working here 51 # self.stop_actions() # is working here 52 # self.stop_action(self.infect_other.im_func) # is working here 53 # sys.stderr.write('t=%s %s healed - stopped %s\n' % (self.sim.now(), self.name, 'self.infect_other')) 54 55 @action(1, start=False)
56 - def infect_other(self):
57 """The zombie infect action. 58 59 Every tick this action is called (using the mosp action decorator). 60 This action looks for people in range of 20 meters, and calls their 61 infect() routine. Action is not active at beginning. It is activated by 62 self.infect_other.start() and then is called every tick.""" 63 if self.p_infected == True: 64 self.p_color = 1 65 self.p_color_rgba = (1.0, 0.1, 0.1, 1.0) 66 self.get_near(20).call(delay=1).infect() 67 if (self.sim.now() > 0) and ((self.sim.now() % 1000) == 0): 68 self.call_stop_actions() # is working here, but only this or above in infect() 69 self.p_color = 2 # AND sometimes errors with KeyError: <mosp.core.FakeNode object at > because FakeNode has no neighbors :( 70 self.p_color_rgba = (0.1,0.9,0.1,1.0)
71 # stop_action(self.infect_other) # not working here: whom._rec[3] = True ## Mark as cancelled - TypeError: 'NoneType' object does not support item assignment
72 # self.stop_actions() # not working here: whom._rec[3] = True ## Mark as cancelled - TypeError: 'NoneType' object does not support item assignment 73 # self.stop_action(self.infect_other.im_func) # not working here: whom._rec[3] = True ## Mark as cancelled - TypeError: 'NoneType' object does not support item assignment 74 75 76 -def main():
77 """Defines the simulation, map, monitors, persons.""" 78 s = Simulation(geo=osm.OSMModel('../data/hannover2.osm', grid_size=50), rel_speed=120, seed=6001) 79 #m = s.add_monitor(ChildprocessPlayerChamplainMonitor, 2) 80 m = s.add_monitor(SocketPlayerMonitor, 2) 81 s.add_persons(StopActionsWiggler, 1, monitor=m, args={"infected":True, "speed":2.4}) 82 s.add_persons(StopActionsWiggler, 49, monitor=m) 83 s.run(until=4000, real_time=True, monitor=True)
84 85 86 if __name__ == '__main__': 87 main() 88