Add SnapshotProcessor and adjust the effected parts

parent 43c91cf5
......@@ -16,6 +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": "scenario_snapshot.txt",
"processors": [18]
} ],
"processors": [ {
"type": "org.vadere.simulator.projects.dataprocessing_mtp.PedestrianPositionProcessor",
......@@ -68,6 +72,9 @@
}, {
"type": "org.vadere.simulator.projects.dataprocessing_mtp.PedestrianOSMStrideLengthProcessor",
"id": 17
}, {
"type": "org.vadere.simulator.projects.dataprocessing_mtp.SnapshotProcessor",
"id": 18
} ],
"attributes": {
"org.vadere.simulator.projects.dataprocessing_mtp.AttributesVelocityProcessor": {
......
package org.vadere.simulator.control;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.nio.file.Path;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.vadere.simulator.projects.ScenarioRunManager;
......@@ -20,6 +10,16 @@ import org.vadere.state.simulation.Step;
import org.vadere.state.simulation.Trajectory;
import org.vadere.util.io.IOUtils;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.nio.file.Path;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class OfflineSimulation {
private final Map<Step, List<Agent>> pedestriansByStep;
......@@ -66,7 +66,7 @@ public class OfflineSimulation {
.map(t -> t.getAgent(step))
.filter(opt -> opt.isPresent()).forEach(opt -> topography.addElement(opt.get()));
return new SimulationState(vadere.getName(), topography, vadere.getScenarioStore(), vadere.getAllWriters(),
step.getSimTimeInSec().orElse(Double.NaN), step.getStepNumber());
step.getSimTimeInSec().orElse(Double.NaN), step.getStepNumber(), vadere.getProcessorManager());
}
private void prepareOutput() {
......
......@@ -128,10 +128,12 @@ public class Simulation {
for (PassiveCallback c : passiveCallbacks) {
c.preLoop(simTimeInSec);
}
processorManager.preLoop(this.simulationState);
}
private void postLoop() {
simulationState = new SimulationState(name, topography, scenarioStore, processorWriter, simTimeInSec, step);
simulationState = new SimulationState(name, topography, scenarioStore, processorWriter, simTimeInSec, step, this.processorManager);
for (ActiveCallback ac : activeCallbacks) {
// ActiveCallbacks must also be Models in this case
simulationState.registerOutputGenerator(ac.getClass(), ac);
......@@ -148,6 +150,8 @@ public class Simulation {
for (Writer writer : processorWriter) {
writer.postLoop(simulationState);
}
processorManager.postLoop(this.simulationState);
}
/**
......@@ -159,7 +163,6 @@ public class Simulation {
processorManager.initOutputFiles();
preLoop();
processorManager.preLoop(this.simulationState);
while (runSimulation) {
synchronized (this) {
......@@ -212,7 +215,6 @@ public class Simulation {
// this is necessary to free the resources (files), the SimulationWriter and processors are writing in!
postLoop();
processorManager.postLoop(this.simulationState);
processorManager.writeOutput();
this.logger.info("Logged all processors in logfiles");
}
......@@ -220,7 +222,7 @@ public class Simulation {
private SimulationState initialSimulationState() {
SimulationState state =
new SimulationState(name, topography.clone(), scenarioStore, processorWriter, simTimeInSec, step);
new SimulationState(name, topography.clone(), scenarioStore, processorWriter, simTimeInSec, step, this.processorManager);
for (ActiveCallback ac : activeCallbacks) {
// ActiveCallbacks must also be Models in this case
......@@ -233,7 +235,7 @@ public class Simulation {
private void updateWriters(double simTimeInSec) {
SimulationState simulationState =
new SimulationState(name, topography, scenarioStore, processorWriter, simTimeInSec, step);
new SimulationState(name, topography, scenarioStore, processorWriter, simTimeInSec, step, this.processorManager);
simulationState.setOutputGeneratorMap(this.simulationState.getOutputGeneratorMap());
this.simulationState = simulationState;
......
package org.vadere.simulator.control;
import java.util.*;
import org.vadere.simulator.projects.ScenarioStore;
import org.vadere.simulator.projects.dataprocessing.writer.ProcessorWriter;
import org.vadere.simulator.projects.dataprocessing_mtp.ProcessorManager;
import org.vadere.state.scenario.Car;
import org.vadere.state.scenario.Pedestrian;
import org.vadere.state.scenario.Topography;
import org.vadere.util.geometry.shapes.VPoint;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
public class SimulationState {
private final Topography topography;
private final Map<Integer, VPoint> pedestrianPositionMap;
......@@ -18,12 +24,15 @@ public class SimulationState {
private final int step;
private final String name;
private final List<ProcessorWriter> processorWriter;
private ProcessorManager processorManager;
protected SimulationState(final String name,
final Topography topography,
final ScenarioStore scenarioStore,
final List<ProcessorWriter> processorWriter,
final double simTimeInSec, final int step) {
final Topography topography,
final ScenarioStore scenarioStore,
final List<ProcessorWriter> processorWriter,
final double simTimeInSec,
final int step,
final ProcessorManager processorManager) {
this.name = name;
this.processorWriter = processorWriter;
this.topography = topography;
......@@ -32,6 +41,7 @@ public class SimulationState {
this.pedestrianPositionMap = new HashMap<>();
this.outputGeneratorMap = new HashMap<>();
this.scenarioStore = scenarioStore;
this.processorManager = processorManager;
for (Pedestrian pedestrian : topography.getElements(Pedestrian.class)) {
pedestrianPositionMap.put(pedestrian.getId(), pedestrian.getPosition());
......@@ -44,8 +54,9 @@ public class SimulationState {
protected SimulationState(final String name,
final Topography topography,
final ScenarioStore scenarioStore,
final double simTimeInSec, final int step) {
this(name, topography, scenarioStore, new ArrayList<>(), simTimeInSec, step);
final double simTimeInSec, final int step,
final ProcessorManager processorManager) {
this(name, topography, scenarioStore, new ArrayList<>(), simTimeInSec, step, processorManager);
}
@Deprecated
......@@ -122,4 +133,7 @@ public class SimulationState {
return name;
}
public ProcessorManager getProcessorManager() {
return this.processorManager;
}
}
......@@ -412,6 +412,10 @@ public class ScenarioRunManager implements Runnable {
return null;
}
public ProcessorManager getProcessorManager() {
return this.processorManager;
}
public DataProcessingJsonManager getDataProcessingJsonManager() {
return this.dataProcessingJsonManager;
}
......
......@@ -210,6 +210,6 @@ public class DataProcessingJsonManager {
}
public ProcessorManager createProcessorManager() {
return new ProcessorManager(this.processors, this.attributes, this.outputFiles);
return new ProcessorManager(this, this.processors, this.attributes, this.outputFiles);
}
}
......@@ -63,8 +63,10 @@ public abstract class OutputFile<K extends Comparable<K>> {
}
private void printHeader(PrintWriter out) {
final List<String> fieldHeaders = composeLine(keyHeaders, p -> Arrays.stream(p.getHeaders()));
writeLine(out, fieldHeaders);
if (keyHeaders.length > 0) {
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 com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import org.vadere.simulator.control.SimulationState;
import org.vadere.simulator.models.Model;
import org.vadere.util.io.IOUtils;
......@@ -11,14 +14,17 @@ import java.util.List;
import java.util.Map;
public class ProcessorManager {
private DataProcessingJsonManager jsonManager;
private Model model;
private Map<Integer, Processor<?, ?>> processorMap;
private Map<Integer, AttributesProcessor> attributesMap;
private List<OutputFile<?>> outputFiles;
public ProcessorManager(List<Processor<?, ?>> processors, List<AttributesProcessor> attributesProcessor, List<OutputFile<?>> outputFiles) {
public ProcessorManager(DataProcessingJsonManager jsonManager, List<Processor<?, ?>> processors, List<AttributesProcessor> attributesProcessor, List<OutputFile<?>> outputFiles) {
this.jsonManager = jsonManager;
this.outputFiles = outputFiles;
this.attributesMap = new LinkedHashMap<>();
......@@ -72,4 +78,8 @@ public class ProcessorManager {
public void writeOutput() {
this.outputFiles.forEach(file -> file.write());
}
public JsonNode serializeToNode() throws JsonProcessingException {
return this.jsonManager.serializeToNode();
}
}
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("");
}
@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
}
}
......@@ -24,6 +24,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.vadere.simulator.control.SimulationState;
import org.vadere.simulator.models.MainModel;
import org.vadere.simulator.projects.ScenarioRunManager;
import org.vadere.simulator.projects.ScenarioStore;
......@@ -569,6 +570,14 @@ public abstract class JsonConverter {
return writer.writeValueAsString(node);
}
public static String serializeSimulationStateSnapshot(final SimulationState state, boolean commitHashIncluded) throws JsonProcessingException {
ObjectNode node = mapper.createObjectNode();
serializeMeta(node, commitHashIncluded, state.getScenarioStore());
node.set(ProcessorWriter.JSON_ATTRIBUTE_NAME, state.getProcessorManager().serializeToNode());
node.set("vadere", serializeVadereNode(state.getScenarioStore()));
return writer.writeValueAsString(node);
}
public static String serializeObject(Object object) throws JsonProcessingException {
return writer.writeValueAsString(mapper.convertValue(object, JsonNode.class));
}
......
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