-
Notifications
You must be signed in to change notification settings - Fork 1
/
event.py
339 lines (263 loc) · 9 KB
/
event.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
"""Simulation Events
This file should contain all of the classes necessary to model the different
kinds of events in the simulation.
"""
from rider import Rider, WAITING, CANCELLED, SATISFIED
from dispatcher import Dispatcher
from driver import Driver
from location import deserialize_location
from monitor import Monitor, RIDER, DRIVER, REQUEST, CANCEL, PICKUP, DROPOFF
class Event:
"""An event.
Events have an ordering that is based on the event timestamp: Events with
older timestamps are less than those with newer timestamps.
This class is abstract; subclasses must implement do().
You may, if you wish, change the API of this class to add
extra public methods or attributes. Make sure that anything
you add makes sense for ALL events, and not just a particular
event type.
Document any such changes carefully!
=== Attributes ===
@type timestamp: int
A timestamp for this event.
"""
def __init__(self, timestamp):
"""Initialize an Event with a given timestamp.
@type self: Event
@type timestamp: int
A timestamp for this event.
Precondition: must be a non-negative integer.
@rtype: None
>>> Event(7).timestamp
7
"""
self.timestamp = timestamp
# The following six 'magic methods' are overridden to allow for easy
# comparison of Event instances. All comparisons simply perform the
# same comparison on the 'timestamp' attribute of the two events.
def __eq__(self, other):
"""Return True iff this Event is equal to <other>.
Two events are equal iff they have the same timestamp.
@type self: Event
@type other: Event
@rtype: bool
>>> first = Event(1)
>>> second = Event(2)
>>> first == second
False
>>> second.timestamp = first.timestamp
>>> first == second
True
"""
return self.timestamp == other.timestamp
def __ne__(self, other):
"""Return True iff this Event is not equal to <other>.
@type self: Event
@type other: Event
@rtype: bool
>>> first = Event(1)
>>> second = Event(2)
>>> first != second
True
>>> second.timestamp = first.timestamp
>>> first != second
False
"""
return not self == other
def __lt__(self, other):
"""Return True iff this Event is less than <other>.
@type self: Event
@type other: Event
@rtype: bool
>>> first = Event(1)
>>> second = Event(2)
>>> first < second
True
>>> second < first
False
"""
return self.timestamp < other.timestamp
def __le__(self, other):
"""Return True iff this Event is less than or equal to <other>.
@type self: Event
@type other: Event
@rtype: bool
>>> first = Event(1)
>>> second = Event(2)
>>> first <= first
True
>>> first <= second
True
>>> second <= first
False
"""
return self.timestamp <= other.timestamp
def __gt__(self, other):
"""Return True iff this Event is greater than <other>.
@type self: Event
@type other: Event
@rtype: bool
>>> first = Event(1)
>>> second = Event(2)
>>> first > second
False
>>> second > first
True
"""
return not self <= other
def __ge__(self, other):
"""Return True iff this Event is greater than or equal to <other>.
@type self: Event
@type other: Event
@rtype: bool
>>> first = Event(1)
>>> second = Event(2)
>>> first >= first
True
>>> first >= second
False
>>> second >= first
True
"""
return not self < other
def __str__(self):
"""Return a string representation of this event.
@type self: Event
@rtype: str
"""
raise NotImplementedError("Implemented in a subclass")
def do(self, dispatcher, monitor):
"""Do this Event.
Update the state of the simulation, using the dispatcher, and any
attributes according to the meaning of the event.
Notify the monitor of any activities that have occurred during the
event.
Return a list of new events spawned by this event (making sure the
timestamps are correct).
Note: the "business logic" of what actually happens should not be
handled in any Event classes.
@type self: Event
@type dispatcher: Dispatcher
@type monitor: Monitor
@rtype: list[Event]
"""
raise NotImplementedError("Implemented in a subclass")
class RiderRequest(Event):
"""A rider requests a driver.
=== Attributes ===
@type rider: Rider
The rider.
"""
def __init__(self, timestamp, rider):
"""Initialize a RiderRequest event.
@type self: RiderRequest
@type rider: Rider
@rtype: None
"""
super().__init__(timestamp)
self.rider = rider
def do(self, dispatcher, monitor):
"""Assign the rider to a driver or add the rider to a waiting list.
If the rider is assigned to a driver, the driver starts driving to
the rider.
Return a Cancellation event. If the rider is assigned to a driver,
also return a Pickup event.
@type self: RiderRequest
@type dispatcher: Dispatcher
@type monitor: Monitor
@rtype: list[Event]
"""
monitor.notify(self.timestamp, RIDER, REQUEST,
self.rider.id, self.rider.origin)
events = []
driver = dispatcher.request_driver(self.rider)
if driver is not None:
travel_time = driver.start_drive(self.rider.origin)
events.append(Pickup(self.timestamp + travel_time, self.rider, driver))
events.append(Cancellation(self.timestamp + self.rider.patience, self.rider))
return events
def __str__(self):
"""Return a string representation of this event.
@type self: RiderRequest
@rtype: str
"""
return "{} -- {}: Request a driver".format(self.timestamp, self.rider)
class DriverRequest(Event):
"""A driver requests a rider.
=== Attributes ===
@type driver: Driver
The driver.
"""
def __init__(self, timestamp, driver):
"""Initialize a DriverRequest event.
@type self: DriverRequest
@type driver: Driver
@rtype: None
"""
super().__init__(timestamp)
self.driver = driver
def do(self, dispatcher, monitor):
"""Register the driver, if this is the first request, and
assign a rider to the driver, if one is available.
If a rider is available, return a Pickup event.
@type self: DriverRequest
@type dispatcher: Dispatcher
@type monitor: Monitor
@rtype: list[Event]
"""
# Notify the monitor about the request.
# Request a rider from the dispatcher.
# If there is one available, the driver starts driving towards the
# rider, and the method returns a Pickup event for when the driver
# arrives at the riders location.
# TODO
pass
def __str__(self):
"""Return a string representation of this event.
@type self: DriverRequest
@rtype: str
"""
return "{} -- {}: Request a rider".format(self.timestamp, self.driver)
class Cancellation(Event):
# TODO
pass
class Pickup(Event):
# TODO
pass
class Dropoff(Event):
# TODO
pass
def create_event_list(filename):
"""Return a list of Events based on raw list of events in <filename>.
Precondition: the file stored at <filename> is in the format specified
by the assignment handout.
@param filename: str
The name of a file that contains the list of events.
@rtype: list[Event]
"""
events = []
with open(filename, "r") as file:
for line in file:
line = line.strip()
if not line or line.startswith("#"):
# Skip lines that are blank or start with #.
continue
# Create a list of words in the line, e.g.
# ['10', 'RiderRequest', 'Cerise', '4,2', '1,5', '15'].
# Note that these are strings, and you'll need to convert some
# of them to a different type.
tokens = line.split()
timestamp = int(tokens[0])
event_type = tokens[1]
# HINT: Use Location.deserialize to convert the location string to
# a location.
if event_type == "DriverRequest":
# TODO
# Create a DriverRequest event.
pass
elif event_type == "RiderRequest":
# TODO
# Create a RiderRequest event.
pass
events.append(event)
return events