[TASK] Implement pause/resume of simulation.
This commit is contained in:
parent
ed8f1da58e
commit
1a47cc094e
|
@ -13,8 +13,9 @@ import lu.jpt.csparqltest.rentacar.RentACarSimulation;
|
||||||
|
|
||||||
public class SimulationContext {
|
public class SimulationContext {
|
||||||
|
|
||||||
// Internal simulation context
|
|
||||||
public enum SimulationState { UNINITIALIZED, INITIALIZED, RUNNING, PAUSED, SUSPENDED, TORNDOWN, ERROR};
|
public enum SimulationState { UNINITIALIZED, INITIALIZED, RUNNING, PAUSED, SUSPENDED, TORNDOWN, ERROR};
|
||||||
|
|
||||||
|
// Internal simulation context
|
||||||
private SimulationState currentState;
|
private SimulationState currentState;
|
||||||
|
|
||||||
// C-SPARQL related
|
// C-SPARQL related
|
||||||
|
@ -48,8 +49,7 @@ public class SimulationContext {
|
||||||
if(this.currentState != SimulationState.RUNNING) {
|
if(this.currentState != SimulationState.RUNNING) {
|
||||||
throw new RuntimeException("Action not allowed in this state!");
|
throw new RuntimeException("Action not allowed in this state!");
|
||||||
}
|
}
|
||||||
// TODO: Implement pausing the generatorThread(s)
|
this.simulation.pauseSimulation();
|
||||||
|
|
||||||
this.currentState = SimulationState.PAUSED;
|
this.currentState = SimulationState.PAUSED;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,8 +57,7 @@ public class SimulationContext {
|
||||||
if(this.currentState != SimulationState.PAUSED) {
|
if(this.currentState != SimulationState.PAUSED) {
|
||||||
throw new RuntimeException("Action not allowed in this state!");
|
throw new RuntimeException("Action not allowed in this state!");
|
||||||
}
|
}
|
||||||
// TODO: Implement resuming the generatorThread(s)
|
this.simulation.resumeSimulation();
|
||||||
|
|
||||||
this.currentState = SimulationState.RUNNING;
|
this.currentState = SimulationState.RUNNING;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -320,8 +320,8 @@ public class TextObserverWindow extends JFrame implements Observer {
|
||||||
}
|
}
|
||||||
Color[] result = new Color[sizeOfPalette];
|
Color[] result = new Color[sizeOfPalette];
|
||||||
float h = 0.0f;
|
float h = 0.0f;
|
||||||
float s = 0.75f;
|
float s = 0.8f;
|
||||||
float b = 0.75f;
|
float b = 0.65f;
|
||||||
for(int i=0; i<sizeOfPalette; i++) {
|
for(int i=0; i<sizeOfPalette; i++) {
|
||||||
h = (((float) i)/((float) sizeOfPalette));
|
h = (((float) i)/((float) sizeOfPalette));
|
||||||
result[i] = Color.getHSBColor(h, s, b);
|
result[i] = Color.getHSBColor(h, s, b);
|
||||||
|
|
|
@ -20,6 +20,7 @@ public class RentACarSimulation implements Runnable {
|
||||||
private List<Driver> drivers;
|
private List<Driver> drivers;
|
||||||
|
|
||||||
private volatile boolean runSimulation;
|
private volatile boolean runSimulation;
|
||||||
|
private volatile boolean pauseSimulation;
|
||||||
|
|
||||||
private RdfStream carStream;
|
private RdfStream carStream;
|
||||||
private RdfStream driverStream;
|
private RdfStream driverStream;
|
||||||
|
@ -41,6 +42,7 @@ public class RentACarSimulation implements Runnable {
|
||||||
this.driverStream = new WindowLoggingRdfStream(DRIVER_STREAM_IRI);
|
this.driverStream = new WindowLoggingRdfStream(DRIVER_STREAM_IRI);
|
||||||
// Green light for simulation
|
// Green light for simulation
|
||||||
this.runSimulation = true;
|
this.runSimulation = true;
|
||||||
|
this.pauseSimulation = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public RdfStream getCarStream() {
|
public RdfStream getCarStream() {
|
||||||
|
@ -58,6 +60,9 @@ public class RentACarSimulation implements Runnable {
|
||||||
public void run() {
|
public void run() {
|
||||||
// Run the simulation as long as needed
|
// Run the simulation as long as needed
|
||||||
while(this.runSimulation) {
|
while(this.runSimulation) {
|
||||||
|
if(this.pauseSimulation) {
|
||||||
|
this.waitForSimulationToResume();
|
||||||
|
}
|
||||||
// A driver will autonomously pick a car, use it for some time and leave it.
|
// A driver will autonomously pick a car, use it for some time and leave it.
|
||||||
for(Driver driver : this.drivers) {
|
for(Driver driver : this.drivers) {
|
||||||
driver.tick();
|
driver.tick();
|
||||||
|
@ -94,6 +99,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
|
* Tells the simulation to stop
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue