Change the json structure for processors and their attributes. Now the...

Change the json structure for processors and their attributes. Now the attributes are defined in the corresponding processor's node.
parent 41641513
......@@ -26,79 +26,38 @@
"id": 1
}, {
"type": "org.vadere.simulator.projects.dataprocessing.processor.PedestrianVelocityProcessor",
"id": 2
"id": 2,
"attributesType": "org.vadere.state.attributes.processor.AttributesVelocityProcessor",
"attributes": {
"pedestrianPositionProcessorId": 1
}
}, {
"type": "org.vadere.simulator.projects.dataprocessing.processor.PedestrianDensityCountingProcessor",
"id": 3
"id": 3,
"attributesType": "org.vadere.state.attributes.processor.AttributesPedestrianDensityCountingProcessor",
"attributes": {
"pedestrianPositionProcessorId": 1,
"radius": 1.5
}
}, {
"type": "org.vadere.simulator.projects.dataprocessing.processor.PedestrianLastPositionProcessor",
"id": 4
"id": 4,
"attributesType": "org.vadere.state.attributes.processor.AttributesPedestrianLastPositionProcessor",
"attributes": {
"pedestrianPositionProcessorId": 1
}
}, {
"type": "org.vadere.simulator.projects.dataprocessing.processor.PedestrianDensityGaussianProcessor",
"id": 5
"id": 5,
"attributesType": "org.vadere.state.attributes.processor.AttributesPedestrianDensityGaussianProcessor",
"attributes": {
"pedestrianPositionProcessorId": 1
}
}, {
"type": "org.vadere.simulator.projects.dataprocessing.processor.AreaDensityVoronoiProcessor",
"id": 6
}, {
"type": "org.vadere.simulator.projects.dataprocessing.processor.AreaSpeedProcessor",
"id": 7
}, {
"type": "org.vadere.simulator.projects.dataprocessing.processor.PedestrianStartTimeProcessor",
"id": 8
}, {
"type": "org.vadere.simulator.projects.dataprocessing.processor.PedestrianEndTimeProcessor",
"id": 9
}, {
"type": "org.vadere.simulator.projects.dataprocessing.processor.PedestrianEvacuationTimeProcessor",
"id": 10
}, {
"type": "org.vadere.simulator.projects.dataprocessing.processor.MeanPedestrianEvacuationTimeProcessor",
"id": 11
}, {
"type": "org.vadere.simulator.projects.dataprocessing.processor.EvacuationTimeProcessor",
"id": 12
}, {
"type": "org.vadere.simulator.projects.dataprocessing.processor.PedestrianFlowProcessor",
"id": 13
}, {
"type": "org.vadere.simulator.projects.dataprocessing.processor.PedestrianOverlapProcessor",
"id": 14
}, {
"type": "org.vadere.simulator.projects.dataprocessing.processor.PedestrianWaitingTimeProcessor",
"id": 15
}, {
"type": "org.vadere.simulator.projects.dataprocessing.processor.PedestrianWaitingEndTimeProcessor",
"id": 16
}, {
"type": "org.vadere.simulator.projects.dataprocessing.processor.PedestrianOSMStrideLengthProcessor",
"id": 17
}, {
"type": "org.vadere.simulator.projects.dataprocessing.processor.PedestrianTargetIdProcessor",
"id": 18
}, {
"type": "org.vadere.simulator.projects.dataprocessing.processor.PedestrianSourceIdProcessor",
"id": 19
} ],
"attributes": {
"org.vadere.state.attributes.processor.AttributesVelocityProcessor": {
"processorId": 2,
"pedestrianPositionProcessorId": 1
},
"org.vadere.state.attributes.processor.AttributesPedestrianDensityCountingProcessor": {
"processorId": 3,
"pedestrianPositionProcessorId": 1,
"radius": 1.5
},
"org.vadere.state.attributes.processor.AttributesPedestrianLastPositionProcessor": {
"processorId": 4,
"pedestrianPositionProcessorId": 1
},
"org.vadere.state.attributes.processor.AttributesPedestrianDensityGaussianProcessor": {
"processorId": 5,
"pedestrianPositionProcessorId": 1
},
"org.vadere.state.attributes.processor.AttributesAreaDensityVoronoiProcessor": {
"processorId": 6,
"id": 6,
"attributesType": "org.vadere.state.attributes.processor.AttributesAreaDensityVoronoiProcessor",
"attributes": {
"measurementArea": {
"x" : 5.0,
"y" : 2.0,
......@@ -113,9 +72,12 @@
"height" : 1.0,
"type" : "RECTANGLE"
}
},
"org.vadere.state.attributes.processor.AttributesAreaSpeedProcessor": {
"processorId": 7,
}
}, {
"type": "org.vadere.simulator.projects.dataprocessing.processor.AreaSpeedProcessor",
"id": 7,
"attributesType": "org.vadere.state.attributes.processor.AttributesAreaSpeedProcessor",
"attributes": {
"measurementArea": {
"x" : 5.0,
"y" : 2.0,
......@@ -125,40 +87,67 @@
},
"pedestrianPositionProcessorId": 1,
"pedestrianVelocityProcessorId": 2
},
"org.vadere.state.attributes.processor.AttributesPedestrianEvacuationTimeProcessor": {
"processorId": 10,
}
}, {
"type": "org.vadere.simulator.projects.dataprocessing.processor.PedestrianStartTimeProcessor",
"id": 8
}, {
"type": "org.vadere.simulator.projects.dataprocessing.processor.PedestrianEndTimeProcessor",
"id": 9
}, {
"type": "org.vadere.simulator.projects.dataprocessing.processor.PedestrianEvacuationTimeProcessor",
"id": 10,
"attributesType": "org.vadere.state.attributes.processor.AttributesPedestrianEvacuationTimeProcessor",
"attributes": {
"pedestrianStartTimeProcessorId": 8
},
"org.vadere.state.attributes.processor.AttributesMeanPedestrianEvacuationTimeProcessor": {
"processorId": 11,
}
}, {
"type": "org.vadere.simulator.projects.dataprocessing.processor.MeanPedestrianEvacuationTimeProcessor",
"id": 11,
"attributesType": "org.vadere.state.attributes.processor.AttributesMeanPedestrianEvacuationTimeProcessor",
"attributes": {
"pedestrianEvacuationTimeProcessorId": 10
},
"org.vadere.state.attributes.processor.AttributesEvacuationTimeProcessor": {
"processorId": 12,
}
}, {
"type": "org.vadere.simulator.projects.dataprocessing.processor.EvacuationTimeProcessor",
"id": 12,
"attributesType": "org.vadere.state.attributes.processor.AttributesEvacuationTimeProcessor",
"attributes": {
"pedestrianEvacuationTimeProcessorId": 10
},
"org.vadere.state.attributes.processor.AttributesPedestrianFlowProcessor": {
"processorId": 13,
}
}, {
"type": "org.vadere.simulator.projects.dataprocessing.processor.PedestrianFlowProcessor",
"id": 13,
"attributesType": "org.vadere.state.attributes.processor.AttributesPedestrianFlowProcessor",
"attributes": {
"pedestrianVelocityProcessorId": 2,
"pedestrianDensityProcessorId": 3
},
"org.vadere.state.attributes.processor.AttributesPedestrianOverlapProcessor": {
"processorId": 14,
}
}, {
"type": "org.vadere.simulator.projects.dataprocessing.processor.PedestrianOverlapProcessor",
"id": 14,
"attributesType": "org.vadere.state.attributes.processor.AttributesPedestrianOverlapProcessor",
"attributes": {
"pedRadius": 0.21
},
"org.vadere.state.attributes.processor.AttributesPedestrianWaitingTimeProcessor": {
"processorId": 15,
}
}, {
"type": "org.vadere.simulator.projects.dataprocessing.processor.PedestrianWaitingTimeProcessor",
"id": 15,
"attributesType": "org.vadere.state.attributes.processor.AttributesPedestrianWaitingTimeProcessor",
"attributes": {
"waitingArea": {
"x" : 5.0,
"y" : 2.0,
"width" : 1.0,
"height" : 1.0,
"type" : "RECTANGLE"
"x": 5.0,
"y": 2.0,
"width": 1.0,
"height": 1.0,
"type": "RECTANGLE"
}
},
"org.vadere.state.attributes.processor.AttributesPedestrianWaitingEndTimeProcessor": {
"processorId": 16,
}
}, {
"type": "org.vadere.simulator.projects.dataprocessing.processor.PedestrianWaitingEndTimeProcessor",
"id": 16,
"attributesType": "org.vadere.state.attributes.processor.AttributesPedestrianWaitingEndTimeProcessor",
"attributes": {
"waitingArea": {
"x" : 5.0,
"y" : 2.0,
......@@ -167,5 +156,14 @@
"type" : "RECTANGLE"
}
}
}
}, {
"type": "org.vadere.simulator.projects.dataprocessing.processor.PedestrianOSMStrideLengthProcessor",
"id": 17
}, {
"type": "org.vadere.simulator.projects.dataprocessing.processor.PedestrianTargetIdProcessor",
"id": 18
}, {
"type": "org.vadere.simulator.projects.dataprocessing.processor.PedestrianSourceIdProcessor",
"id": 19
} ]
}
package org.vadere.simulator.projects.dataprocessing;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
......@@ -17,7 +21,6 @@ import org.vadere.util.reflection.DynamicClassInstantiator;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
......@@ -36,7 +39,7 @@ public class DataProcessingJsonManager {
public static final String PROCESSORS_KEY = "processors";
private static final String PROCESSORID_KEY = "id";
private static final String ATTRIBUTESTYPE_KEY = "attributesType";
public static final String ATTRIBUTES_KEY = "attributes";
public static final String DEFAULT_SEPARATOR = " ";
......@@ -46,25 +49,32 @@ public class DataProcessingJsonManager {
private static final DynamicClassInstantiator<OutputFile<?>> outputFileInstantiator;
private static final DynamicClassInstantiator<Processor<?, ?>> processorInstantiator;
private static final DynamicClassInstantiator<AttributesProcessor> attributesInstantiator;
private List<OutputFile<?>> outputFiles;
private List<Processor<?, ?>> processors;
private List<AttributesProcessor> attributes;
static {
mapper = JsonConverter.getMapper();
writer = mapper.writerWithDefaultPrettyPrinter();
SimpleModule sm = new SimpleModule();
sm.addDeserializer(ProcessorStore.class, new JsonDeserializer<ProcessorStore>() {
@Override
public ProcessorStore deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
return null;
}
});
mapper.registerModule(sm);
outputFileInstantiator = new DynamicClassInstantiator<>();
processorInstantiator = new DynamicClassInstantiator<>();
attributesInstantiator = new DynamicClassInstantiator<>();
}
public DataProcessingJsonManager() {
this.outputFiles = new ArrayList<>();
this.processors = new ArrayList<>();
this.attributes = new ArrayList<>();
}
public void addOutputFile(final OutputFileStore fileStore) {
......@@ -81,13 +91,10 @@ public class DataProcessingJsonManager {
public void addProcessor(final ProcessorStore processorStore) {
Processor<?, ?> processor = processorInstantiator.createObject(processorStore.getType());
processor.setId(processorStore.getId());
processor.setAttributes(processorStore.getAttributes());
this.processors.add(processor);
}
public void addAttributes(final AttributesProcessor attributes) {
this.attributes.add(attributes);
}
private static JsonNode serializeOutputFile(final OutputFile outputFile) {
ObjectNode node = mapper.createObjectNode();
......@@ -109,11 +116,15 @@ public class DataProcessingJsonManager {
node.put(TYPE_KEY, processor.getClass().getName());
node.put(PROCESSORID_KEY, processor.getId());
return node;
}
if (processor.getAttributes() != null) {
node.put(ATTRIBUTESTYPE_KEY, processor.getAttributes().getClass().getName());
if (!processor.getAttributes().getClass().equals(AttributesProcessor.class)) {
node.set(ATTRIBUTES_KEY, mapper.convertValue(processor.getAttributes(), JsonNode.class));
}
}
private static JsonNode serializeAttributesProcessor(final AttributesProcessor attributes) {
return mapper.convertValue(attributes, JsonNode.class);
return node;
}
public String serialize() throws JsonProcessingException {
......@@ -125,7 +136,6 @@ public class DataProcessingJsonManager {
ArrayNode outputFilesArrayNode = mapper.createArrayNode();
ArrayNode processorsArrayNode = mapper.createArrayNode();
ObjectNode attributesNode = mapper.createObjectNode();
// part 1: output files
this.outputFiles.forEach(file -> {
......@@ -137,14 +147,8 @@ public class DataProcessingJsonManager {
processorsArrayNode.add(serializeProcessor(proc));
});
// part 3: attributes
this.attributes.forEach(att -> {
attributesNode.set(att.getClass().getName(), serializeAttributesProcessor(att));
});
main.set(FILES_KEY, outputFilesArrayNode);
main.set(PROCESSORS_KEY, processorsArrayNode);
main.set(ATTRIBUTES_KEY, attributesNode);
return main;
}
......@@ -177,7 +181,6 @@ public class DataProcessingJsonManager {
ArrayNode outputFilesArrayNode = (ArrayNode) node.get(FILES_KEY);
ArrayNode processorsArrayNode = (ArrayNode) node.get(PROCESSORS_KEY);
JsonNode attributesNode = node.get(ATTRIBUTES_KEY);
// part 1: output files
if (outputFilesArrayNode != null)
......@@ -189,25 +192,35 @@ public class DataProcessingJsonManager {
// part 2: processor
if (processorsArrayNode != null)
for (JsonNode processorNode : processorsArrayNode) {
ProcessorStore processorStore = mapper.treeToValue(processorNode, ProcessorStore.class);
ProcessorStore processorStore = deserializeProcessorStore(processorNode);
manager.addProcessor(processorStore);
}
// part 3: attributes
if (attributesNode != null) {
Iterator<String> it = attributesNode.fieldNames();
while (it.hasNext()) {
String fieldName = it.next();
JsonNode attributeNode = attributesNode.get(fieldName);
AttributesProcessor attributes = mapper.treeToValue(attributeNode, attributesInstantiator.getClassFromName(fieldName));
manager.addAttributes(attributes);
return manager;
}
private static ProcessorStore deserializeProcessorStore(JsonNode node) {
ProcessorStore store = new ProcessorStore();
store.setType(node.get(TYPE_KEY).asText());
store.setId(node.get(PROCESSORID_KEY).asInt());
if(node.has(ATTRIBUTESTYPE_KEY)) {
String attType = node.get(ATTRIBUTESTYPE_KEY).asText();
store.setAttributesType(attType);
try {
store.setAttributes(mapper.readValue(node.get(ATTRIBUTES_KEY).toString(), mapper.getTypeFactory().constructFromCanonical(attType)));
}
catch (Exception ex) {
ex.printStackTrace();
}
}
return manager;
return store;
}
public ProcessorManager createProcessorManager() {
return new ProcessorManager(this, this.processors, this.attributes, this.outputFiles);
return new ProcessorManager(this, this.processors, this.outputFiles);
}
}
......@@ -7,7 +7,6 @@ import org.vadere.simulator.control.SimulationState;
import org.vadere.simulator.models.Model;
import org.vadere.simulator.projects.dataprocessing.outputfile.OutputFile;
import org.vadere.simulator.projects.dataprocessing.processor.Processor;
import org.vadere.state.attributes.processor.AttributesProcessor;
import org.vadere.util.io.IOUtils;
import java.util.LinkedHashMap;
......@@ -20,23 +19,18 @@ public class ProcessorManager {
private Model model;
private Map<Integer, Processor<?, ?>> processorMap;
private Map<Integer, AttributesProcessor> attributesMap;
private List<OutputFile<?>> outputFiles;
public ProcessorManager(DataProcessingJsonManager jsonManager, List<Processor<?, ?>> processors, List<AttributesProcessor> attributesProcessor, List<OutputFile<?>> outputFiles) {
public ProcessorManager(DataProcessingJsonManager jsonManager, List<Processor<?, ?>> processors, List<OutputFile<?>> outputFiles) {
this.jsonManager = jsonManager;
this.outputFiles = outputFiles;
this.attributesMap = new LinkedHashMap<>();
for (AttributesProcessor att : attributesProcessor)
this.attributesMap.put(att.getProcessorId(), att);
this.processorMap = new LinkedHashMap<>();
for (Processor<?, ?> proc : processors)
this.processorMap.put(proc.getId(), proc);
processors.forEach(proc -> proc.init(this.attributesMap.get(proc.getId()), this));
processors.forEach(proc -> proc.init(this));
}
public void setModel(Model model) {
......@@ -67,10 +61,6 @@ public class ProcessorManager {
this.processorMap.values().forEach(proc -> proc.postLoop(state));
}
public AttributesProcessor getAttributes(int processorId) {
return this.attributesMap.get(processorId);
}
public void setOutputPath(String directory) {
this.outputFiles.forEach(file -> file.setFileName(IOUtils.getPath(directory, String.format("%s", file.getFileName())).toString()));
}
......
......@@ -2,14 +2,13 @@ package org.vadere.simulator.projects.dataprocessing.processor;
import org.vadere.simulator.projects.dataprocessing.ProcessorManager;
import org.vadere.state.attributes.processor.AttributesAreaDensityVoronoiProcessor;
import org.vadere.state.attributes.processor.AttributesProcessor;
public class AreaDensityVoronoiProcessor extends AreaDensityProcessor {
@Override
public void init(final AttributesProcessor attributes, final ProcessorManager manager) {
super.init(attributes, manager);
public void init(final ProcessorManager manager) {
super.init(manager);
AttributesAreaDensityVoronoiProcessor att = (AttributesAreaDensityVoronoiProcessor) attributes;
AttributesAreaDensityVoronoiProcessor att = (AttributesAreaDensityVoronoiProcessor) this.getAttributes();
this.setAlgorithm(new AreaDensityVoronoiAlgorithm(this.getMeasurementArea(), att.getVoronoiArea()));
}
}
......@@ -3,15 +3,14 @@ package org.vadere.simulator.projects.dataprocessing.processor;
import org.vadere.simulator.projects.dataprocessing.ProcessorManager;
import org.vadere.simulator.projects.dataprocessing.datakey.TimestepDataKey;
import org.vadere.state.attributes.processor.AttributesAreaProcessor;
import org.vadere.state.attributes.processor.AttributesProcessor;
import org.vadere.util.geometry.shapes.VRectangle;
public abstract class AreaProcessor<V> extends Processor<TimestepDataKey, V> {
private VRectangle measurementArea;
@Override
public void init(final AttributesProcessor attributes, final ProcessorManager manager) {
AttributesAreaProcessor att = (AttributesAreaProcessor) attributes;
public void init(final ProcessorManager manager) {
AttributesAreaProcessor att = (AttributesAreaProcessor) this.getAttributes();
this.measurementArea = att.getMeasurementArea();
}
......
package org.vadere.simulator.projects.dataprocessing.processor;
import java.util.Map;
import org.vadere.simulator.control.SimulationState;
import org.vadere.simulator.projects.dataprocessing.datakey.PedestrianIdDataKey;
import org.vadere.simulator.projects.dataprocessing.ProcessorManager;
import org.vadere.simulator.projects.dataprocessing.datakey.PedestrianIdDataKey;
import org.vadere.simulator.projects.dataprocessing.datakey.TimestepDataKey;
import org.vadere.simulator.projects.dataprocessing.datakey.TimestepPedestrianIdDataKey;
import org.vadere.state.attributes.processor.AttributesAreaSpeedProcessor;
import org.vadere.state.attributes.processor.AttributesProcessor;
import org.vadere.util.geometry.shapes.VPoint;
import java.util.Map;
public class AreaSpeedProcessor extends AreaProcessor<Double> {
private PedestrianPositionProcessor pedPosProc;
private PedestrianVelocityProcessor pedVelProc;
......@@ -45,11 +44,11 @@ public class AreaSpeedProcessor extends AreaProcessor<Double> {
}
@Override
public void init(final AttributesProcessor attributes, final ProcessorManager manager) {
AttributesAreaSpeedProcessor att = (AttributesAreaSpeedProcessor) attributes;
public void init(final ProcessorManager manager) {
AttributesAreaSpeedProcessor att = (AttributesAreaSpeedProcessor) this.getAttributes();
this.pedPosProc = (PedestrianPositionProcessor) manager.getProcessor(att.getPedestrianPositionProcessorId());
this.pedVelProc = (PedestrianVelocityProcessor) manager.getProcessor(att.getPedestrianVelocityProcessorId());
super.init(attributes, manager);
super.init(manager);
}
}
package org.vadere.simulator.projects.dataprocessing.processor;
import java.util.Collection;
import java.util.Collections;
import org.vadere.simulator.control.SimulationState;
import org.vadere.simulator.projects.dataprocessing.datakey.NoDataKey;
import org.vadere.simulator.projects.dataprocessing.ProcessorManager;
import org.vadere.simulator.projects.dataprocessing.datakey.NoDataKey;
import org.vadere.state.attributes.processor.AttributesEvacuationTimeProcessor;
import org.vadere.state.attributes.processor.AttributesProcessor;
import org.vadere.state.scenario.Pedestrian;
import java.util.Collection;
import java.util.Collections;
public class EvacuationTimeProcessor extends Processor<NoDataKey, Double> {
private PedestrianEvacuationTimeProcessor pedEvacTimeProc;
......@@ -34,8 +33,8 @@ public class EvacuationTimeProcessor extends Processor<NoDataKey, Double> {
}
@Override
public void init(final AttributesProcessor attributes, final ProcessorManager manager) {
AttributesEvacuationTimeProcessor att = (AttributesEvacuationTimeProcessor) attributes;
public void init(final ProcessorManager manager) {
AttributesEvacuationTimeProcessor att = (AttributesEvacuationTimeProcessor) this.getAttributes();
this.pedEvacTimeProc = (PedestrianEvacuationTimeProcessor) manager.getProcessor(att.getPedestrianEvacuationTimeProcessorId());
}
}
package org.vadere.simulator.projects.dataprocessing.processor;
import org.vadere.simulator.control.SimulationState;
import org.vadere.simulator.projects.dataprocessing.datakey.NoDataKey;
import org.vadere.simulator.projects.dataprocessing.ProcessorManager;
import org.vadere.simulator.projects.dataprocessing.datakey.NoDataKey;
import org.vadere.state.attributes.processor.AttributesMeanPedestrianEvacuationTimeProcessor;
import org.vadere.state.attributes.processor.AttributesProcessor;