Commit 6c627813 authored by Stefan Schuhbaeck's avatar Stefan Schuhbaeck
Browse files

Merge branch 'measurementAreaRefactorProcessors_rebased' into 'master'

Measurement area refactor processors rebased

See merge request !55
parents dcffc7d3 83cb5e48
Pipeline #103785 passed with stages
in 137 minutes and 9 seconds
package org.vadere.simulator.projects.migration.incidents;
package org.vadere.simulator.projects.migration.incident.incidents;
import java.util.List;
import org.vadere.simulator.projects.migration.Tree;
import org.vadere.simulator.projects.migration.incident.Tree;
import org.vadere.simulator.projects.migration.MigrationException;
public class RenameInArrayIncident extends Incident { // better name?
......
package org.vadere.simulator.projects.migration.incidents;
package org.vadere.simulator.projects.migration.incident.incidents;
import java.util.List;
import org.vadere.simulator.projects.migration.Tree;
import org.vadere.simulator.projects.migration.incident.Tree;
import org.vadere.simulator.projects.migration.MigrationException;
public class RenameIncident extends Incident {
......
package org.vadere.simulator.projects.migration.incidents;
package org.vadere.simulator.projects.migration.incident.incidents;
import org.vadere.simulator.projects.migration.Tree;
import org.vadere.simulator.projects.migration.incident.Tree;
import org.vadere.simulator.entrypoints.Version;
import org.vadere.simulator.projects.migration.MigrationException;
......
package org.vadere.simulator.projects.migration.incidents.specialized;
package org.vadere.simulator.projects.migration.incident.incidents.specialized;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
......@@ -7,14 +7,14 @@ import org.vadere.simulator.models.potential.PotentialFieldObstacleCompact;
import org.vadere.simulator.models.potential.PotentialFieldObstacleOSM;
import org.vadere.simulator.models.potential.PotentialFieldPedestrianCompact;
import org.vadere.simulator.models.potential.PotentialFieldPedestrianOSM;
import org.vadere.simulator.projects.migration.Tree;
import org.vadere.simulator.projects.migration.incident.Tree;
import org.vadere.simulator.projects.migration.MigrationException;
import org.vadere.simulator.projects.migration.incidents.Incident;
import org.vadere.simulator.projects.migration.incident.incidents.Incident;
import org.vadere.state.attributes.models.AttributesOSM;
import org.vadere.state.attributes.models.AttributesPotentialCompact;
import org.vadere.state.attributes.models.AttributesPotentialOSM;
import static org.vadere.simulator.projects.migration.IncidentDatabase.path;
import static org.vadere.simulator.projects.migration.incident.IncidentDatabase.path;
/**
* For one particular case, not generalizable.
......
package org.vadere.simulator.projects.migration.incidents.specialized;
package org.vadere.simulator.projects.migration.incident.incidents.specialized;
import static org.vadere.simulator.projects.migration.IncidentDatabase.path;
import static org.vadere.simulator.projects.migration.incident.IncidentDatabase.path;
import org.vadere.simulator.projects.migration.Tree;
import org.vadere.simulator.projects.migration.incident.Tree;
import org.vadere.simulator.projects.migration.MigrationException;
import org.vadere.simulator.projects.migration.incidents.Incident;
import org.vadere.simulator.projects.migration.incident.incidents.Incident;
import org.vadere.state.attributes.scenario.AttributesSource;
import org.vadere.state.util.StateJsonConverter;
......
package org.vadere.simulator.projects.migration.jolttranformation;
import com.fasterxml.jackson.databind.JsonNode;
import org.vadere.annotation.factories.migrationassistant.MigrationTransformation;
import org.vadere.simulator.entrypoints.Version;
import org.vadere.simulator.projects.migration.MigrationException;
import org.vadere.state.util.StateJsonConverter;
import java.util.LinkedHashMap;
@MigrationTransformation(targetVersionLabel = "0.2")
public class JoltTransformV1toV2 extends JoltTransformation {
public JoltTransformV1toV2() {
super(Version.V0_2);
}
@Override
protected void initPostHooks() {
postTransformHooks.add(JoltTransformV1toV2::sort);
}
@SuppressWarnings("unchecked")
public static JsonNode sort (JsonNode node) throws MigrationException{
LinkedHashMap source = (LinkedHashMap) StateJsonConverter.convertJsonNodeToObject(node);
LinkedHashMap<Object, Object> sortedRoot = new LinkedHashMap<>();
putObject(sortedRoot, source, "name");
putObject(sortedRoot, source, "description");
putObject(sortedRoot, source, "release");
putObject(sortedRoot, source, "commithash");
putObject(sortedRoot, source, "processWriters", "files", "processors", "isTimestamped");
putObject(sortedRoot, source, "scenario", "mainModel", "attributesModel", "attributesSimulation", "eventInfos", "topography");
return StateJsonConverter.deserializeToNode(sortedRoot);
}
}
package org.vadere.simulator.projects.migration.jsontranformation;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.vadere.simulator.entrypoints.Version;
import org.vadere.simulator.projects.migration.MigrationException;
import org.vadere.state.util.StateJsonConverter;
import org.vadere.util.logging.Logger;
import java.util.LinkedHashMap;
import java.util.LinkedList;
public abstract class AbstractJsonTransformation implements JsonTransformation, JsonNodeExplorer {
protected final static Logger logger = Logger.getLogger(AbstractJsonTransformation.class);
LinkedList<JsonTransformationHook> postHooks;
LinkedList<JsonTransformationHook> preHooks;
public AbstractJsonTransformation() {
this.postHooks = new LinkedList<>();
this.preHooks = new LinkedList<>();
initDefaultHooks();
}
public static JsonTransformation get(Version currentVersion) throws MigrationException {
if (currentVersion.equalOrSamller(Version.UNDEFINED))
throw new MigrationException("There is now Transformation for Version " + Version.UNDEFINED.toString());
if (currentVersion.equals(Version.latest()))
throw new MigrationException("No Transformation needed. Already latest Version!");
org.vadere.simulator.projects.migration.jsontranformation.JsonTransformationFactory factory
= org.vadere.simulator.projects.migration.jsontranformation.JsonTransformationFactory.instance();
JsonTransformation ret;
try {
ret = factory.getInstanceOf(currentVersion.nextVersion().label('_'));
} catch (ClassNotFoundException e) {
throw new MigrationException("Cannot find Transformation in Factory for Version " + currentVersion.nextVersion().label());
}
return ret;
}
@Override
public void addPreHookFirst(JsonTransformationHook hook) {
preHooks.addFirst(hook);
}
@Override
public void addPreHookLast(JsonTransformationHook hook) {
preHooks.addLast(hook);
}
@Override
public void addPostHookFirst(JsonTransformationHook hook) {
postHooks.addFirst(hook);
}
@Override
public void addPostHookLast(JsonTransformationHook hook) {
postHooks.addLast(hook);
}
@Override
public JsonNode applyPreHooks(JsonNode node) throws MigrationException {
JsonNode ret = node;
for (JsonTransformationHook hook : preHooks) {
ret = hook.applyHook(ret);
}
return ret;
}
@Override
public JsonNode applyPostHooks(JsonNode node) throws MigrationException {
JsonNode ret = node;
for (JsonTransformationHook hook : postHooks) {
ret = hook.applyHook(ret);
}
return ret;
}
@Override
abstract public JsonNode applyTransformation(JsonNode node) throws MigrationException;
protected abstract void initDefaultHooks();
/**
* Create a Copy of Json and put nodes in user specified order
*
* @param target new LinkedHashMap with wherer to put nodes
* @param source source
* @param key key to add to new HashMap
* @param children Specify Order on second level
*/
public static void putObject(LinkedHashMap<Object, Object> target,
LinkedHashMap<Object, Object> source,
String key, String... children) throws MigrationException {
Object obj = source.get(key);
if (obj == null) {
throw new MigrationException("Scenario must contain Key: " + key);
}
if (children.length > 0) {
LinkedHashMap<Object, Object> node = new LinkedHashMap<>();
LinkedHashMap<Object, Object> parent = (LinkedHashMap<Object, Object>) obj;
for (String childKey : children) {
Object childObj = parent.get(childKey);
if (childObj != null) {
node.put(childKey, childObj);
}
}
obj = node;
}
target.put(key, obj);
}
public JsonNode addCommitHashWarningIfMissing(JsonNode node){
JsonNode commitHash = path(node, "commithash");
if (commitHash.isMissingNode()){
((ObjectNode)node).put("commithash", "warning: no commit hash");
}
return node;
}
public JsonNode sort (JsonNode node) throws MigrationException{
LinkedHashMap source = (LinkedHashMap) StateJsonConverter.convertJsonNodeToObject(node);
LinkedHashMap<Object, Object> sortedRoot = new LinkedHashMap<>();
putObject(sortedRoot, source, "name");
putObject(sortedRoot, source, "description");
putObject(sortedRoot, source, "release");
putObject(sortedRoot, source, "commithash");
putObject(sortedRoot, source, "processWriters", "files", "processors", "isTimestamped");
putObject(sortedRoot, source, "scenario", "mainModel", "attributesModel", "attributesSimulation", "eventInfos", "topography");
return StateJsonConverter.deserializeToNode(sortedRoot);
}
}
package org.vadere.simulator.projects.migration.jolttranformation;
package org.vadere.simulator.projects.migration.jsontranformation;
import com.bazaarvoice.jolt.Chainr;
import com.bazaarvoice.jolt.Diffy;
......@@ -7,72 +7,21 @@ import com.fasterxml.jackson.databind.JsonNode;
import org.vadere.simulator.entrypoints.Version;
import org.vadere.simulator.projects.migration.MigrationException;
import org.vadere.simulator.projects.migration.jolttranformation.JoltTransformationFactory;
import org.vadere.state.util.StateJsonConverter;
import org.vadere.util.logging.Logger;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.concurrent.ConcurrentHashMap;
public abstract class JoltTransformation implements JsonNodeExplorer{
public abstract class JoltTransformation extends AbstractJsonTransformation implements JsonTransformation, JsonNodeExplorer {
protected final static Logger logger = Logger.getLogger(JoltTransformation.class);
private static ConcurrentHashMap<Version, JoltTransformation> transformations = new ConcurrentHashMap<>();
private final Chainr chainr; // Transformation from (version -1) to version
private final Chainr identity; // Identity of version
private final Diffy diffy;
protected ArrayList<PostTransformHook> postTransformHooks;
public static JoltTransformation get(Version currentVersion) throws MigrationException {
if (currentVersion.equalOrSamller(Version.UNDEFINED))
throw new MigrationException("There is now Transformation for Version " + Version.UNDEFINED.toString());
if (currentVersion.equals(Version.latest()))
throw new MigrationException("No Transformation needed. Already latest Version!");
JoltTransformationFactory factory = JoltTransformationFactory.instance();
JoltTransformation ret;
try {
ret = factory.getInstanceOf(currentVersion.nextVersion().label('_'));
} catch (ClassNotFoundException e) {
throw new MigrationException("Cannot find Transformation in Factory for Version " + currentVersion.nextVersion().label());
}
return ret;
}
public static Path getTransforamtionFileFromRessource(Version toVersion) {
String transformString = getTransforamtionResourcePath(
toVersion.previousVersion().label('-'),
toVersion.label('-'));
URI res;
try {
res = JoltTransformation.class.getResource(transformString).toURI();
} catch (URISyntaxException e) {
throw new RuntimeException("Cannot find transformation file for version " + toVersion.label());
}
return Paths.get(res);
}
public static Path getIdenityFileFromRessource(Version v) {
String idenityString = getIdentiyResoucrePath(v.label('-'));
URI res = null;
try {
res = JoltTransformation.class.getResource(idenityString).toURI();
} catch (URISyntaxException e) {
throw new RuntimeException("Cannot find identity file for version" + v.label());
}
return Paths.get(res);
}
private final Diffy diffy; // diff tool to check if jolt transformation worked.
public static Path getTransforamtionFileFromFileSystem(Path baseDir, Version toVersion) {
String transformString = getTransforamtionResourcePath(
......@@ -88,8 +37,6 @@ public abstract class JoltTransformation implements JsonNodeExplorer{
}
public static String getTransforamtionResourcePath(String from, String to) {
return "/transform_v" + from.toUpperCase() + "_to_v" + to.toUpperCase() + ".json";
}
......@@ -99,12 +46,10 @@ public abstract class JoltTransformation implements JsonNodeExplorer{
}
public JoltTransformation(String transformation, String identity) {
super();
this.chainr = Chainr.fromSpec(JsonUtils.classpathToList(transformation));
this.identity = Chainr.fromSpec(JsonUtils.classpathToList(identity));
// Output of Transformation
this.postTransformHooks = new ArrayList<>();
this.diffy = new Diffy();
initPostHooks();
}
public JoltTransformation(Version targetVersion) {
......@@ -114,11 +59,13 @@ public abstract class JoltTransformation implements JsonNodeExplorer{
getIdentiyResoucrePath(targetVersion.label('-')));
}
@Override
public JsonNode applyTransformation(JsonNode root) throws MigrationException {
Object rootObject = StateJsonConverter.convertJsonNodeToObject(root);
rootObject = applyTransformation(rootObject);
JsonNode jsonRoot = StateJsonConverter.deserializeToNode(rootObject);
return applyPostHooks(jsonRoot);
return jsonRoot;
}
private Object applyTransformation(Object root) throws MigrationException {
......@@ -134,60 +81,10 @@ public abstract class JoltTransformation implements JsonNodeExplorer{
}
}
public JsonNode applyPostHooks(JsonNode root) throws MigrationException {
JsonNode ret = root;
for (PostTransformHook hook : postTransformHooks) {
ret = hook.applyHook(ret);
}
return ret;
}
/**
* add PostHooks in the correct order.
*/
protected abstract void initPostHooks();
public ArrayList<PostTransformHook> getPostTransformHooks() {
return postTransformHooks;
}
public void setPostTransformHooks(ArrayList<PostTransformHook> postTransformHooks) {
this.postTransformHooks = postTransformHooks;
}
/**
* Create a Copy of Json and put nodes in user specified order
*
* @param target new LinkedHashMap with wherer to put nodes
* @param source source
* @param key key to add to new HashMap
* @param children Specify Order on second level
*/
static void putObject(LinkedHashMap<Object, Object> target,
LinkedHashMap<Object, Object> source,
String key, String... children) throws MigrationException {
Object obj = source.get(key);
if (obj == null) {
throw new MigrationException("Scenario must contain Key: " + key);
}
if (children.length > 0) {
LinkedHashMap<Object, Object> node = new LinkedHashMap<>();
LinkedHashMap<Object, Object> parent = (LinkedHashMap<Object, Object>) obj;
for (String childKey : children) {
Object childObj = parent.get(childKey);
if (childObj != null) {
// throw new MigrationException("Object with Key " + key + " does not has child with key" + childKey);
node.put(childKey, childObj);
}
}
obj = node;
}
target.put(key, obj);
}
protected abstract void initDefaultHooks();
}
package org.vadere.simulator.projects.migration;
package org.vadere.simulator.projects.migration.jsontranformation;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import org.vadere.simulator.entrypoints.Version;
import org.vadere.simulator.projects.migration.jolttranformation.JoltTransformation;
import org.vadere.simulator.projects.migration.MigrationAssistant;
import org.vadere.simulator.projects.migration.MigrationException;
import org.vadere.simulator.projects.migration.MigrationLogger;
import org.vadere.simulator.projects.migration.MigrationOptions;
import org.vadere.simulator.projects.migration.MigrationResult;
import org.vadere.state.util.StateJsonConverter;
import org.vadere.util.io.IOUtils;
import org.vadere.util.logging.Logger;
......@@ -22,18 +26,18 @@ import static org.vadere.util.io.IOUtils.OUTPUT_DIR;
import static org.vadere.util.io.IOUtils.SCENARIO_DIR;
public class JoltMigrationAssistant extends MigrationAssistant {
public class JsonMigrationAssistant extends MigrationAssistant {
private final static Logger logger = Logger.getLogger(JoltMigrationAssistant.class);
private final static Logger logger = Logger.getLogger(JsonMigrationAssistant.class);
private MigrationLogger migrationLogger;
public JoltMigrationAssistant(MigrationOptions options) {
public JsonMigrationAssistant(MigrationOptions options) {
super(options);
migrationLogger = new MigrationLogger();
}
public JoltMigrationAssistant() {
public JsonMigrationAssistant() {
this(MigrationOptions.defaultOptions());
}
......@@ -54,12 +58,12 @@ public class JoltMigrationAssistant extends MigrationAssistant {
Path scenarioDir = Paths.get(projectFolderPath, SCENARIO_DIR);
if (Files.exists(scenarioDir)) {
stats = analyzeDirectory(scenarioDir, SCENARIO_DIR);
stats = migrateDirectory(scenarioDir, SCENARIO_DIR);
}
Path outputDir = Paths.get(projectFolderPath, OUTPUT_DIR);
if (Files.exists(outputDir)) {
MigrationResult outputDirStats = analyzeDirectory(outputDir, OUTPUT_DIR);
MigrationResult outputDirStats = migrateDirectory(outputDir, OUTPUT_DIR);
stats.add(outputDirStats);
}
return stats;
......@@ -67,7 +71,7 @@ public class JoltMigrationAssistant extends MigrationAssistant {
// will return null if current and target version match
@Override
public String convertFile(Path scenarioFilePath, Version targetVersion) throws MigrationException {
public String migrateScenarioFile(Path scenarioFilePath, Version targetVersion) throws MigrationException {
JsonNode node;
try {
String json = IOUtils.readTextFile(scenarioFilePath);
......@@ -122,8 +126,8 @@ public class JoltMigrationAssistant extends MigrationAssistant {
@Override
public void migrateFile(Path scenarioFilePath, Version targetVersion, Path outputFile) throws MigrationException {
String json = convertFile(scenarioFilePath, targetVersion);
public void migrateScenarioFile(Path scenarioFilePath, Version targetVersion, Path outputFile) throws MigrationException {
String json = migrateScenarioFile(scenarioFilePath, targetVersion);
if (json == null) {
logger.info("Nothing todo scenarioFile up-to-date");
return;
......@@ -172,7 +176,7 @@ public class JoltMigrationAssistant extends MigrationAssistant {
}
private MigrationResult analyzeDirectory(Path dir, String dirName) throws IOException {
private MigrationResult migrateDirectory(Path dir, String dirName) throws IOException {
Path legacyDir = dir.getParent().resolve(LEGACY_DIR).resolve(dirName);
......@@ -188,7 +192,7 @@ public class JoltMigrationAssistant extends MigrationAssistant {
Path scenarioFilePath = Paths.get(file.getAbsolutePath());
try {
if (analyzeScenario(scenarioFilePath, legacyDir, dirName)) {
if (migrateScenario(scenarioFilePath, legacyDir, dirName)) {
stats.legacy++;
} else {
stats.upToDate++;
......@@ -213,11 +217,14 @@ public class JoltMigrationAssistant extends MigrationAssistant {
}
public JsonNode transform(JsonNode currentJson, Version targetVersion) throws MigrationException {
JoltTransformation t = JoltTransformation.get(targetVersion.previousVersion());
return t.applyTransformation(currentJson);
JsonTransformation t = AbstractJsonTransformation.get(targetVersion.previousVersion());
currentJson = t.applyPreHooks(currentJson);
currentJson = t.applyTransformation(currentJson);
currentJson = t.applyPostHooks(currentJson);
return currentJson;
}
private boolean analyzeScenario(Path scenarioFilePath, Path legacyDir, String dirName)
private boolean migrateScenario(Path scenarioFilePath, Path legacyDir, String dirName)
throws IOException, MigrationException {
String json = IOUtils.readTextFile(scenarioFilePath);
JsonNode node = StateJsonConverter.deserializeToNode(json);
......
package org.vadere.simulator.projects.migration.jolttranformation;
package org.vadere.simulator.projects.migration.jsontranformation;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;