1"""
2Module provides a base class for events.
3"""
4from abc import ABC
5import math
6
7
[docs]
8class Event(ABC):
9 """
10 Base class for an event.
11
12 Parameters
13 ----------
14 start_time : `int`
15 Starting time (seconds since the simulation start) of this event.
16 end_time : `int`, optional
17 Time (seconds since the simulation start) when this event ends -- None if it never ends.
18
19 The default is None.
20 """
21 def __init__(self, start_time: int, end_time: int = None, **kwds):
22 if not isinstance(start_time, int) or start_time < 0:
23 raise ValueError("'start_time' must be a positive integer specifying the time " +
24 "at which this event starts.")
25 if end_time is not None and not isinstance(end_time, int):
26 raise ValueError("'end_time' must be either None or a positive integer specifiying " +
27 "the time at which this event ends.")
28 if end_time is not None:
29 if start_time >= end_time:
30 raise ValueError("'start_time' must be smaller than 'end_time'")
31
32 self.__start_time = start_time
33 self.__end_time = end_time if end_time is not None else math.inf
34
35 super().__init__(**kwds)
36
37 @property
38 def start_time(self) -> int:
39 """
40 Gets the start time (seconds since the simulation start) of this event.
41
42 Returns
43 -------
44 `int`
45 Start time of this event.
46 """
47 return self.__start_time
48
49 @property
50 def end_time(self) -> int:
51 """
52 Gets the end time (seconds since the simulation start) of this event.
53 float("inf") if it never ends.
54
55 Returns
56 -------
57 `int`
58 End time of this event.
59 """
60 return self.__end_time
61
[docs]
62 def get_attributes(self) -> dict:
63 """
64 Gets all attributes to be serialized -- these attributes are passed to the
65 constructor when the object is deserialized.
66
67 Returns
68 -------
69 `dict`
70 Dictionary of attributes -- i.e. pairs of attribute name and value.
71 """
72 return {"start_time": self.__start_time, "end_time": self.__end_time}
73
74 def __str__(self) -> str:
75 return f"start_time: {self.__start_time} end_time: {self.__end_time}"
76
77 def __eq__(self, other) -> bool:
78 if not isinstance(other, Event):
79 raise TypeError(f"Can not compare 'Event' instance with '{type(other)}' instance")
80
81 return self.__start_time == other.start_time and self.__end_time == other.end_time