|
| 1 | +/* |
| 2 | + * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne |
| 3 | + * and others contributors as indicated |
| 4 | + * by the @authors tag. All rights reserved. |
| 5 | + * See the copyright.txt in the distribution for a |
| 6 | + * full listing of individual contributors. |
| 7 | + * This copyrighted material is made available to anyone wishing to use, |
| 8 | + * modify, copy, or redistribute it subject to the terms and conditions |
| 9 | + * of the GNU Lesser General Public License, v. 2.1. |
| 10 | + * This program is distributed in the hope that it will be useful, but WITHOUT A |
| 11 | + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 12 | + * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 13 | + * You should have received a copy of the GNU Lesser General Public License, |
| 14 | + * v.2.1 along with this distribution; if not, write to the Free Software |
| 15 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 16 | + * MA 02110-1301, USA. |
| 17 | + * |
| 18 | + * (C) 1990-2008, |
| 19 | + */ |
| 20 | + |
| 21 | +/* |
| 22 | + * Copyright (C) 1996, 1997, 1998, |
| 23 | + * |
| 24 | + * Department of Computing Science, |
| 25 | + * The University, |
| 26 | + * Newcastle upon Tyne, |
| 27 | + * UK. |
| 28 | + * |
| 29 | + * $Id: Scheduler.java,v 1.3 1998/12/07 08:28:10 nmcl Exp $ |
| 30 | + */ |
| 31 | + |
| 32 | +package org.javasim; |
| 33 | + |
| 34 | +import java.util.NoSuchElementException; |
| 35 | + |
| 36 | +import org.javasim.internal.SimulationProcessIterator; |
| 37 | +import org.javasim.internal.SimulationProcessList; |
| 38 | + |
| 39 | +/** |
| 40 | + * This is the scheduler: the heart of the simulation system. |
| 41 | + * |
| 42 | + * Note: unlike in SIMULA, an active process is removed from the simulation |
| 43 | + * queue prior to being activated. |
| 44 | + * |
| 45 | + * @author marklittle |
| 46 | + * |
| 47 | + */ |
| 48 | +public class Simulation |
| 49 | +{ |
| 50 | + /** |
| 51 | + * This routine resets the simulation time to zero and removes all |
| 52 | + * entries from the scheduler queue (as their times may no longer |
| 53 | + * be valid). Whatever operation caused the processes to become |
| 54 | + * suspended will raise the RestartSimulation exception, which the |
| 55 | + * application should catch. It should then perform any work necessary |
| 56 | + * to put the process back in a state ready for restarting the simulation |
| 57 | + * before calling Cancel on the process. |
| 58 | + * |
| 59 | + * @throws SimulationException if an error occurs. |
| 60 | + */ |
| 61 | + |
| 62 | + public static synchronized void reset () throws SimulationException |
| 63 | + { |
| 64 | + Simulation._simulationReset = true; |
| 65 | + |
| 66 | + try |
| 67 | + { |
| 68 | + Scheduler.reset(); |
| 69 | + } |
| 70 | + finally |
| 71 | + { |
| 72 | + Simulation._simulationReset = false; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Is the simulation undergoing a reset? Processes should call this |
| 78 | + * method to determine whether the simulation is being reset. If it |
| 79 | + * is, then they should act accordingly. |
| 80 | + * |
| 81 | + * @return <code>true</code> if the simulation is being reset, <code>false</code> otherwise. |
| 82 | + */ |
| 83 | + |
| 84 | + public static synchronized boolean isReset () |
| 85 | + { |
| 86 | + return Simulation._simulationReset; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Stop the simulation. Processes should call this |
| 91 | + * method to determine whether the simulation is being stopped. If it |
| 92 | + * is, then they should act accordingly. |
| 93 | + */ |
| 94 | + |
| 95 | + public static synchronized void stop () |
| 96 | + { |
| 97 | + Simulation.schedulerRunning = false; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Start the simulation either from the start or from where it was |
| 102 | + * previously stopped. |
| 103 | + */ |
| 104 | + |
| 105 | + public static synchronized void start () |
| 106 | + { |
| 107 | + Simulation.schedulerRunning = true; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Has the simulation started? |
| 112 | + * |
| 113 | + * @return <code>true</code> if the simulation is running, <code>false</code> |
| 114 | + * otherwise. |
| 115 | + */ |
| 116 | + |
| 117 | + protected static synchronized boolean isStarted () |
| 118 | + { |
| 119 | + return Simulation.schedulerRunning; |
| 120 | + } |
| 121 | + |
| 122 | + private static boolean schedulerRunning = false; |
| 123 | + private static boolean _simulationReset = false; |
| 124 | +} |
0 commit comments