SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
Event.java
1/*
2 * Class: Event
3 * Description: provides event scheduling tools
4 * Environment: Java
5 * Software: SSJ
6 * Copyright (C) 2001 Pierre L'Ecuyer and Universite de Montreal
7 * Organization: DIRO, Universite de Montreal
8 * @author
9 * @since
10 *
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 *
24 */
25package umontreal.ssj.simevents;
26
53public abstract class Event implements Comparable<Event> {
54
55 protected Simulator sim;
56 // simulator linked with the current event
57
58 protected double priority;
59 // priority of the event. Priority is a second parameter (after eventTime)
60 // used to class events for their running order, in the EventList.
61
62 protected double eventTime;
63 // Planned time of occurence of this event. Negative if not planned.
64 // Is protected because it is used (changed) in Process.
65
66 // Replace that with instanceof simProcess to completely detach processes.
67 // protected boolean isProcess = false;
68 // Will be true for objects of the subclass Process of the class Event.
69 // (i.e., true if this event is a process.)
70
71 private int myra = 0;
72 // A new event must always occur after those with same time and
73 // same priority in the Event list. myra is used for that in
74 // SplayTree.java.
75
76 // For internal use
77 public final int getRa() {
78 return myra;
79 }
80
81 public final void setRa(int r) {
82 myra = r;
83 }
84
126 * @param delay simulation time that must pass before the event happens
127 */
128 public Event() {
130 }
131
135 * @param sim Instance of class Simulator associated with the new Event
136 */
137 public Event(Simulator sim) {
138 if (sim == null)
139 throw new NullPointerException();
140 eventTime = -10.0;
141 priority = 1.0;
142 this.sim = sim;
143 }
144
153 * @param delay simulation time that must pass before the event happens
154 */
155 public void schedule(double delay) {
156 if (delay < 0.0)
157 throw new IllegalArgumentException("Cannot schedule in the past.");
158 if (eventTime > -1.0)
159 throw new IllegalStateException("Event already scheduled");
160 eventTime = sim.time() + delay;
161 sim.eventList.add(this);
162 }
163
166 * at the current time (as the next event).
167 */
168 public void scheduleNext() {
169 if (eventTime > -1.0)
170 throw new IllegalStateException("Event already scheduled");
171 eventTime = sim.time();
172 priority = 0.0;
173 sim.eventList.addFirst(this);
174 }
175
188 * @param other event before which this event will be scheduled
189 */
190 public void scheduleBefore(Event other) {
191 if (eventTime > -1.0)
192 throw new IllegalStateException("Event already scheduled");
193 eventTime = other.eventTime;
194 priority = other.priority;
195 sim.eventList.addBefore(this, other);
196 }
197
202 * @param other event after which this event will be scheduled
203 */
204 public void scheduleAfter(Event other) {
205 if (eventTime > -1.0)
206 throw new IllegalStateException("Event already scheduled");
207 eventTime = other.eventTime;
208 priority = other.priority;
209 sim.eventList.addAfter(this, other);
210 }
211
215 * @param delay simulation time units that must elapse before the event happens
216 */
217 public void reschedule(double delay) {
218 if (delay < 0.0)
219 throw new IllegalArgumentException("Cannot schedule in the past.");
220 if (eventTime < -1.0)
221 throw new IllegalStateException("Event not scheduled");
222 sim.getEventList().remove(this);
223 eventTime = sim.time() + delay;
224 sim.getEventList().add(this);
225 }
226
231 * @return `true` if the event could be cancelled
232 */
233 public boolean cancel() {
234 boolean removed = false;
235 if (eventTime >= sim.time())
236 removed = sim.getEventList().remove(this);
237 eventTime = -10.0;
238 return removed;
239 }
240
246 * @return `true` if an event of this class was found and cancelled
247 */
248 public final boolean cancel(String type) {
249 Event ev = sim.getEventList().getFirstOfClass(type);
250 return ev.cancel();
251 }
252
256 * @return the simulator linked to the event
257 */
258 public final Simulator simulator() {
259 return sim;
260 }
261
266 * @param sim the Simulator
267 */
268 public final void setSimulator(Simulator sim) {
269 if (sim == null)
270 throw new NullPointerException();
271 if (eventTime > -1.0)
272 throw new UnsupportedOperationException("Unable to set Simulator, current Event already scheduled");
273 this.sim = sim;
274 }
275
279 * @return the time of occurence of the event
280 */
281 public final double time() {
282 return eventTime;
283 }
284
290 * @param time new time of occurence for the event
291 */
292 public final void setTime(double time) {
293 if (eventTime > -1.0)
294 throw new UnsupportedOperationException("Unable to set time, current Event already scheduled");
295 eventTime = time;
296 }
297
301 * @return the priority of the event
302 */
303 public final double priority() {
304 return priority;
305 }
306
312 * @param priority new priority for the event
313 */
314 public final void setPriority(double priority) {
315 if (eventTime > -1.0)
316 throw new UnsupportedOperationException("Unable to set priority, current Event already scheduled");
317 this.priority = priority;
318 }
319
325 * equal, or larger priority than event `e`.
326 */
327 public int compareTo(Event e) {
328 if (eventTime < e.time())
329 return -1;
330 if (eventTime > e.time())
331 return 1;
332 // Si le moment de declenchement des "Event" est identique, on
333 // examine leurs priorites.
334 if (priority < e.priority())
335 return -1;
336 if (priority > e.priority())
337 return 1;
338 return 0;
339 }
340
346 public abstract void actions();
347
348}
boolean cancel()
Cancels this event before it occurs.
Definition Event.java:231
void scheduleNext()
Schedules this event as the first event in the event list, to be executed at the current time (as the...
Definition Event.java:166
void scheduleAfter(Event other)
Schedules this event to happen just after, and at the same time, as the event other.
Definition Event.java:202
void reschedule(double delay)
Cancels this event and reschedules it to happen in delay time units.
Definition Event.java:215
final void setPriority(double priority)
Sets the priority of this event to inPriority.
Definition Event.java:312
Event()
Constructs a new event instance, which can be placed afterwards into the event list of the default si...
Definition Event.java:126
final void setSimulator(Simulator sim)
Sets the simulator associated with this event to sim.
Definition Event.java:266
abstract void actions()
This is the method that is executed when this event occurs.
int compareTo(Event e)
Compares this object with the specified object e for order.
Definition Event.java:325
void schedule(double delay)
Schedules this event to happen in delay time units, i.e., at time sim.time() + delay,...
Definition Event.java:153
final Simulator simulator()
Returns the simulator linked to this event.
Definition Event.java:256
void scheduleBefore(Event other)
Schedules this event to happen just before, and at the same time, as the event other.
Definition Event.java:188
final double time()
Returns the (planned) time of occurence of this event.
Definition Event.java:279
final double priority()
Returns the priority of this event.
Definition Event.java:301
final void setTime(double time)
Sets the (planned) time of occurence of this event to time.
Definition Event.java:290
Represents the executive of a discrete-event simulator.
static Simulator getDefaultSimulator()
Returns the default simulator instance used by the deprecated class.