📄 simulator.java
字号:
package simulator;import java.util.PriorityQueue;import util.Verbose;/** * A discrete event simulator event queue. * * This class is the core of a discrete event simulator. It contains a queue of * events, and is responsible for dispatching the events in sequential order. */public class Simulator extends Verbose{ private long evSeq = 1; private int maxSize = 0; private int sizeSeq = 0; private double timeNotifyStep = -1; private double lastTimeNotified = Double.MIN_VALUE; // Sort the queue based on event time private PriorityQueue<Event> globalEventQueue = new PriorityQueue<Event>(100, new Event.EventComparator()); private double currentTime = 0; /** Returns the current simulated time. */ public double getCurrentTime() { return currentTime; } /** * Schedules the {@link Event} <code>e</code> to be fired at some point in * the future. Throws {@line IllegalArgumentException} if the event's time * is in the past. * * @param e * the event to be scheduled. */ public void scheduleGlobalEvent(Event e) { if (e.getTime() < currentTime) { throw new IllegalArgumentException("Events must not have times in the past (current time: " + currentTime + " event time: " + e.getTime() + ")"); } assert (e.getTime() >= currentTime); e.setSeq(evSeq++); if (evSeq == Long.MAX_VALUE) { throw new RuntimeException("Event Sequence Number is too large!"); } if (vShouldLog(Verbose.DEBUG4)) vprint("SCHED_EVENT: " + e); assert !globalEventQueue.contains(e) : "Event to schedule is already in the queue!"; globalEventQueue.add(e); } /** Cancels the specified event. It must exist in the queue. */ public void cancelGlobalEvent(Event e) { boolean result = globalEventQueue.remove(e); assert !globalEventQueue.contains(e); if (!result) { throw new IllegalArgumentException("Event to cancel was not removed"); } } /** Fire the next event in the event queue. */ public void step() { if (globalEventQueue.isEmpty()) { throw new IllegalStateException("Attempted to step an empty simulator"); } Event e = globalEventQueue.poll(); // Move time forward to this event's time assert e.getTime() >= currentTime : "Scheduled event has time < currentTime!"; currentTime = e.getTime(); if (timeNotifyStep > 0 && lastTimeNotified + timeNotifyStep <= currentTime) { lastTimeNotified = currentTime; System.err.println("TIME: " + Double.toString(currentTime)); } int s = globalEventQueue.size(); if (s > maxSize) maxSize = s; if (++sizeSeq > 10000) { // System.out.println("Q-SIZE: " + s + "; MAX: " + maxSize); if (maxSize > 3000) { // System.out.println("QUEUE: " + queue); // System.err.println("QUEUE>3000"); } } if (vShouldLog(Verbose.DEBUG4)) vprint("FIRE_EVENT: " + e); // Fire the event e.fire(this); } /** Runs the simulation until the event queue is empty. */ public void simulate() { while (!globalEventQueue.isEmpty()) { step(); } } public void vprint(String str) { vprint(str, getCurrentTime()); } public void vprint(int lev, String str) { vprint(lev, str, getCurrentTime()); } public void setTimeNotifyStep(double time) { timeNotifyStep = time; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -