Commit ed5d18ce authored by Felix Dietrich's avatar Felix Dietrich
Browse files

WIP dojo

parent 7688aaf7
......@@ -25,10 +25,10 @@ import org.vadere.gui.projectview.view.ProjectView;
import org.vadere.gui.projectview.view.ScenarioJPanel;
import org.vadere.gui.topographycreator.model.AgentWrapper;
import org.vadere.gui.topographycreator.model.TopographyCreatorModel;
import org.vadere.simulator.projects.io.JsonConverter;
import org.vadere.state.attributes.Attributes;
import org.vadere.state.scenario.Pedestrian;
import org.vadere.state.scenario.ScenarioElement;
import org.vadere.state.util.StateJsonConverter;
/**
* The ScenarioElementView display's a ScenarioElement in JSON-Format.
......@@ -131,14 +131,14 @@ public class ScenarioElementView extends JPanel implements ISelectScenarioElemen
// JsonSerializerVShape shapeSerializer = new JsonSerializerVShape();
Pedestrian ped = null;
try {
ped = JsonConverter.deserializePedestrian(json);
ped = StateJsonConverter.deserializePedestrian(json);
} catch (IOException e) {
e.printStackTrace();
}
((AgentWrapper) element).setAgentInitialStore(ped);
} else {
try {
Attributes attributes = JsonConverter.deserializeScenarioElementType(json, element.getType());
Attributes attributes = StateJsonConverter.deserializeScenarioElementType(json, element.getType());
ReflectionAttributeModifier.setAttributes(element, attributes);
ScenarioJPanel.removeJsonParsingErrorMsg();
ProjectView.getMainWindow().refreshScenarioNames();
......@@ -177,11 +177,11 @@ public class ScenarioElementView extends JPanel implements ISelectScenarioElemen
try {
if (scenarioElement instanceof AgentWrapper) {
this.txtrTextfiletextarea.setText(
JsonConverter.serializeObject(((AgentWrapper) scenarioElement).getAgentInitialStore()));
StateJsonConverter.serializeObject(((AgentWrapper) scenarioElement).getAgentInitialStore()));
} else if (scenarioElement instanceof Pedestrian) {
this.txtrTextfiletextarea.setText(JsonConverter.serializeObject(scenarioElement));
this.txtrTextfiletextarea.setText(StateJsonConverter.serializeObject(scenarioElement));
} else {
this.txtrTextfiletextarea.setText(JsonConverter
this.txtrTextfiletextarea.setText(StateJsonConverter
.serializeObject(ReflectionAttributeModifier.getAttributes(scenarioElement)));
}
} catch (JsonProcessingException e) {
......
......@@ -21,7 +21,6 @@ import org.apache.log4j.Logger;
import org.vadere.gui.components.model.SimulationModel;
import org.vadere.gui.postvisualization.utils.PotentialFieldContainer;
import org.vadere.simulator.projects.ScenarioRunManager;
import org.vadere.simulator.projects.io.JsonConverter;
import org.vadere.state.scenario.Agent;
import org.vadere.state.scenario.Pedestrian;
import org.vadere.state.scenario.ScenarioElement;
......@@ -29,6 +28,7 @@ import org.vadere.state.scenario.Topography;
import org.vadere.state.scenario.TopographyIterator;
import org.vadere.state.simulation.Step;
import org.vadere.state.simulation.Trajectory;
import org.vadere.state.util.StateJsonConverter;
import org.vadere.util.io.IOUtils;
import org.vadere.util.io.parser.JsonLogicParser;
import org.vadere.util.io.parser.VPredicate;
......@@ -221,7 +221,7 @@ public class PostvisualizationModel extends SimulationModel<PostvisualizationCon
}
public Optional<Color> getColor(final Agent agent) {
JsonNode jsonObj = JsonConverter.toJsonNode(agent);
JsonNode jsonObj = StateJsonConverter.toJsonNode(agent);
Optional<Map.Entry<Integer, VPredicate<JsonNode>>> firstEntry = colorEvalFunctions.entrySet().stream().filter(
entry -> {
try {
......
......@@ -11,8 +11,8 @@ import org.vadere.gui.topographycreator.view.TopographyWindow;
import org.vadere.simulator.projects.ProjectFinishedListener;
import org.vadere.simulator.projects.ScenarioRunManager;
import org.vadere.simulator.projects.VadereProject;
import org.vadere.simulator.projects.io.JsonConverter;
import org.vadere.state.scenario.Topography;
import org.vadere.state.util.StateJsonConverter;
import org.vadere.util.io.IOUtils;
import java.awt.*;
......@@ -119,7 +119,7 @@ public class ScenarioJPanel extends JPanel implements IProjectChangeListener, Pr
Messages.getString("Tab.Model.confirmLoadTemplate.title"),
JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION) {
try {
attributesModelView.setText(JsonConverter.serializeModelPreset(modelDefinition));
attributesModelView.setText(StateJsonConverter.serializeModelPreset(modelDefinition));
} catch (Exception e1) {
e1.printStackTrace();
}
......@@ -136,7 +136,7 @@ public class ScenarioJPanel extends JPanel implements IProjectChangeListener, Pr
@Override
public void actionPerformed(ActionEvent e) {
try {
attributesModelView.setText(JsonConverter.addAttributesModel(attributesClassName,
attributesModelView.setText(StateJsonConverter.addAttributesModel(attributesClassName,
attributesModelView.getText()));
} catch (IOException e1) {
e1.printStackTrace();
......
......@@ -11,9 +11,9 @@ 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.*;
......@@ -169,18 +169,18 @@ public class TextView extends JPanel {
try {
switch (attributeType) {
case MODEL:
ModelDefinition modelDefinition = JsonConverter.deserializeModelDefinition(json);
ModelDefinition modelDefinition = StateJsonConverter.deserializeModelDefinition(json);
currentScenario.getScenarioStore().mainModel = modelDefinition.getMainModel();
currentScenario.setAttributesModel(modelDefinition.getAttributesList());
break;
case SIMULATION:
currentScenario.setAttributesSimulation(JsonConverter.deserializeAttributesSimulation(json));
currentScenario.setAttributesSimulation(StateJsonConverter.deserializeAttributesSimulation(json));
break;
case OUTPUTPROCESSOR:
currentScenario.setDataProcessingJsonManager(DataProcessingJsonManager.deserialize(json));
break;
case TOPOGRAPHY:
currentScenario.setTopography(JsonConverter.deserializeTopography(json));
currentScenario.setTopography(StateJsonConverter.deserializeTopography(json));
break;
}
currentScenario.updateCurrentStateSerialized();
......@@ -212,12 +212,12 @@ public class TextView extends JPanel {
switch (attributeType) {
case MODEL:
this.txtrTextfiletextarea.setText(JsonConverter.serializeMainModelAttributesModelBundle(
this.txtrTextfiletextarea.setText(StateJsonConverter.serializeMainModelAttributesModelBundle(
scenario.getSortedAttributesMode(), scenario.getScenarioStore().mainModel));
break;
case SIMULATION:
this.txtrTextfiletextarea
.setText(JsonConverter.serializeAttributesSimulation(scenario.getAttributesSimulation()));
.setText(StateJsonConverter.serializeAttributesSimulation(scenario.getAttributesSimulation()));
break;
case OUTPUTPROCESSOR:
this.txtrTextfiletextarea.setText(scenario.getDataProcessingJsonManager().serialize());
......@@ -226,7 +226,7 @@ public class TextView extends JPanel {
case TOPOGRAPHY:
Topography topography = scenario.getTopography().clone();
topography.removeBoundary();
this.txtrTextfiletextarea.setText(JsonConverter.serializeTopography(topography));
this.txtrTextfiletextarea.setText(StateJsonConverter.serializeTopography(topography));
break;
}
this.txtrTextfiletextarea.setCaretPosition(0);
......
......@@ -4,8 +4,8 @@ import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import org.vadere.simulator.projects.io.JsonConverter;
import org.vadere.state.scenario.Topography;
import org.vadere.state.util.StateJsonConverter;
import com.fasterxml.jackson.core.JsonProcessingException;
......@@ -29,7 +29,7 @@ public class JSONWriter {
}
public static void writeTopography(Topography topography, PrintStream stream) throws JsonProcessingException {
stream.print(JsonConverter.serializeTopography(topography));
stream.print(StateJsonConverter.serializeTopography(topography));
stream.flush();
}
}
......@@ -8,7 +8,6 @@ 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;
......@@ -16,6 +15,7 @@ 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;
......@@ -69,9 +69,9 @@ public class TestProjectWriterAndReader {
VadereProject projectJson = IOVadere.readProjectJson(testFileJson);
assertEquals(
projectJson.getScenarios().stream().map(scenario -> JsonConverter.serializeScenarioRunManager(scenario))
projectJson.getScenarios().stream().map(scenario -> StateJsonConverter.serializeScenarioRunManager(scenario))
.collect(Collectors.toList()),
testProject.getScenarios().stream().map(scenario -> JsonConverter.serializeScenarioRunManager(scenario))
testProject.getScenarios().stream().map(scenario -> StateJsonConverter.serializeScenarioRunManager(scenario))
.collect(Collectors.toList()));
}
}
package org.vadere.simulator.projects;
import org.vadere.simulator.projects.io.JsonConverter;
import org.vadere.state.util.StateJsonConverter;
import org.vadere.util.io.IOUtils;
import java.io.IOException;
......@@ -73,7 +73,7 @@ public class ProjectWriter {
for (ScenarioRunManager scenario : project.getScenarios()) {
IOUtils.writeTextFile(
getScenarioPath(scenarioPath, scenario).toString(),
JsonConverter.serializeScenarioRunManager(scenario, includeCommitHash));
StateJsonConverter.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(),
JsonConverter.serializeScenarioRunManager(scenario, true));
StateJsonConverter.serializeScenarioRunManager(scenario, true));
}
public static Path getScenarioPath(Path scenariosDir, ScenarioRunManager scenario) {
......
......@@ -9,11 +9,11 @@ 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.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;
......@@ -78,7 +78,7 @@ public class ScenarioRunManager implements Runnable {
}
public void saveChanges() { // get's called by VadereProject.saveChanges on init
savedStateSerialized = JsonConverter.serializeScenarioRunManager(this);
savedStateSerialized = StateJsonConverter.serializeScenarioRunManager(this);
currentStateSerialized = savedStateSerialized;
}
......@@ -87,11 +87,11 @@ public class ScenarioRunManager implements Runnable {
}
public void updateCurrentStateSerialized() {
currentStateSerialized = JsonConverter.serializeScenarioRunManager(this);
currentStateSerialized = StateJsonConverter.serializeScenarioRunManager(this);
}
public String getDiff() {
String currentStateSerialized = JsonConverter.serializeScenarioRunManager(this);
String currentStateSerialized = StateJsonConverter.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(JsonConverter.serializeScenarioRunManager(this, true));
out.println(StateJsonConverter.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 = JsonConverter.cloneScenarioRunManager(this);
clonedScenario = StateJsonConverter.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 = JsonConverter.deserializeScenarioRunManager(savedStateSerialized);
ScenarioRunManager srm = StateJsonConverter.deserializeScenarioRunManager(savedStateSerialized);
// not all necessary! only the ones that could have changed
this.scenarioStore = srm.scenarioStore;
this.outputPath = srm.outputPath;
......
......@@ -5,11 +5,11 @@ 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;
import org.vadere.state.scenario.Topography;
import org.vadere.state.util.StateJsonConverter;
import org.vadere.util.reflection.VadereClassNotFoundException;
import com.fasterxml.jackson.core.JsonProcessingException;
......@@ -49,7 +49,7 @@ public class ScenarioStore {
@Override
public ScenarioStore clone() {
try {
return JsonConverter.cloneScenarioStore(this);
return StateJsonConverter.cloneScenarioStore(this);
} catch (IOException | VadereClassNotFoundException e) {
throw new RuntimeException(e);
// Do not return null or Optional, that does not make sense!
......@@ -57,7 +57,7 @@ public class ScenarioStore {
}
public String hashOfJsonRepresentation() throws JsonProcessingException {
return DigestUtils.sha1Hex(JsonConverter.serializeObject(this));
return DigestUtils.sha1Hex(StateJsonConverter.serializeObject(this));
}
}
......@@ -12,8 +12,8 @@ import org.vadere.simulator.projects.dataprocessing.outputfile.OutputFile;
import org.vadere.simulator.projects.dataprocessing.processor.DataProcessor;
import org.vadere.simulator.projects.dataprocessing.store.DataProcessorStore;
import org.vadere.simulator.projects.dataprocessing.store.OutputFileStore;
import org.vadere.simulator.projects.io.JsonConverter;
import org.vadere.state.attributes.processor.AttributesProcessor;
import org.vadere.state.util.StateJsonConverter;
import org.vadere.util.reflection.DynamicClassInstantiator;
import java.io.IOException;
......@@ -59,7 +59,7 @@ public class DataProcessingJsonManager {
private boolean isTimestamped;
static {
mapper = JsonConverter.getMapper();
mapper = StateJsonConverter.getMapper();
writer = mapper.writerWithDefaultPrettyPrinter();
outputFileInstantiator = new DynamicClassInstantiator<>();
......
......@@ -5,6 +5,7 @@ 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;
......@@ -25,7 +26,7 @@ public class IOVadere {
private static Logger logger = LogManager.getLogger(IOVadere.class);
public static ScenarioRunManager fromJson(final String json) throws IOException {
return JsonConverter.deserializeScenarioRunManager(json);
return StateJsonConverter.deserializeScenarioRunManager(json);
}
public static VadereProject readProjectJson(final String filepath)
......@@ -51,7 +52,7 @@ public class IOVadere {
for (File file : IOUtils.getFilesInScenarioDirectory(p)) {
ScenarioRunManager scenario =
JsonConverter.deserializeScenarioRunManager(IOUtils.readTextFile(file.getAbsolutePath()));
StateJsonConverter.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.");
......
......@@ -7,7 +7,6 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.vadere.simulator.projects.io.JsonConverter;
import org.vadere.simulator.projects.migration.MigrationAssistant.Version;
import org.vadere.simulator.projects.migration.incidents.AddTextNodeIncident;
import org.vadere.simulator.projects.migration.incidents.DeletionIncident;
......@@ -18,6 +17,7 @@ import org.vadere.simulator.projects.migration.incidents.RenameInArrayIncident;
import org.vadere.simulator.projects.migration.incidents.RenameIncident;
import org.vadere.simulator.projects.migration.incidents.specialized.AttributesPotentialCompactVSosmIncident;
import org.vadere.simulator.projects.migration.incidents.specialized.MoveSpawnDelayIntoDistributionParametersIncident;
import org.vadere.state.util.StateJsonConverter;
import static org.vadere.simulator.projects.migration.MigrationAssistant.Version.*;
......@@ -64,7 +64,7 @@ public class IncidentDatabase {
addIncident(NOT_A_RELEASE, new MissingMainModelIncident( // must come AFTER the model renaming that was done in the loop before
path("vadere"),
JsonConverter.MAIN_MODEL_KEY,
StateJsonConverter.MAIN_MODEL_KEY,
path("vadere", "attributesModel")));
addIncident(NOT_A_RELEASE, new AddTextNodeIncident(
......@@ -77,7 +77,7 @@ public class IncidentDatabase {
addIncident(NOT_A_RELEASE, new DeletionIncident(path("topographyhash")));
addIncident(NOT_A_RELEASE, new DeletionIncident(path("attributeshash")));
addIncident(NOT_A_RELEASE, new RenameIncident(path("vadere"), JsonConverter.SCENARIO_KEY));
addIncident(NOT_A_RELEASE, new RenameIncident(path("vadere"), StateJsonConverter.SCENARIO_KEY));
// - - - - - - - - - - - - "0.1" to "0.2" - - - - - - - - - - - -
......
......@@ -14,10 +14,10 @@ import java.util.stream.Collectors;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.vadere.simulator.projects.io.JsonConverter;
import org.vadere.simulator.projects.migration.incidents.ExceptionIncident;
import org.vadere.simulator.projects.migration.incidents.Incident;
import org.vadere.simulator.projects.migration.incidents.VersionBumpIncident;
import org.vadere.state.util.StateJsonConverter;
import org.vadere.util.io.IOUtils;
public class MigrationAssistant {
......@@ -147,7 +147,7 @@ public class MigrationAssistant {
private static boolean analyzeScenario(Path scenarioFilePath, Path legacyDir, StringBuilder log, boolean isScenario) throws IOException, MigrationException {
String json = IOUtils.readTextFile(scenarioFilePath);
JsonNode node = JsonConverter.deserializeToNode(json);
JsonNode node = StateJsonConverter.deserializeToNode(json);
Graph graph = new Graph(node);
String outputScenarioParentFolderName = isScenario ? "" : scenarioFilePath.getParent().getFileName().toString() + " _ ";
......@@ -198,7 +198,7 @@ public class MigrationAssistant {
if (legacyDir != null) {
moveFileAddExtension(scenarioFilePath, legacyDir, LEGACY_EXTENSION, false);
}
IOUtils.writeTextFile(scenarioFilePath.toString(), JsonConverter.serializeJsonNode(node));
IOUtils.writeTextFile(scenarioFilePath.toString(), StateJsonConverter.serializeJsonNode(node));
return true;
}
......
......@@ -2,9 +2,9 @@ package org.vadere.simulator.projects.migration.incidents;
import com.fasterxml.jackson.databind.JsonNode;
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;
......@@ -25,7 +25,7 @@ public class ExceptionIncident extends Incident {
public void resolve(Graph graph, StringBuilder log) throws MigrationException {
while (true) {
try {
JsonConverter.deserializeScenarioRunManagerFromNode(node);
StateJsonConverter.deserializeScenarioRunManagerFromNode(node);
break;
} catch (IOException e) {
String errMsg = e.getMessage();
......
......@@ -2,11 +2,11 @@ package org.vadere.simulator.projects.migration.incidents.specialized;
import static org.vadere.simulator.projects.migration.IncidentDatabase.path;
import org.vadere.simulator.projects.io.JsonConverter;
import org.vadere.simulator.projects.migration.Graph;
import org.vadere.simulator.projects.migration.MigrationException;
import org.vadere.simulator.projects.migration.incidents.Incident;
import org.vadere.state.attributes.scenario.AttributesSource;
import org.vadere.state.util.StateJsonConverter;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
......@@ -38,7 +38,7 @@ public class MoveSpawnDelayIntoDistributionParametersIncident extends Incident {
(distribution == null
|| distribution.asText().equals(AttributesSource.CONSTANT_DISTRIBUTION))) {
s.set("distributionParameters", JsonConverter.toJsonNode(new Double[] {spawnDelay}));
s.set("distributionParameters", StateJsonConverter.toJsonNode(new Double[] {spawnDelay}));
}
s.remove("spawnDelay");
......
package org.vadere.state.attributes;
import java.lang.reflect.Field;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
/**
* Abstract class for all static simulation attributes. Provides reflection
* based methods to convert the fields and values of the Attributes classes from
* and to key-value store.
*
*
*/
public abstract class Attributes {
public Attributes() {}
/**
* Retrieves the values from store and tries to set the fields of the given
* Attributes object.
*
* @param store
*/
protected void fromKeyValueStore(JsonElement store) {
Attributes newAtt = new Gson().fromJson(store, this.getClass());
Field[] fields = this.getClass().getFields();
for (Field field : fields) {
Object value;
try {
value = newAtt.getClass().getField(field.getName()).get(newAtt);
field.set(this, value);
} catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException e) {
e.printStackTrace();
}
}
}
}
package org.vadere.state.attributes;
import java.io.IOException;
import java.lang.reflect.Field;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.vadere.util.io.IOUtils;
import com.google.gson.Gson;
import org.vadere.state.util.StateJsonConverter;
import org.vadere.state.util.TextOutOfNodeException;
public class AttributesBuilder<T extends Attributes> {
private static Logger logger = LogManager.getLogger(AttributesBuilder.class);
private final T attributes;
private final Gson gson;
@SuppressWarnings("unchecked")
@Deprecated
public AttributesBuilder(T attributes) {
this.gson = IOUtils.getGson();
this.attributes = (T) gson.fromJson(gson.toJson(attributes), attributes.getClass());
this.attributes = cloneAttribute(attributes);
}
public void setField(String name, Object value) {
......@@ -34,8 +30,14 @@ public class AttributesBuilder<T extends Attributes> {
}
@SuppressWarnings("unchecked")
public T build() {
return (T) gson.fromJson(gson.toJson(attributes), attributes.getClass());
return (T) cloneAttribute(attributes);
}
@SuppressWarnings("unchecked")
private T cloneAttribute(T attributes)
{
// TODO should we implement cloneable in Attributes instead?
return (T) StateJsonConverter.deserializeObjectFromJson(StateJsonConverter.serializeObject(attributes), attributes.getClass());
}
}
package org.vadere.state.attributes.scenario;
import java.io.IOException;
import java.util.Arrays;
import org.apache.commons.math3.distribution.RealDistribution;
import org.vadere.state.scenario.ConstantDistribution;
import org.vadere.state.util.StateJsonConverter;
import org.vadere.state.util.TextOutOfNodeException;
import org.vadere.util.io.IOUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
public class SourceTestAttributesBuilder {
private double startTime = 1;
......@@ -18,7 +23,7 @@ public class SourceTestAttributesBuilder {
public AttributesSource getResult() {
String json = generateSourceAttributesJson();
return IOUtils.getGson().fromJson(json, AttributesSource.class);
return StateJsonConverter.deserializeObjectFromJson(json, AttributesSource.class);
}