Commit 060fcc7e authored by Jakob Schöttl's avatar Jakob Schöttl
Browse files

Fixed remaining errors

parent ed5d18ce
package org.vadere.gui.components.utils;
import org.vadere.state.attributes.scenario.AttributesDynamicElement;
import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
/**
* This Exclusion strategy exclude the {@link AttributesDynamicElement#id} from serialization. This
* is useful if you want
* to protect the id from changes via text.
*
*/
public class GsonAttributeDynamicElementExclusionStrategy implements ExclusionStrategy {
@Override
public boolean shouldSkipField(final FieldAttributes f) {
return (f.getDeclaringClass() == AttributesDynamicElement.class) && f.getName().equals("id");
}
@Override
public boolean shouldSkipClass(final Class<?> clazz) {
return false;
}
}
package org.vadere.gui.components.view;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import java.awt.*;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.IOException;
import java.io.InputStream;
import javax.swing.*;
import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
......@@ -30,6 +31,9 @@ import org.vadere.state.scenario.Pedestrian;
import org.vadere.state.scenario.ScenarioElement;
import org.vadere.state.util.StateJsonConverter;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
/**
* The ScenarioElementView display's a ScenarioElement in JSON-Format.
*/
......@@ -174,18 +178,14 @@ public class ScenarioElementView extends JPanel implements ISelectScenarioElemen
jsonValidIndicator.hide();
}
} else {
try {
if (scenarioElement instanceof AgentWrapper) {
this.txtrTextfiletextarea.setText(
StateJsonConverter.serializeObject(((AgentWrapper) scenarioElement).getAgentInitialStore()));
} else if (scenarioElement instanceof Pedestrian) {
this.txtrTextfiletextarea.setText(StateJsonConverter.serializeObject(scenarioElement));
} else {
this.txtrTextfiletextarea.setText(StateJsonConverter
.serializeObject(ReflectionAttributeModifier.getAttributes(scenarioElement)));
}
} catch (JsonProcessingException e) {
// ?
if (scenarioElement instanceof AgentWrapper) {
this.txtrTextfiletextarea.setText(
StateJsonConverter.serializeObject(((AgentWrapper) scenarioElement).getAgentInitialStore()));
} else if (scenarioElement instanceof Pedestrian) {
this.txtrTextfiletextarea.setText(StateJsonConverter.serializeObject(scenarioElement));
} else {
this.txtrTextfiletextarea.setText(StateJsonConverter
.serializeObject(ReflectionAttributeModifier.getAttributes(scenarioElement)));
}
}
}
......
package org.vadere.gui.projectview.utils;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
@Deprecated
public abstract class JsonFieldValidator {
private static Map<Class, Set<String>> fieldNamesMap = new HashMap<>();
public static void validateFields(Class cls, String jsonString) throws JsonParseException {
JsonObject jsonObj = new JsonParser().parse(jsonString).getAsJsonObject();
Set<String> classFieldNames = getFieldNames(cls);
for (Map.Entry<String, JsonElement> entry : jsonObj.entrySet())
if (!classFieldNames.contains(entry.getKey()))
throw new JsonParseException("The field \"" + entry.getKey() + "\" is unknown");
}
public static Set<String> getFieldNames(Class cls) {
if (fieldNamesMap.containsKey(cls))
return fieldNamesMap.get(cls);
Set<String> fieldNames = new HashSet<>();
for (Field field : cls.getDeclaredFields())
fieldNames.add(field.getName());
fieldNamesMap.put(cls, fieldNames);
return fieldNames;
}
public static void clear() {
fieldNamesMap.clear();
}
}
package org.vadere.gui.projectview.view;
import com.fasterxml.jackson.core.JsonProcessingException;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.util.prefs.Preferences;
import javax.swing.AbstractButton;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
......@@ -11,23 +29,13 @@ 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.DataProcessingJsonManager;
import org.vadere.simulator.projects.io.JsonConverter;
import org.vadere.state.attributes.ModelDefinition;
import org.vadere.state.scenario.Topography;
import org.vadere.state.util.StateJsonConverter;
import org.vadere.util.io.IOUtils;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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;
import com.fasterxml.jackson.core.JsonProcessingException;
/**
* Shows text like the JSON formatted attributes.
......@@ -169,7 +177,7 @@ public class TextView extends JPanel {
try {
switch (attributeType) {
case MODEL:
ModelDefinition modelDefinition = StateJsonConverter.deserializeModelDefinition(json);
ModelDefinition modelDefinition = JsonConverter.deserializeModelDefinition(json);
currentScenario.getScenarioStore().mainModel = modelDefinition.getMainModel();
currentScenario.setAttributesModel(modelDefinition.getAttributesList());
break;
......
package org.vadere.gui.vadere;
import static org.junit.Assert.assertEquals;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.LinkedList;
import java.util.stream.Collectors;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import org.junit.BeforeClass;
import org.junit.Test;
import org.vadere.simulator.models.osm.OptimalStepsModel;
import org.vadere.simulator.projects.ProjectWriter;
import org.vadere.simulator.projects.ScenarioRunManager;
import org.vadere.simulator.projects.ScenarioStore;
import org.vadere.simulator.projects.VadereProject;
import org.vadere.simulator.projects.ProjectWriter;
import org.vadere.simulator.projects.io.IOVadere;
import org.vadere.simulator.projects.io.JsonConverter;
import org.vadere.state.attributes.Attributes;
import org.vadere.state.attributes.AttributesSimulation;
import org.vadere.state.attributes.models.AttributesFloorField;
......@@ -15,19 +27,8 @@ import org.vadere.state.attributes.models.AttributesOSM;
import org.vadere.state.attributes.models.AttributesPotentialCompact;
import org.vadere.state.attributes.models.AttributesPotentialOSM;
import org.vadere.state.scenario.Topography;
import org.vadere.state.util.StateJsonConverter;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.LinkedList;
import java.util.stream.Collectors;
import static org.junit.Assert.assertEquals;
public class TestProjectWriterAndReader {
private static VadereProject testProject;
......@@ -69,9 +70,9 @@ public class TestProjectWriterAndReader {
VadereProject projectJson = IOVadere.readProjectJson(testFileJson);
assertEquals(
projectJson.getScenarios().stream().map(scenario -> StateJsonConverter.serializeScenarioRunManager(scenario))
projectJson.getScenarios().stream().map(scenario -> JsonConverter.serializeScenarioRunManager(scenario))
.collect(Collectors.toList()),
testProject.getScenarios().stream().map(scenario -> StateJsonConverter.serializeScenarioRunManager(scenario))
testProject.getScenarios().stream().map(scenario -> JsonConverter.serializeScenarioRunManager(scenario))
.collect(Collectors.toList()));
}
}
package org.vadere.simulator.projects;
import org.vadere.state.util.StateJsonConverter;
import org.vadere.util.io.IOUtils;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.vadere.simulator.projects.io.JsonConverter;
import org.vadere.util.io.IOUtils;
/**
* Writes a {@link VadereProject} to a file.
*
......@@ -73,7 +73,7 @@ public class ProjectWriter {
for (ScenarioRunManager scenario : project.getScenarios()) {
IOUtils.writeTextFile(
getScenarioPath(scenarioPath, scenario).toString(),
StateJsonConverter.serializeScenarioRunManager(scenario, includeCommitHash));
JsonConverter.serializeScenarioRunManager(scenario, includeCommitHash));
}
}
......@@ -82,7 +82,7 @@ public class ProjectWriter {
Path scenariosDir = Files.createDirectories(Paths.get(projectFolderPath, IOUtils.SCENARIO_DIR));
IOUtils.writeTextFile(
getScenarioPath(scenariosDir, scenario).toString(),
StateJsonConverter.serializeScenarioRunManager(scenario, true));
JsonConverter.serializeScenarioRunManager(scenario, true));
}
public static Path getScenarioPath(Path scenariosDir, ScenarioRunManager scenario) {
......
package org.vadere.simulator.projects;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.vadere.simulator.control.PassiveCallback;
import org.vadere.simulator.control.Simulation;
import org.vadere.simulator.models.MainModel;
import org.vadere.simulator.models.MainModelBuilder;
import org.vadere.simulator.projects.dataprocessing.ModelTest;
import org.vadere.simulator.projects.dataprocessing.DataProcessingJsonManager;
import org.vadere.simulator.projects.dataprocessing.ModelTest;
import org.vadere.simulator.projects.dataprocessing.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.scenario.AttributesAgent;
import org.vadere.state.scenario.Topography;
import org.vadere.state.util.StateJsonConverter;
import org.vadere.util.io.IOUtils;
import org.vadere.util.reflection.VadereClassNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import difflib.DiffUtils;
/**
......@@ -78,7 +78,7 @@ public class ScenarioRunManager implements Runnable {
}
public void saveChanges() { // get's called by VadereProject.saveChanges on init
savedStateSerialized = StateJsonConverter.serializeScenarioRunManager(this);
savedStateSerialized = JsonConverter.serializeScenarioRunManager(this);
currentStateSerialized = savedStateSerialized;
}
......@@ -87,11 +87,11 @@ public class ScenarioRunManager implements Runnable {
}
public void updateCurrentStateSerialized() {
currentStateSerialized = StateJsonConverter.serializeScenarioRunManager(this);
currentStateSerialized = JsonConverter.serializeScenarioRunManager(this);
}
public String getDiff() {
String currentStateSerialized = StateJsonConverter.serializeScenarioRunManager(this);
String currentStateSerialized = JsonConverter.serializeScenarioRunManager(this);
if (!savedStateSerialized.equals(currentStateSerialized)) {
StringBuilder diff = new StringBuilder();
List<String> original = new ArrayList<>(Arrays.asList(savedStateSerialized.split("\n")));
......@@ -129,7 +129,7 @@ public class ScenarioRunManager implements Runnable {
createAndSetOutputDirectory();
try (PrintWriter out = new PrintWriter(Paths.get(this.outputPath.toString(), this.getName() + IOUtils.SCENARIO_FILE_EXTENSION).toString())) {
out.println(StateJsonConverter.serializeScenarioRunManager(this, true));
out.println(JsonConverter.serializeScenarioRunManager(this, true));
}
// Run simulation main loop from start time = 0 seconds
......@@ -284,7 +284,7 @@ public class ScenarioRunManager implements Runnable {
public ScenarioRunManager clone() {
ScenarioRunManager clonedScenario = null;
try {
clonedScenario = StateJsonConverter.cloneScenarioRunManager(this);
clonedScenario = JsonConverter.cloneScenarioRunManager(this);
clonedScenario.outputPath = outputPath;
} catch (IOException | VadereClassNotFoundException e) {
logger.error(e);
......@@ -307,7 +307,7 @@ public class ScenarioRunManager implements Runnable {
public void discardChanges() {
try {
ScenarioRunManager srm = StateJsonConverter.deserializeScenarioRunManager(savedStateSerialized);
ScenarioRunManager srm = JsonConverter.deserializeScenarioRunManager(savedStateSerialized);
// not all necessary! only the ones that could have changed
this.scenarioStore = srm.scenarioStore;
this.outputPath = srm.outputPath;
......
......@@ -5,6 +5,7 @@ import java.util.ArrayList;
import java.util.List;
import org.apache.commons.codec.digest.DigestUtils;
import org.vadere.simulator.projects.io.JsonConverter;
import org.vadere.state.attributes.Attributes;
import org.vadere.state.attributes.AttributesSimulation;
import org.vadere.state.attributes.scenario.AttributesCar;
......@@ -49,7 +50,7 @@ public class ScenarioStore {
@Override
public ScenarioStore clone() {
try {
return StateJsonConverter.cloneScenarioStore(this);
return JsonConverter.cloneScenarioStore(this);
} catch (IOException | VadereClassNotFoundException e) {
throw new RuntimeException(e);
// Do not return null or Optional, that does not make sense!
......
......@@ -5,7 +5,6 @@ import org.apache.log4j.Logger;
import org.vadere.simulator.projects.ScenarioRunManager;
import org.vadere.simulator.projects.VadereProject;
import org.vadere.simulator.projects.migration.MigrationAssistant;
import org.vadere.state.util.StateJsonConverter;
import org.vadere.util.io.IOUtils;
import org.xml.sax.SAXException;
......@@ -26,7 +25,7 @@ public class IOVadere {
private static Logger logger = LogManager.getLogger(IOVadere.class);
public static ScenarioRunManager fromJson(final String json) throws IOException {
return StateJsonConverter.deserializeScenarioRunManager(json);
return JsonConverter.deserializeScenarioRunManager(json);
}
public static VadereProject readProjectJson(final String filepath)
......@@ -52,7 +51,7 @@ public class IOVadere {
for (File file : IOUtils.getFilesInScenarioDirectory(p)) {
ScenarioRunManager scenario =
StateJsonConverter.deserializeScenarioRunManager(IOUtils.readTextFile(file.getAbsolutePath()));
JsonConverter.deserializeScenarioRunManager(IOUtils.readTextFile(file.getAbsolutePath()));
if (!scenarioNames.add(scenario.getName())) {
logger.error("there are two scenarios with the same name!");
throw new IOException("Found two scenarios with the same name.");
......
package org.vadere.simulator.projects.migration.incidents;
import com.fasterxml.jackson.databind.JsonNode;
import java.io.IOException;
import org.apache.commons.lang3.tuple.Pair;
import org.vadere.simulator.projects.io.JsonConverter;
import org.vadere.simulator.projects.migration.Graph;
import org.vadere.simulator.projects.migration.MigrationException;
import org.vadere.state.util.StateJsonConverter;
import java.io.IOException;
import com.fasterxml.jackson.databind.JsonNode;
public class ExceptionIncident extends Incident {
......@@ -25,7 +26,7 @@ public class ExceptionIncident extends Incident {
public void resolve(Graph graph, StringBuilder log) throws MigrationException {
while (true) {
try {
StateJsonConverter.deserializeScenarioRunManagerFromNode(node);
JsonConverter.deserializeScenarioRunManagerFromNode(node);
break;
} catch (IOException e) {
String errMsg = e.getMessage();
......
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