Commit 421ec71b authored by Stefan Schuhbaeck's avatar Stefan Schuhbaeck

add factory class for JoltTransformations based on annotations

parent a7e19309
......@@ -159,7 +159,7 @@ public abstract class AbstractFactoryProcessor extends AbstractProcessor {
protected void createSingletone(final String instanceType, PrintWriter writer){
writer.append(" private static ").append(instanceType).append(" instance;").println();
writer.println();
writer.println(" //performance threadsafe Singletone. Sync block will only be used once");
writer.println(" //good performance threadsafe Singletone. Sync block will only be used once");
writer.append(" public static ").append(instanceType).append(" instance(){").println();
writer.println(" if(instance == null){");
writer.append(" synchronized (").append(instanceType).append(".class){").println();
......
package org.vadere.annotation.factories.migrationassistant;
import com.google.auto.service.AutoService;
import org.vadere.annotation.factories.BaseFactoryProcessor;
import java.io.PrintWriter;
import java.util.Set;
import javax.annotation.processing.Processor;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.type.MirroredTypeException;
@SupportedAnnotationTypes("org.vadere.annotation.factories.migrationassistant.MigrationTransformation")
@SupportedSourceVersion(SourceVersion.RELEASE_8)
@AutoService(Processor.class)
public class JoltTransformationFactoryProcessor extends BaseFactoryProcessor {
@Override
protected void addImports(Set<? extends Element> elements, PrintWriter writer) {
}
@Override
protected void addMembers(Set<? extends Element> elements, PrintWriter writer) {
}
@Override
protected void addLastConstructor(Set<? extends Element> elements, PrintWriter writer) {
for (Element e : elements) {
MigrationTransformation annotation = e.getAnnotation(MigrationTransformation.class);
String versionLabel;
try {
versionLabel = annotation.targetVersionLabel();
} catch (MirroredTypeException ex) {
versionLabel = ex.getTypeMirror().toString();
}
writer.append(" addMember(");
writer.append(quote(versionLabel)).append(", ");
writer.append(e.getSimpleName().toString()).append(".class, ");
writer.append("this::").append("get").append(name(e)).append(");");
writer.println();
}
}
@Override
protected void addLast(Set<? extends Element> elements, PrintWriter writer) {
}
}
package org.vadere.annotation.factories.migrationassistant;
import org.vadere.annotation.factories.FactoryType;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@FactoryType(
factoryClassName = "JoltTransformationFactory",
extendedClassName = "JoltTransformationBaseFactory",
factoryImports = {"org.vadere.simulator.projects.migration.jolttranformation.JoltTransformationBaseFactory"},
factoryPackage = "org.vadere.simulator.projects.migration.jolttranformation"
)
public @interface MigrationTransformation {
String targetVersionLabel();
}
......@@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.vadere.annotation.factories.migrationassistant.MigrationTransformation;
import org.vadere.simulator.entrypoints.Version;
import org.vadere.simulator.models.bhm.BehaviouralHeuristicsModel;
import org.vadere.simulator.models.gnm.GradientNavigationModel;
......@@ -25,11 +26,12 @@ import org.vadere.state.util.StateJsonConverter;
import java.util.Iterator;
import java.util.LinkedHashMap;
@MigrationTransformation(targetVersionLabel = "0.1")
public class JoltTransformV0toV1 extends JoltTransformation {
JoltTransformV0toV1(String transformation, String identity, Version version) throws MigrationException {
super(transformation, identity, version);
JoltTransformV0toV1() {
super(Version.V0_1);
}
@Override
......
......@@ -2,16 +2,18 @@ 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(String transformation, String identity, Version version) throws MigrationException {
super(transformation, identity, version);
public JoltTransformV1toV2() {
super(Version.V0_2);
}
......
package org.vadere.simulator.projects.migration.jolttranformation;
import org.vadere.annotation.factories.migrationassistant.MigrationTransformation;
import org.vadere.simulator.entrypoints.Version;
import org.vadere.simulator.projects.migration.MigrationException;
@MigrationTransformation(targetVersionLabel = "0.3")
public class JoltTransformV2toV3 extends JoltTransformation {
public JoltTransformV2toV3(String transformation, String identity, Version version) throws MigrationException {
super(transformation, identity, version);
public JoltTransformV2toV3() {
super(Version.V0_3);
}
@Override
......
......@@ -3,19 +3,21 @@ package org.vadere.simulator.projects.migration.jolttranformation;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.vadere.annotation.factories.migrationassistant.MigrationTransformation;
import org.vadere.simulator.entrypoints.Version;
import org.vadere.simulator.projects.migration.MigrationException;
import java.util.Random;
@MigrationTransformation(targetVersionLabel = "0.4")
public class JoltTransformV3toV4 extends JoltTransformation {
public JoltTransformV3toV4(String transformation, String identity, Version version) throws MigrationException {
super(transformation, identity, version);
public JoltTransformV3toV4() {
super(Version.V0_4);
}
@Override
protected void initPostHooks() throws MigrationException {
protected void initPostHooks() {
postTransformHooks.add(this::presetSeedValues);
postTransformHooks.add(JoltTransformV1toV2::sort);
}
......
......@@ -2,17 +2,19 @@ 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;
@MigrationTransformation(targetVersionLabel = "0.5")
public class JoltTransformV4toV5 extends JoltTransformation {
public JoltTransformV4toV5(String transformation, String identity, Version version) throws MigrationException {
super(transformation, identity, version);
public JoltTransformV4toV5() {
super(Version.V0_4);
}
@Override
protected void initPostHooks() throws MigrationException {
protected void initPostHooks() {
postTransformHooks.add(this::cleanupPedestrianOverlapProcessorAttribute);
postTransformHooks.add(this::addOverlapProcessors);
postTransformHooks.add(JoltTransformV1toV2::sort);
......
......@@ -9,6 +9,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.log4j.Logger;
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 java.net.URI;
......@@ -39,43 +40,61 @@ public abstract class JoltTransformation {
throw new MigrationException("No Transformation needed. Already latest Version!");
String transformationResource = getTransforamtionResourcePath(currentVersion.label('-'), currentVersion.nextVersion().label('-'));
String identityResource = getIdentiyResoucrePath(currentVersion.nextVersion().label('-'));
JoltTransformation ret = transformations.getOrDefault(currentVersion, null);
if ( ret == null) {
switch (currentVersion) {
case NOT_A_RELEASE:
ret = new JoltTransformV0toV1(transformationResource, identityResource, currentVersion);
break;
case V0_1:
ret = new JoltTransformV1toV2(transformationResource, identityResource, currentVersion);
break;
case V0_2:
ret = new JoltTransformV2toV3(transformationResource, identityResource, currentVersion);
break;
case V0_3:
ret = new JoltTransformV3toV4(transformationResource, identityResource, currentVersion);
break;
default:
throw new MigrationException("No Transformation defined for Verson " + currentVersion.toString());
}
transformations.put(currentVersion, ret);
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());
}
// String transformationResource = getTransforamtionResourcePath(currentVersion.label('-'), currentVersion.nextVersion().label('-'));
// String identityResource = getIdentiyResoucrePath(currentVersion.nextVersion().label('-'));
//
// JoltTransformation ret = transformations.getOrDefault(currentVersion, null);
// if ( ret == null) {
// switch (currentVersion) {
// case NOT_A_RELEASE:
// ret = new JoltTransformV0toV1(transformationResource, identityResource, currentVersion);
// break;
// case V0_1:
// ret = new JoltTransformV1toV2(transformationResource, identityResource, currentVersion);
// break;
// case V0_2:
// ret = new JoltTransformV2toV3(transformationResource, identityResource, currentVersion);
// break;
// case V0_3:
// ret = new JoltTransformV3toV4(transformationResource, identityResource, currentVersion);
// break;
// default:
// throw new MigrationException("No Transformation defined for Verson " + currentVersion.toString());
// }
// transformations.put(currentVersion, ret);
// }
return ret;
}
public static Path getTransforamtionFile(Version toVersion) throws URISyntaxException {
public static Path getTransforamtionFile(Version toVersion){
String transformString = getTransforamtionResourcePath(
toVersion.previousVersion().label('-'),
toVersion.label('-'));
URI res = JoltTransformation.class.getResource(transformString).toURI();
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 getIdenityFile(Version v) throws URISyntaxException {
public static Path getIdenityFile(Version v){
String idenityString = getIdentiyResoucrePath(v.label('-'));
URI res = JoltTransformation.class.getResource(idenityString).toURI();
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);
}
......@@ -87,7 +106,7 @@ public abstract class JoltTransformation {
return "/identity_v" + to.toUpperCase() + ".json";
}
public JoltTransformation(String transformation, String identity, Version version) throws MigrationException {
public JoltTransformation(String transformation, String identity) {
this.chainr = Chainr.fromSpec(JsonUtils.classpathToList(transformation));
this.identity = Chainr.fromSpec(JsonUtils.classpathToList(identity));
// Output of Transformation
......@@ -96,6 +115,13 @@ public abstract class JoltTransformation {
initPostHooks();
}
public JoltTransformation(Version targetVersion) {
this(getTransforamtionResourcePath(
targetVersion.previousVersion().label('-'),
targetVersion.label('-')),
getIdentiyResoucrePath(targetVersion.label('-')));
}
public JsonNode applyTransformation(JsonNode root) throws MigrationException {
Object rootObject = StateJsonConverter.convertJsonNodeToObject(root);
rootObject = applyTransformation(rootObject);
......@@ -127,7 +153,7 @@ public abstract class JoltTransformation {
/**
* add PostHooks in the correct order.
*/
protected abstract void initPostHooks() throws MigrationException;
protected abstract void initPostHooks();
protected void addToObjectNode(JsonNode node, String key, String value){
......
package org.vadere.simulator.projects.migration.jolttranformation;
import org.vadere.util.factory.BaseFactory;
import org.vadere.util.factory.FactoryObject;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.function.Supplier;
/**
* This is the Base version for the JoltTransformationFactory which automatically build by
* a Annotation Processor at build time. Do not directly use this class
*/
public class JoltTransformationBaseFactory extends BaseFactory<JoltTransformation, FactoryObject<JoltTransformation>> {
HashMap<String, JoltTransformation> transformationMap;
public JoltTransformationBaseFactory(){
this.transformationMap = new LinkedHashMap<>();
}
public void addMember(String versionLabel, Class<?> clazz, Supplier supplier){
supplierMap.put(versionLabel, new FactoryObject<>(clazz, supplier));
}
@Override
public JoltTransformation getInstanceOf(String key) throws ClassNotFoundException {
if (transformationMap.containsKey(key)){
return transformationMap.get(key);
} else {
if (supplierMap.containsKey(key)){
JoltTransformation tmp = supplierMap.get(key).getSupplier().get();
transformationMap.put(key, tmp);
return tmp;
}
throw new ClassNotFoundException("No class associated With Key: " + key + " in this Factory");
}
}
}
......@@ -52,7 +52,7 @@ public class JoltTransformV0toV1Test extends JoltTransformationTest{
// All postHooks should bee used here
@Test
public void TestPostHooks2() throws IOException, MigrationException, URISyntaxException {
JoltTransformation transformation = new JoltTransformV0toV1(TRANSFORM, IDENTITY, Version.V0_1);
JoltTransformation transformation = factory.getJoltTransformV0toV1();
String TEST2 = "/migration/vNOT-A-RELEASE_to_v0.1_Test2.scenario";
JsonNode in = getJson(TEST2);
JsonNode out = transformation.applyTransformation(in);
......@@ -73,7 +73,7 @@ public class JoltTransformV0toV1Test extends JoltTransformationTest{
// should fail because no main model was found
@Test(expected = MigrationException.class)
public void TestPostHooks3() throws IOException, MigrationException, URISyntaxException {
JoltTransformation transformation = new JoltTransformV0toV1(TRANSFORM, IDENTITY, Version.V0_1);
JoltTransformation transformation = factory.getJoltTransformV0toV1();
String TEST3 = "/migration/vNOT-A-RELEASE_to_v0.1_Test3.scenario";
JsonNode in = getJson(TEST3);
transformation.applyTransformation(in);
......
......@@ -27,6 +27,8 @@ import static org.junit.Assert.assertThat;
abstract class JoltTransformationTest {
protected org.vadere.simulator.projects.migration.jolttranformation.JoltTransformationFactory factory = org.vadere.simulator.projects.migration.jolttranformation.JoltTransformationFactory.instance();
abstract protected Path getTestDir();
......
......@@ -28,25 +28,18 @@ public abstract class BaseFactory<T, O extends FactoryObject<T>> {
}
public T getInstanceOf(String clazz) throws ClassNotFoundException {
if (supplierMap.containsKey(clazz))
return supplierMap.get(clazz).getSupplier().get();
public T getInstanceOf(String key) throws ClassNotFoundException {
if (supplierMap.containsKey(key))
return supplierMap.get(key).getSupplier().get();
throw new ClassNotFoundException(clazz + " is not a class or is not contained within Factory");
throw new ClassNotFoundException("No class associated With Key: " + key + " in this Factory");
}
public T getInstanceOf(Class clazz) throws ClassNotFoundException {
return getInstanceOf(clazz.getCanonicalName());
}
public Supplier<T> getSupplierOf(String clazz) throws ClassNotFoundException {
if (supplierMap.containsKey(clazz))
return supplierMap.get(clazz).getSupplier();
public Supplier<T> getSupplierOf(String key) throws ClassNotFoundException {
if (supplierMap.containsKey(key))
return supplierMap.get(key).getSupplier();
throw new ClassNotFoundException(clazz + " is not a class or is not contained within Factory");
throw new ClassNotFoundException("No class associated With Key: " + key + " in this Factory");
}
public Supplier<T> getSupplierOf(Class clazz) throws ClassNotFoundException {
return getSupplierOf(clazz.getCanonicalName());
}
}
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