-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulator.java
More file actions
52 lines (48 loc) · 1.38 KB
/
Simulator.java
File metadata and controls
52 lines (48 loc) · 1.38 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
import java.util.PriorityQueue;
/**
* This class implements a discrete event simulator.
* The simulator maintains a priority queue of events.
* It runs through the events and simulates each one until
* the queue is empty.
*
* @author Wei Tsang
* @version CS2030S AY25/26 Semester 2
*/
public class Simulator {
/** The event queue. */
private final PriorityQueue<Event> events;
/**
* The constructor for a simulator. It takes in
* a simulation as an argument, and calls the
* getInitialEvents method of that simulation to
* initialize the event queue.
*
* @param simulation The simulation to simulate.
*/
public Simulator(Simulation simulation) {
this.events = new PriorityQueue<Event>();
for (Event e : simulation.getInitialEvents()) {
this.events.add(e);
}
}
/**
* Run the simulation until no more events is in
* the queue. For each event in the queue (in
* increasing order of time), print out its string
* representation, then simulate it. If the
* simulation returns one or more events, add them
* to the queue, and repeat.
*/
public void run() {
Event event = this.events.poll();
while (event != null) {
System.out.println(event);
Event[] newEvents = event.simulate();
for (Event e : newEvents) {
this.events.add(e);
}
event = this.events.poll();
}
return;
}
}