SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
Simulator.java
1/*
2 * Class: Simulator
3 * Description: Represents the executive of a discrete-event simulator
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
27import umontreal.ssj.simevents.eventlist.EventList;
28import umontreal.ssj.simevents.eventlist.SplayTree;
29
51public class Simulator {
52
53 protected double currentTime = 0.0;
54 // The current simulation time (clock).
55
56 protected EventList eventList;
57 // The list of future events.
58 // Can be changed by the method \texttt{init}.
59
60 protected boolean stopped = true;
61 // Becomes true when the simulation has ended
62 // (stopped has been called or the event list is empty).
63
64 protected boolean simulating = false;
65
66 protected ContinuousState continuousState = null;
67
75 public static Simulator defaultSimulator = null;
76
80 public Simulator() {
81 eventList = new SplayTree();
82 }
83
87 public Simulator(EventList eventList) {
88 if (eventList == null)
89 throw new NullPointerException();
90 this.eventList = eventList;
91 }
92
98 public double time() {
99 return currentTime;
100 }
101
106 public void init() {
107 // This has to be done another way in order to separate events and processes.
108// SimProcess.killAll();
109 currentTime = 0.0;
110 eventList.clear();
111 stopped = false;
112 simulating = false;
113 }
114
129 public void init(EventList evlist) {
130 if (evlist == null)
131 throw new NullPointerException();
132 eventList = evlist;
133 init(); // will clear the events in evlist
134 }
135
142 return eventList;
143 }
144
149 public boolean isSimulating() {
150 return simulating;
151 }
152
158 public boolean isStopped() {
159 return stopped;
160 }
161
169 if (stopped)
170 return null;
171 Event ev = eventList.removeFirst();
172 if (ev == null)
173 return null;
174 currentTime = ev.eventTime;
175 ev.eventTime = -10.0;
176 return ev;
177 }
178
183 public void start() {
184 if (eventList.isEmpty())
185 throw new IllegalStateException("start() called with an empty event list");
186 stopped = false;
187 simulating = true;
188 Event ev;
189 try {
190 while ((ev = removeFirstEvent()) != null && !stopped) {
191 // while (!stopped && (ev = eventList.removeFirst()) != null) {
192 // currentTime = ev.eventTime;
193 // ev.eventTime = -10.0;
194 ev.actions();
195 // if ev is a thread object associated to a process,
196 // the control will be transfered to this thread and the
197 // executive will be passivated in the actions() method.
198 }
199 } finally {
200 stopped = true;
201 simulating = false;
202 }
203 }
204
212 public void stop() {
213 stopped = true;
214 }
215
225 if (continuousState == null)
226 continuousState = new ContinuousState(this);
227 return continuousState;
228 }
229
233
242 if (defaultSimulator == null)
244 return defaultSimulator;
245 }
246
247}
248
Represents the portion of the simulator’s state associated with continuous-time simulation.
This abstract class provides event scheduling tools.
Definition Event.java:53
abstract void actions()
This is the method that is executed when this event occurs.
Event removeFirstEvent()
Removes the first event from the event list and sets the simulation clock to its event time.
void init(EventList evlist)
Same as init, but also sets evlist as the event list to be used.
EventList getEventList()
Gets the currently used event list.
void stop()
Tells the simulation executive to stop as soon as it takes control, and to return control to the prog...
ContinuousState continuousState()
Returns the current state of continuous variables being integrated during the simulation.
void start()
Starts the simulation executive.
void init()
Reinitializes the simulation executive by clearing up the event list, and resetting the simulation cl...
Simulator()
Constructs a new simulator using a splay tree for the event list.
boolean isSimulating()
Determines if this simulator is currently running, i.e., executing scheduled events.
static Simulator defaultSimulator
Represents the default simulator being used by the class Sim, and the no-argument constructor of Even...
static Simulator getDefaultSimulator()
Returns the default simulator instance used by the deprecated class.
double time()
Returns the current value of the simulation clock.
boolean isStopped()
Determines if this simulator was stopped by an event.
Simulator(EventList eventList)
Constructs a new simulator using eventList for the event list.
An implementation of EventList using a splay tree.
An interface for implementations of event lists.