Make DataProcessingJsonManager responsible for data processing json stuff

parent 0f789c4e
......@@ -10,16 +10,12 @@ import org.fife.ui.rsyntaxtextarea.Theme;
import org.vadere.gui.components.utils.Messages;
import org.vadere.gui.projectview.VadereApplication;
import org.vadere.simulator.projects.ScenarioRunManager;
import org.vadere.simulator.projects.dataprocessing_mtp.DataProcessingJsonManager;
import org.vadere.simulator.projects.io.JsonConverter;
import org.vadere.state.attributes.ModelDefinition;
import org.vadere.state.scenario.Topography;
import org.vadere.util.io.IOUtils;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
......@@ -27,6 +23,12 @@ import java.io.IOException;
import java.io.InputStream;
import java.util.prefs.Preferences;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
/**
* Shows text like the JSON formatted attributes.
*
......@@ -172,12 +174,10 @@ public class TextView extends JPanel {
currentScenario.setAttributesModel(modelDefinition.getAttributesList());
break;
case SIMULATION:
currentScenario
.setAttributesSimulation(JsonConverter.deserializeAttributesSimulation(json));
currentScenario.setAttributesSimulation(JsonConverter.deserializeAttributesSimulation(json));
break;
case OUTPUTPROCESSOR:
currentScenario.setProcessorManager(JsonConverter.deserializeProcessorManager(json));
currentScenario.setDataProcessingJsonManager(DataProcessingJsonManager.deserialize(json));
break;
case TOPOGRAPHY:
currentScenario.setTopography(JsonConverter.deserializeTopography(json));
......@@ -220,7 +220,7 @@ public class TextView extends JPanel {
.setText(JsonConverter.serializeAttributesSimulation(scenario.getAttributesSimulation()));
break;
case OUTPUTPROCESSOR:
this.txtrTextfiletextarea.setText(JsonConverter.serializeProcessorManager(scenario.getProcessorManager()));
this.txtrTextfiletextarea.setText(scenario.getDataProcessingJsonManager().serialize());
break;
case TOPOGRAPHY:
......
......@@ -99,7 +99,7 @@ public class ScenarioRunLocked extends ScenarioRunManager {
modelBuilder.createModelAndRandom();
simulation = new Simulation(modelBuilder.getModel(), currentTime, getName(),
scenarioStore, passiveCallbacks, writers,
modelBuilder.getRandom(), thisVadere.getProcessorManager());
modelBuilder.getRandom(), thisVadere.processorManager);
} else {
currentTime = simulation.getCurrentTime();
// restart timer of the simulation
......
......@@ -10,6 +10,7 @@ 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;
......@@ -53,6 +54,8 @@ public class ScenarioRunManager implements Runnable {
private List<ModelTest> modelTests;
protected final List<PassiveCallback> passiveCallbacks;
protected List<ProcessorWriter> writers;
private DataProcessingJsonManager dataProcessingJsonManager;
protected ProcessorManager processorManager;
private ScenarioFinishedListener finishedListener;
......@@ -157,6 +160,8 @@ public class ScenarioRunManager implements Runnable {
logger.info(String.format("Initializing scenario. Start of scenario '%s'...", this.getName()));
scenarioStore.topography.reset();
this.processorManager = this.dataProcessingJsonManager.createProcessorManager();
}
public void setScenarioFailed(final boolean scenarioFailed) {
......@@ -407,12 +412,11 @@ public class ScenarioRunManager implements Runnable {
return null;
}
public ProcessorManager getProcessorManager() {
return processorManager;
public DataProcessingJsonManager getDataProcessingJsonManager() {
return this.dataProcessingJsonManager;
}
public void setProcessorManager(ProcessorManager processorManager) {
this.processorManager = processorManager;
public void setDataProcessingJsonManager(final DataProcessingJsonManager manager) {
this.dataProcessingJsonManager = manager;
}
}
package org.vadere.simulator.projects.dataprocessing_mtp;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.vadere.simulator.projects.io.JsonConverter;
import org.vadere.util.reflection.DynamicClassInstantiator;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
public class DataProcessingJsonManager {
//TODO Change to 'dataprocessing'
public static final String DATAPROCCESSING_KEY = "processWriters";
public static final String FILES_KEY = "files";
private static final String TYPE_KEY = "type";
private static final String FILENAME_KEY = "filename";
private static final String FILE_PROCESSORS_KEY = "processors";
public static final String PROCESSORS_KEY = "processors";
private static final String PROCESSORID_KEY = "id";
public static final String ATTRIBUTES_KEY = "attributes";
private static ObjectMapper mapper;
public static ObjectWriter writer;
private static final DynamicClassInstantiator<OutputFile<?>> outputFileInstantiator;
private static final DynamicClassInstantiator<Processor<?, ?>> processorInstantiator;
private static final DynamicClassInstantiator<AttributesProcessor> attributesInstantiator;
private List<OutputFile<?>> outputFiles;
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;
}
static {
mapper = JsonConverter.getMapper();
writer = mapper.writerWithDefaultPrettyPrinter();
outputFileInstantiator = new DynamicClassInstantiator<>();
processorInstantiator = new DynamicClassInstantiator<>();
attributesInstantiator = new DynamicClassInstantiator<>();
}
private DataProcessingJsonManager() {
this.outputFiles = new ArrayList<>();
this.processors = new ArrayList<>();
this.attributes = new ArrayList<>();
}
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()));
OutputFile<?> file = outputFileInstantiator.createObject(fileStore.type);
file.setFileName(fileStore.filename);
file.setProcessorIds(fileStore.processors);
this.outputFiles.add(file);
}
public void addProcessor(final ProcessorStore processorStore) {
Processor<?, ?> processor = processorInstantiator.createObject(processorStore.type);
processor.setId(processorStore.id);
this.processors.add(processor);
}
public void addAttributes(final AttributesProcessor attributes) {
this.attributes.add(attributes);
}
private static JsonNode serializeOutputFile(final OutputFile outputFile) {
ObjectNode node = mapper.createObjectNode();
node.put(TYPE_KEY, outputFile.getClass().getName());
node.put(FILENAME_KEY, outputFile.getFileName());
node.set(FILE_PROCESSORS_KEY, mapper.convertValue(outputFile.getProcessorIds(), JsonNode.class));
return node;
}
private static JsonNode serializeProcessor(final Processor processor) {
ObjectNode node = mapper.createObjectNode();
node.put(TYPE_KEY, processor.getClass().getName());
node.put(PROCESSORID_KEY, processor.getId());
return node;
}
private static JsonNode serializeAttributesProcessor(final AttributesProcessor attributes) {
return mapper.convertValue(attributes, JsonNode.class);
}
public String serialize() throws JsonProcessingException {
return writer.writeValueAsString(serializeToNode());
}
public JsonNode serializeToNode() {
ObjectNode main = mapper.createObjectNode();
ArrayNode outputFilesArrayNode = mapper.createArrayNode();
ArrayNode processorsArrayNode = mapper.createArrayNode();
ObjectNode attributesNode = mapper.createObjectNode();
// part 1: output files
this.outputFiles.forEach(file -> {
outputFilesArrayNode.add(serializeOutputFile(file));
});
// part 2: processors
this.processors.forEach(proc -> {
processorsArrayNode.add(serializeProcessor(proc));
});
// part 3: attributes
this.attributes.forEach(att -> {
attributesNode.set(att.getClass().getName(), serializeAttributesProcessor(att));
});
main.set(FILES_KEY, outputFilesArrayNode);
main.set(PROCESSORS_KEY, processorsArrayNode);
main.set(ATTRIBUTES_KEY, attributesNode);
return main;
}
public static DataProcessingJsonManager deserialize(String json) throws IOException {
JsonNode node = json.isEmpty() ? mapper.createObjectNode() : mapper.readTree(json);
return deserializeFromNode(node);
}
public static DataProcessingJsonManager deserializeFromNode(JsonNode node) throws IOException {
DataProcessingJsonManager manager = new DataProcessingJsonManager();
ArrayNode outputFilesArrayNode = (ArrayNode) node.get(FILES_KEY);
ArrayNode processorsArrayNode = (ArrayNode) node.get(PROCESSORS_KEY);
JsonNode attributesNode = node.get(ATTRIBUTES_KEY);
// part 1: output files
if (outputFilesArrayNode != null)
for (JsonNode fileNode : outputFilesArrayNode) {
OutputFileStore fileStore = mapper.treeToValue(fileNode, OutputFileStore.class);
manager.addOutputFile(fileStore);
}
// part 2: processors
if (processorsArrayNode != null)
for (JsonNode processorNode : processorsArrayNode) {
ProcessorStore processorStore = mapper.treeToValue(processorNode, ProcessorStore.class);
manager.addProcessor(processorStore);
}
// part 3: attributes
if (attributesNode != null) {
Iterator<String> it = attributesNode.fieldNames();
while (it.hasNext()) {
String fieldName = it.next();
JsonNode attributeNode = attributesNode.get(fieldName);
AttributesProcessor attributes = mapper.treeToValue(attributeNode, attributesInstantiator.getClassFromName(fieldName));
manager.addAttributes(attributes);
}
}
return manager;
}
public ProcessorManager createProcessorManager() {
return new ProcessorManager(this.processors, this.attributes, this.outputFiles);
}
}
......@@ -9,7 +9,6 @@ import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class ProcessorManager {
......@@ -45,10 +44,6 @@ public class ProcessorManager {
return this.processorMap.containsKey(id) ? this.processorMap.get(id) : null;
}
public List<OutputFile<?>> getOutputFiles() {
return this.outputFiles;
}
public Model getModel() {
return this.model;
}
......@@ -65,14 +60,6 @@ public class ProcessorManager {
this.processorMap.values().forEach(proc -> proc.postLoop(state));
}
public Set<Integer> getProcessorIds() {
return this.processorMap.keySet();
}
public Set<Integer> getAttributesProcessorIds() {
return this.attributesMap.keySet();
}
public AttributesProcessor getAttributes(int processorId) {
return this.attributesMap.get(processorId);
}
......
......@@ -28,10 +28,7 @@ import org.vadere.simulator.models.MainModel;
import org.vadere.simulator.projects.ScenarioRunManager;
import org.vadere.simulator.projects.ScenarioStore;
import org.vadere.simulator.projects.dataprocessing.writer.ProcessorWriter;
import org.vadere.simulator.projects.dataprocessing_mtp.AttributesProcessor;
import org.vadere.simulator.projects.dataprocessing_mtp.OutputFile;
import org.vadere.simulator.projects.dataprocessing_mtp.Processor;
import org.vadere.simulator.projects.dataprocessing_mtp.ProcessorManager;
import org.vadere.simulator.projects.dataprocessing_mtp.DataProcessingJsonManager;
import org.vadere.state.attributes.Attributes;
import org.vadere.state.attributes.AttributesSimulation;
import org.vadere.state.attributes.ModelDefinition;
......@@ -173,6 +170,10 @@ public abstract class JsonConverter {
mapper.registerModule(sm);
}
public static ObjectMapper getMapper() {
return mapper;
}
// - - - - DESERIALIZING - - - -
public static JsonNode deserializeToNode(String dev) throws IOException {
......@@ -248,116 +249,6 @@ public abstract class JsonConverter {
}
}
private static class ProcessorStore {
String type;
int id;
}
private static class OutputFileStore {
String type;
String filename;
List<Integer> processors;
}
public static ProcessorManager deserializeProcessorManager(String json) throws IOException {
JsonNode node;
if (json.isEmpty()) {
node = mapper.createObjectNode();
} else {
node = mapper.readTree(json);
}
return deserializeProcessorManagerFromNode(node);
}
public static ProcessorManager deserializeProcessorManagerFromNode(JsonNode node) throws IOException {
ArrayNode outputFilesArrayNode = (ArrayNode) node.get("files");
ArrayNode processorsArrayNode = (ArrayNode) node.get("processors");
JsonNode attributesNode = node.get("attributes");
// part 1: output files
List<OutputFile<?>> outputFiles = new ArrayList<>();
if (outputFilesArrayNode != null) {
DynamicClassInstantiator<OutputFile<?>> instantiator1 = new DynamicClassInstantiator<>();
for (JsonNode fileNode : outputFilesArrayNode) {
OutputFileStore writerStore = mapper.treeToValue(fileNode, OutputFileStore.class);
OutputFile<?> file = instantiator1.createObject(writerStore.type);
file.setFileName(writerStore.filename);
file.setProcessorIds(writerStore.processors);
outputFiles.add(file);
}
}
// part 2: processors
List<Processor<?, ?>> processors = new ArrayList<>();
if (processorsArrayNode != null) {
DynamicClassInstantiator<Processor<?, ?>> instantiator2 = new DynamicClassInstantiator<>();
for (JsonNode processorNode : processorsArrayNode) {
ProcessorStore processorStore = mapper.treeToValue(processorNode, ProcessorStore.class);
Processor<?, ?> processor = instantiator2.createObject(processorStore.type);
processor.setId(processorStore.id);
processors.add(processor);
}
}
// part 3: attributes
List<AttributesProcessor> attributes = new ArrayList<>();
if (attributesNode != null) {
DynamicClassInstantiator<AttributesProcessor> instantiator3 = new DynamicClassInstantiator<>();
Iterator<String> it = attributesNode.fieldNames();
while (it.hasNext()) {
String fieldName = it.next();
JsonNode attributeNode = attributesNode.get(fieldName);
AttributesProcessor attribute = mapper.treeToValue(attributeNode, instantiator3.getClassFromName(fieldName));
attributes.add(attribute);
}
}
return new ProcessorManager(processors, attributes, outputFiles);
}
public static String serializeProcessorManager(ProcessorManager processorManager) throws JsonProcessingException {
return writer.writeValueAsString(serializeProcessorManagerToNode(processorManager));
}
private static JsonNode serializeProcessorManagerToNode(ProcessorManager processorManager) {
ObjectNode main = mapper.createObjectNode();
ArrayNode outputFilesArrayNode = mapper.createArrayNode();
ArrayNode processorsArrayNode = mapper.createArrayNode();
ObjectNode attributesNode = mapper.createObjectNode();
if (processorManager != null) {
// part 1: output files
processorManager.getOutputFiles().forEach(file -> {
ObjectNode node = mapper.createObjectNode();
node.put("type", file.getClass().getName());
node.put("filename", file.getFileName());
node.set("processors", mapper.convertValue(file.getProcessorIds(), JsonNode.class));
outputFilesArrayNode.add(node);
});
// part 2: processors
processorManager.getProcessorIds().forEach(processorId -> {
ObjectNode node = mapper.createObjectNode();
node.put("type", processorManager.getProcessor(processorId).getClass().getName());
node.put("id", processorId);
processorsArrayNode.add(node);
});
// part 3: attributes
processorManager.getAttributesProcessorIds().forEach(processorId -> {
AttributesProcessor attributeProcessor = processorManager.getAttributes(processorId);
attributesNode.set(attributeProcessor.getClass().getName(), mapper.convertValue(attributeProcessor, JsonNode.class));
});
}
main.set("files", outputFilesArrayNode);
main.set("processors", processorsArrayNode);
main.set("attributes", attributesNode);
return main;
}
public static ScenarioRunManager deserializeScenarioRunManager(String json) throws IOException {
return deserializeScenarioRunManagerFromNode(mapper.readTree(json));
}
......@@ -380,7 +271,7 @@ public abstract class JsonConverter {
//ProcessorWriter.fromJsonList(node.get(ProcessorWriter.JSON_ATTRIBUTE_NAME).toString())
// .forEach(writer -> scenarioRunManager.addWriter(writer));
scenarioRunManager.setProcessorManager(deserializeProcessorManagerFromNode(rootNode.get("processWriters")));
scenarioRunManager.setDataProcessingJsonManager(DataProcessingJsonManager.deserializeFromNode(rootNode.get(DataProcessingJsonManager.DATAPROCCESSING_KEY)));
scenarioRunManager.saveChanges();
if (scenarioRunManager.getTopography() == null)
......@@ -529,7 +420,7 @@ public abstract class JsonConverter {
ObjectNode rootNode = mapper.createObjectNode();
serializeMeta(rootNode, commitHashIncluded, scenarioStore);
//rootNode.set(ProcessorWriter.JSON_ATTRIBUTE_NAME, serializeProcessorWriters(scenarioRunManager.getAllWriters()));
rootNode.set(ProcessorWriter.JSON_ATTRIBUTE_NAME, serializeProcessorManagerToNode(scenarioRunManager.getProcessorManager()));
rootNode.set(DataProcessingJsonManager.DATAPROCCESSING_KEY, scenarioRunManager.getDataProcessingJsonManager().serializeToNode());
rootNode.set("vadere", serializeVadereNode(scenarioStore));
return rootNode;
}
......
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