Add store files for output presets ('postvis.trajectories') and create default output definition.

Remove SnapshotProcessor, since scenario snapshots are now handled separately by the ScenarioRunManager.
parent 48edacd4
......@@ -16,14 +16,10 @@
"type": "org.vadere.simulator.projects.dataprocessing_mtp.NoDataKeyOutputFile",
"filename": "output_nokey.txt",
"processors": [ 11, 12 ]
}, {
"type": "org.vadere.simulator.projects.dataprocessing_mtp.NoDataKeyOutputFile",
"filename": "snapshot.scenario",
"processors": [ 18 ]
}, {
"type": "org.vadere.simulator.projects.dataprocessing_mtp.TimestepPedestrianIdOutputFile",
"filename": "test.trajectories",
"processors": [ 1, 19 ]
"filename": "postvis.trajectories",
"processors": [ 1, 18 ]
} ],
"processors": [ {
"type": "org.vadere.simulator.projects.dataprocessing_mtp.PedestrianPositionProcessor",
......@@ -76,15 +72,12 @@
}, {
"type": "org.vadere.simulator.projects.dataprocessing_mtp.PedestrianOSMStrideLengthProcessor",
"id": 17
}, {
"type": "org.vadere.simulator.projects.dataprocessing_mtp.SnapshotProcessor",
"id": 18
}, {
"type": "org.vadere.simulator.projects.dataprocessing_mtp.PedestrianTargetIdProcessor",
"id": 19
"id": 18
}, {
"type": "org.vadere.simulator.projects.dataprocessing_mtp.PedestrianSourceIdProcessor",
"id": 20
"id": 19
} ],
"attributes": {
"org.vadere.simulator.projects.dataprocessing_mtp.AttributesVelocityProcessor": {
......
......@@ -4,6 +4,7 @@ import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.vadere.gui.projectview.model.ProjectViewModel;
import org.vadere.simulator.projects.ScenarioRunManager;
import org.vadere.simulator.projects.dataprocessing_mtp.DataProcessingJsonManager;
import org.vadere.state.scenario.Topography;
import java.io.IOException;
......@@ -28,6 +29,7 @@ public class ActionAddScenario extends ActionAbstractAddScenario {
@Override
protected ScenarioRunManager generateVadere(final String name) throws IOException {
ScenarioRunManager newScenario = new ScenarioRunManager(name);
newScenario.setDataProcessingJsonManager(DataProcessingJsonManager.createDefault());
if (topography != null) {
newScenario.setTopography(topography);
logger.info("set topography to scenario");
......
......@@ -19,6 +19,7 @@ import org.vadere.util.io.IOUtils;
import org.vadere.util.reflection.VadereClassNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
......@@ -74,7 +75,10 @@ public class ScenarioRunManager implements Runnable {
this.writers = new LinkedList<>();
this.scenarioStore = store;
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.saveChanges();
}
public void saveChanges() { // get's called by VadereProject.saveChanges on init
......@@ -115,6 +119,10 @@ public class ScenarioRunManager implements Runnable {
// prepare processors and simulation data writer
prepareOutput();
try (PrintWriter out = new PrintWriter(Paths.get(this.outputPath.toString(), this.getName() + IOUtils.SCENARIO_FILE_EXTENSION).toString())) {
out.println(JsonConverter.serializeScenarioRunManager(this, true));
}
ModelBuilder modelBuilder = new ModelBuilder(scenarioStore);
modelBuilder.createModelAndRandom();
final MainModel mainModel = modelBuilder.getModel();
......
......@@ -21,6 +21,8 @@ public class DataProcessingJsonManager {
//TODO Change to 'dataprocessing'
public static final String DATAPROCCESSING_KEY = "processWriters";
public static final String TRAJECTORIES_FILENAME = "postvis.trajectories";
public static final String FILES_KEY = "files";
private static final String TYPE_KEY = "type";
private static final String FILENAME_KEY = "filename";
......@@ -32,7 +34,7 @@ public class DataProcessingJsonManager {
public static final String ATTRIBUTES_KEY = "attributes";
private static final String DEFAULT_SEPARATOR = " ";
public static final String DEFAULT_SEPARATOR = " ";
private static ObjectMapper mapper;
public static ObjectWriter writer;
......@@ -45,32 +47,6 @@ public class DataProcessingJsonManager {
private List<Processor<?, ?>> processors;
private List<AttributesProcessor> attributes;
private static class ProcessorStore {
String type;
int id;
}
private static class OutputFileStore {
String type;
String filename;
List<Integer> processors;
private String separator;
public OutputFileStore() {
this.separator = DEFAULT_SEPARATOR;
}
public String getSeparator() {
return this.separator;
}
public void setSeparator(String separator) {
if (separator != null) {
this.separator = separator;
}
}
}
static {
mapper = JsonConverter.getMapper();
writer = mapper.writerWithDefaultPrettyPrinter();
......@@ -80,7 +56,7 @@ public class DataProcessingJsonManager {
attributesInstantiator = new DynamicClassInstantiator<>();
}
private DataProcessingJsonManager() {
public DataProcessingJsonManager() {
this.outputFiles = new ArrayList<>();
this.processors = new ArrayList<>();
this.attributes = new ArrayList<>();
......@@ -88,18 +64,18 @@ public class DataProcessingJsonManager {
public void addOutputFile(final OutputFileStore fileStore) {
// If fileName already exists, change it by removing and readding
this.outputFiles.removeAll(this.outputFiles.stream().filter(f -> f.getFileName().equals(fileStore.filename)).collect(Collectors.toList()));
this.outputFiles.removeAll(this.outputFiles.stream().filter(f -> f.getFileName().equals(fileStore.getFilename())).collect(Collectors.toList()));
OutputFile<?> file = outputFileInstantiator.createObject(fileStore.type);
file.setFileName(fileStore.filename);
file.setProcessorIds(fileStore.processors);
OutputFile<?> file = outputFileInstantiator.createObject(fileStore.getType());
file.setFileName(fileStore.getFilename());
file.setProcessorIds(fileStore.getProcessors());
file.setSeparator(fileStore.getSeparator());
this.outputFiles.add(file);
}
public void addProcessor(final ProcessorStore processorStore) {
Processor<?, ?> processor = processorInstantiator.createObject(processorStore.type);
processor.setId(processorStore.id);
Processor<?, ?> processor = processorInstantiator.createObject(processorStore.getType());
processor.setId(processorStore.getId());
this.processors.add(processor);
}
......@@ -168,13 +144,30 @@ public class DataProcessingJsonManager {
return main;
}
public static DataProcessingJsonManager deserialize(String json) throws IOException {
JsonNode node = json.isEmpty() ? mapper.createObjectNode() : mapper.readTree(json);
public static DataProcessingJsonManager createDefault() {
try {
return deserializeFromNode(mapper.convertValue(OutputPresets.getOutputDefinition(), JsonNode.class));
}
catch (JsonProcessingException ex) {
ex.printStackTrace();
}
return null;
}
public static DataProcessingJsonManager deserialize(String json) {
try {
JsonNode node = json.isEmpty() ? mapper.createObjectNode() : mapper.readTree(json);
return deserializeFromNode(node);
}
catch (IOException ex) {
ex.printStackTrace();
}
return deserializeFromNode(node);
return null;
}
public static DataProcessingJsonManager deserializeFromNode(JsonNode node) throws IOException {
public static DataProcessingJsonManager deserializeFromNode(JsonNode node) throws JsonProcessingException {
DataProcessingJsonManager manager = new DataProcessingJsonManager();
ArrayNode outputFilesArrayNode = (ArrayNode) node.get(FILES_KEY);
......
package org.vadere.simulator.projects.dataprocessing_mtp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class OutputDefinitionStore {
private List<OutputFileStore> files;
private List<ProcessorStore> processors;
private Map<String, AttributesProcessor> attributes;
public OutputDefinitionStore() {
this.files = new ArrayList<>();
this.processors = new ArrayList<>();
}
public void addOutputFile(OutputFileStore file) {
this.files.add(file);
}
public void addProcessor(ProcessorStore processor) {
this.processors.add(processor);
}
public void addAttributes(AttributesProcessor attributes) {
this.attributes.put(attributes.getClass().getName(), attributes);
}
}
......@@ -63,11 +63,8 @@ public abstract class OutputFile<K extends Comparable<K>> {
}
private void printHeader(PrintWriter out) {
// Print headers only if the line is not empty, i.e. the keyHeaders and/or processor headers are given
if (keyHeaders.length + this.processors.stream().map(p -> p.getHeaders().length).reduce(0, (x,y) -> x+y) > 0) {
final List<String> fieldHeaders = composeLine(keyHeaders, p -> Arrays.stream(p.getHeaders()));
writeLine(out, fieldHeaders);
}
final List<String> fieldHeaders = composeLine(keyHeaders, p -> Arrays.stream(p.getHeaders()));
writeLine(out, fieldHeaders);
}
private void printRow(final PrintWriter out, final K key) {
......
package org.vadere.simulator.projects.dataprocessing_mtp;
import java.util.List;
public class OutputFileStore {
private String type;
private String filename;
private List<Integer> processors;
private String separator;
public OutputFileStore() {
this.separator = DataProcessingJsonManager.DEFAULT_SEPARATOR;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
public List<Integer> getProcessors() {
return processors;
}
public void setProcessors(List<Integer> processors) {
this.processors = processors;
}
public String getSeparator() {
return this.separator;
}
public void setSeparator(String separator) {
if (separator != null) {
this.separator = separator;
}
}
}
package org.vadere.simulator.projects.dataprocessing_mtp;
import java.util.Arrays;
public final class OutputPresets {
private static OutputPresets instance;
private final OutputDefinitionStore outputDefinition;
private OutputPresets() {
this.outputDefinition = new OutputDefinitionStore();
ProcessorStore processor1 = new ProcessorStore();
processor1.setType("org.vadere.simulator.projects.dataprocessing_mtp.PedestrianPositionProcessor");
processor1.setId(1);
this.outputDefinition.addProcessor(processor1);
ProcessorStore processor2 = new ProcessorStore();
processor2.setType("org.vadere.simulator.projects.dataprocessing_mtp.PedestrianTargetIdProcessor");
processor2.setId(2);
this.outputDefinition.addProcessor(processor2);
OutputFileStore outputFile = new OutputFileStore();
outputFile.setType("org.vadere.simulator.projects.dataprocessing_mtp.TimestepPedestrianIdOutputFile");
outputFile.setFilename(DataProcessingJsonManager.TRAJECTORIES_FILENAME);
outputFile.setProcessors(Arrays.asList(1, 2));
this.outputDefinition.addOutputFile(outputFile);
}
public static OutputDefinitionStore getOutputDefinition() {
return getInstance().outputDefinition;
}
public static OutputPresets getInstance() {
if (instance == null) {
instance = new OutputPresets();
}
return instance;
}
}
package org.vadere.simulator.projects.dataprocessing_mtp;
public class ProcessorStore {
private String type;
private int id;
public String getType() {
return this.type;
}
public void setType(String type) {
this.type = type;
}
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
}
package org.vadere.simulator.projects.dataprocessing_mtp;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.vadere.simulator.control.SimulationState;
import org.vadere.simulator.projects.io.JsonConverter;
public class SnapshotProcessor extends Processor<NoDataKey, String> {
public SnapshotProcessor() {
super(new String[] { });
}
@Override
public void preLoop(SimulationState state) {
try {
this.addValue(NoDataKey.key(), JsonConverter.serializeSimulationStateSnapshot(state, true));
}
catch (JsonProcessingException ex) {
this.addValue(NoDataKey.key(), ex.getMessage());
}
}
@Override
protected void doUpdate(final SimulationState state) {
// No update needed
}
@Override
public void init(final AttributesProcessor attributes, final ProcessorManager manager) {
// No initialization needed
}
}
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