11.3.2021, 9:00 - 11:00: Due to updates GitLab may be unavailable for some minutes between 09:00 and 11:00.

Commit b39e6733 authored by Benedikt Zoennchen's avatar Benedikt Zoennchen

fix compatibility issues between output processors and PostVis.

parent d609f8c4
package org.vadere.simulator.projects.io; package org.vadere.simulator.projects.io;
import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;
import org.apache.commons.math3.util.Pair; import org.apache.commons.math3.util.Pair;
import org.apache.log4j.LogManager; import org.apache.log4j.LogManager;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
...@@ -14,19 +15,11 @@ import org.vadere.util.geometry.shapes.VPoint; ...@@ -14,19 +15,11 @@ import org.vadere.util.geometry.shapes.VPoint;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.LinkedList; import java.util.*;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
* A TrajectoryReader is the counterpart of the * A TrajectoryReader is the counterpart of the {@link PedestrianPositionProcessor}.
* {@link PedestrianPositionProcessor}.
*
*
* This reader trys to generate a {@link java.util.stream.Stream< scenario.Pedestrian >} by
* reading it from a file.
*/ */
public class TrajectoryReader { public class TrajectoryReader {
...@@ -36,34 +29,107 @@ public class TrajectoryReader { ...@@ -36,34 +29,107 @@ public class TrajectoryReader {
private AttributesAgent attributesPedestrian; private AttributesAgent attributesPedestrian;
private static final String SPLITTER = " ";
private Set<String> pedestrianIdKeys;
private Set<String> stepKeys;
private Set<String> xKeys;
private Set<String> yKeys;
private Set<String> targetIdKeys;
private int pedIdIndex;
private int stepIndex;
private int xIndex;
private int yIndex;
private int targetIdIndex;
public TrajectoryReader(final Path trajectoryFilePath, final Scenario scenario) throws IOException { public TrajectoryReader(final Path trajectoryFilePath, final Scenario scenario) throws IOException {
this.trajectoryFilePath = trajectoryFilePath; this(trajectoryFilePath, scenario.getAttributesPedestrian());
this.attributesPedestrian = scenario.getAttributesPedestrian();
} }
public TrajectoryReader(final Path trajectoryFilePath) { public TrajectoryReader(final Path trajectoryFilePath) {
this.trajectoryFilePath = trajectoryFilePath; this(trajectoryFilePath, new AttributesAgent());
this.attributesPedestrian = new AttributesAgent();
} }
private TrajectoryReader(final Path trajectoryFilePath, final AttributesAgent attributesAgent) {
this.trajectoryFilePath = trajectoryFilePath;
this.attributesPedestrian = attributesAgent;
pedestrianIdKeys = new HashSet<>();
stepKeys = new HashSet<>();
xKeys = new HashSet<>();
yKeys = new HashSet<>();
targetIdKeys = new HashSet<>();
pedestrianIdKeys.add("id");
pedestrianIdKeys.add("pedestrianId");
stepKeys.add("timeStep");
stepKeys.add("step");
xKeys.add("x");
yKeys.add("y");
targetIdKeys.add("targetId");
pedIdIndex = -1;
stepIndex = -1;
xIndex = -1;
yIndex = -1;
targetIdIndex = -1;
}
public Map<Step, List<Agent>> readFile() throws IOException { public Map<Step, List<Agent>> readFile() throws IOException {
return Files.lines(this.trajectoryFilePath) // 1. Get the correct column
.skip(1) // Skip header line String header = Files.lines(this.trajectoryFilePath).findFirst().get();
.map(line -> line.split(" ")) String[] columns = header.split(SPLITTER);
.map(cells -> {
int step = Integer.parseInt(cells[0]); for(int index = 0; index < columns.length; index++) {
int pedestrianId = Integer.parseInt(cells[1]); if(pedestrianIdKeys.contains(columns[index])) {
VPoint pos = new VPoint(Double.parseDouble(cells[2]), Double.parseDouble(cells[3])); pedIdIndex = index;
int targetId = Integer.parseInt(cells[4]); }
else if(stepKeys.contains(columns[index])) {
Pedestrian ped = new Pedestrian(new AttributesAgent(this.attributesPedestrian, pedestrianId), new Random()); stepIndex = index;
ped.setPosition(pos); }
LinkedList<Integer> targets = new LinkedList<Integer>(); else if(xKeys.contains(columns[index])) {
targets.addFirst(targetId); xIndex = index;
ped.setTargets(targets); }
else if(yKeys.contains(columns[index])) {
return Pair.create(new Step(Integer.parseInt(cells[0])), ped); yIndex = index;
}) }
.collect(Collectors.groupingBy(pair -> pair.getKey(), Collectors.mapping(pair -> pair.getValue(), Collectors.toList()))); else if(targetIdKeys.contains(columns[index])) {
targetIdIndex = index;
}
}
try {
if(pedIdIndex != -1 && xIndex != -1 && yIndex != -1 && stepIndex != -1) {
return Files.lines(this.trajectoryFilePath)
.skip(1) // Skip header line
.map(line -> line.split(" "))
.map(cells -> {
int step = Integer.parseInt(cells[stepIndex]);
int pedestrianId = Integer.parseInt(cells[pedIdIndex]);
VPoint pos = new VPoint(Double.parseDouble(cells[xIndex]), Double.parseDouble(cells[yIndex]));
int targetId = targetIdIndex != -1 ? Integer.parseInt(cells[targetIdIndex]) : -1;
Pedestrian ped = new Pedestrian(new AttributesAgent(this.attributesPedestrian, pedestrianId), new Random());
ped.setPosition(pos);
LinkedList<Integer> targets = new LinkedList<Integer>();
targets.addFirst(targetId);
ped.setTargets(targets);
return Pair.create(new Step(Integer.parseInt(cells[0])), ped);
})
.collect(Collectors.groupingBy(pair -> pair.getKey(), Collectors.mapping(pair -> pair.getValue(), Collectors.toList())));
}
else {
throw new IOException("could not read trajectory file, some colums are missing.");
}
}
catch (Exception e) {
logger.warn("could not read trajectory file. The file format might not be compatible or it is missing.");
throw e;
}
} }
} }
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