Commit 5a100535 authored by Stefan Schuhbaeck's avatar Stefan Schuhbaeck
Browse files

Add SimulationOutput class

SimulationOutput holds the scenario information of simulation runs.
parent 3225e84a
package org.vadere.simulator.projects;
import java.nio.file.Path;
/**
* Represents the directory holding a simulation output
*
* @author Stefan Schuhbäck
*/
public class SimulationOutput {
private Scenario simulatedScenario;private Path dir;
private boolean isDirty;
public SimulationOutput(Path directory, Scenario scenario){
this.dir = directory;
this.simulatedScenario = scenario;
this.isDirty = false;
}
public Scenario getSimulatedScenario() {
return simulatedScenario;
}
}
package org.vadere.simulator.projects.io;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.vadere.simulator.projects.Scenario;
import org.vadere.simulator.projects.SimulationOutput;
import org.vadere.simulator.projects.VadereProject;
import org.vadere.state.scenario.Agent;
import org.vadere.state.simulation.Step;
......@@ -185,6 +188,42 @@ public abstract class IOOutput {
}
}
/**
* Returns {@link SimulationOutput} if supplied directory is a valid output directory.
* @param project VadereProject
* @param directory Directory containing a simulated data
* @return SimulationOutput contained in selected directory
*/
public static Optional<SimulationOutput> getSimulationOutput(final VadereProject project, final File directory ){
Optional<Scenario> scenario = readOutputFile(project, directory);
Optional<Map<Step, List<Agent>>> trajectories = readTrajectories(project, directory);
if (scenario.isPresent() && trajectories.isPresent()){
return Optional.of(new SimulationOutput(directory.toPath(), scenario.get()));
} else {
//if directory is not a valid OutputDirectory
return Optional.empty();
}
}
/**
* Returns valid {@link SimulationOutput} of {@link VadereProject}
* @param project VadereProject
* @return All valid {@link SimulationOutput}s found in selected project.
*/
public static ConcurrentMap<String, SimulationOutput> getSimulationOutputs(final VadereProject project){
List<File> simOutDir = IOOutput.listAllDirs(project);
ConcurrentMap<String, SimulationOutput> simulationOutputs = new ConcurrentHashMap<>();
simOutDir.forEach( f -> {
Optional<Scenario> scenario = readOutputFile(project, f);
Optional<Map<Step, List<Agent>>> trajectories = readTrajectories(project, f);
if (scenario.isPresent() && trajectories.isPresent()){
SimulationOutput out = new SimulationOutput(f.toPath(), scenario.get());
simulationOutputs.put(f.getName(), out);
}
});
return simulationOutputs;
}
private static Optional<Scenario> readOutputFile(final VadereProject project, final File directory) {
try {
final Path pathToSnapshot = getPathToOutputFile(project, directory.getName(), IOUtils.SCENARIO_FILE_EXTENSION);
......
package org.vadere.simulator.projects.io;
import static org.junit.Assert.*;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;
import java.util.concurrent.ConcurrentMap;
import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.junit.Test;
import org.vadere.simulator.projects.SimulationOutput;
import org.vadere.simulator.projects.VadereProject;
/**
*
* @author Stefan Schuhbäck
*/
public class IOOutputTest {
private Path projectDir;
private Path corruptedOutput;
private VadereProject project;
@Before
public void setup() throws URISyntaxException, IOException {
projectDir = Paths.get(getClass().getResource("/data/simpleProject").toURI());
corruptedOutput = Paths.get(getClass().getResource("/data/corruptedOutput").toURI());
project = IOVadere.readProject(projectDir.toString());
}
@Test
public void getSimulationOutput() throws Exception {
String out1Dir = "output/test_postvis_2018-01-17_16-56-37.307";
Optional<SimulationOutput> out1 =
IOOutput.getSimulationOutput(project, projectDir.resolve(out1Dir).toFile());
assertTrue(out1.isPresent());
SimulationOutput simOut = out1.get();
assertEquals("test_postvis",simOut.getSimulatedScenario().getName());
String out2Dir = "output/corrupt/test_postvis_2018-01-19_13-38-01.695";
Optional<SimulationOutput> out2 =
IOOutput.getSimulationOutput(project, projectDir.resolve(out2Dir).toFile());
assertFalse(
"The selected Directory is corrupted and should not be a valid SimulationOutput",
out2.isPresent());
}
@Test
public void getSimulationOutputs() throws Exception {
ConcurrentMap<String, SimulationOutput> simOutputs;
simOutputs = IOOutput.getSimulationOutputs(project);
assertEquals("There should be 14 valid SimulationOutputs",14,simOutputs.size());
FileUtils.copyDirectory(corruptedOutput.toFile(), projectDir.resolve("output").toFile());
simOutputs = IOOutput.getSimulationOutputs(project);
assertEquals("There should be 14 valid SimulationOutputs",14,simOutputs.size());
File f = Paths.get(projectDir.toString(),"output/test_postvis_2018-01-19_13-38-11.666").toFile();
FileUtils.deleteDirectory(f);
}
}
\ No newline at end of file
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