Skip to content

Commit cde25a5

Browse files
committed
Created Simulation class and refactored
#8
1 parent 48e1bbc commit cde25a5

8 files changed

Lines changed: 141 additions & 67 deletions

File tree

src/main/java/org/javasim/Scheduler.java

Lines changed: 2 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,10 @@ public static double currentTime ()
7070
* @throws SimulationException if an error occurs.
7171
*/
7272

73-
public static synchronized void reset () throws SimulationException
73+
static synchronized void reset () throws SimulationException
7474
{
7575
boolean finished = false;
7676
SimulationProcess tmp = SimulationProcess.current();
77-
78-
Scheduler._simulationReset = true;
7977

8078
// set resetting process to idle
8179

@@ -130,57 +128,10 @@ public static synchronized void reset () throws SimulationException
130128
} while (!finished);
131129

132130
Scheduler.SimulatedTime = 0.0;
133-
Scheduler._simulationReset = false;
134131

135132
SimulationProcess.Current = null;
136133
}
137134

138-
/**
139-
* Is the simulation undergoing a reset? Processes should call this
140-
* method to determine whether the simulation is being reset. If it
141-
* is, then they should act accordingly.
142-
*
143-
* @return <code>true</code> if the simulation is being reset, <code>false</code> otherwise.
144-
*/
145-
146-
public static synchronized boolean simulationReset ()
147-
{
148-
return Scheduler._simulationReset;
149-
}
150-
151-
/**
152-
* Stop the simulation. Processes should call this
153-
* method to determine whether the simulation is being stopped. If it
154-
* is, then they should act accordingly.
155-
*/
156-
157-
public static synchronized void stopSimulation ()
158-
{
159-
Scheduler.schedulerRunning = false;
160-
}
161-
162-
/**
163-
* Start the simulation either from the start or from where it was
164-
* previously stopped.
165-
*/
166-
167-
public static synchronized void startSimulation ()
168-
{
169-
Scheduler.schedulerRunning = true;
170-
}
171-
172-
/**
173-
* Has the simulation started?
174-
*
175-
* @return <code>true</code> if the simulation is running, <code>false</code>
176-
* otherwise.
177-
*/
178-
179-
protected static synchronized boolean simulationStarted ()
180-
{
181-
return Scheduler.schedulerRunning;
182-
}
183-
184135
private Scheduler ()
185136
{
186137
}
@@ -194,7 +145,7 @@ private Scheduler ()
194145

195146
static synchronized boolean schedule () throws SimulationException
196147
{
197-
if (Scheduler.simulationStarted())
148+
if (Simulation.isStarted())
198149
{
199150
SimulationProcess p = SimulationProcess.current();
200151

@@ -256,9 +207,6 @@ static double getSimulationTime ()
256207

257208
private static double SimulatedTime = 0.0;
258209
private static SimulationProcessList ReadyQueue = new SimulationProcessList();
259-
260-
private static boolean schedulerRunning = false;
261-
private static boolean _simulationReset = false;
262210

263211
static Scheduler theScheduler = new Scheduler();
264212
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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+
}

src/main/java/org/javasim/SimulationProcess.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ protected void suspendProcess () throws RestartException
553553
{
554554
}
555555

556-
if (Scheduler.simulationReset())
556+
if (Simulation.isReset())
557557
throw new RestartException();
558558
}
559559

src/test/java/org/javasim/examples/basic/BasicExampleUnitTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
package org.javasim.examples.basic;
2222

23-
import org.javasim.Scheduler;
23+
import org.javasim.Simulation;
2424
import org.junit.Test;
2525

2626
import static org.junit.Assert.*;
@@ -38,7 +38,7 @@ public void testNoBreaks ()
3838

3939
try
4040
{
41-
Scheduler.reset();
41+
Simulation.reset();
4242
}
4343
catch (final Throwable ex)
4444
{
@@ -56,7 +56,7 @@ public void testBreaks ()
5656

5757
try
5858
{
59-
Scheduler.reset();
59+
Simulation.reset();
6060
}
6161
catch (final Throwable ex)
6262
{

src/test/java/org/javasim/examples/basic/MachineShop.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import org.javasim.RestartException;
2424
import org.javasim.Scheduler;
25+
import org.javasim.Simulation;
2526
import org.javasim.SimulationException;
2627
import org.javasim.SimulationProcess;
2728

@@ -57,7 +58,7 @@ public void run ()
5758
B.activate();
5859
}
5960

60-
Scheduler.startSimulation();
61+
Simulation.start();
6162

6263
while (MachineShop.ProcessedJobs < 1000)
6364
hold(1000);
@@ -77,7 +78,7 @@ public void run ()
7778
System.out.println("Average number of jobs present = "
7879
+ (JobsInQueue / CheckFreq));
7980

80-
Scheduler.stopSimulation();
81+
Simulation.stop();
8182

8283
A.terminate();
8384
MachineShop.M.terminate();

src/test/java/org/javasim/examples/interrupt/InterruptExampleUnitTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
package org.javasim.examples.interrupt;
2222

23-
import org.javasim.Scheduler;
23+
import org.javasim.Simulation;
2424

2525
import org.junit.Test;
2626

@@ -38,7 +38,7 @@ public void test ()
3838

3939
try
4040
{
41-
Scheduler.reset();
41+
Simulation.reset();
4242
}
4343
catch (final Throwable ex)
4444
{

src/test/java/org/javasim/examples/interrupt/MachineShop.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import org.javasim.RestartException;
2424
import org.javasim.Scheduler;
25+
import org.javasim.Simulation;
2526
import org.javasim.SimulationEntity;
2627
import org.javasim.SimulationException;
2728
import org.javasim.SimulationProcess;
@@ -45,14 +46,14 @@ public void run ()
4546
A.activate();
4647
s.activate();
4748

48-
Scheduler.startSimulation();
49+
Simulation.start();
4950

5051
waitFor(cpu);
5152

5253
System.out.println("Total jobs processed " + ProcessedJobs);
5354
System.out.println("Total signals processed " + SignalledJobs);
5455

55-
Scheduler.stopSimulation();
56+
Simulation.stop();
5657

5758
MachineShop.cpu.terminate();
5859
A.terminate();

src/test/java/org/javasim/tests/SimulationProcessUnitTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
package org.javasim.tests;
2222

2323
import org.javasim.RestartException;
24-
import org.javasim.Scheduler;
24+
import org.javasim.Simulation;
2525
import org.javasim.SimulationException;
2626
import org.javasim.SimulationProcess;
2727
import org.javasim.streams.ExponentialStream;
@@ -64,11 +64,11 @@ public void run ()
6464

6565
A.activateDelay(2000);
6666

67-
Scheduler.startSimulation();
67+
Simulation.start();
6868

6969
hold(1000);
7070

71-
Scheduler.stopSimulation();
71+
Simulation.stop();
7272

7373
A.terminate();
7474

0 commit comments

Comments
 (0)