Commit 8f8886af authored by Tobias Meuser's avatar Tobias Meuser
Browse files

Added sumo library to peerfact, missing *.jar dependency

parent 6e7cdecd
......@@ -28,17 +28,15 @@ import java.util.Map;
import java.util.Queue;
import java.util.Set;
import org.kom.probsense.information.Position;
import com.kom.probsense.sumo.simulation.controller.VehicleController;
import com.kom.probsense.sumo.simulation.controller.traci.TraciSimulationController;
import com.kom.probsense.sumo.simulation.controller.xml.XMLSimulationController;
import de.tud.kom.p2psim.api.network.SimNetInterface;
import de.tud.kom.p2psim.api.topology.movement.MovementModel;
import de.tud.kom.p2psim.api.topology.movement.SimLocationActuator;
import de.tud.kom.p2psim.impl.network.routed.RoutedNetLayer;
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.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.xml.XMLSimulationController;
import de.tudarmstadt.maki.simonstrator.api.Event;
import de.tudarmstadt.maki.simonstrator.api.EventHandler;
import de.tudarmstadt.maki.simonstrator.api.Host;
......@@ -139,31 +137,6 @@ public class VehicleMovementModel implements MovementModel, EventHandler, Future
this.height = height;
}
/**
* Constructor for the movement model using the a generated sumo trace file
* @param timeBetweenMoveOperations The time between two movement operations.
* @param sumoTrace The path to the vehicle movement file (*.xml)
* @param offsetX The offset that should be used. If no offset is required, offset 0 shall be used.
* @param offsetY The offset that should be used. If no offset is required, offset 0 shall be used.
* @param width The width of the scenario.
* @param height The height of the scenario.
*/
@XMLConfigurableConstructor({ "timeBetweenMoveOperations", "sumoTrace", "offsetX", "offsetY", "width", "height" })
public VehicleMovementModel(long timeBetweenMoveOperations, String sumoTrace, int offsetX, int offsetY, int width, int height) {
this.timeBetweenMoveOperations = timeBetweenMoveOperations;
this.components = new LinkedList<>();
this.freeComponents = new LinkedList<>();
this.idComponentMatcher = new HashMap<>();
this.sumoExe = null;
this.sumoConfigFile = null;
this.sumoTrace = sumoTrace;
this.sumoIntersections = null;
this.offsetX = offsetX;
this.offsetY = offsetY;
this.width = width;
this.height = height;
}
/**
* Constructor for the movement model using the sumo TraCI API
* @param timeBetweenMoveOperations The time between two movement operations.
......
package de.tud.kom.p2psim.impl.topology.movement.vehicular.information;
import java.awt.geom.Point2D;
import java.util.HashMap;
import java.util.Map;
import de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.SimulationSetupInformationProvider;
public class LocationUtils {
private static boolean _saveID = false;
private static Map<String, Long> _ids = new HashMap<>();
private static long _currentID = 0;
public static Position createPosition(Point2D pPosition, double pHeading) {
Position position = SimulationSetupInformationProvider.getOnlyInstance().getUpperLeft();
double lat = position.getY() + pPosition.getY() / 10000000 * 90;
double lon = position.getX() + pPosition.getX()
/ (20000000 * Math.cos(position.getY() / 180 * Math.PI)) * 180;
return new Position(lon, lat, position.getAlt(), pHeading);
}
public static long calculateID(String pID) {
long numericalID = -1;
if (!_saveID) {
try {
String[] split = pID.split("_");
for (int i = 0; i < split.length; i++) {
numericalID *= 100000;
numericalID += Integer.valueOf(split[i]);
}
} catch (NumberFormatException e) {
_saveID = true;
}
}
if (_saveID) {
synchronized (_ids) {
if (!_ids.containsKey(pID)) {
_ids.put(pID, _currentID++);
}
numericalID = _ids.get(pID);
}
}
return numericalID;
}
public static double calculateDistance(Position start, Position end) {
return Math.sqrt(Math.pow(end.getY() - start.getY(), 2) + Math.pow(end.getX() - start.getX(), 2)) / 1000.0;
}
}
package de.tud.kom.p2psim.impl.topology.movement.vehicular.information;
import java.util.Comparator;
public class NoDublicatePositionComparator implements Comparator<Position> {
@Override
public int compare(Position pPos1, Position pPos2) {
int compare = Double.compare(pPos1.getY(), pPos2.getY());
if (compare == 0) {
compare = Double.compare(pPos1.getX(), pPos2.getX());
if (compare == 0) {
compare = Double.compare(pPos1.getAlt(), pPos2.getAlt());
if (compare == 0) {
compare = Double.compare(pPos1.getHeading(), pPos2.getHeading());
}
}
}
return compare;
}
}
package de.tud.kom.p2psim.impl.topology.movement.vehicular.information;
import java.io.Serializable;
public class Position implements Serializable, Comparable<Position> {
/**
*
*/
private static final long serialVersionUID = -5007352031660976321L;
public static final double EQUAL_DISTANCE = 0.0;
private static final double EQUAL_HEADING = 5;
private static final double EQUAL_ALT = 10;
private double _y;
private double _x;
private double _heading;
private double _alt;
private double _speed;
public Position(double pX, double pY, double pAlt, double pHeading, double pSpeed) {
_x = pX;
_y = pY;
_alt = pAlt;
_heading = pHeading;
_speed = pSpeed;
}
public Position(double pX, double pY, double pAlt, double pHeading) {
this(pX, pY, pAlt, pHeading, 0);
}
public double getY() {
return _y;
}
public void setY(double pY) {
_y = pY;
}
public double getX() {
return _x;
}
public void setX(double pX) {
_x = pX;
}
public double getHeading() {
return _heading;
}
public void setHeading(double pHeading) {
_heading = pHeading;
}
public double getAlt() {
return _alt;
}
public void setAlt(double pAlt) {
_alt = pAlt;
}
public double getSpeed() {
return _speed;
}
public void setSpeed(double pSpeed) {
_speed = pSpeed;
}
@Override
public String toString() {
return _y + "/" + _x + " (" + _heading + ")";
}
@Override
public int hashCode() {
return 0;
}
@Override
public boolean equals(Object pObj) {
if (pObj instanceof Position) {
Position start = (Position) pObj;
double difference = LocationUtils.calculateDistance(start, this);
if (difference <= EQUAL_DISTANCE) {
if (Math.abs(getAlt() - start.getAlt()) < EQUAL_ALT) {
if (Math.abs(getHeading() - start.getHeading()) < EQUAL_HEADING) {
return true;
}
}
}
}
return false;
}
@Override
public int compareTo(Position pPosition) {
if (Math.abs(_y - pPosition.getY()) < 1 / 1000.0
&& Math.abs(_x - pPosition.getX()) < 1 / 1000.0
&& Math.abs(_alt - pPosition.getAlt()) <= EQUAL_ALT
&& Math.abs(_heading - pPosition.getHeading()) <= EQUAL_HEADING) {
if (equals(pPosition)) {
return 0;
}
}
int compare = Double.compare(_y, pPosition.getY());
if (compare == 0) {
compare = Double.compare(_x, pPosition.getX());
if (compare == 0) {
compare = Double.compare(_alt, pPosition.getAlt());
if (compare == 0) {
compare = Double.compare(_heading, pPosition.getHeading());
}
}
}
return compare;
}
}
package de.tud.kom.p2psim.impl.topology.movement.vehicular.information;
import java.util.ArrayList;
import java.util.List;
public class Route {
private List<Street> _waypoints;
public Route() {
_waypoints = new ArrayList<>();
}
public List<Street> getStreets() {
return _waypoints;
}
}
package de.tud.kom.p2psim.impl.topology.movement.vehicular.information;
public class Street {
private String _edgeID;
public Street(String pEdgeID) {
_edgeID = pEdgeID;
}
public String getEdgeID() {
return _edgeID;
}
}
package de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller;
import java.util.List;
import de.tud.kom.p2psim.impl.topology.movement.vehicular.information.Position;
public interface SimulationSetupExtractor {
void setObservedArea(double pStartX, double pStartY, double pEndX, double pEndY);
void init();
List<Position> getAllIntersections(boolean pCluster);
}
package de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller;
import java.io.FileInputStream;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
import de.tud.kom.p2psim.impl.topology.movement.vehicular.information.Position;
import de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.xml.CancelParsingSAXException;
import de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.xml.SimulationSetupInformationHandler;
public class SimulationSetupInformationProvider {
private static final SimulationSetupInformationProvider ONLY_INSTANCE = new SimulationSetupInformationProvider();
private SimulationSetupInformationHandler _handler = new SimulationSetupInformationHandler();
private SimulationSetupInformationProvider() {
}
public static SimulationSetupInformationProvider getOnlyInstance() {
return ONLY_INSTANCE;
}
public Position getUpperLeft() {
return _handler.getUpperLeft();
}
public Position getLowerRight() {
return _handler.getLowerRight();
}
public static void init(String pNetFileLocation) {
try {
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
parser.parse(new FileInputStream(pNetFileLocation), ONLY_INSTANCE._handler);
} catch (CancelParsingSAXException e) {
return;
} catch (ParserConfigurationException | SAXException | IOException e) {
e.printStackTrace();
}
}
}
package de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller;
import java.util.List;
import de.tud.kom.p2psim.impl.topology.movement.vehicular.information.Position;
public interface VehicleController {
void setObservedArea(double pStartX, double pStartY, double pEndX, double pEndY);
Position getVehiclePosition(String pVehicleID);
Position getVehiclePosition(double pStep, String pVehicleID);
boolean nextStep();
List<String> getAllVehicles();
List<String> getAllVehicles(double pStep);
double getMaximumAvailablePrediction();
double getStep();
double getStart();
void init();
}
package de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.csv;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import de.tud.kom.p2psim.impl.topology.movement.vehicular.information.Position;
public class RoadSideUnitInformationHandler {
private List<Position> _positions = new ArrayList<>();
private boolean _observedAreaSet = false;
private double _startX = -1;
private double _startY = -1;
private double _endX = -1;
private double _endY = -1;
public List<Position> getIntersections() {
if (!_observedAreaSet) {
return _positions;
} else {
List<Position> result = new ArrayList<>();
for (Position position : _positions) {
if (_startX <= position.getX() && position.getX() <= _endX && _startY <= position.getY() && position.getY() <= _endY) {
result.add(new Position(position.getX() - _startX, position.getY() - _startY, 0, 0));
}
}
return result;
}
}
public void parseIntersectionsFile(String pRoadSideUnitDataPath) {
File file = new File(pRoadSideUnitDataPath);
try {
FileInputStream inputStream = new FileInputStream(file);
Scanner scanner = new Scanner(inputStream);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.contains(";")) {
String[] split = line.split(";");
if (split.length == 2) {
double x = Double.parseDouble(split[0]);
double y = Double.parseDouble(split[1]);
_positions.add(new Position(x, y, 0, 0));
}
}
}
scanner.close();
} catch (FileNotFoundException e) {
throw new UnsupportedOperationException("Unable to read file", e);
}
}
public void setObservedArea(double pStartX, double pStartY, double pEndX, double pEndY) {
_startX = pStartX;
_startY = pStartY;
_endX = pEndX;
_endY = pEndY;
_observedAreaSet = true;
}
}
package de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.traci;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import de.tud.kom.p2psim.impl.topology.movement.vehicular.information.LocationUtils;
import de.tud.kom.p2psim.impl.topology.movement.vehicular.information.Position;
import de.tud.kom.p2psim.impl.topology.movement.vehicular.information.Route;
import de.tud.kom.p2psim.impl.topology.movement.vehicular.information.Street;
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.tudresden.sumo.cmd.Junction;
import de.tudresden.sumo.cmd.Simulation;
import de.tudresden.sumo.cmd.Vehicle;
import de.tudresden.sumo.util.SumoCommand;
import de.tudresden.ws.container.SumoPosition2D;
import de.tudresden.ws.container.SumoStringList;
import it.polito.appeal.traci.SumoTraciConnection;
public class TraciSimulationController implements VehicleController, SimulationSetupExtractor {
private static final Map<String, TraciSimulationController> CONTROLLER = new HashMap<>();
private static final double CLUSTERING_DISTANCE = 50;
private String _sumoExe;
private String _configFile;
private SumoTraciConnection _connection;
private double _start = -1;
private double _step = -1;
private double _startX;
private double _startY;
private double _endX;
private double _endY;
private Map<Double, Map<String, Position>> _positonsByTimestamp = new HashMap<>();
private int _futureInformation = 10;
private boolean _initalized = false;
private boolean _observedAreaSet;
private Map<String, Double> _vehiclesOutOfRange = new HashMap<>();
public static synchronized TraciSimulationController createSimulationController(String pSumoExe, String pConfigFile) {
if (!CONTROLLER.containsKey(pConfigFile)) {
CONTROLLER.put(pConfigFile, new TraciSimulationController(pSumoExe, pConfigFile));
}
return CONTROLLER.get(pConfigFile);
}
private TraciSimulationController(String pSumoExe, String pConfigFile) {
_sumoExe = pSumoExe;
_configFile = pConfigFile;
}
public static VehicleController getSimulationController() {
return CONTROLLER.values().iterator().next();
}
@Override
public synchronized void init() {
if (!_initalized) {
_connection = new SumoTraciConnection(_sumoExe, _configFile);
try {
_connection.runServer();
for (int i = 0; i < _futureInformation; i++) {
nextStep();
}
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
e.printStackTrace();
}
Runnable shutdownHook = new Runnable() {
@Override
public void run() {
_connection.close();
}
};
Runtime.getRuntime().addShutdownHook(new Thread(shutdownHook));
_initalized = true;
}
}
@Override
public Position getVehiclePosition(String pVehicleID) {
return getVehiclePosition(_step, pVehicleID);
}
@Override
public Position getVehiclePosition(double pStep, String pVehicleID) {
Map<String, Position> map = _positonsByTimestamp.get(pStep);
return map.get(pVehicleID);
}
@Override
public List<Position> getAllIntersections(boolean pCluster) {
List<Position> result = new ArrayList<>();
SumoCommand intersectionCommand = Junction.getIDList();
Object intersectionObject = requestObject(intersectionCommand);
SumoStringList intersections = (SumoStringList) intersectionObject;
for (String intersection : intersections) {
SumoCommand positionCommand = Junction.getPosition(intersection);
Object positionObject = requestObject(positionCommand);
SumoPosition2D sumoPosition = (SumoPosition2D) positionObject;
if (_observedAreaSet) {
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));
}
} else {
result.add(new Position(sumoPosition.x, sumoPosition.y, 0, 0));
}
}
if (pCluster) {
List<Position> tempResult = new ArrayList<>();
outer:for (int i = 0; i < result.size(); i++) {
Position position = result.get(i);
for (int j = 0; j < tempResult.size(); j++) {
Position addedPosition = tempResult.get(j);
if (position != addedPosition && LocationUtils.calculateDistance(position, addedPosition) < CLUSTERING_DISTANCE / 1000.0) {
continue outer;
}
}
tempResult.add(position);
}
result = tempResult;
}
return result;
}
@Override
public boolean nextStep() {
try {
_connection.do_timestep();
try {
int temp = (Integer) _connection.do_job_get(Simulation.getCurrentTime());
_step = temp / 1000.0 - _futureInformation;
if (_start == -1) {
_start = _step + _futureInformation;
}
double newStep = _step + _futureInformation;
if (!_positonsByTimestamp.containsKey(newStep)) {
Map<String, Position> vehiclePositions = new HashMap<>();
_positonsByTimestamp.put(newStep, vehiclePositions);
List<String> allVehicles = requestAllVehicles();
for (String vehicle : allVehicles) {
Position vehiclePosition = requestVehiclePosition(vehicle);
if (vehiclePosition != null) {
vehiclePositions.put(vehicle, vehiclePosition);
}
}
}
if (_positonsByTimestamp.containsKey(_step - 1)) {
_positonsByTimestamp.remove(_step - 1);
}
} catch (Exception e) {
e.printStackTrace();
}
return true;
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
private Position requestVehiclePosition(String pVehicleID) {
if (_vehiclesOutOfRange.containsKey(pVehicleID)) {
if (_vehiclesOutOfRange.get(pVehicleID) < _step) {
return null;
} else {
_vehiclesOutOfRange.remove(pVehicleID);
}
}
SumoCommand positionCommand = Vehicle.getPosition(pVehicleID);
SumoCommand angleCommand = Vehicle.getAngle(pVehicleID);
SumoCommand speedCommand = Vehicle.getSpeed(pVehicleID);
Object positionObject = requestObject(positionCommand);
Object angleObject = requestObject(angleCommand);
Object speedObject = requestObject(speedCommand);
SumoPosition2D sumoPosition = (SumoPosition2D) positionObject;
double angle = (Double) angleObject;
double speed = (Double) speedObject;
if (_observedAreaSet) {
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);
} else {
double diffX = _startX - sumoPosition.x;
if (diffX < 0 || sumoPosition.x - _endX < diffX) {
diffX = sumoPosition.x - _endX;
}
if (diffX < 0) {
diffX = 0;
}
double diffY = _startY - sumoPosition.y;
if (diffY < 0 || sumoPosition.y - _endY < diffY) {
diffY = sumoPosition.y - _endY;
}
if (diffY < 0) {
diffY = 0;
}
double diff = Math.sqrt(Math.pow(diffX, 2) + Math.pow(diffY, 2));
double timeTillNextJoin = diff / 50;
// _vehiclesOutOfRange.put(pVehicleID, _step + timeTillNextJoin);
return null;
}
} else {
return new Position(sumoPosition.x, sumoPosition.y, 0, angle);
}
}
private List<String> requestAllVehicles() {
SumoCommand idList = Vehicle.getIDList();
Object object = requestObject(idList);
SumoStringList list = (SumoStringList)object;
List<String> result = new ArrayList<>();
for (String vehicle : list) {
result.add(vehicle);
}
return result;
}
@Override
public List<String> getAllVehicles() {
return getAllVehicles(_step);
}
@Override
public List<String> getAllVehicles(double pStep) {
Map<String, Position> map = _positonsByTimestamp.get(pStep);
return new ArrayList<>(map.keySet());
}
public Route getRoute(String pVehicleID) {
SumoCommand routeCommand = Vehicle.getRoute(pVehicleID);
Object object = requestObject(routeCommand);
SumoStringList streetList = (SumoStringList) object;
Route route = new Route();
List<Street> streets = route.getStreets();
for (String street : streetList) {
streets.add(new Street(street));
}
return route;
}
public Object requestObject(SumoCommand routeCommand) {
Object object = null;
try {
object = _connection.do_job_get(routeCommand);
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
e.printStackTrace();
}
return object;
}
@Override
public double getStep() {
return _step;
}
@Override
public double getStart() {
return _start;
}
@Override
public double getMaximumAvailablePrediction() {
double max = Collections.max(_positonsByTimestamp.keySet());
return max;
}
@Override
public void setObservedArea(double pStartX, double pStartY, double pEndX, double pEndY) {
_startX = pStartX;
_startY = pStartY;
_endX = pEndX;
_endY = pEndY;
_observedAreaSet = true;
}
}
package de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.xml;
import org.xml.sax.SAXException;
public class CancelParsingSAXException extends SAXException {
/**
*
*/
private static final long serialVersionUID = 1L;
}
package de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.xml;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import de.tud.kom.p2psim.impl.topology.movement.vehicular.information.Position;
public class SimulationSetupInformationHandler extends DefaultHandler {
private Position _upperLeft;
private Position _lowerRight;
@Override
public void startElement(String pUri, String pLocalName, String pQName, Attributes pAttributes)
throws SAXException {
if (pQName.equals("location")) {
String meterBoundary = pAttributes.getValue("convBoundary");
if (meterBoundary != null) {
String[] edges = meterBoundary.split(",");
_upperLeft = new Position(Double.valueOf(edges[0]), Double.valueOf(edges[1]), 0, 0);
_lowerRight = new Position(Double.valueOf(edges[2]), Double.valueOf(edges[3]), 0, 0);
}
throw new CancelParsingSAXException();
}
}
public Position getLowerRight() {
return _lowerRight;
}
public Position getUpperLeft() {
return _upperLeft;
}
}
package de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.xml;
import java.util.HashMap;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import de.tud.kom.p2psim.impl.topology.movement.vehicular.information.Position;
public class VehicleDataInformationHandler extends DefaultHandler {
private boolean _next = true;
private boolean _run = true;
private HashMap<String, Position> _vehiclePositions = new HashMap<>();
private double _currentStep = -1;
private boolean _observedAreaSet = false;
private double _startX = -1;
private double _startY = -1;
private double _endX = -1;
private double _endY = -1;
private HashMap<String, Position> _nextVehiclePositions = new HashMap<>();
private double _nextStep = -1;
private static final String doublePattern = "-?[0-9]*\\.?[0-9]+";
private boolean _terminated = false;
@Override
public void startElement(String pUri, String pLocalName, String pQName, Attributes pAttributes)
throws SAXException {
if (pQName.equals("timestep")) {
while (!_next) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
_next = false;
String value = pAttributes.getValue("time");
if (value.matches(doublePattern)) {
_nextStep = Double.valueOf(value);
}
} else if (pQName.equals("vehicle")) {
String id = pAttributes.getValue("id");
String lonString = pAttributes.getValue("x");
String latString = pAttributes.getValue("y");
String speedString = pAttributes.getValue("speed");
String headingString = pAttributes.getValue("angle");
if (lonString.matches(doublePattern) && latString.matches(doublePattern) && headingString.matches(doublePattern) && speedString.matches(doublePattern) ) {
double lon = Double.valueOf(lonString);
double lat = Double.valueOf(latString);
double heading = Double.valueOf(headingString);
double speed = Double.valueOf(speedString);
if (_observedAreaSet) {
if (_startX <= lon && lon <= _endX && _startY <= lat && lat <= _endY) {
_nextVehiclePositions.put(id, new Position(lon - _startX, lat - _startY, 0, heading, speed));
}
} else {
_nextVehiclePositions.put(id, new Position(lon, lat, 0, heading, speed));
}
}
}
}
@Override
public void endElement(String pUri, String pLocalName, String pQName) throws SAXException {
if (pQName.equals("timestep")) {
_run = false;
} else if (pQName.equals("fcd-export")) {
while (!_next) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
_next = false;
_run = false;
_terminated = true;
}
}
public void setObservedArea(double pStartX, double pStartY, double pEndX, double pEndY) {
_startX = pStartX;
_startY = pStartY;
_endX = pEndX;
_endY = pEndY;
_observedAreaSet = true;
}
public boolean isTerminated() {
return _terminated;
}
public HashMap<String, Position> getVehiclePositions() {
return _vehiclePositions;
}
public void readNext() {
while (_run) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
_vehiclePositions = _nextVehiclePositions;
_currentStep = _nextStep;
_nextStep = -1;
_nextVehiclePositions = new HashMap<>();
_run = true;
_next = true;
}
public boolean isRunning() {
return _run;
}
public double getStep() {
return _currentStep;
}
}
package de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.xml;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
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.csv.RoadSideUnitInformationHandler;
public class XMLSimulationController implements VehicleController, SimulationSetupExtractor, Runnable {
private String _vehicleDataPath;
private String _roadSideUnitDataPath;
private List<String> _vehicles;
private Map<Double, Map<String, Position>> _positonsByTimestamp = new HashMap<>();
private int _futureInformation = 10;
private boolean _initalized = false;
private VehicleDataInformationHandler _vehicleDataInformationHandler = new VehicleDataInformationHandler();
private RoadSideUnitInformationHandler _roadSideUnitInformationHandler = new RoadSideUnitInformationHandler();
private double _start = -1;
private double _step;
public XMLSimulationController(String pVehicleDataPath, String pRoadSideUnitDataPath) {
this(pVehicleDataPath);
_roadSideUnitDataPath = pRoadSideUnitDataPath;
}
public XMLSimulationController(String pVehicleDataPath) {
_vehicleDataPath = pVehicleDataPath;
}
@Override
public void init() {
if (!_initalized) {
if (_vehicleDataPath != null) {
Thread thread = new Thread(this);
thread.start();
for (int i = 0; i < _futureInformation; i++) {
nextStep();
}
}
if (_roadSideUnitDataPath != null) {
_roadSideUnitInformationHandler.parseIntersectionsFile(_roadSideUnitDataPath);
}
_initalized = true;
}
}
@Override
public Position getVehiclePosition(String pVehicleID) {
return getVehiclePosition(_step, pVehicleID);
}
public Position requestVehiclePosition(String pVehicleID) {
return _vehicleDataInformationHandler.getVehiclePositions().get(pVehicleID);
}
@Override
public boolean nextStep() {
_vehicleDataInformationHandler.readNext();
_step = _vehicleDataInformationHandler.getStep() - _futureInformation;
if (_start == -1) {
_start = _vehicleDataInformationHandler.getStep();
}
double newStep = _step + _futureInformation;
if (!_positonsByTimestamp.containsKey(newStep)) {
Map<String, Position> vehiclePositions = new HashMap<>();
_positonsByTimestamp.put(newStep, vehiclePositions);
List<String> allVehicles = new ArrayList<>(_vehicleDataInformationHandler.getVehiclePositions().keySet());
for (String vehicle : allVehicles) {
Position vehiclePosition = requestVehiclePosition(vehicle);
if (vehiclePosition != null) {
vehiclePositions.put(vehicle, vehiclePosition);
}
}
}
return !_vehicleDataInformationHandler.isTerminated();
}
@Override
public List<String> getAllVehicles() {
return getAllVehicles(_step);
}
@Override
public double getStep() {
return _step;
}
@Override
public double getStart() {
return _start;
}
@Override
public void run() {
try {
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
parser.parse(new FileInputStream(_vehicleDataPath), _vehicleDataInformationHandler);
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void setObservedArea(double pStartX, double pStartY, double pEndX, double pEndY) {
_vehicleDataInformationHandler.setObservedArea(pStartX, pStartY, pEndX, pEndY);
_roadSideUnitInformationHandler.setObservedArea(pStartX, pStartY, pEndX, pEndY);
}
@Override
public Position getVehiclePosition(double pStep, String pVehicleID) {
Map<String, Position> map = _positonsByTimestamp.get(pStep);
return map.get(pVehicleID);
}
@Override
public List<String> getAllVehicles(double pStep) {
Map<String, Position> map = _positonsByTimestamp.get(pStep);
return new ArrayList<>(map.keySet());
}
@Override
public double getMaximumAvailablePrediction() {
double max = Collections.max(_positonsByTimestamp.keySet());
return max;
}
@Override
public List<Position> getAllIntersections(boolean pCluster) {
return _roadSideUnitInformationHandler.getIntersections();
}
}
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