[TASK] Implement pause/resume of simulation.

This commit is contained in:
Jan Philipp Timme 2016-10-02 20:12:18 +02:00
parent ed8f1da58e
commit 1a47cc094e
3 changed files with 28 additions and 7 deletions

View File

@ -13,8 +13,9 @@ import lu.jpt.csparqltest.rentacar.RentACarSimulation;
public class SimulationContext {
// Internal simulation context
public enum SimulationState { UNINITIALIZED, INITIALIZED, RUNNING, PAUSED, SUSPENDED, TORNDOWN, ERROR};
// Internal simulation context
private SimulationState currentState;
// C-SPARQL related
@ -48,8 +49,7 @@ public class SimulationContext {
if(this.currentState != SimulationState.RUNNING) {
throw new RuntimeException("Action not allowed in this state!");
}
// TODO: Implement pausing the generatorThread(s)
this.simulation.pauseSimulation();
this.currentState = SimulationState.PAUSED;
}
@ -57,8 +57,7 @@ public class SimulationContext {
if(this.currentState != SimulationState.PAUSED) {
throw new RuntimeException("Action not allowed in this state!");
}
// TODO: Implement resuming the generatorThread(s)
this.simulation.resumeSimulation();
this.currentState = SimulationState.RUNNING;
}

View File

@ -320,8 +320,8 @@ public class TextObserverWindow extends JFrame implements Observer {
}
Color[] result = new Color[sizeOfPalette];
float h = 0.0f;
float s = 0.75f;
float b = 0.75f;
float s = 0.8f;
float b = 0.65f;
for(int i=0; i<sizeOfPalette; i++) {
h = (((float) i)/((float) sizeOfPalette));
result[i] = Color.getHSBColor(h, s, b);

View File

@ -20,6 +20,7 @@ public class RentACarSimulation implements Runnable {
private List<Driver> drivers;
private volatile boolean runSimulation;
private volatile boolean pauseSimulation;
private RdfStream carStream;
private RdfStream driverStream;
@ -41,6 +42,7 @@ public class RentACarSimulation implements Runnable {
this.driverStream = new WindowLoggingRdfStream(DRIVER_STREAM_IRI);
// Green light for simulation
this.runSimulation = true;
this.pauseSimulation = false;
}
public RdfStream getCarStream() {
@ -58,6 +60,9 @@ public class RentACarSimulation implements Runnable {
public void run() {
// Run the simulation as long as needed
while(this.runSimulation) {
if(this.pauseSimulation) {
this.waitForSimulationToResume();
}
// A driver will autonomously pick a car, use it for some time and leave it.
for(Driver driver : this.drivers) {
driver.tick();
@ -93,6 +98,23 @@ public class RentACarSimulation implements Runnable {
}
}
}
private synchronized void waitForSimulationToResume() {
try {
while(this.pauseSimulation) this.wait();
} catch (InterruptedException e) {
// No matter what, just wait again.
}
}
public synchronized void pauseSimulation() {
this.pauseSimulation = true;
}
public synchronized void resumeSimulation() {
this.pauseSimulation = false;
this.notify();
}
/**
* Tells the simulation to stop