Store every output in the same folder. Scenario snapshots and trajectories are...

Store every output in the same folder. Scenario snapshots and trajectories are handled by new processors. Adjust the TrajectoryReader.
parent 6b5868b9
......@@ -18,8 +18,12 @@
"processors": [ 11, 12 ]
}, {
"type": "org.vadere.simulator.projects.dataprocessing_mtp.NoDataKeyOutputFile",
"filename": "scenario_snapshot.txt",
"processors": [18]
"filename": "snapshot.scenario",
"processors": [ 18 ]
}, {
"type": "org.vadere.simulator.projects.dataprocessing_mtp.TimestepPedestrianIdOutputFile",
"filename": "test.trajectories",
"processors": [ 1, 19 ]
} ],
"processors": [ {
"type": "org.vadere.simulator.projects.dataprocessing_mtp.PedestrianPositionProcessor",
......@@ -75,6 +79,12 @@
}, {
"type": "org.vadere.simulator.projects.dataprocessing_mtp.SnapshotProcessor",
"id": 18
}, {
"type": "org.vadere.simulator.projects.dataprocessing_mtp.PedestrianTargetIdProcessor",
"id": 19
}, {
"type": "org.vadere.simulator.projects.dataprocessing_mtp.PedestrianSourceIdProcessor",
"id": 20
} ],
"attributes": {
"org.vadere.simulator.projects.dataprocessing_mtp.AttributesVelocityProcessor": {
......
......@@ -7,27 +7,20 @@ import org.vadere.simulator.control.Simulation;
import org.vadere.simulator.models.MainModel;
import org.vadere.simulator.models.ModelBuilder;
import org.vadere.simulator.projects.dataprocessing.processors.ModelTest;
import org.vadere.simulator.projects.dataprocessing.processors.PedestrianPositionProcessor;
import org.vadere.simulator.projects.dataprocessing.processors.SnapshotOutputProcessor;
import org.vadere.simulator.projects.dataprocessing.writer.ProcessorWriter;
import org.vadere.simulator.projects.dataprocessing_mtp.DataProcessingJsonManager;
import org.vadere.simulator.projects.dataprocessing_mtp.ProcessorManager;
import org.vadere.simulator.projects.io.JsonConverter;
import org.vadere.state.attributes.Attributes;
import org.vadere.state.attributes.AttributesSimulation;
import org.vadere.state.attributes.processors.AttributesPedestrianPositionProcessor;
import org.vadere.state.attributes.processors.AttributesWriter;
import org.vadere.state.attributes.scenario.AttributesAgent;
import org.vadere.state.scenario.Topography;
import org.vadere.util.io.IOUtils;
import org.vadere.util.reflection.VadereClassNotFoundException;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
......@@ -81,8 +74,8 @@ public class ScenarioRunManager implements Runnable {
this.modelTests = new LinkedList<>();
this.writers = new LinkedList<>();
this.scenarioStore = store;
this.outputPath = 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.processorOutputPath = Paths.get(IOUtils.DATAPROCESSING_DIR);
this.setOutputPaths(Paths.get(IOUtils.OUTPUT_DIR), Paths.get(IOUtils.DATAPROCESSING_DIR)); // TODO [priority=high] [task=bugfix] [Error?] this is a relative path. If you start the application via eclipse this will be VadereParent/output
}
public void saveChanges() { // get's called by VadereProject.saveChanges on init
......@@ -243,8 +236,8 @@ public class ScenarioRunManager implements Runnable {
}
public void setOutputPaths(final Path outputPath, final Path processedOutputPath) {
this.outputPath = outputPath;
this.processorOutputPath = processedOutputPath;
String dateString = new SimpleDateFormat(IOUtils.DATE_FORMAT).format(new Date());
this.outputPath = Paths.get(outputPath.toString(), String.format("%s_%s", this.getName(), dateString));
}
public void setName(String name) {
......@@ -296,58 +289,7 @@ public class ScenarioRunManager implements Runnable {
// Output stuff...
private void prepareOutput() {
if (getAttributesSimulation().isWriteSimulationData()) {
DateFormat format = new SimpleDateFormat(IOUtils.DATE_FORMAT);
writers.clear();
int writerCounter = 0; // needed to distinguish writers with the same name
String dateString = format.format(new Date());
String dirName = String.format("%s_%s", this.getName(), dateString);
ProcessorWriter snapshotWriter = new ProcessorWriter(new SnapshotOutputProcessor(), new AttributesWriter());
ProcessorWriter trajectoryWriter = new ProcessorWriter(
new PedestrianPositionProcessor(new AttributesPedestrianPositionProcessor(true)),
new AttributesWriter());
String snapshotFileName =
String.format("%s%s", getName(), snapshotWriter.getProcessor().getFileExtension());
snapshotFileName = IOUtils.getPath(outputPath.resolve(dirName).toString(), snapshotFileName).toString();
String trajectoyFileName =
String.format("%s%s", getName(), trajectoryWriter.getProcessor().getFileExtension());
trajectoyFileName = IOUtils.getPath(outputPath.resolve(dirName).toString(), trajectoyFileName).toString();
try {
snapshotWriter.setOutputStream(new FileOutputStream(snapshotFileName, false));
snapshotWriter.setWriteHeader(false);
trajectoryWriter.setOutputStream(new FileOutputStream(trajectoyFileName, false));
trajectoryWriter.setWriteHeader(true);
writers.add(snapshotWriter);
writers.add(trajectoryWriter);
} catch (IOException e) {
logger.error(e);
}
for (ProcessorWriter writer : processorWriters) {
Path processorOutputPath = null;
processorOutputPath = this.processorOutputPath;
String filename;
if (simpleOutputProcessorName) {
filename = String.format("%s%s", this.getName(), writer.getProcessor().getFileExtension());
} else {
filename = String.format("%s_%s_%d_%s%s", this.getName(), writer.getProcessor().getName(),
(writerCounter++), dateString, writer.getProcessor().getFileExtension());
}
String procFileName = IOUtils.getPath(processorOutputPath.toString(), filename).toString();
try {
writer.setOutputStream(new FileOutputStream(procFileName, false));
} catch (FileNotFoundException e) {
logger.error(e);
}
writers.add(writer);
}
// New processors
this.processorManager.setOutputPath(this.processorOutputPath.toString());
}
this.processorManager.setOutputPath(this.outputPath.toString());
}
@Override
......
......@@ -7,8 +7,6 @@ import org.vadere.simulator.control.SimulationState;
import org.vadere.simulator.models.Model;
import org.vadere.util.io.IOUtils;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
......@@ -71,8 +69,7 @@ public class ProcessorManager {
}
public void setOutputPath(String directory) {
String dateString = new SimpleDateFormat(IOUtils.DATE_FORMAT).format(new Date());
this.outputFiles.forEach(file -> file.setFileName(IOUtils.getPath(directory, String.format("%s_%s", dateString, file.getFileName())).toString()));
this.outputFiles.forEach(file -> file.setFileName(IOUtils.getPath(directory, String.format("%s", file.getFileName())).toString()));
}
public void writeOutput() {
......
package org.vadere.simulator.projects.io;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import org.apache.commons.math3.util.Pair;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.vadere.simulator.projects.ScenarioRunManager;
......@@ -21,9 +10,18 @@ import org.vadere.state.scenario.Agent;
import org.vadere.state.scenario.Pedestrian;
import org.vadere.state.simulation.Step;
import org.vadere.util.data.Row;
import org.vadere.util.data.Table;
import org.vadere.util.data.Tupel;
import org.vadere.util.io.TableReader;
import org.vadere.util.geometry.shapes.VPoint;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Random;
import java.util.stream.Collectors;
/**
* A TrajectoryReader is the counterpart of the
......@@ -52,101 +50,23 @@ public class TrajectoryReader {
}
public Map<Step, List<Agent>> readFile() throws IOException {
Map<Step, List<Agent>> pedestrianByStep;
TableReader tableReader = new TableReader();
String[] headlines;
try (Stream<String> lines = Files.lines(trajectoryFilePath)) {
headlines = tableReader.readHeadLine(lines);
}
Table table;
try (Stream<String> lines = Files.lines(trajectoryFilePath)) {
table = tableReader.readTable(lines, headlines);
}
Stream<Row> rowStream =
StreamSupport.stream(Spliterators.spliteratorUnknownSize(table.iterator(), Spliterator.ORDERED), false);
Stream<Tupel<Step, Agent>> tupelStream =
rowStream.map(row -> rowToTupel(row)).filter(tupel -> tupel.isPresent()).map(tupel -> tupel.get());
pedestrianByStep = tupelStream.collect(
Collectors.groupingBy(tupel -> tupel.v1, Collectors.mapping(tupel -> tupel.v2, Collectors.toList())));
return pedestrianByStep;
}
return Files.lines(this.trajectoryFilePath)
.skip(1) // Skip header line
.map(line -> line.split(" "))
.map(cells -> {
int step = Integer.parseInt(cells[0]);
int pedestrianId = Integer.parseInt(cells[1]);
VPoint pos = new VPoint(Double.parseDouble(cells[2]), Double.parseDouble(cells[3]));
int targetId = Integer.parseInt(cells[4]);
Pedestrian ped = new Pedestrian(new AttributesAgent(this.attributesPedestrian, pedestrianId), new Random());
ped.setPosition(pos);
LinkedList<Integer> targets = new LinkedList<Integer>();
targets.addFirst(targetId);
ped.setTargets(targets);
private Optional<Tupel<Step, Agent>> rowToTupel(final Row row) {
try {
Agent ped = PedestrianPositionProcessor.rowToPedestrian(row, attributesPedestrian);
Step step = PedestrianPositionProcessor.rowToStep(row);
return Optional.of(Tupel.of(step, ped));
} catch (NumberFormatException | NullPointerException e) {
logger.warn("could not parse row " + row + " into a step or pedestrian");
return Optional.empty();
}
return Pair.create(new Step(Integer.parseInt(cells[0])), ped);
})
.collect(Collectors.groupingBy(pair -> pair.getKey(), Collectors.mapping(pair -> pair.getValue(), Collectors.toList())));
}
/*
* private static Step rowToStep(final Row row) {
* return new Step(Integer.parseInt(row.getEntry("step").toString()),
* Double.parseDouble(row.getEntry("time").toString()));
* }
*
* private Pedestrian rowToPedestrian(final Row row) {
* Pedestrian pedestrian = new Pedestrian(new AttributesPedestrian(attributesPedestrian,
* Integer.parseInt(row.getEntry("id").toString())), new Random());
* pedestrian.setPosition(new VPoint(Double.parseDouble(row.getEntry("x").toString()),
* Double.parseDouble(row.getEntry("y").toString())));
* LinkedList<Integer> targets = new LinkedList<>();
*
* if(row.getColumnNames().contains("targetId")) {
* targets.addFirst(Integer.parseInt(row.getEntry("targetId").toString()));
* }
* else {
* targets.addFirst(-1);
* }
* pedestrian.setTargets(targets);
*
*
*
*
* good idea but too slow!
* JsonElement jsonTree = IOUtils.getGson().toJsonTree(pedestrian, Pedestrian.class);
* for(String name : row.getColumnNames()) {
* setField(jsonTree, name, row.getEntry(name));
* }
*
* pedestrian = IOUtils.getGson().fromJson(jsonTree, Pedestrian.class);
* return pedestrian;
* }
*
* private static boolean setField(final JsonElement element, final String key, final Object
* value) {
* if(!element.isJsonObject()) {
* return false;
* }
* else {
* JsonObject obj = element.getAsJsonObject();
* JsonElement el = obj.get(key);
*
* if(el != null && el.isJsonPrimitive()) {
* obj.add(key, IOUtils.getGson().toJsonTree(value));
* return true;
* }
* else if(el != null) {
* return false;
* }
* else {
* for (Map.Entry<String, JsonElement> entry : obj.entrySet()) {
* if(setField(entry.getValue(), key, value)) {
* return true;
* }
* }
* return false;
* }
* }
* }
*
* private boolean isValid(final String[] cNames) {
* return requiredColumnNames.containsAll(Arrays.asList(cNames));
* }
*/
}
......@@ -49,7 +49,7 @@ public class IOUtils {
public static final String CORRUPT_DIR = "corrupt";
public static final String DATAPROCESSING_DIR = "data processing";
public static final String DATAPROCESSING_DIR = "output";
public static final String VADERE_PROJECT_FILENAME = "vadere.project";
......
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