Commit 7850d53e authored by Tobias Meuser's avatar Tobias Meuser
Browse files

Fixed bugs in serialization and added manipulation of edges

parent e0b6b9a6
package de.tudarmstadt.maki.simonstrator.api.component.vehicular.api;
public interface EdgeController {
void setObservedArea(double pStartX, double pStartY, double pEndX, double pEndY);
void init();
void setEdgeMaxSpeed(String pEdgeID, double pMaxSpeed);
}
package de.tudarmstadt.maki.simonstrator.api.component.vehicular.api;
import java.util.List;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.api.information.Position;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.RoadNetwork;
public interface SimulationSetupExtractor {
void setObservedArea(double pStartX, double pStartY, double pEndX, double pEndY);
void init();
List<Position> getAllIntersections(boolean pCluster);
double getScenarioWidth();
double getScenarioHeight();
RoadNetwork getRoadNetwork();
}
package de.tudarmstadt.maki.simonstrator.api.component.vehicular.api;
import java.util.List;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.api.information.Position;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.RoadNetworkEdge;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.RoadNetworkRoute;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.exception.NoAdditionalRouteAvailableException;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.exception.NoExitAvailableException;
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);
RoadNetworkRoute getCurrentRoute(String pVehicleID);
double getMaximumAvailablePrediction();
void rerouteVehicle(String pVehicle, RoadNetworkRoute pRoute);
RoadNetworkRoute findNewRoute(String pVehicle)
throws NoAdditionalRouteAvailableException;
RoadNetworkRoute findNewRoute(String pVehicle,
List<RoadNetworkEdge> pEdgesToAvoid, boolean pKeepDestination)
throws NoAdditionalRouteAvailableException,
NoExitAvailableException;
double getStep();
double getStart();
void init();
double getVehicleSpeed(String pVehicleID);
void stopVehicle(String pVehicle);
}
package de.tudarmstadt.maki.simonstrator.api.component.vehicular.api.information;
import java.util.HashMap;
import java.util.Map;
public class LocationUtils {
private static boolean _saveID = false;
private static Map<String, Long> _ids = new HashMap<>();
private static long _currentID = 0;
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.tudarmstadt.maki.simonstrator.api.component.vehicular.api.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.tudarmstadt.maki.simonstrator.api.component.vehicular.api.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.tudarmstadt.maki.simonstrator.api.component.vehicular.api.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.tudarmstadt.maki.simonstrator.api.component.vehicular.api.information;
public class Street {
private String _edgeID;
public Street(String pEdgeID) {
_edgeID = pEdgeID;
}
public String getEdgeID() {
return _edgeID;
}
}
...@@ -21,15 +21,19 @@ ...@@ -21,15 +21,19 @@
package de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork; package de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.api.EdgeController;
public class RoadNetwork { public class RoadNetwork {
private Map<String, RoadNetworkEdge> _roadNetwork; private Map<String, RoadNetworkEdge> _roadNetwork;
public RoadNetwork(SerializableRoadNetwork pRoadNetwork) { public RoadNetwork(SerializableRoadNetwork pRoadNetwork, EdgeController pEdgeController) {
_roadNetwork = new HashMap<>();
for (SerializableRoadNetworkEdge edge : pRoadNetwork.getAvailableEdges()) { for (SerializableRoadNetworkEdge edge : pRoadNetwork.getAvailableEdges()) {
_roadNetwork.put(edge.getEdgeID(), new RoadNetworkEdge(edge)); _roadNetwork.put(edge.getEdgeID(), new RoadNetworkEdge(edge, pEdgeController));
} }
for (SerializableRoadNetworkEdge edge : pRoadNetwork.getAvailableEdges()) { for (SerializableRoadNetworkEdge edge : pRoadNetwork.getAvailableEdges()) {
......
...@@ -24,23 +24,33 @@ import java.util.ArrayList; ...@@ -24,23 +24,33 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.api.EdgeController;
public class RoadNetworkEdge { public class RoadNetworkEdge {
private String _edgeID; private String _edgeID;
private double _angle; private double _angle;
private int _laneAmount; private int _laneAmount;
private double _maxSpeed = 0; private double _originalMaxSpeed = 0;
private List<RoadNetworkEdge> _accessibleEdges = new ArrayList<>(); private List<RoadNetworkEdge> _accessibleEdges = new ArrayList<>();
public RoadNetworkEdge(SerializableRoadNetworkEdge pEdge) { private final EdgeController _edgeController;
public RoadNetworkEdge(SerializableRoadNetworkEdge pEdge, EdgeController pEdgeController) {
_edgeController = pEdgeController;
_edgeID = pEdge.getEdgeID(); _edgeID = pEdge.getEdgeID();
_angle = pEdge.getAngle(); _angle = pEdge.getAngle();
_laneAmount = pEdge.getLaneAmount(); _laneAmount = pEdge.getLaneAmount();
_maxSpeed = pEdge.getMaxSpeed();
_originalMaxSpeed = pEdge.getMaxSpeed();
} }
public RoadNetworkEdge(String pEdgeID, double pAngle) { public RoadNetworkEdge(String pEdgeID, double pAngle, EdgeController pEdgeController) {
_edgeController = pEdgeController;
_edgeID = pEdgeID; _edgeID = pEdgeID;
_angle = pAngle; _angle = pAngle;
...@@ -55,8 +65,8 @@ public class RoadNetworkEdge { ...@@ -55,8 +65,8 @@ public class RoadNetworkEdge {
return _angle; return _angle;
} }
public double getMaxSpeed() { public double getOriginalMaxSpeed() {
return _maxSpeed; return _originalMaxSpeed;
} }
public int getLaneAmount() { public int getLaneAmount() {
...@@ -67,9 +77,9 @@ public class RoadNetworkEdge { ...@@ -67,9 +77,9 @@ public class RoadNetworkEdge {
_laneAmount++; _laneAmount++;
} }
public void setMaxSpeed(double pMaxSpeed) { public void setOriginalMaxSpeed(double pMaxSpeed) {
if (pMaxSpeed > _maxSpeed) { if (pMaxSpeed > _originalMaxSpeed) {
_maxSpeed = pMaxSpeed; _originalMaxSpeed = pMaxSpeed;
} }
} }
...@@ -84,7 +94,7 @@ public class RoadNetworkEdge { ...@@ -84,7 +94,7 @@ public class RoadNetworkEdge {
} }
public String getDescription() { public String getDescription() {
return "id: " + _edgeID + ", lanes: " + _laneAmount + ", speed limit: " + _maxSpeed; return "id: " + _edgeID + ", lanes: " + _laneAmount + ", speed limit: " + _originalMaxSpeed;
} }
@Override @Override
...@@ -95,4 +105,8 @@ public class RoadNetworkEdge { ...@@ -95,4 +105,8 @@ public class RoadNetworkEdge {
public boolean isInternal() { public boolean isInternal() {
return _edgeID.startsWith(":"); return _edgeID.startsWith(":");
} }
public void changeEdgeMaxSpeed(double pMaxSpeed) {
_edgeController.setEdgeMaxSpeed(getEdgeID(), pMaxSpeed);
}
} }
...@@ -93,4 +93,13 @@ public class RoadNetworkRoute { ...@@ -93,4 +93,13 @@ public class RoadNetworkRoute {
} }
return buffer.toString(); return buffer.toString();
} }
public boolean containsEdges(List<RoadNetworkEdge> pBlocked) {
for (RoadNetworkEdge roadNetworkEdge : pBlocked) {
if (containsEdge(roadNetworkEdge)) {
return true;
}
}
return false;
}
} }
...@@ -22,6 +22,7 @@ package de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork; ...@@ -22,6 +22,7 @@ package de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -33,6 +34,7 @@ public class SerializableRoadNetwork implements Serializable { ...@@ -33,6 +34,7 @@ public class SerializableRoadNetwork implements Serializable {
private Map<String, SerializableRoadNetworkEdge> _roadNetwork; private Map<String, SerializableRoadNetworkEdge> _roadNetwork;
public SerializableRoadNetwork(RoadNetwork pRoadNetwork) { public SerializableRoadNetwork(RoadNetwork pRoadNetwork) {
_roadNetwork = new HashMap<>();
for (RoadNetworkEdge edge : pRoadNetwork.getAvailableEdges()) { for (RoadNetworkEdge edge : pRoadNetwork.getAvailableEdges()) {
_roadNetwork.put(edge.getEdgeID(), new SerializableRoadNetworkEdge(edge)); _roadNetwork.put(edge.getEdgeID(), new SerializableRoadNetworkEdge(edge));
} }
......
...@@ -42,7 +42,7 @@ public class SerializableRoadNetworkEdge implements Serializable { ...@@ -42,7 +42,7 @@ public class SerializableRoadNetworkEdge implements Serializable {
_edgeID = pEdge.getEdgeID(); _edgeID = pEdge.getEdgeID();
_angle = pEdge.getAngle(); _angle = pEdge.getAngle();
_laneAmount = pEdge.getLaneAmount(); _laneAmount = pEdge.getLaneAmount();
_maxSpeed = pEdge.getMaxSpeed(); _maxSpeed = pEdge.getOriginalMaxSpeed();
for (RoadNetworkEdge edge : pEdge.getAccessibleEdges()) { for (RoadNetworkEdge edge : pEdge.getAccessibleEdges()) {
_accessibleEdgeIDs.add(edge.getEdgeID()); _accessibleEdgeIDs.add(edge.getEdgeID());
......
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