Commit 79548cc4 authored by Stefan Schuhbaeck's avatar Stefan Schuhbaeck
Browse files

add additional attribute to OutputFileClass to allow Key/File Mapping

There is a 1:1 Relation ship between DataKeys and OutputFiles. This Mapping is introduced into the OutputFileClass Annotation to create the mapping wihtin the OutputFileBaseFactory.  
parent 76b8e099
......@@ -20,6 +20,11 @@ public abstract class BaseFactoryProcessor extends AbstractProcessor {
protected String factoryClassName;
protected String factoryPackage;
protected String[] factoryImports;
protected String baseClass;
public BaseFactoryProcessor(){
baseClass = "BaseFactory";
}
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
......@@ -66,6 +71,8 @@ public abstract class BaseFactoryProcessor extends AbstractProcessor {
protected abstract void addImports(Set<? extends Element> elements, PrintWriter writer);
protected abstract void addLast(Set<? extends Element> elements, PrintWriter writer);
protected abstract void addMembers(Set<? extends Element> elements, PrintWriter writer);
protected abstract void addLastConstructor(Set<? extends Element> elements, PrintWriter writer);
private void writeFactory(Set<? extends Element> elements) throws IOException {
JavaFileObject jFile = processingEnv.getFiler().createSourceFile(factoryClassName);
......@@ -89,7 +96,10 @@ public abstract class BaseFactoryProcessor extends AbstractProcessor {
out.println();
out.println();
out.append("public class ").append(factoryClassName).append(" extends BaseFactory<").append(factoryType).append("> {").println();
out.append("public class ").append(factoryClassName).append(" extends ").append(baseClass).append("<").append(factoryType).append("> {").println();
// out.append("public class ").append(factoryClassName).append(" extends BaseFactory<").append(factoryType).append("> {").println();
out.println();
addMembers(elements, out);
out.println();
Util.createSingletone(factoryClassName, out);
......@@ -99,21 +109,23 @@ public abstract class BaseFactoryProcessor extends AbstractProcessor {
out.append(" private ").append(factoryClassName).append("(){").println();
out.println("// add Members to Factory");
out.println(" // add Members to Factory");
for (Element e : elements) {
out.append(" addMember(");
out.append(" addMember(");
out.append(e.getSimpleName().toString()).append(".class, ");
out.append("this::").append("get").append(name(e)).append(", ");
out.append(Util.quote(label(e))).append(", ");
out.append(Util.quote(descr(e))).append(");");
out.println();
}
out.println();
addLastConstructor(elements, out);
out.println(" }");
out.println();
out.println();
out.println("// Getters");
out.println(" // Getters");
for (Element element : elements) {
TypeElement p = (TypeElement)element;
out.append(" public ").append(p.getSimpleName())
......
......@@ -9,7 +9,7 @@ import java.lang.annotation.Target;
factoryImports = {
"org.vadere.simulator.projects.dataprocessing.processor.DataProcessor"
},
factoryName = "DataProcessorFactoryXX",
factoryName = "DataProcessorFactory",
factoryPackage = "org.vadere.simulator.projects.dataprocessing.processor"
)
public @interface DataProcessorClass {
......
......@@ -33,6 +33,17 @@ public class DataProcessorFactoryProcessor extends BaseFactoryProcessor {
writer.println("import org.vadere.simulator.projects.dataprocessing.store.DataProcessorStore;");
}
@Override
protected void addMembers(Set<? extends Element> elements, PrintWriter writer) {
}
@Override
protected void addLastConstructor(Set<? extends Element> elements, PrintWriter writer) {
}
@Override
protected void addLast(Set<? extends Element> elements, PrintWriter writer) {
writer.println(" public DataProcessor<?, ?> createDataProcessor(DataProcessorStore dataProcessorStore) {");
......
......@@ -10,11 +10,12 @@ import java.lang.annotation.Target;
"org.vadere.simulator.projects.dataprocessing.outputfile.OutputFile",
"org.vadere.simulator.projects.dataprocessing.datakey.DataKey"
},
factoryName = "OutputFileFactoryXX",
factoryName = "OutputFileFactory",
factoryPackage = "org.vadere.simulator.projects.dataprocessing.outputfile"
)
public @interface OutputFileClass {
String label() default "";
String description() default "";
Class dataKeyMapping();
}
......@@ -10,12 +10,18 @@ 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.element.TypeElement;
import javax.lang.model.type.MirroredTypeException;
@SupportedAnnotationTypes("org.vadere.annotation.factories.OutputFileClass")
@SupportedSourceVersion(SourceVersion.RELEASE_8)
@AutoService(Processor.class)
public class OutputFileFactoryProcessor extends BaseFactoryProcessor {
public OutputFileFactoryProcessor(){
baseClass = "OutputFileBaseFactory";
}
protected String label(Element element){
OutputFileClass dataProcessorClass = element.getAnnotation(OutputFileClass.class);
String label = dataProcessorClass.label();
......@@ -33,7 +39,30 @@ public class OutputFileFactoryProcessor extends BaseFactoryProcessor {
writer.println("import org.vadere.simulator.projects.dataprocessing.datakey.DataKey;");
writer.println("import org.vadere.simulator.projects.dataprocessing.datakey.OutputFileMap;");
writer.println("import org.vadere.simulator.projects.dataprocessing.store.OutputFileStore;");
writer.println("import org.vadere.util.factory.OutputFileBaseFactory;");
writer.println("import java.util.Arrays;");
writer.println("import java.util.HashMap;");
}
@Override
protected void addLastConstructor(Set<? extends Element> elements, PrintWriter writer) {
for (Element e : elements) {
OutputFileClass annotation = e.getAnnotation(OutputFileClass.class);
String classValue;
try{
classValue =annotation.dataKeyMapping().getName();
} catch (MirroredTypeException ex){
classValue = ex.getTypeMirror().toString();
}
int lastPoint = classValue.lastIndexOf('.');
TypeElement p = (TypeElement)e;
writer.append(" addExistingKey(")
.append(Util.quote(classValue.substring(lastPoint+1))).append(", ")
.append(Util.quote(p.getQualifiedName().toString()))
.println(");");
}
}
@Override
......@@ -84,5 +113,10 @@ public class OutputFileFactoryProcessor extends BaseFactoryProcessor {
writer.println(" }");
}
@Override
protected void addMembers(Set<? extends Element> elements, PrintWriter writer) {
}
}
......@@ -65,8 +65,8 @@ public class DataProcessingJsonManager {
this.outputFiles = new ArrayList<>();
this.dataProcessors = new ArrayList<>();
this.isTimestamped = true;
this.outputFileFactory = new OutputFileFactory();
this.processorFactory = new DataProcessorFactory();
this.outputFileFactory = OutputFileFactory.instance();
this.processorFactory = DataProcessorFactory.instance();
}
private static JsonNode serializeOutputFile(final OutputFile<?> outputFile) {
......
......@@ -3,7 +3,7 @@ package org.vadere.simulator.projects.dataprocessing.outputfile;
import org.vadere.annotation.factories.OutputFileClass;
import org.vadere.simulator.projects.dataprocessing.datakey.IdDataKey;
@OutputFileClass()
@OutputFileClass(dataKeyMapping = IdDataKey.class)
public class IdOutputFile extends OutputFile<IdDataKey> {
public IdOutputFile() {
......
......@@ -7,7 +7,7 @@ import org.vadere.simulator.projects.dataprocessing.datakey.NoDataKey;
* @author Mario Teixeira Parente
*
*/
@OutputFileClass()
@OutputFileClass(dataKeyMapping = NoDataKey.class)
public class NoDataKeyOutputFile extends OutputFile<NoDataKey> {
public NoDataKeyOutputFile() {
super(new String[] { });
......
package org.vadere.simulator.projects.dataprocessing.outputfile;
import org.vadere.simulator.projects.dataprocessing.datakey.DataKey;
import org.vadere.simulator.projects.dataprocessing.datakey.OutputFileMap;
import org.vadere.simulator.projects.dataprocessing.store.OutputFileStore;
import org.vadere.util.reflection.DynamicClassInstantiator;
import org.vadere.util.reflection.VadereNoOutputfileForDataKeyException;
import java.util.Arrays;
public class OutputFileFactory {
private final DynamicClassInstantiator<OutputFile<?>> outputFileInstantiator;
public OutputFileFactory() {
outputFileInstantiator = new DynamicClassInstantiator<>();
}
public OutputFile<?> createOutputfile(OutputFileStore fileStore) {
OutputFile<?> file = outputFileInstantiator.createObject(fileStore.getType());
file.setRelativeFileName(fileStore.getFilename());
file.setProcessorIds(fileStore.getProcessors());
file.setSeparator(fileStore.getSeparator());
return file;
}
public OutputFile<?> createDefaultOutputfile() {
OutputFileStore fileStore = new OutputFileStore();
OutputFile<?> file = outputFileInstantiator.createObject(fileStore.getType());
file.setSeparator(fileStore.getSeparator());
return file;
}
public OutputFile<?> createOutputfile(String type) {
OutputFileStore fileStore = new OutputFileStore();
fileStore.setType(type);
OutputFile<?> file = outputFileInstantiator.createObject(fileStore.getType());
file.setSeparator(fileStore.getSeparator());
return file;
}
public OutputFile<?> createOutputfile(Class type) {
return createOutputfile(type.getCanonicalName());
}
public OutputFile<?> createOutputfile(Class type, Integer... processorsIds) {
OutputFile<?> file = createOutputfile(type.getCanonicalName());
file.setProcessorIds(Arrays.asList(processorsIds));
return file;
}
public OutputFile<?> createDefaultOutputfileByDataKey(Class<? extends DataKey<?>> keyType, Integer... processorsIds) {
OutputFile<?> file = createDefaultOutputfileByDataKey(keyType);
file.setProcessorIds(Arrays.asList(processorsIds));
return file;
}
public OutputFile<?> createDefaultOutputfileByDataKey(Class<? extends DataKey<?>> keyType) {
OutputFileMap outputFileMap = keyType.getAnnotation(OutputFileMap.class);
return createOutputfile(outputFileMap.outputFileClass());
}
}
......@@ -7,7 +7,7 @@ import org.vadere.simulator.projects.dataprocessing.datakey.PedestrianIdKey;
* @author Mario Teixeira Parente
*
*/
@OutputFileClass()
@OutputFileClass(dataKeyMapping = PedestrianIdKey.class)
public class PedestrianIdOutputFile extends OutputFile<PedestrianIdKey> {
public PedestrianIdOutputFile() {
......
......@@ -7,7 +7,7 @@ import org.vadere.simulator.projects.dataprocessing.datakey.TimestepKey;
* @author Mario Teixeira Parente
*
*/
@OutputFileClass()
@OutputFileClass(dataKeyMapping = TimestepKey.class)
public class TimestepOutputFile extends OutputFile<TimestepKey> {
public TimestepOutputFile() {
......
......@@ -7,7 +7,7 @@ import org.vadere.simulator.projects.dataprocessing.datakey.TimestepPedestrianId
* @author Mario Teixeira Parente
*
*/
@OutputFileClass()
@OutputFileClass(dataKeyMapping = TimestepPedestrianIdKey.class)
public class TimestepPedestrianIdOutputFile extends OutputFile<TimestepPedestrianIdKey> {
public TimestepPedestrianIdOutputFile() {
......
package org.vadere.simulator.projects.dataprocessing.outputfile;
import org.vadere.annotation.factories.OutputFileClass;
import org.vadere.simulator.projects.dataprocessing.datakey.TimestepPedestrianIdKey;
import org.vadere.simulator.projects.dataprocessing.datakey.TimestepPositionKey;
@OutputFileClass()
@OutputFileClass(dataKeyMapping = TimestepPositionKey.class)
public class TimestepPositionOutputFile extends OutputFile<TimestepPositionKey> {
public TimestepPositionOutputFile() {
super(TimestepPositionKey.getHeaders());
......
......@@ -3,7 +3,7 @@ package org.vadere.simulator.projects.dataprocessing.outputfile;
import org.vadere.annotation.factories.OutputFileClass;
import org.vadere.simulator.projects.dataprocessing.datakey.TimestepRowKey;
@OutputFileClass()
@OutputFileClass(dataKeyMapping = TimestepRowKey.class)
public class TimestepRowOutputFile extends OutputFile<TimestepRowKey> {
public TimestepRowOutputFile() {
super("timeStep", "row");
......
package org.vadere.simulator.projects.dataprocessing.processor;
import org.vadere.simulator.projects.dataprocessing.store.DataProcessorStore;
import org.vadere.util.reflection.DynamicClassInstantiator;
public class DataProcessorFactory {
private DynamicClassInstantiator<DataProcessor<?, ?>> processorInstantiator;
public DataProcessorFactory() {
processorInstantiator = new DynamicClassInstantiator<>();
}
public DataProcessor<?, ?> createDataProcessor(DataProcessorStore dataProcessorStore) {
DataProcessor<?, ?> processor = processorInstantiator.createObject(dataProcessorStore.getType());
processor.setId(dataProcessorStore.getId());
processor.setAttributes(dataProcessorStore.getAttributes());
return processor;
}
public DataProcessor<?, ?> createDataProcessor(String type) {
DataProcessorStore dataProcessorStore = new DataProcessorStore();
dataProcessorStore.setType(type);
DataProcessor<?, ?> processor = processorInstantiator.createObject(dataProcessorStore.getType());
processor.setId(dataProcessorStore.getId());
processor.setAttributes(dataProcessorStore.getAttributes());
return processor;
}
public DataProcessor<?, ?> createDataProcessor(Class type) {
return createDataProcessor(type.getCanonicalName());
}
}
......@@ -3,6 +3,7 @@ package org.vadere.simulator.projects.dataprocessing.processor;
import org.mockito.Mockito;
import org.vadere.simulator.control.SimulationState;
import org.vadere.simulator.projects.dataprocessing.ProcessorManager;
import org.vadere.simulator.projects.dataprocessing.processor.DataProcessorFactory;
import org.vadere.simulator.projects.dataprocessing.datakey.DataKey;
import org.vadere.simulator.projects.dataprocessing.outputfile.OutputFile;
import org.vadere.simulator.projects.dataprocessing.outputfile.OutputFileFactory;
......@@ -70,8 +71,8 @@ public abstract class ProcessorTestEnv<K extends DataKey<K>, V> {
testedProcessor = null;
outputFile = null;
requiredProcessors = new LinkedList<>();
processorFactory = new DataProcessorFactory();
outputFileFactory = new OutputFileFactory();
processorFactory = DataProcessorFactory.instance();
outputFileFactory = OutputFileFactory.instance();
}
/**
......
package org.vadere.util.factory;
import java.util.HashMap;
import java.util.function.Supplier;
import java.util.stream.Collectors;
public class OutputFileBaseFactory<T> extends BaseFactory<T> {
protected HashMap<String, String> dataKeyMap;
public OutputFileBaseFactory(){
dataKeyMap = new HashMap<>();
}
protected void addExistingKey(String keyName, String outputFileName){
dataKeyMap.put(keyName, outputFileName);
}
public HashMap<String, String> getDataKeyOutputFileMap() {
return dataKeyMap;
}
}
Supports Markdown
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