/** Overridden version of the run method. Processes the simulation main loop. */ @Override public void run() { while (isStarted() && !terminate) { try { // synchronize on ui so that // we can prevent reset() etc // while half-way through a step synchronized (ui) { simulateOneStep(); ui.updateGUI(); } Thread.sleep(10); } catch (InterruptedException ie) { ie.printStackTrace(); } synchronized (this) { while (isPaused()) { try { this.wait(); } catch (InterruptedException ie) { ie.printStackTrace(); } } } } }
/** * Bi-directional pause. A call to this method will invert the current pausing state and update * the GUI to reflect this if the simulation is currently running. */ public static void pause() { if (isStarted()) { // if we're started. paused = !paused; if (ui != null) { ui.updateGUI(); } } }
/** * Performs necessary calculations that need to update the user interface after environment * variables have changed */ public static void settingsChanged() { if (ui == null) { // if we don't have a user interface reference ui = new UserInterface(); // make one. return; } reset(); ui.clearJunctionCache(); ui.updateGUI(); if (paused == true) { // if we were paused beforehand paused = false; // we are not paused anymore } }
/** Resets the simulation to default values. */ public static void reset() { // cannot stop until the current step // and rendering is complete synchronized (ui) { IJunction jn = (IJunction) getOption(JUNCTION_TYPE); jn.removeVehicles(); totalTimeSteps = (int) settings.get(TIME_STEP); setOption(TIME_STEP, 0); started = false; paused = false; ui.clearJunctionCache(); ui.updateGUI(); } }