Commit 2925602b authored by Jakob Schöttl's avatar Jakob Schöttl

Remove ScenarioFinishedListener

parent 0043e842
......@@ -72,7 +72,7 @@ public class VadereConsole {
try {
Scenario scenario = ScenarioFactory.createVadereWithProjectDirectory(projectDirectory,
vadereName + IOUtils.SCENARIO_FILE_EXTENSION, vadereName);
new ScenarioRun(scenario).run();
new ScenarioRun(scenario, null).run();
} catch (IOException e) {
logger.error(e);
}
......
package org.vadere.simulator.projects;
/**
* Used to notify the caller when a Runnable finishes its {@code run} method.
* This is useful when we want to want to do something when a thread finishes.
*
* @author Jakob Schöttl
*
*/
public interface RunnableFinishedListener {
void finished(Runnable runnable);
}
package org.vadere.simulator.projects;
public interface ScenarioFinishedListener {
void scenarioStarted(final Scenario scenario);
void scenarioFinished(final Scenario scenario);
void scenarioRunThrewException(final Scenario scenario, final Throwable ex);
}
......@@ -37,18 +37,20 @@ public class ScenarioRun implements Runnable {
private final DataProcessingJsonManager dataProcessingJsonManager;
private ScenarioFinishedListener finishedListener;
private Simulation simulation;
private ProcessorManager processorManager;
private final Scenario scenario;
private final ScenarioStore scenarioStore; // contained in scenario, but here for convenience
public ScenarioRun(final Scenario scenario) {
private final RunnableFinishedListener finishedListener;
public ScenarioRun(final Scenario scenario, RunnableFinishedListener scenarioFinishedListener) {
this.scenario = scenario;
this.scenarioStore = scenario.getScenarioStore();
this.dataProcessingJsonManager = new DataProcessingJsonManager();
this.setOutputPaths(Paths.get(IOUtils.OUTPUT_DIR)); // TODO [priority=high] [task=bugfix] [Error?] this is a relative path. If you start the application via eclipse this will be VadereParent/output
this.finishedListener = scenarioFinishedListener;
}
/**
......@@ -61,9 +63,6 @@ public class ScenarioRun implements Runnable {
try {
logger.info(String.format("Initializing scenario. Start of scenario '%s'...", scenario.getName()));
if (finishedListener != null)
this.finishedListener.scenarioStarted(scenario);
scenarioStore.topography.reset();
MainModelBuilder modelBuilder = new MainModelBuilder(scenarioStore);
......@@ -95,13 +94,11 @@ public class ScenarioRun implements Runnable {
public void simulationFailed(Throwable e) {
e.printStackTrace();
logger.error(e);
if (finishedListener != null)
finishedListener.scenarioRunThrewException(scenario, e);
}
protected void doAfterSimulation() {
if (finishedListener != null)
finishedListener.scenarioFinished(scenario);
finishedListener.finished(this);
logger.info(String.format("Simulation of scenario %s finished.", scenario.getName()));
}
......@@ -123,10 +120,6 @@ public class ScenarioRun implements Runnable {
}
}
public void setScenarioFinishedListener(ScenarioFinishedListener finishedListener) {
this.finishedListener = finishedListener;
}
public void pause() {
if (simulation != null) { // TODO throw an illegal state exception if simulation is not running
simulation.pause();
......
......@@ -21,7 +21,7 @@ import java.util.stream.Collectors;
* A VadereProject holds a list of {@link Scenario}s and functionality to manage them.
*
*/
public class VadereProject implements ScenarioFinishedListener {
public class VadereProject {
private static Logger logger = LogManager.getLogger(VadereProject.class);
......@@ -94,8 +94,10 @@ public class VadereProject implements ScenarioFinishedListener {
currentScenarioThread.setUncaughtExceptionHandler((t, ex) -> {
currentScenarioRun.simulationFailed(ex);
singleScenarioFinishedListener.forEach(l -> l.error(currentScenarioRun.getScenario(), scenariosLeft.size(), ex));
notifySimulationListenersSimulationError(currentScenarioRun.getScenario(), ex);
});
notifySimulationListenersSimulationStarted(getCurrentScenario());
currentScenarioThread.start();
}
......@@ -103,22 +105,6 @@ public class VadereProject implements ScenarioFinishedListener {
runScenarios(Collections.singleton(scenario));
}
/**
* Calls the next scenario if available.
*/
@Override
public void scenarioFinished(final Scenario scenario) {
notifyScenarioRMListenerAboutPostRun(scenario);
if (scenariosLeft.isEmpty()) {
for (ProjectFinishedListener listener : projectFinishedListener) {
listener.postProjectRun(this);
}
} else {
prepareAndStartScenarioRunThread();
}
}
private void notifyProjectListenerAboutPreRun() {
for (ProjectFinishedListener l : projectFinishedListener) {
l.preProjectRun(this);
......@@ -131,15 +117,13 @@ public class VadereProject implements ScenarioFinishedListener {
}
}
@Override
public void scenarioRunThrewException(final Scenario scenario, final Throwable ex) {
private void notifySimulationListenersSimulationError(final Scenario scenario, final Throwable ex) {
for (SingleScenarioFinishedListener l : singleScenarioFinishedListener) {
l.error(currentScenarioRun.getScenario(), scenariosLeft.size(), ex);
}
}
@Override
public void scenarioStarted(final Scenario scenario) {
private void notifySimulationListenersSimulationStarted(final Scenario scenario) {
for (SingleScenarioFinishedListener l : singleScenarioFinishedListener) {
l.scenarioStarted(currentScenarioRun.getScenario(), scenariosLeft.size() + 1);
}
......@@ -150,12 +134,11 @@ public class VadereProject implements ScenarioFinishedListener {
notifySingleScenarioFinishListener(nextScenario);
final ScenarioRun scenarioRun = new ScenarioRun(nextScenario);
final ScenarioRun scenarioRun = new ScenarioRun(nextScenario, scenarioFinishedListener);
scenarioRun.setOutputPaths(outputDirectory);
if (visualization != null) {
scenarioRun.addPassiveCallback(visualization);
}
scenarioRun.setScenarioFinishedListener(this);
return scenarioRun;
}
......@@ -278,4 +261,21 @@ public class VadereProject implements ScenarioFinishedListener {
public int[] getMigrationStats() {
return migrationStats;
}
/** Starts the next simulation if any. */
private RunnableFinishedListener scenarioFinishedListener = new RunnableFinishedListener() {
@Override
public void finished(Runnable runnable) {
notifyScenarioRMListenerAboutPostRun(getCurrentScenario());
if (scenariosLeft.isEmpty()) {
for (ProjectFinishedListener listener : projectFinishedListener) {
listener.postProjectRun(VadereProject.this);
}
} else {
prepareAndStartScenarioRunThread();
}
}
};
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment