diff --git a/src/main/java/lu/jpt/csparqltest/SimulationContext.java b/src/main/java/lu/jpt/csparqltest/SimulationContext.java index 3d36552..bc849f4 100644 --- a/src/main/java/lu/jpt/csparqltest/SimulationContext.java +++ b/src/main/java/lu/jpt/csparqltest/SimulationContext.java @@ -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; } diff --git a/src/main/java/lu/jpt/csparqltest/gui/TextObserverWindow.java b/src/main/java/lu/jpt/csparqltest/gui/TextObserverWindow.java index 6552e01..95a3fba 100644 --- a/src/main/java/lu/jpt/csparqltest/gui/TextObserverWindow.java +++ b/src/main/java/lu/jpt/csparqltest/gui/TextObserverWindow.java @@ -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 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