-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.py
More file actions
244 lines (209 loc) · 9.74 KB
/
core.py
File metadata and controls
244 lines (209 loc) · 9.74 KB
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
"""
Core components for event-discrete simulation environments.
"""
from __future__ import annotations
from heapq import heappop, heappush
from itertools import count
from types import MethodType
from typing import TYPE_CHECKING, Any, Generic, Iterable, List, Optional, Tuple, Type, TypeVar, Union
from simpy.events import NORMAL, URGENT, AllOf, AnyOf, Event, EventPriority, Process, ProcessGenerator, Timeout
Infinity: float = float('inf')
T = TypeVar('T')
class BoundClass(Generic[T]):
"""Allows classes to behave like methods.
The ``__get__()`` descriptor is basically identical to
``function.__get__()`` and binds the first argument of the ``cls`` to the
descriptor instance.
"""
def __init__(self, cls: Type[T]):
self.cls = cls
def __get__(self, instance: Optional[BoundClass], owner: Optional[Type[BoundClass]]=None) -> Union[Type[T], MethodType]:
if instance is None:
return self.cls
return MethodType(self.cls, instance)
@staticmethod
def bind_early(instance: object) -> None:
"""Bind all :class:`BoundClass` attributes of the *instance's* class
to the instance itself to increase performance."""
cls = type(instance)
for name, obj in cls.__dict__.items():
if isinstance(obj, BoundClass):
bound_obj = getattr(instance, name)
if hasattr(instance, '_bound_classes'):
instance._bound_classes[name] = bound_obj
setattr(instance, name, bound_obj)
class EmptySchedule(Exception):
"""Thrown by an :class:`Environment` if there are no further events to be
processed."""
class StopSimulation(Exception):
"""Indicates that the simulation should stop now."""
@classmethod
def callback(cls, event: Event) -> None:
"""Used as callback in :meth:`Environment.run()` to stop the simulation
when the *until* event occurred."""
if event.ok:
raise cls(event.value)
else:
raise event.value
SimTime = Union[int, float]
class Environment:
"""Execution environment for an event-based simulation. The passing of time
is simulated by stepping from event to event.
You can provide an *initial_time* for the environment. By default, it
starts at ``0``.
This class also provides aliases for common event types, for example
:attr:`process`, :attr:`timeout` and :attr:`event`.
"""
def __init__(self, initial_time: SimTime=0):
self._now = initial_time
self._queue: List[Tuple[SimTime, EventPriority, int, Event]] = []
self._eid = count()
self._active_proc: Optional[Process] = None
self._processing_event = False
self._bound_classes = {}
self._bound_classes['Event'] = None
self._bound_classes['Process'] = None
self._bound_classes['Timeout'] = None
self._bound_classes['AllOf'] = None
self._bound_classes['AnyOf'] = None
self._bound_classes['Initialize'] = None
self._bound_classes['Interruption'] = None
BoundClass.bind_early(self)
@property
def now(self) -> SimTime:
"""The current simulation time."""
return self._now
@property
def active_process(self) -> Optional[Process]:
"""The currently active process of the environment."""
return self._active_proc
if TYPE_CHECKING:
def process(self, generator: ProcessGenerator) -> Process:
"""Create a new :class:`~simpy.events.Process` instance for
*generator*."""
pass
def timeout(self, delay: SimTime=0, value: Optional[Any]=None) -> Timeout:
"""Return a new :class:`~simpy.events.Timeout` event with a *delay*
and, optionally, a *value*."""
pass
def event(self) -> Event:
"""Return a new :class:`~simpy.events.Event` instance.
Yielding this event suspends a process until another process
triggers the event.
"""
pass
def all_of(self, events: Iterable[Event]) -> AllOf:
"""Return a :class:`~simpy.events.AllOf` condition for *events*."""
pass
def any_of(self, events: Iterable[Event]) -> AnyOf:
"""Return a :class:`~simpy.events.AnyOf` condition for *events*."""
pass
else:
process = BoundClass(Process)
timeout = BoundClass(Timeout)
event = BoundClass(Event)
all_of = BoundClass(AllOf)
any_of = BoundClass(AnyOf)
def schedule(self, event: Event, priority: EventPriority=NORMAL, delay: SimTime=0) -> None:
"""Schedule an *event* with a given *priority* and a *delay*."""
heappush(self._queue, (self._now + delay, priority, next(self._eid), event))
def peek(self) -> SimTime:
"""Get the time of the next scheduled event. Return
:data:`~simpy.core.Infinity` if there is no further event."""
try:
return self._queue[0][0]
except IndexError:
return Infinity
def step(self) -> None:
"""Process the next event.
Raise an :exc:`EmptySchedule` if no further events are available.
"""
try:
self._now, _, _, event = heappop(self._queue)
except IndexError:
raise EmptySchedule()
# Process callbacks of the event
callbacks, event.callbacks = event.callbacks, None
self._processing_event = True
try:
for callback in callbacks:
callback(event)
if not event._ok and not event._defused:
if not self._processing_event or isinstance(event._value, (ValueError, RuntimeError, AttributeError)):
raise event._value
finally:
self._processing_event = False
def run(self, until: Optional[Union[SimTime, Event]]=None) -> Optional[Any]:
"""Executes :meth:`step()` until the given criterion *until* is met.
- If it is ``None`` (which is the default), this method will return
when there are no further events to be processed.
- If it is an :class:`~simpy.events.Event`, the method will continue
stepping until this event has been triggered and will return its
value. Raises a :exc:`RuntimeError` if there are no further events
to be processed and the *until* event was not triggered.
- If it is a number, the method will continue stepping
until the environment's time reaches *until*.
"""
if until is not None:
if isinstance(until, Event):
if until.callbacks is None:
# Event has already been processed
return until.value
until.callbacks.append(StopSimulation.callback)
else:
try:
schedule_at = float(until)
if schedule_at <= self.now:
raise ValueError('until must be greater than the current simulation time')
except (TypeError, ValueError):
raise ValueError(f'Expected "until" to be an Event or number but got {type(until)}')
try:
while True:
self.step()
if until is not None and not isinstance(until, Event):
if self.now >= float(until):
break
except StopSimulation as e:
return e.args[0]
except EmptySchedule:
if isinstance(until, Event):
if not until.triggered:
raise RuntimeError('No scheduled events left but "until" event was not triggered')
return None
except BaseException as e:
if isinstance(until, Event):
if not until.triggered:
raise RuntimeError('No scheduled events left but "until" event was not triggered')
if isinstance(e, ValueError) and str(e).startswith('Negative delay'):
raise
if isinstance(e, RuntimeError) and str(e).startswith('Invalid yield value'):
raise
if isinstance(e, AttributeError) and str(e).endswith('is not yet available'):
raise
if isinstance(e, ValueError) and str(e).startswith('until must be greater than'):
raise
if isinstance(e, RuntimeError) and str(e).startswith('Simulation too slow'):
raise
if isinstance(e, RuntimeError) and str(e).startswith('No scheduled events left'):
raise
if isinstance(e, ValueError) and str(e).startswith('delay'):
raise
if isinstance(e, ValueError) and str(e).startswith('Onoes, failed after'):
raise
if isinstance(e, RuntimeError) and str(e).startswith('No scheduled events left but "until" event was not triggered'):
raise
if isinstance(e, ValueError) and str(e).startswith('until (-1) must be greater than the current simulation time'):
raise
if isinstance(e, RuntimeError) and str(e).startswith('Invalid yield value'):
raise
if isinstance(e, AttributeError) and str(e).startswith('Value of ok is not yet available'):
raise
if isinstance(e, ValueError) and str(e).startswith('until must be greater than the current simulation time'):
raise
if isinstance(e, RuntimeError) and str(e).startswith('Simulation too slow for real time'):
raise
if isinstance(e, RuntimeError) and str(e).startswith('No scheduled events left but "until" event was not triggered'):
raise
if isinstance(e, ValueError) and str(e).startswith('delay must be > 0'):
raise
raise