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
16 """Exception raised if a Person tries to enter a closed Location.
17 @author: B. Henne"""
18
20 """Inits the Exception."""
21 self.value = value
22
24 return repr(self.value)
25
26
28 """A real-world location a Person can visit/interact with.
29
30 Inheritance base for complex Locations.
31 @author: B. Henne"""
32
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
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
48 timenow = self.sim.now()
49 pass
50
51 self.leavetimes[person] = timenow + duration
52 pass
53
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
65 """On leave person is removed from visitors and is reactivated."""
66 pass
67
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
76
77
79 """Manages visitors (what is done in location?) and triggers leaving."""
80 pass
81
82 while True:
83 now = self.sim.now()
84
85 pids = [p.p_id for p in self.visitors]
86 pass
87
88
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
95
96 self.sleep = self.nextleave - now
97 yield hold, self, self.sleep
98
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
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
123 """Set Cafe to be open."""
124 self.open = True
125
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
144
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