Package mosp :: Module locations
[hide private]
[frames] | no frames]

Source Code for Module mosp.locations

  1  """Models of real-world locations 
  2   
  3  to be used with RoutingNode.worldObject""" 
  4   
  5  from SimPy.SimulationRT import Process, hold 
  6  import sys 
  7  import logging 
  8   
  9  __author__ = "B. Henne, P. Tute" 
 10  __contact__ = "henne@dcsec.uni-hannover.de" 
 11  __copyright__ = "(c) 2010-2012, DCSec, Leibniz Universitaet Hannover, Germany" 
 12  __license__ = "GPLv3" 
 13   
 14   
15 -class LocationClosedException(Exception):
16 """Exception raised if a Person tries to enter a closed Location. 17 @author: B. Henne""" 18
19 - def __init__(self, value):
20 """Inits the Exception.""" 21 self.value = value
22
23 - def __str__(self):
24 return repr(self.value)
25 26
27 -class Location(Process):
28 """A real-world location a Person can visit/interact with. 29 30 Inheritance base for complex Locations. 31 @author: B. Henne""" 32
33 - def __init__(self, name, sim):
34 """Inits the Location.""" 35 super(Location, self).__init__(name=name, sim=sim) 36 self.visitors = [] 37 self.leavetimes = {} 38 self.nextleave = self.sleep = 1000000 #self.sim.duration would be optimal
39
40 - def interact(self, person, duration=600):
41 """Interact method normally calls visit.""" 42 self.visit(person, duration)
43
44 - def visit(self, person, duration):
45 """On visit a person becomes visitor for duration ticks. After visit person is reactivated.""" 46 self.visitors.append(person) 47 # add user to cafe statistics somewhere here 48 timenow = self.sim.now() 49 pass # replaces next logging statement 50 #logging.debug("t=%s person %s visites for %s ticks\n" % (timenow,person.p_id, duration)) 51 self.leavetimes[person] = timenow + duration 52 pass # replaces next logging statement 53 #logging.debug("t=%s person will leave at %s\n" % (timenow, self.leavetimes[person])) 54 if self.nextleave < timenow: 55 self.nextleave = duration+timenow 56 else: 57 self.nextleave = min(self.nextleave, duration+timenow) 58 self.sleep = self.nextleave - timenow 59 if self.sleep == 0: 60 self.sleep = 1 61 assert self.sleep >= 0 62 self.interrupt(self)
63
64 - def leave(self, person):
65 """On leave person is removed from visitors and is reactivated.""" 66 pass # replaces next logging statement 67 #logging.debug("t=%s person %s leaves\n" % (self.sim.now(),person.p_id)) 68 del self.leavetimes[person] 69 if len(self.leavetimes) == 0: 70 self.nextleave = self.sleep = 1000000 71 else: 72 self.nextleave = min(self.leavetimes.values()) 73 self.visitors.remove(person) 74 person.reactivate()
75 # maybe modify person, e.g. set as infected, if not done in server() 76 # remove user from cafe statistics 77
78 - def serve(self):
79 """Manages visitors (what is done in location?) and triggers leaving.""" 80 pass # replaces next logging statement 81 #logging.debug('Location (type %s) %s open\n' % (self.__class__.__name__, self.name)) 82 while True: 83 now = self.sim.now() 84 # do something 85 pids = [p.p_id for p in self.visitors] 86 pass # replaces next logging statement 87 #logging.debug('t=%s STATS #p=%s, pid=%s\n' % (now, self.sleep, pids)) 88 # do something 89 for person in self.leavetimes.keys(): 90 if now >= self.leavetimes[person]: 91 for p in self.visitors: 92 if p.p_id == person.p_id: 93 self.leave(p) 94 pass # replaces next logging statement 95 #logging.debug('t=%s now sleep for %s\n' % (now, self.sleep)) 96 self.sleep = self.nextleave - now 97 yield hold, self, self.sleep
98 # or use events? 99 100 101 PersonWakeUp = Location 102 103
104 -class Cafe(Location):
105 """Cafe is a simple cafe Location. 106 107 A Cafe can open() and close(). 108 @author: B. Henne""" 109
110 - def __init__(self, name, sim):
111 """Inits the Cafe.""" 112 super(Cafe, self).__init__(name=name, sim=sim) 113 self.open = True
114
115 - def interact(self, person, duration=600):
116 """Implements the interaction with the Cafe: visit() if Cafe is open.""" 117 if self.open: 118 self.visit(person, self.sim.random.randint(duration/2,duration)) 119 else: 120 raise LocationClosedException(self.name)
121
122 - def open(self):
123 """Set Cafe to be open.""" 124 self.open = True
125
126 - def close(self):
127 """Close the Cafe, kick all its visitors.""" 128 self.open = False 129 for p in self.visitors: 130 self.leave(p)
131 132
133 -class Exit(Process, set):
134 """Exit location formerly known as mosp.exit.Exit 135 136 @deprecated: old code base, now Exits are (inherited from) Locations. 137 @author: P. Tute"""
138 - def __init__(self, random, sim, sleeptime=10):
139 """Inits the Exit.""" 140 Process.__init__(self, name='Exit handler', sim=sim) 141 set.__init__(self) 142 self.random = random 143 self.sleeptime = sleeptime #: time between two go()s
144
145 - def go(self):
146 """This does the exit do: Reactivate any contained Person and sleep again.""" 147 while 23: 148 if self: 149 person = self.random.choice(list(self)) 150 self.sim.reactivate(person) 151 self.remove(person) 152 yield hold, self, self.sleeptime
153