The name of the initial branch for new projects is now "main" instead of "master". Existing projects remain unchanged. More information: https://doku.lrz.de/display/PUBLIC/GitLab

Commit fe131414 authored by Benedikt Zoennchen's avatar Benedikt Zoennchen
Browse files

Merge branch 'processor_test' into 'develop'

Add Unit Test to Processors

See merge request !25
parents deec92bf 560f50e1
......@@ -18,6 +18,7 @@ VadereGui/*.png
VadereGui/*.svg
VadereGui/*.mov
VadereUtils/testreports/*.txt
VadereUtils/output/*
VadereSimulator/resources/current_commit_hash.txt
#model test output
......
......@@ -6,6 +6,7 @@ import org.apache.log4j.Logger;
import org.vadere.simulator.models.MainModel;
import org.vadere.simulator.models.Model;
import org.vadere.simulator.projects.dataprocessing.datakey.DataKey;
import org.vadere.simulator.projects.dataprocessing.datakey.OutputFileMap;
import org.vadere.simulator.projects.dataprocessing.outputfile.OutputFile;
import org.vadere.simulator.projects.dataprocessing.processor.DataProcessor;
import org.vadere.state.attributes.Attributes;
......@@ -67,15 +68,8 @@ public class ClassFinder {
.map(c -> {
// Find corresponding outputfile class
try {
List<Class<?>> opClasses = getClasses(OutputFile.class.getPackage().getName());
Optional<Class<?>> corrOpClass = opClasses
.stream()
.filter(opc -> !Modifier.isAbstract(opc.getModifiers()))
.filter(opc -> ((ParameterizedType) opc.getGenericSuperclass()).getActualTypeArguments()[0].getTypeName().equals(c.getName()))
.findFirst();
return Pair.of((Class) c, corrOpClass);
OutputFileMap annotation = c.getAnnotation(OutputFileMap.class);
return Pair.of((Class) c, Optional.of(annotation.outputFileClass()));
} catch (Exception ex) {
ex.printStackTrace();
}
......
......@@ -3,18 +3,14 @@ package org.vadere.simulator.control;
import org.jetbrains.annotations.Nullable;
import org.vadere.simulator.models.MainModel;
import org.vadere.simulator.projects.ScenarioStore;
import org.vadere.state.scenario.Car;
import org.vadere.state.scenario.Pedestrian;
import org.vadere.state.scenario.Topography;
import org.vadere.util.geometry.shapes.VPoint;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
public class SimulationState {
private final Topography topography;
private final Map<Integer, VPoint> pedestrianPositionMap;
private final double simTimeInSec;
private final ScenarioStore scenarioStore;
private final int step;
......@@ -26,31 +22,23 @@ public class SimulationState {
final ScenarioStore scenarioStore,
final double simTimeInSec,
final int step,
@Nullable final MainModel mainModel) {
@Nullable final MainModel mainModel) {
this.name = name;
this.topography = topography;
this.simTimeInSec = simTimeInSec;
this.step = step;
this.pedestrianPositionMap = new HashMap<>();
this.scenarioStore = scenarioStore;
this.mainModel = mainModel;
for (Pedestrian pedestrian : topography.getElements(Pedestrian.class)) {
pedestrianPositionMap.put(pedestrian.getId(), pedestrian.getPosition());
}
for (Car car : topography.getElements(Car.class)) {
pedestrianPositionMap.put(car.getId(), car.getPosition());
}
}
@Deprecated
public SimulationState(final Map<Integer, VPoint> pedestrianPositionMap, final Topography topography,
final double simTimeInSec, final int step) {
final double simTimeInSec, final int step) {
this.name = "";
this.topography = topography;
this.simTimeInSec = simTimeInSec;
this.step = step;
this.pedestrianPositionMap = pedestrianPositionMap;
this.scenarioStore = null;
this.mainModel = null;
}
......@@ -69,9 +57,6 @@ public class SimulationState {
return step;
}
public Map<Integer, VPoint> getPedestrianPositionMap() {
return pedestrianPositionMap;
}
public ScenarioStore getScenarioStore() {
return scenarioStore;
......@@ -82,8 +67,8 @@ public class SimulationState {
}
/**
* Returns the main model of the simulation if it is an online simulation, otherwise the main model
* is unknown and therefore null.
* Returns the main model of the simulation if it is an online simulation, otherwise the main
* model is unknown and therefore null.
*
* @return online simulation => main model, otherwise null.
*/
......
package org.vadere.simulator.models.osm;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import org.vadere.simulator.models.SpeedAdjuster;
import org.vadere.simulator.models.osm.optimization.StepCircleOptimizer;
import org.vadere.simulator.models.osm.stairOptimization.StairStepOptimizer;
import org.vadere.simulator.models.osm.updateScheme.UpdateSchemeEventDriven;
import org.vadere.simulator.models.osm.updateScheme.UpdateSchemeOSM;
import org.vadere.simulator.models.osm.updateScheme.UpdateSchemeOSM.CallMethod;
import org.vadere.simulator.models.osm.updateScheme.UpdateSchemeParallel;
import org.vadere.simulator.models.osm.updateScheme.UpdateSchemeSequential;
import org.vadere.simulator.models.osm.updateScheme.UpdateSchemeOSM.CallMethod;
import org.vadere.simulator.models.potential.fields.IPotentialFieldTarget;
import org.vadere.simulator.models.potential.fields.PotentialFieldAgent;
import org.vadere.simulator.models.potential.fields.PotentialFieldObstacle;
import org.vadere.simulator.models.potential.fields.IPotentialFieldTarget;
import org.vadere.simulator.models.potential.fields.PotentialFieldTargetRingExperiment;
import org.vadere.state.attributes.models.AttributesOSM;
import org.vadere.state.attributes.scenario.AttributesAgent;
......@@ -29,23 +23,25 @@ import org.vadere.util.geometry.Vector2D;
import org.vadere.util.geometry.shapes.VCircle;
import org.vadere.util.geometry.shapes.VPoint;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
public class PedestrianOSM extends Pedestrian {
private final AttributesOSM attributesOSM;
private final transient StepCircleOptimizer stepCircleOptimizer;
private final transient UpdateSchemeOSM updateScheme;
private transient IPotentialFieldTarget potentialFieldTarget;
private transient PotentialFieldObstacle potentialFieldObstacle;
private transient PotentialFieldAgent potentialFieldPedestrian;
private final transient Topography topography;
private final double stepLength;
private final double stepDeviation;
private List<SpeedAdjuster> speedAdjusters;
private final double minStepLength;
private transient IPotentialFieldTarget potentialFieldTarget;
private transient PotentialFieldObstacle potentialFieldObstacle;
private transient PotentialFieldAgent potentialFieldPedestrian;
private List<SpeedAdjuster> speedAdjusters;
private double durationNextStep;
private VPoint nextPosition;
private VPoint lastPosition;
......@@ -60,17 +56,17 @@ public class PedestrianOSM extends Pedestrian {
// calculated by (current position - last position)/(period of time).
private double speedByAbsoluteDistance;
private List<Double>[] strides;
private LinkedList<Double>[] strides; // strides[0] = length strides[1] = time
private StairStepOptimizer stairStepOptimizer;
@SuppressWarnings("unchecked")
PedestrianOSM(AttributesOSM attributesOSM,
AttributesAgent attributesPedestrian, Topography topography,
Random random, IPotentialFieldTarget potentialFieldTarget,
PotentialFieldObstacle potentialFieldObstacle,
PotentialFieldAgent potentialFieldPedestrian,
List<SpeedAdjuster> speedAdjusters,
StepCircleOptimizer stepCircleOptimizer) {
AttributesAgent attributesPedestrian, Topography topography,
Random random, IPotentialFieldTarget potentialFieldTarget,
PotentialFieldObstacle potentialFieldObstacle,
PotentialFieldAgent potentialFieldPedestrian,
List<SpeedAdjuster> speedAdjusters,
StepCircleOptimizer stepCircleOptimizer) {
super(attributesPedestrian, random);
......@@ -98,9 +94,9 @@ public class PedestrianOSM extends Pedestrian {
this.minStepLength = 0;
}
this.strides = (ArrayList<Double>[]) (new ArrayList<?>[2]);
this.strides[0] = new ArrayList<>();
this.strides[1] = new ArrayList<>();
this.strides = (LinkedList<Double>[]) (new LinkedList<?>[2]);
this.strides[0] = new LinkedList<>();
this.strides[1] = new LinkedList<>();
}
private static UpdateSchemeOSM createUpdateScheme(UpdateType updateType, PedestrianOSM pedestrian) {
......@@ -198,7 +194,7 @@ public class PedestrianOSM extends Pedestrian {
if (attributesOSM.isDynamicStepLength()) {
return attributesOSM.getStepLengthIntercept()
+ attributesOSM.getStepLengthSlopeSpeed()
* getDesiredSpeed()
* getDesiredSpeed()
+ stepDeviation;
} else {
return stepLength;
......@@ -259,55 +255,55 @@ public class PedestrianOSM extends Pedestrian {
return timeOfNextStep;
}
public VPoint getNextPosition() {
return nextPosition;
public void setTimeOfNextStep(double timeOfNextStep) {
this.timeOfNextStep = timeOfNextStep;
}
public VPoint getLastPosition() {
return lastPosition;
public VPoint getNextPosition() {
return nextPosition;
}
public double getTimeCredit() {
return timeCredit;
public void setNextPosition(VPoint nextPosition) {
this.nextPosition = nextPosition;
}
public Collection<? extends Agent> getRelevantPedestrians() {
return relevantPedestrians;
public VPoint getLastPosition() {
return lastPosition;
}
public double getDurationNextStep() {
return durationNextStep;
public void setLastPosition(VPoint lastPosition) {
this.lastPosition = lastPosition;
}
public AttributesOSM getAttributesOSM() {
return attributesOSM;
public double getTimeCredit() {
return timeCredit;
}
public List<Double>[] getStrides() {
return strides;
public void setTimeCredit(double timeCredit) {
this.timeCredit = timeCredit;
}
// Setters...
public void setNextPosition(VPoint nextPosition) {
this.nextPosition = nextPosition;
public Collection<? extends Agent> getRelevantPedestrians() {
return relevantPedestrians;
}
public void setLastPosition(VPoint lastPosition) {
this.lastPosition = lastPosition;
public double getDurationNextStep() {
return durationNextStep;
}
public void setTimeCredit(double timeCredit) {
this.timeCredit = timeCredit;
public void setDurationNextStep(double durationNextStep) {
this.durationNextStep = durationNextStep;
}
public void setTimeOfNextStep(double timeOfNextStep) {
this.timeOfNextStep = timeOfNextStep;
public AttributesOSM getAttributesOSM() {
return attributesOSM;
}
public void setDurationNextStep(double durationNextStep) {
this.durationNextStep = durationNextStep;
public LinkedList<Double>[] getStrides() {
return strides;
}
public Topography getTopography() {
......@@ -317,7 +313,7 @@ public class PedestrianOSM extends Pedestrian {
public double getMinStepLength() {
return minStepLength;
}
@Override
public PedestrianOSM clone() {
throw new RuntimeException("clone is not supported for PedestrianOSM; it seems hard to implement.");
......
......@@ -15,7 +15,6 @@ import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
......@@ -41,10 +40,11 @@ public class ProjectOutput {
}
/**
* Returns cached output directories. This function does not check if cached {@link SimulationOutput}
* is marked dirty. It is assumed {@link #update()} was called prior to this function.
* Returns cached output directories. This function does not check if cached {@link
* SimulationOutput} is marked dirty. It is assumed {@link #update()} was called prior to this
* function.
*
* @return list of output directories corresponding to cached {@link SimulationOutput}
* @return list of output directories corresponding to cached {@link SimulationOutput}
*/
public List<File> getAllOutputDirs() {
Path out = project.getOutputDir();
......@@ -55,11 +55,11 @@ public class ProjectOutput {
}
/**
* This function does not check if cached {@link SimulationOutput}
* is dirty. It is assumed {@link #update()} was called prior to this function.
* This function does not check if cached {@link SimulationOutput} is dirty. It is assumed
* {@link #update()} was called prior to this function.
*
* @param scenario Prior runs to this {@link Scenario}
* @return List of prior simulation runs matching selected {@link Scenario}
* @param scenario Prior runs to this {@link Scenario}
* @return List of prior simulation runs matching selected {@link Scenario}
*/
public List<File> listSelectedOutputDirs(final Scenario scenario) {
List<File> out = new ArrayList<>();
......@@ -79,10 +79,10 @@ public class ProjectOutput {
}
/**
* If the output directory is present it is marked dirty and will be re-check
* in the next call of {@link #update()}
* If the output directory is present it is marked dirty and will be re-check in the next call
* of {@link #update()}
*
* @param dirName directory name of {@link SimulationOutput}
* @param dirName directory name of {@link SimulationOutput}
*/
synchronized public void markDirty(String dirName) {
getSimulationOutput(dirName).ifPresent(SimulationOutput::setDirty);
......@@ -92,18 +92,19 @@ public class ProjectOutput {
return Optional.ofNullable(simulationOutputs.get(dirName));
}
public Scenario getScenario(String dirName){
public Scenario getScenario(String dirName) {
return getSimulationOutput(dirName).get().getSimulatedScenario();
}
/**
* re-check dirty {@link SimulationOutput} and add new valid output dirs to {@link ProjectOutput}
* re-check dirty {@link SimulationOutput} and add new valid output dirs to {@link
* ProjectOutput}
*/
public void update() {
try {
Files.walkFileTree(project.getOutputDir(), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs){
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
String dirName = dir.toFile().getName();
......@@ -118,9 +119,12 @@ public class ProjectOutput {
Optional<SimulationOutput> outDir = getSimulationOutput(dirName);
// only re-check existing SimulationOutput if they are dirty
if (outDir.isPresent() && outDir.get().isDirty()) {
if (outDir.isPresent()) {
if (!outDir.get().isDirty())
return FileVisitResult.CONTINUE;
Optional<SimulationOutput> newSim = IOOutput.getSimulationOutput(project, dir.toFile());
if (newSim.isPresent()){
if (newSim.isPresent()) {
simulationOutputs.put(dirName, newSim.get());
} else {
simulationOutputs.remove(dirName);
......
......@@ -10,12 +10,13 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
import org.vadere.simulator.models.MainModel;
import org.vadere.simulator.projects.dataprocessing.outputfile.NoDataKeyOutputFile;
import org.vadere.simulator.projects.dataprocessing.outputfile.OutputFile;
import org.vadere.simulator.projects.dataprocessing.outputfile.OutputFileFactory;
import org.vadere.simulator.projects.dataprocessing.processor.DataProcessor;
import org.vadere.simulator.projects.dataprocessing.processor.DataProcessorFactory;
import org.vadere.simulator.projects.dataprocessing.store.DataProcessorStore;
import org.vadere.simulator.projects.dataprocessing.store.OutputFileStore;
import org.vadere.state.attributes.processor.AttributesProcessor;
import org.vadere.state.util.StateJsonConverter;
import org.vadere.util.reflection.DynamicClassInstantiator;
import java.io.IOException;
import java.util.ArrayList;
......@@ -24,263 +25,247 @@ import java.util.stream.Collectors;
/**
* @author Mario Teixeira Parente
*
*/
public class DataProcessingJsonManager {
//TODO Change to 'dataprocessing'
public static final String DATAPROCCESSING_KEY = "processWriters";
public static final String TRAJECTORIES_FILENAME = "postvis.trajectories";
public static final String FILES_KEY = "files";
private static final String TYPE_KEY = "type";
private static final String FILENAME_KEY = "filename";
private static final String FILE_PROCESSORS_KEY = "processors";
private static final String SEPARATOR_KEY = "separator";
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";
private static final String TIMESTAMP_KEY = "isTimestamped";
public static final String DEFAULT_SEPARATOR = " ";
public static final String DEFAULT_OUTPUTFILE_TYPE = NoDataKeyOutputFile.class.getName();
private static ObjectMapper mapper;
public static ObjectWriter writer;
private static final DynamicClassInstantiator<OutputFile<?>> outputFileInstantiator;
private static final DynamicClassInstantiator<DataProcessor<?, ?>> processorInstantiator;
private List<OutputFile<?>> outputFiles;
private List<DataProcessor<?, ?>> dataProcessors;
private boolean isTimestamped;
static {
mapper = StateJsonConverter.getMapper();
writer = mapper.writerWithDefaultPrettyPrinter();
outputFileInstantiator = new DynamicClassInstantiator<>();
processorInstantiator = new DynamicClassInstantiator<>();
}
public DataProcessingJsonManager() {
this.outputFiles = new ArrayList<>();
this.dataProcessors = new ArrayList<>();
this.isTimestamped = true;
}
public List<OutputFile<?>> getOutputFiles() {
return outputFiles;
}
public List<DataProcessor<?, ?>> getDataProcessors() {
return dataProcessors;
}
public void addOutputFile(final OutputFileStore fileStore) {
// If fileName already exists, change it by removing and readding
this.outputFiles.removeAll(this.outputFiles.stream().filter(f -> f.getFileName().equals(fileStore.getFilename())).collect(Collectors.toList()));
this.outputFiles.add(instantiateOutputFile(fileStore));
}
private OutputFile<?> instantiateOutputFile(final OutputFileStore fileStore) {
OutputFile<?> file = outputFileInstantiator.createObject(fileStore.getType());
file.setRelativeFileName(fileStore.getFilename());
file.setProcessorIds(fileStore.getProcessors());
file.setSeparator(fileStore.getSeparator());
return file;
}
public int replaceOutputFile(OutputFileStore fileStore) {
int index = indexOf(fileStore.getFilename());
this.outputFiles.remove(index);
this.outputFiles.add(index, instantiateOutputFile(fileStore));
return index;
}
private int indexOf(String filename) {
for (int i = 0; i < outputFiles.size(); i ++) {
if (outputFiles.get(i).getFileName().equals(filename)) {
return i;
}
}
return -1;
}
public void addProcessor(final DataProcessorStore dataProcessorStore) {
DataProcessor<?, ?> dataProcessor = processorInstantiator.createObject(dataProcessorStore.getType());
dataProcessor.setId(dataProcessorStore.getId());
dataProcessor.setAttributes(dataProcessorStore.getAttributes());
this.dataProcessors.add(dataProcessor);
}
public void addInstantiatedProcessor(final DataProcessor<?, ?> dataProcessor) {
this.dataProcessors.add(dataProcessor);
}
public void updateDataProcessor(final DataProcessor<?, ?> oldDataProcessor, final DataProcessorStore newDataProcessorStore) {
this.dataProcessors.remove(oldDataProcessor);
addProcessor(newDataProcessorStore);
}
public boolean isTimestamped() {
return this.isTimestamped;
}
public void setTimestamped(boolean isTimestamped) {
this.isTimestamped = isTimestamped;
}
private static JsonNode serializeOutputFile(final OutputFile<?> outputFile) {
ObjectNode node = mapper.createObjectNode();
node.put(TYPE_KEY, outputFile.getClass().getName());
node.put(FILENAME_KEY, outputFile.getFileName());
node.set(FILE_PROCESSORS_KEY, mapper.convertValue(outputFile.getProcessorIds(), JsonNode.class));
final String separator = outputFile.getSeparator();
if (separator != DEFAULT_SEPARATOR) {
node.put(SEPARATOR_KEY, separator);
}
return node;
}
public static JsonNode serializeProcessor(final DataProcessor<?, ?> dataProcessor) {
ObjectNode node = mapper.createObjectNode();
node.put(TYPE_KEY, dataProcessor.getClass().getName());
node.put(PROCESSORID_KEY, dataProcessor.getId());
if (dataProcessor.getAttributes() != null) {
node.put(ATTRIBUTESTYPE_KEY, dataProcessor.getAttributes().getClass().getName());