The name of the initial branch for new projects is now "main" instead of "master". Existing projects remain unchanged. More information: https://doku.lrz.de/display/PUBLIC/GitLab

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

Remove ScenarioFinishedListener

parent 0043e842
...@@ -72,7 +72,7 @@ public class VadereConsole { ...@@ -72,7 +72,7 @@ public class VadereConsole {
try { try {
Scenario scenario = ScenarioFactory.createVadereWithProjectDirectory(projectDirectory, Scenario scenario = ScenarioFactory.createVadereWithProjectDirectory(projectDirectory,
vadereName + IOUtils.SCENARIO_FILE_EXTENSION, vadereName); vadereName + IOUtils.SCENARIO_FILE_EXTENSION, vadereName);
new ScenarioRun(scenario).run(); new ScenarioRun(scenario, null).run();
} catch (IOException e) { } catch (IOException e) {
logger.error(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 { ...@@ -37,18 +37,20 @@ public class ScenarioRun implements Runnable {
private final DataProcessingJsonManager dataProcessingJsonManager; private final DataProcessingJsonManager dataProcessingJsonManager;
private ScenarioFinishedListener finishedListener;
private Simulation simulation; private Simulation simulation;
private ProcessorManager processorManager; private ProcessorManager processorManager;
private final Scenario scenario; private final Scenario scenario;
private final ScenarioStore scenarioStore; // contained in scenario, but here for convenience 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.scenario = scenario;
this.scenarioStore = scenario.getScenarioStore(); this.scenarioStore = scenario.getScenarioStore();
this.dataProcessingJsonManager = new DataProcessingJsonManager(); 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.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 { ...@@ -61,9 +63,6 @@ public class ScenarioRun implements Runnable {
try { try {
logger.info(String.format("Initializing scenario. Start of scenario '%s'...", scenario.getName())); logger.info(String.format("Initializing scenario. Start of scenario '%s'...", scenario.getName()));
if (finishedListener != null)
this.finishedListener.scenarioStarted(scenario);
scenarioStore.topography.reset(); scenarioStore.topography.reset();
MainModelBuilder modelBuilder = new MainModelBuilder(scenarioStore); MainModelBuilder modelBuilder = new MainModelBuilder(scenarioStore);
...@@ -95,13 +94,11 @@ public class ScenarioRun implements Runnable { ...@@ -95,13 +94,11 @@ public class ScenarioRun implements Runnable {
public void simulationFailed(Throwable e) { public void simulationFailed(Throwable e) {
e.printStackTrace(); e.printStackTrace();
logger.error(e); logger.error(e);
if (finishedListener != null)
finishedListener.scenarioRunThrewException(scenario, e);
} }
protected void doAfterSimulation() { protected void doAfterSimulation() {
if (finishedListener != null) if (finishedListener != null)
finishedListener.scenarioFinished(scenario); finishedListener.finished(this);
logger.info(String.format("Simulation of scenario %s finished.", scenario.getName())); logger.info(String.format("Simulation of scenario %s finished.", scenario.getName()));
} }
...@@ -123,10 +120,6 @@ public class ScenarioRun implements Runnable { ...@@ -123,10 +120,6 @@ public class ScenarioRun implements Runnable {
} }
} }
public void setScenarioFinishedListener(ScenarioFinishedListener finishedListener) {
this.finishedListener = finishedListener;
}
public void pause() { public void pause() {
if (simulation != null) { // TODO throw an illegal state exception if simulation is not running if (simulation != null) { // TODO throw an illegal state exception if simulation is not running
simulation.pause(); simulation.pause();
......
...@@ -21,7 +21,7 @@ import java.util.stream.Collectors; ...@@ -21,7 +21,7 @@ import java.util.stream.Collectors;
* A VadereProject holds a list of {@link Scenario}s and functionality to manage them. * 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); private static Logger logger = LogManager.getLogger(VadereProject.class);
...@@ -94,8 +94,10 @@ public class VadereProject implements ScenarioFinishedListener { ...@@ -94,8 +94,10 @@ public class VadereProject implements ScenarioFinishedListener {
currentScenarioThread.setUncaughtExceptionHandler((t, ex) -> { currentScenarioThread.setUncaughtExceptionHandler((t, ex) -> {
currentScenarioRun.simulationFailed(ex); currentScenarioRun.simulationFailed(ex);
singleScenarioFinishedListener.forEach(l -> l.error(currentScenarioRun.getScenario(), scenariosLeft.size(), ex)); notifySimulationListenersSimulationError(currentScenarioRun.getScenario(), ex);
}); });
notifySimulationListenersSimulationStarted(getCurrentScenario());
currentScenarioThread.start(); currentScenarioThread.start();
} }
...@@ -103,22 +105,6 @@ public class VadereProject implements ScenarioFinishedListener { ...@@ -103,22 +105,6 @@ public class VadereProject implements ScenarioFinishedListener {
runScenarios(Collections.singleton(scenario)); 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() { private void notifyProjectListenerAboutPreRun() {
for (ProjectFinishedListener l : projectFinishedListener) { for (ProjectFinishedListener l : projectFinishedListener) {
l.preProjectRun(this); l.preProjectRun(this);
...@@ -131,15 +117,13 @@ public class VadereProject implements ScenarioFinishedListener { ...@@ -131,15 +117,13 @@ public class VadereProject implements ScenarioFinishedListener {
} }
} }
@Override private void notifySimulationListenersSimulationError(final Scenario scenario, final Throwable ex) {
public void scenarioRunThrewException(final Scenario scenario, final Throwable ex) {
for (SingleScenarioFinishedListener l : singleScenarioFinishedListener) { for (SingleScenarioFinishedListener l : singleScenarioFinishedListener) {
l.error(currentScenarioRun.getScenario(), scenariosLeft.size(), ex); l.error(currentScenarioRun.getScenario(), scenariosLeft.size(), ex);
} }
} }
@Override private void notifySimulationListenersSimulationStarted(final Scenario scenario) {
public void scenarioStarted(final Scenario scenario) {
for (SingleScenarioFinishedListener l : singleScenarioFinishedListener) { for (SingleScenarioFinishedListener l : singleScenarioFinishedListener) {
l.scenarioStarted(currentScenarioRun.getScenario(), scenariosLeft.size() + 1); l.scenarioStarted(currentScenarioRun.getScenario(), scenariosLeft.size() + 1);
} }
...@@ -150,12 +134,11 @@ public class VadereProject implements ScenarioFinishedListener { ...@@ -150,12 +134,11 @@ public class VadereProject implements ScenarioFinishedListener {
notifySingleScenarioFinishListener(nextScenario); notifySingleScenarioFinishListener(nextScenario);
final ScenarioRun scenarioRun = new ScenarioRun(nextScenario); final ScenarioRun scenarioRun = new ScenarioRun(nextScenario, scenarioFinishedListener);
scenarioRun.setOutputPaths(outputDirectory); scenarioRun.setOutputPaths(outputDirectory);
if (visualization != null) { if (visualization != null) {
scenarioRun.addPassiveCallback(visualization); scenarioRun.addPassiveCallback(visualization);
} }
scenarioRun.setScenarioFinishedListener(this);
return scenarioRun; return scenarioRun;
} }
...@@ -278,4 +261,21 @@ public class VadereProject implements ScenarioFinishedListener { ...@@ -278,4 +261,21 @@ public class VadereProject implements ScenarioFinishedListener {
public int[] getMigrationStats() { public int[] getMigrationStats() {
return migrationStats; 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