Commit 207c7d24 authored by Tobias Meuser's avatar Tobias Meuser Committed by Jose Ignacio Monreal Bailey
Browse files

Fixed vertically mirrored vehicles bug

    - Introduced function for getting the bounardies of a scenario
        in the SimulationSetupExtractor.
    - Currently only a dummy implementation in the xml-based
        extractor, correct implementation in the TraCI based one
parent d844c6d1
...@@ -34,6 +34,7 @@ import de.tud.kom.p2psim.api.topology.movement.SimLocationActuator; ...@@ -34,6 +34,7 @@ import de.tud.kom.p2psim.api.topology.movement.SimLocationActuator;
import de.tud.kom.p2psim.impl.network.routed.RoutedNetLayer; import de.tud.kom.p2psim.impl.network.routed.RoutedNetLayer;
import de.tud.kom.p2psim.impl.topology.PositionVector; import de.tud.kom.p2psim.impl.topology.PositionVector;
import de.tud.kom.p2psim.impl.topology.movement.vehicular.information.Position; import de.tud.kom.p2psim.impl.topology.movement.vehicular.information.Position;
import de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.SimulationSetupExtractor;
import de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.VehicleController; import de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.VehicleController;
import de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.traci.TraciSimulationController; import de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.traci.TraciSimulationController;
import de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.xml.XMLSimulationController; import de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.xml.XMLSimulationController;
...@@ -72,9 +73,13 @@ public class VehicleMovementModel implements MovementModel, EventHandler, Future ...@@ -72,9 +73,13 @@ public class VehicleMovementModel implements MovementModel, EventHandler, Future
private final int width; private final int width;
private final int height; private final int height;
private double scenarioWidth = 0;
private double scenarioHeight = 0;
private final String sumoExe; private final String sumoExe;
private final String sumoConfigFile; private final String sumoConfigFile;
private final String sumoTrace; private final String sumoTrace;
private String sumoIntersections;
private final long timestepConversion = Time.SECOND; private final long timestepConversion = Time.SECOND;
...@@ -84,8 +89,6 @@ public class VehicleMovementModel implements MovementModel, EventHandler, Future ...@@ -84,8 +89,6 @@ public class VehicleMovementModel implements MovementModel, EventHandler, Future
private List<Position> intersections; private List<Position> intersections;
private String sumoIntersections;
/** /**
* Constructor for the movement model using the sumo TraCI API * Constructor for the movement model using the sumo TraCI API
...@@ -221,6 +224,7 @@ public class VehicleMovementModel implements MovementModel, EventHandler, Future ...@@ -221,6 +224,7 @@ public class VehicleMovementModel implements MovementModel, EventHandler, Future
if (!initialized) { if (!initialized) {
Event.scheduleWithDelay(timeBetweenMoveOperations, this, null, 0); Event.scheduleWithDelay(timeBetweenMoveOperations, this, null, 0);
SimulationSetupExtractor extractor;
if (sumoExe != null) { if (sumoExe != null) {
TraciSimulationController simulationController = TraciSimulationController.createSimulationController(sumoExe, sumoConfigFile); TraciSimulationController simulationController = TraciSimulationController.createSimulationController(sumoExe, sumoConfigFile);
_controller = simulationController; _controller = simulationController;
...@@ -228,7 +232,7 @@ public class VehicleMovementModel implements MovementModel, EventHandler, Future ...@@ -228,7 +232,7 @@ public class VehicleMovementModel implements MovementModel, EventHandler, Future
_controller.setObservedArea(offsetX, offsetY, offsetX + width, offsetY + height); _controller.setObservedArea(offsetX, offsetY, offsetX + width, offsetY + height);
_controller.nextStep(); _controller.nextStep();
intersections = simulationController.getAllIntersections(true); extractor = simulationController;
} else { } else {
XMLSimulationController simulationController; XMLSimulationController simulationController;
if (sumoIntersections == null || sumoIntersections.equals("")) { if (sumoIntersections == null || sumoIntersections.equals("")) {
...@@ -242,8 +246,12 @@ public class VehicleMovementModel implements MovementModel, EventHandler, Future ...@@ -242,8 +246,12 @@ public class VehicleMovementModel implements MovementModel, EventHandler, Future
_controller.init(); _controller.init();
_controller.nextStep(); _controller.nextStep();
intersections = simulationController.getAllIntersections(true); extractor = simulationController;
} }
intersections = extractor.getAllIntersections(true);
scenarioWidth = extractor.getScenarioWidth();
scenarioHeight = extractor.getScenarioHeight();
System.out.println("Initialization: done."); System.out.println("Initialization: done.");
} }
...@@ -280,7 +288,12 @@ public class VehicleMovementModel implements MovementModel, EventHandler, Future ...@@ -280,7 +288,12 @@ public class VehicleMovementModel implements MovementModel, EventHandler, Future
} }
SimLocationActuator component = requestSimActuator(vehicle); SimLocationActuator component = requestSimActuator(vehicle);
component.updateCurrentLocation(new PositionVector(position.getX() / SCALING_FACTOR, position.getY() / SCALING_FACTOR)); if (scenarioHeight != -1) {
component.updateCurrentLocation(new PositionVector(position.getX() / SCALING_FACTOR, scenarioHeight - position.getY() / SCALING_FACTOR));
} else {
// This would be vertically mirrored
component.updateCurrentLocation(new PositionVector(position.getX() / SCALING_FACTOR, position.getY() / SCALING_FACTOR));
}
component.setMovementSpeed(position.getSpeed() / SCALING_FACTOR); component.setMovementSpeed(position.getSpeed() / SCALING_FACTOR);
......
...@@ -6,8 +6,11 @@ import de.tud.kom.p2psim.impl.topology.movement.vehicular.information.Position; ...@@ -6,8 +6,11 @@ import de.tud.kom.p2psim.impl.topology.movement.vehicular.information.Position;
public interface SimulationSetupExtractor { public interface SimulationSetupExtractor {
void setObservedArea(double pStartX, double pStartY, double pEndX, double pEndY); void setObservedArea(double pStartX, double pStartY, double pEndX, double pEndY);
void init(); void init();
List<Position> getAllIntersections(boolean pCluster);
List<Position> getAllIntersections(boolean pCluster);
double getScenarioWidth();
double getScenarioHeight();
} }
...@@ -16,316 +16,345 @@ import de.tudresden.sumo.cmd.Junction; ...@@ -16,316 +16,345 @@ import de.tudresden.sumo.cmd.Junction;
import de.tudresden.sumo.cmd.Simulation; import de.tudresden.sumo.cmd.Simulation;
import de.tudresden.sumo.cmd.Vehicle; import de.tudresden.sumo.cmd.Vehicle;
import de.tudresden.sumo.util.SumoCommand; import de.tudresden.sumo.util.SumoCommand;
import de.tudresden.ws.container.SumoBoundingBox;
import de.tudresden.ws.container.SumoPosition2D; import de.tudresden.ws.container.SumoPosition2D;
import de.tudresden.ws.container.SumoStringList; import de.tudresden.ws.container.SumoStringList;
import it.polito.appeal.traci.SumoTraciConnection; import it.polito.appeal.traci.SumoTraciConnection;
public class TraciSimulationController implements VehicleController, SimulationSetupExtractor { public class TraciSimulationController implements VehicleController, SimulationSetupExtractor {
private static final Map<String, TraciSimulationController> CONTROLLER = new HashMap<>(); private static final Map<String, TraciSimulationController> CONTROLLER = new HashMap<>();
private static final double CLUSTERING_DISTANCE = 50; private static final double CLUSTERING_DISTANCE = 50;
private String _sumoExe; private String _sumoExe;
private String _configFile; private String _configFile;
private SumoTraciConnection _connection; private SumoTraciConnection _connection;
private double _start = -1; private double _start = -1;
private double _step = -1; private double _step = -1;
private double _startX; private double _startX;
private double _startY; private double _startY;
private double _endX; private double _endX;
private double _endY; private double _endY;
private Map<Double, Map<String, Position>> _positonsByTimestamp = new HashMap<>(); private Map<Double, Map<String, Position>> _positonsByTimestamp = new HashMap<>();
private int _futureInformation = 10; private int _futureInformation = 10;
private boolean _initalized = false; private boolean _initalized = false;
private boolean _observedAreaSet; private boolean _observedAreaSet;
private Map<String, Double> _vehiclesOutOfRange = new HashMap<>(); private Map<String, Double> _vehiclesOutOfRange = new HashMap<>();
public static synchronized TraciSimulationController createSimulationController(String pSumoExe, String pConfigFile) { public static synchronized TraciSimulationController createSimulationController(String pSumoExe, String pConfigFile) {
if (!CONTROLLER.containsKey(pConfigFile)) { if (!CONTROLLER.containsKey(pConfigFile)) {
CONTROLLER.put(pConfigFile, new TraciSimulationController(pSumoExe, pConfigFile)); CONTROLLER.put(pConfigFile, new TraciSimulationController(pSumoExe, pConfigFile));
} }
return CONTROLLER.get(pConfigFile); return CONTROLLER.get(pConfigFile);
} }
private TraciSimulationController(String pSumoExe, String pConfigFile) { private TraciSimulationController(String pSumoExe, String pConfigFile) {
_sumoExe = pSumoExe; _sumoExe = pSumoExe;
_configFile = pConfigFile; _configFile = pConfigFile;
} }
public static VehicleController getSimulationController() { public static VehicleController getSimulationController() {
return CONTROLLER.values().iterator().next(); return CONTROLLER.values().iterator().next();
} }
@Override @Override
public synchronized void init() { public synchronized void init() {
if (!_initalized) { if (!_initalized) {
_connection = new SumoTraciConnection(_sumoExe, _configFile); _connection = new SumoTraciConnection(_sumoExe, _configFile);
try { try {
_connection.runServer(); _connection.runServer();
for (int i = 0; i < _futureInformation; i++) { for (int i = 0; i < _futureInformation; i++) {
nextStep(); nextStep();
} }
} catch (RuntimeException e) { } catch (RuntimeException e) {
throw e; throw e;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
Runnable shutdownHook = new Runnable() { Runnable shutdownHook = new Runnable() {
@Override @Override
public void run() { public void run() {
_connection.close(); _connection.close();
} }
}; };
Runtime.getRuntime().addShutdownHook(new Thread(shutdownHook)); Runtime.getRuntime().addShutdownHook(new Thread(shutdownHook));
_initalized = true; _initalized = true;
} }
} }
@Override @Override
public Position getVehiclePosition(String pVehicleID) { public Position getVehiclePosition(String pVehicleID) {
return getVehiclePosition(_step, pVehicleID); return getVehiclePosition(_step, pVehicleID);
} }
@Override @Override
public Position getVehiclePosition(double pStep, String pVehicleID) { public Position getVehiclePosition(double pStep, String pVehicleID) {
Map<String, Position> map = _positonsByTimestamp.get(pStep); Map<String, Position> map = _positonsByTimestamp.get(pStep);
return map.get(pVehicleID); return map.get(pVehicleID);
} }
@Override @Override
public List<Position> getAllIntersections(boolean pCluster) { public List<Position> getAllIntersections(boolean pCluster) {
List<Position> result = new ArrayList<>(); List<Position> result = new ArrayList<>();
SumoCommand intersectionCommand = Junction.getIDList(); SumoCommand intersectionCommand = Junction.getIDList();
Object intersectionObject = requestObject(intersectionCommand); Object intersectionObject = requestObject(intersectionCommand);
SumoStringList intersections = (SumoStringList) intersectionObject; SumoStringList intersections = (SumoStringList) intersectionObject;
for (String intersection : intersections) { for (String intersection : intersections) {
SumoCommand positionCommand = Junction.getPosition(intersection); SumoCommand positionCommand = Junction.getPosition(intersection);
Object positionObject = requestObject(positionCommand); Object positionObject = requestObject(positionCommand);
SumoPosition2D sumoPosition = (SumoPosition2D) positionObject; SumoPosition2D sumoPosition = (SumoPosition2D) positionObject;
if (_observedAreaSet) { if (_observedAreaSet) {
if (_startX <= sumoPosition.x && sumoPosition.x <= _endX && _startY <= sumoPosition.y && sumoPosition.y <= _endY) { if (_startX <= sumoPosition.x && sumoPosition.x <= _endX && _startY <= sumoPosition.y && sumoPosition.y <= _endY) {
result.add(new Position(sumoPosition.x - _startX, sumoPosition.y - _startY, 0, 0)); result.add(new Position(sumoPosition.x - _startX, sumoPosition.y - _startY, 0, 0));
} }
} else { } else {
result.add(new Position(sumoPosition.x, sumoPosition.y, 0, 0)); result.add(new Position(sumoPosition.x, sumoPosition.y, 0, 0));
} }
} }
if (pCluster) { if (pCluster) {
List<Position> tempResult = new ArrayList<>(); List<Position> tempResult = new ArrayList<>();
outer:for (int i = 0; i < result.size(); i++) { outer:for (int i = 0; i < result.size(); i++) {
Position position = result.get(i); Position position = result.get(i);
for (int j = 0; j < tempResult.size(); j++) { for (int j = 0; j < tempResult.size(); j++) {
Position addedPosition = tempResult.get(j); Position addedPosition = tempResult.get(j);
if (position != addedPosition && LocationUtils.calculateDistance(position, addedPosition) < CLUSTERING_DISTANCE / 1000.0) { if (position != addedPosition && LocationUtils.calculateDistance(position, addedPosition) < CLUSTERING_DISTANCE / 1000.0) {
continue outer; continue outer;
} }
} }
tempResult.add(position); tempResult.add(position);
} }
result = tempResult; result = tempResult;
} }
return result; return result;
} }
@Override @Override
public boolean nextStep() { public boolean nextStep() {
try { try {
_connection.do_timestep(); _connection.do_timestep();
try { try {
int temp = (Integer) _connection.do_job_get(Simulation.getCurrentTime()); int temp = (Integer) _connection.do_job_get(Simulation.getCurrentTime());
_step = temp / 1000.0 - _futureInformation; _step = temp / 1000.0 - _futureInformation;
if (_start == -1) { if (_start == -1) {
_start = _step + _futureInformation; _start = _step + _futureInformation;
} }
double newStep = _step + _futureInformation; double newStep = _step + _futureInformation;
if (!_positonsByTimestamp.containsKey(newStep)) { if (!_positonsByTimestamp.containsKey(newStep)) {
Map<String, Position> vehiclePositions = new HashMap<>(); Map<String, Position> vehiclePositions = new HashMap<>();
_positonsByTimestamp.put(newStep, vehiclePositions); _positonsByTimestamp.put(newStep, vehiclePositions);
List<String> allVehicles = requestAllVehicles(); List<String> allVehicles = requestAllVehicles();
for (String vehicle : allVehicles) { for (String vehicle : allVehicles) {
Position vehiclePosition = requestVehiclePosition(vehicle); Position vehiclePosition = requestVehiclePosition(vehicle);
if (vehiclePosition != null) { if (vehiclePosition != null) {
vehiclePositions.put(vehicle, vehiclePosition); vehiclePositions.put(vehicle, vehiclePosition);
} }
} }
} }
if (_positonsByTimestamp.containsKey(_step - 1)) { if (_positonsByTimestamp.containsKey(_step - 1)) {
_positonsByTimestamp.remove(_step - 1); _positonsByTimestamp.remove(_step - 1);
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
return true; return true;
} catch (RuntimeException e) { } catch (RuntimeException e) {
throw e; throw e;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
return false; return false;
} }
private Position requestVehiclePosition(String pVehicleID) { private Position requestVehiclePosition(String pVehicleID) {
if (_vehiclesOutOfRange.containsKey(pVehicleID)) { if (_vehiclesOutOfRange.containsKey(pVehicleID)) {
if (_vehiclesOutOfRange.get(pVehicleID) < _step) { if (_vehiclesOutOfRange.get(pVehicleID) < _step) {
return null; return null;
} else { } else {
_vehiclesOutOfRange.remove(pVehicleID); _vehiclesOutOfRange.remove(pVehicleID);
} }
} }
SumoCommand positionCommand = Vehicle.getPosition(pVehicleID); SumoCommand positionCommand = Vehicle.getPosition(pVehicleID);
SumoCommand angleCommand = Vehicle.getAngle(pVehicleID); SumoCommand angleCommand = Vehicle.getAngle(pVehicleID);
SumoCommand speedCommand = Vehicle.getSpeed(pVehicleID); SumoCommand speedCommand = Vehicle.getSpeed(pVehicleID);
Object positionObject = requestObject(positionCommand); Object positionObject = requestObject(positionCommand);
Object angleObject = requestObject(angleCommand); Object angleObject = requestObject(angleCommand);
Object speedObject = requestObject(speedCommand); Object speedObject = requestObject(speedCommand);
SumoPosition2D sumoPosition = (SumoPosition2D) positionObject; SumoPosition2D sumoPosition = (SumoPosition2D) positionObject;
double angle = (Double) angleObject; double angle = (Double) angleObject;
double speed = (Double) speedObject; double speed = (Double) speedObject;
if (_observedAreaSet) { if (_observedAreaSet) {
if (_startX <= sumoPosition.x && sumoPosition.x <= _endX && _startY <= sumoPosition.y && sumoPosition.y <= _endY) { if (_startX <= sumoPosition.x && sumoPosition.x <= _endX && _startY <= sumoPosition.y && sumoPosition.y <= _endY) {
return new Position(sumoPosition.x - _startX, sumoPosition.y - _startY, 0, angle, speed); return new Position(sumoPosition.x - _startX, sumoPosition.y - _startY, 0, angle, speed);
} else { } else {
double diffX = _startX - sumoPosition.x; double diffX = _startX - sumoPosition.x;
if (diffX < 0 || sumoPosition.x - _endX < diffX) { if (diffX < 0 || sumoPosition.x - _endX < diffX) {
diffX = sumoPosition.x - _endX; diffX = sumoPosition.x - _endX;
} }
if (diffX < 0) { if (diffX < 0) {
diffX = 0; diffX = 0;
} }
double diffY = _startY - sumoPosition.y; double diffY = _startY - sumoPosition.y;
if (diffY < 0 || sumoPosition.y - _endY < diffY) { if (diffY < 0 || sumoPosition.y - _endY < diffY) {
diffY = sumoPosition.y - _endY; diffY = sumoPosition.y - _endY;
} }
if (diffY < 0) { if (diffY < 0) {
diffY = 0; diffY = 0;
} }
double diff = Math.sqrt(Math.pow(diffX, 2) + Math.pow(diffY, 2)); double diff = Math.sqrt(Math.pow(diffX, 2) + Math.pow(diffY, 2));
double timeTillNextJoin = diff / 50; double timeTillNextJoin = diff / 50;
// _vehiclesOutOfRange.put(pVehicleID, _step + timeTillNextJoin); // _vehiclesOutOfRange.put(pVehicleID, _step + timeTillNextJoin);
return null; return null;
} }
} else { } else {
return new Position(sumoPosition.x, sumoPosition.y, 0, angle); return new Position(sumoPosition.x, sumoPosition.y, 0, angle);
} }
} }
private List<String> requestAllVehicles() { private List<String> requestAllVehicles() {
SumoCommand idList = Vehicle.getIDList(); SumoCommand idList = Vehicle.getIDList();
Object object = requestObject(idList); Object object = requestObject(idList);
SumoStringList list = (SumoStringList)object; SumoStringList list = (SumoStringList)object;
List<String> result = new ArrayList<>(); List<String> result = new ArrayList<>();
for (String vehicle : list) { for (String vehicle : list) {
result.add(vehicle); result.add(vehicle);
} }
return result; return result;
} }
@Override @Override
public List<String> getAllVehicles() { public List<String> getAllVehicles() {
return getAllVehicles(_step); return getAllVehicles(_step);
} }
@Override @Override
public List<String> getAllVehicles(double pStep) { public List<String> getAllVehicles(double pStep) {
Map<String, Position> map = _positonsByTimestamp.get(pStep); Map<String, Position> map = _positonsByTimestamp.get(pStep);
return new ArrayList<>(map.keySet()); return new ArrayList<>(map.keySet());
} }
public Route getRoute(String pVehicleID) { public Route getRoute(String pVehicleID) {
SumoCommand routeCommand = Vehicle.getRoute(pVehicleID); SumoCommand routeCommand = Vehicle.getRoute(pVehicleID);
Object object = requestObject(routeCommand); Object object = requestObject(routeCommand);
SumoStringList streetList = (SumoStringList) object; SumoStringList streetList = (SumoStringList) object;
Route route = new Route(); Route route = new Route();
List<Street> streets = route.getStreets(); List<Street> streets = route.getStreets();
for (String street : streetList) { for (String street : streetList) {
streets.add(new Street(street)); streets.add(new Street(street));
} }
return route; return route;
} }
public Object requestObject(SumoCommand routeCommand) { public Object requestObject(SumoCommand routeCommand) {
Object object = null; Object object = null;
try { try {
object = _connection.do_job_get(routeCommand); object = _connection.do_job_get(routeCommand);
} catch (RuntimeException e) { } catch (RuntimeException e) {
throw e; throw e;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
return object; return object;
} }
@Override @Override
public double getStep() { public double getStep() {
return _step; return _step;
} }
@Override @Override
public double getStart() { public double getStart() {
return _start; return _start;
} }
@Override @Override
public double getMaximumAvailablePrediction() { public double getMaximumAvailablePrediction() {
double max = Collections.max(_positonsByTimestamp.keySet()); double max = Collections.max(_positonsByTimestamp.keySet());
return max; return max;
} }
@Override @Override
public void setObservedArea(double pStartX, double pStartY, double pEndX, double pEndY) { public void setObservedArea(double pStartX, double pStartY, double pEndX, double pEndY) {
_startX = pStartX; _startX = pStartX;
_startY = pStartY; _startY = pStartY;
_endX = pEndX; _endX = pEndX;
_endY = pEndY; _endY = pEndY;
_observedAreaSet = true; _observedAreaSet = true;
} }
@Override
public double getScenarioWidth() {
SumoCommand netBoundaryCommand = Simulation.getNetBoundary();
try {
SumoBoundingBox netBoundary = (SumoBoundingBox) _connection.do_job_get(netBoundaryCommand);
return netBoundary.x_max + netBoundary.x_min;
} catch (Exception e) {
//Nothing to do
}
return -1;
}
@Override
public double getScenarioHeight() {
SumoCommand netBoundaryCommand = Simulation.getNetBoundary();
try {
SumoBoundingBox netBoundary = (SumoBoundingBox) _connection.do_job_get(netBoundaryCommand);
System.out.println(netBoundary.y_max);
System.out.println(netBoundary.y_min);
return netBoundary.y_max + netBoundary.y_min;
} catch (Exception e) {
//Nothing to do
}
return -1;
}
} }
...@@ -16,143 +16,153 @@ import de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.contro ...@@ -16,143 +16,153 @@ import de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.contro
import de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.csv.RoadSideUnitInformationHandler; import de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.csv.RoadSideUnitInformationHandler;
public class XMLSimulationController implements VehicleController, SimulationSetupExtractor, Runnable { public class XMLSimulationController implements VehicleController, SimulationSetupExtractor, Runnable {
private String _vehicleDataPath; private String _vehicleDataPath;
private String _roadSideUnitDataPath; private String _roadSideUnitDataPath;
private List<String> _vehicles; private List<String> _vehicles;
private Map<Double, Map<String, Position>> _positonsByTimestamp = new HashMap<>(); private Map<Double, Map<String, Position>> _positonsByTimestamp = new HashMap<>();
private int _futureInformation = 10; private int _futureInformation = 10;
private boolean _initalized = false; private boolean _initalized = false;
private VehicleDataInformationHandler _vehicleDataInformationHandler = new VehicleDataInformationHandler(); private VehicleDataInformationHandler _vehicleDataInformationHandler = new VehicleDataInformationHandler();
private RoadSideUnitInformationHandler _roadSideUnitInformationHandler = new RoadSideUnitInformationHandler(); private RoadSideUnitInformationHandler _roadSideUnitInformationHandler = new RoadSideUnitInformationHandler();
private double _start = -1; private double _start = -1;
private double _step; private double _step;
public XMLSimulationController(String pVehicleDataPath, String pRoadSideUnitDataPath) { public XMLSimulationController(String pVehicleDataPath, String pRoadSideUnitDataPath) {
this(pVehicleDataPath); this(pVehicleDataPath);
_roadSideUnitDataPath = pRoadSideUnitDataPath; _roadSideUnitDataPath = pRoadSideUnitDataPath;
} }
public XMLSimulationController(String pVehicleDataPath) { public XMLSimulationController(String pVehicleDataPath) {
_vehicleDataPath = pVehicleDataPath; _vehicleDataPath = pVehicleDataPath;
} }
@Override @Override
public void init() { public void init() {
if (!_initalized) { if (!_initalized) {
if (_vehicleDataPath != null) { if (_vehicleDataPath != null) {
Thread thread = new Thread(this); Thread thread = new Thread(this);
thread.start(); thread.start();
for (int i = 0; i < _futureInformation; i++) { for (int i = 0; i < _futureInformation; i++) {
nextStep(); nextStep();
} }
} }
if (_roadSideUnitDataPath != null) { if (_roadSideUnitDataPath != null) {
_roadSideUnitInformationHandler.parseIntersectionsFile(_roadSideUnitDataPath); _roadSideUnitInformationHandler.parseIntersectionsFile(_roadSideUnitDataPath);
} }
_initalized = true; _initalized = true;
} }
} }
@Override @Override
public Position getVehiclePosition(String pVehicleID) { public Position getVehiclePosition(String pVehicleID) {
return getVehiclePosition(_step, pVehicleID); return getVehiclePosition(_step, pVehicleID);
} }
public Position requestVehiclePosition(String pVehicleID) { public Position requestVehiclePosition(String pVehicleID) {
return _vehicleDataInformationHandler.getVehiclePositions().get(pVehicleID); return _vehicleDataInformationHandler.getVehiclePositions().get(pVehicleID);
} }
@Override @Override
public boolean nextStep() { public boolean nextStep() {
_vehicleDataInformationHandler.readNext(); _vehicleDataInformationHandler.readNext();
_step = _vehicleDataInformationHandler.getStep() - _futureInformation; _step = _vehicleDataInformationHandler.getStep() - _futureInformation;
if (_start == -1) { if (_start == -1) {
_start = _vehicleDataInformationHandler.getStep(); _start = _vehicleDataInformationHandler.getStep();
} }
double newStep = _step + _futureInformation; double newStep = _step + _futureInformation;
if (!_positonsByTimestamp.containsKey(newStep)) { if (!_positonsByTimestamp.containsKey(newStep)) {
Map<String, Position> vehiclePositions = new HashMap<>(); Map<String, Position> vehiclePositions = new HashMap<>();
_positonsByTimestamp.put(newStep, vehiclePositions); _positonsByTimestamp.put(newStep, vehiclePositions);
List<String> allVehicles = new ArrayList<>(_vehicleDataInformationHandler.getVehiclePositions().keySet()); List<String> allVehicles = new ArrayList<>(_vehicleDataInformationHandler.getVehiclePositions().keySet());
for (String vehicle : allVehicles) { for (String vehicle : allVehicles) {
Position vehiclePosition = requestVehiclePosition(vehicle); Position vehiclePosition = requestVehiclePosition(vehicle);
if (vehiclePosition != null) { if (vehiclePosition != null) {
vehiclePositions.put(vehicle, vehiclePosition); vehiclePositions.put(vehicle, vehiclePosition);
} }
} }
} }
return !_vehicleDataInformationHandler.isTerminated(); return !_vehicleDataInformationHandler.isTerminated();
} }
@Override @Override
public List<String> getAllVehicles() { public List<String> getAllVehicles() {
return getAllVehicles(_step); return getAllVehicles(_step);
} }
@Override @Override
public double getStep() { public double getStep() {
return _step; return _step;
} }
@Override @Override
public double getStart() { public double getStart() {
return _start; return _start;
} }
@Override @Override
public void run() { public void run() {
try { try {
SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
parser.parse(new FileInputStream(_vehicleDataPath), _vehicleDataInformationHandler); parser.parse(new FileInputStream(_vehicleDataPath), _vehicleDataInformationHandler);
} catch (RuntimeException e) { } catch (RuntimeException e) {
throw e; throw e;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
@Override @Override
public void setObservedArea(double pStartX, double pStartY, double pEndX, double pEndY) { public void setObservedArea(double pStartX, double pStartY, double pEndX, double pEndY) {
_vehicleDataInformationHandler.setObservedArea(pStartX, pStartY, pEndX, pEndY); _vehicleDataInformationHandler.setObservedArea(pStartX, pStartY, pEndX, pEndY);
_roadSideUnitInformationHandler.setObservedArea(pStartX, pStartY, pEndX, pEndY); _roadSideUnitInformationHandler.setObservedArea(pStartX, pStartY, pEndX, pEndY);
} }
@Override @Override
public Position getVehiclePosition(double pStep, String pVehicleID) { public Position getVehiclePosition(double pStep, String pVehicleID) {
Map<String, Position> map = _positonsByTimestamp.get(pStep); Map<String, Position> map = _positonsByTimestamp.get(pStep);
return map.get(pVehicleID); return map.get(pVehicleID);
} }
@Override @Override
public List<String> getAllVehicles(double pStep) { public List<String> getAllVehicles(double pStep) {
Map<String, Position> map = _positonsByTimestamp.get(pStep); Map<String, Position> map = _positonsByTimestamp.get(pStep);
return new ArrayList<>(map.keySet()); return new ArrayList<>(map.keySet());
} }
@Override @Override
public double getMaximumAvailablePrediction() { public double getMaximumAvailablePrediction() {
double max = Collections.max(_positonsByTimestamp.keySet()); double max = Collections.max(_positonsByTimestamp.keySet());
return max; return max;
} }
@Override @Override
public List<Position> getAllIntersections(boolean pCluster) { public List<Position> getAllIntersections(boolean pCluster) {
return _roadSideUnitInformationHandler.getIntersections(); return _roadSideUnitInformationHandler.getIntersections();
} }
@Override
public double getScenarioWidth() {
return -1;
}
@Override
public double getScenarioHeight() {
return -1;
}
} }
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