[clean] Apply formatting and add formatter configuration.
This commit is contained in:
committed by
Mikael CAPELLE
parent
2f936d44ec
commit
730cda6426
@@ -1,17 +1,14 @@
|
||||
<?xml version="1.0"?>
|
||||
<project
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
<parent>
|
||||
<groupId>org.insa.graphs</groupId>
|
||||
<artifactId>be-graphes-all</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
|
||||
<artifactId>be-graphes-model</artifactId>
|
||||
<name>be-graphes-model</name>
|
||||
|
||||
|
||||
</project>
|
||||
|
@@ -7,18 +7,15 @@ import java.util.EnumSet;
|
||||
* <p>
|
||||
* Class containing access restrictions for roads/arcs.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* This class maps transport modes to their restriction and provide interface
|
||||
* based on EnumSet to query restrictions.
|
||||
* This class maps transport modes to their restriction and provide interface based on
|
||||
* EnumSet to query restrictions.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* To each transport is associated at most one restriction per road (no
|
||||
* restriction corresponds to {@link AccessRestriction#UNKNOWN} but a road can
|
||||
* have different restrictions for different modes.
|
||||
* To each transport is associated at most one restriction per road (no restriction
|
||||
* corresponds to {@link AccessRestriction#UNKNOWN} but a road can have different
|
||||
* restrictions for different modes.
|
||||
* </p>
|
||||
*
|
||||
*/
|
||||
public class AccessRestrictions {
|
||||
|
||||
@@ -73,24 +70,20 @@ public class AccessRestrictions {
|
||||
|
||||
/**
|
||||
* {@code EnumSet} containing all possible transport modes.
|
||||
*
|
||||
*
|
||||
*/
|
||||
public static final EnumSet<AccessMode> ALL = EnumSet.allOf(AccessMode.class);
|
||||
|
||||
/**
|
||||
* {@code EnumSet} containing all vehicle transport modes.
|
||||
*
|
||||
*/
|
||||
public static final EnumSet<AccessMode> VEHICLE = EnumSet.range(AccessMode.BICYCLE,
|
||||
AccessMode.PUBLIC_TRANSPORT);
|
||||
public static final EnumSet<AccessMode> VEHICLE =
|
||||
EnumSet.range(AccessMode.BICYCLE, AccessMode.PUBLIC_TRANSPORT);
|
||||
|
||||
/**
|
||||
* {@code EnumSet} containing all motorized vehicle transport modes.
|
||||
*
|
||||
*/
|
||||
public static final EnumSet<AccessMode> MOTOR_VEHICLE = EnumSet
|
||||
.range(AccessMode.SMALL_MOTORCYCLE, AccessMode.PUBLIC_TRANSPORT);
|
||||
public static final EnumSet<AccessMode> MOTOR_VEHICLE =
|
||||
EnumSet.range(AccessMode.SMALL_MOTORCYCLE, AccessMode.PUBLIC_TRANSPORT);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -103,53 +96,52 @@ public class AccessRestrictions {
|
||||
public enum AccessRestriction {
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
*/
|
||||
ALLOWED,
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
*/
|
||||
FORBIDDEN,
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
*/
|
||||
PRIVATE,
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
*/
|
||||
DESTINATION,
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
*/
|
||||
DELIVERY,
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
*/
|
||||
CUSTOMERS,
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
*/
|
||||
FORESTRY,
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
*/
|
||||
UNKNOWN;
|
||||
|
||||
/**
|
||||
* {@code EnumSet} corresponding to restrictions that are not totally private.
|
||||
*
|
||||
*/
|
||||
public static final EnumSet<AccessRestriction> ALLOWED_FOR_SOMETHING = EnumSet.of(
|
||||
AccessRestriction.ALLOWED, AccessRestriction.DESTINATION,
|
||||
AccessRestriction.DESTINATION, AccessRestriction.DELIVERY,
|
||||
AccessRestriction.CUSTOMERS, AccessRestriction.FORESTRY);
|
||||
public static final EnumSet<AccessRestriction> ALLOWED_FOR_SOMETHING =
|
||||
EnumSet.of(AccessRestriction.ALLOWED, AccessRestriction.DESTINATION,
|
||||
AccessRestriction.DESTINATION, AccessRestriction.DELIVERY,
|
||||
AccessRestriction.CUSTOMERS, AccessRestriction.FORESTRY);
|
||||
|
||||
}
|
||||
|
||||
@@ -161,16 +153,15 @@ public class AccessRestrictions {
|
||||
*/
|
||||
public AccessRestrictions() {
|
||||
this.restrictions = new EnumMap<>(AccessMode.class);
|
||||
for (AccessMode mode: AccessMode.values()) {
|
||||
for (AccessMode mode : AccessMode.values()) {
|
||||
this.restrictions.put(mode, AccessRestriction.UNKNOWN);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new AccessRestrictions instances with the given restrictions.
|
||||
*
|
||||
* @param restrictions Map of restrictions for this instance of
|
||||
* AccessRestrictions.
|
||||
*
|
||||
* @param restrictions Map of restrictions for this instance of AccessRestrictions.
|
||||
*/
|
||||
public AccessRestrictions(EnumMap<AccessMode, AccessRestriction> restrictions) {
|
||||
this.restrictions = restrictions;
|
||||
@@ -178,9 +169,8 @@ public class AccessRestrictions {
|
||||
|
||||
/**
|
||||
* Retrieve the restriction corresponding to the given mode.
|
||||
*
|
||||
*
|
||||
* @param mode Mode for which the restriction should be retrieved.
|
||||
*
|
||||
* @return Restriction for the given mode.
|
||||
*/
|
||||
public AccessRestriction getRestrictionFor(AccessMode mode) {
|
||||
@@ -190,24 +180,22 @@ public class AccessRestrictions {
|
||||
/**
|
||||
* Check if the restriction associated with the given mode is one of the given
|
||||
* restrictions.
|
||||
*
|
||||
* @param mode Mode for which to check the restrictions.
|
||||
*
|
||||
* @param mode Mode for which to check the restrictions.
|
||||
* @param restrictions List of queried restrictions for the mode.
|
||||
*
|
||||
* @return {@code true} if the restriction of the given mode is one of the given
|
||||
* restrictions.
|
||||
*/
|
||||
public boolean isAllowedForAny(AccessMode mode, EnumSet<AccessRestriction> restrictions) {
|
||||
public boolean isAllowedForAny(AccessMode mode,
|
||||
EnumSet<AccessRestriction> restrictions) {
|
||||
return restrictions.contains(getRestrictionFor(mode));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the restriction for the given mode corresponds to the given
|
||||
* restriction.
|
||||
*
|
||||
* @param mode Mode for which the restriction should be checked.
|
||||
* Check if the restriction for the given mode corresponds to the given restriction.
|
||||
*
|
||||
* @param mode Mode for which the restriction should be checked.
|
||||
* @param restriction Restriction to check against.
|
||||
*
|
||||
* @return {@code true} if the restriction of the given mode corresponds to the
|
||||
* given restriction.
|
||||
*/
|
||||
@@ -218,10 +206,9 @@ public class AccessRestrictions {
|
||||
/**
|
||||
* Check if the restriction associated to each given mode is one of the
|
||||
* restrictions. The restriction may not be the same for all modes.
|
||||
*
|
||||
* @param modes Modes for which restrictions should be checked.
|
||||
*
|
||||
* @param modes Modes for which restrictions should be checked.
|
||||
* @param restrictions Set of wanted restrictions for the modes.
|
||||
*
|
||||
* @return {@code true} if all the given modes are allowed for any of the given
|
||||
* restrictions.
|
||||
*/
|
||||
|
@@ -4,18 +4,16 @@ import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Interface representing an arc in the graph. {@code Arc} is an interface and
|
||||
* not a class to allow us to represent two-ways roads in a memory efficient
|
||||
* manner (without having to duplicate attributes).
|
||||
* Interface representing an arc in the graph. {@code Arc} is an interface and not a
|
||||
* class to allow us to represent two-ways roads in a memory efficient manner (without
|
||||
* having to duplicate attributes).
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* Arc should never be created manually but always using the
|
||||
* {@link Node#linkNodes(Node, Node, float, RoadInformation, java.util.ArrayList)}
|
||||
* method to ensure proper instantiation of the {@link ArcForward} and
|
||||
* {@link ArcBackward} classes.
|
||||
* </p>
|
||||
*
|
||||
*/
|
||||
public abstract class Arc {
|
||||
|
||||
@@ -36,9 +34,8 @@ public abstract class Arc {
|
||||
|
||||
/**
|
||||
* Compute the time required to travel this arc if moving at the given speed.
|
||||
*
|
||||
*
|
||||
* @param speed Speed to compute the travel time.
|
||||
*
|
||||
* @return Time (in seconds) required to travel this arc at the given speed (in
|
||||
* kilometers-per-hour).
|
||||
*/
|
||||
@@ -49,9 +46,8 @@ public abstract class Arc {
|
||||
/**
|
||||
* Compute and return the minimum time required to travel this arc, or the time
|
||||
* required to travel this arc at the maximum speed allowed.
|
||||
*
|
||||
*
|
||||
* @return Minimum time required to travel this arc, in seconds.
|
||||
*
|
||||
* @see Arc#getTravelTime(double)
|
||||
*/
|
||||
public double getMinimumTravelTime() {
|
||||
|
@@ -5,10 +5,8 @@ import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Implementation of Arc that represents a "backward" arc in a graph, i.e., an
|
||||
* arc that is the reverse of another one. This arc only holds a reference to
|
||||
* the original arc.
|
||||
*
|
||||
* Implementation of Arc that represents a "backward" arc in a graph, i.e., an arc that
|
||||
* is the reverse of another one. This arc only holds a reference to the original arc.
|
||||
*/
|
||||
class ArcBackward extends Arc {
|
||||
|
||||
@@ -16,9 +14,8 @@ class ArcBackward extends Arc {
|
||||
private final Arc originalArc;
|
||||
|
||||
/**
|
||||
* Create a new backward arc which corresponds to the reverse arc of the given
|
||||
* arc.
|
||||
*
|
||||
* Create a new backward arc which corresponds to the reverse arc of the given arc.
|
||||
*
|
||||
* @param originalArc Original forwarc arc corresponding to this backward arc.
|
||||
*/
|
||||
protected ArcBackward(Arc originalArc) {
|
||||
|
@@ -4,9 +4,8 @@ import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Implementation of Arc that represents a "forward" arc in a graph, this is the
|
||||
* arc implementation that stores data relative to the arc.
|
||||
*
|
||||
* Implementation of Arc that represents a "forward" arc in a graph, this is the arc
|
||||
* implementation that stores data relative to the arc.
|
||||
*/
|
||||
class ArcForward extends Arc {
|
||||
|
||||
@@ -24,15 +23,15 @@ class ArcForward extends Arc {
|
||||
|
||||
/**
|
||||
* Create a new ArcForward with the given attributes.
|
||||
*
|
||||
*
|
||||
* @param origin Origin of this arc.
|
||||
* @param dest Destination of this arc.
|
||||
* @param length Length of this arc (in meters).
|
||||
* @param roadInformation Road information for this arc.
|
||||
* @param points Points representing this arc.
|
||||
*/
|
||||
protected ArcForward(Node origin, Node dest, float length, RoadInformation roadInformation,
|
||||
List<Point> points) {
|
||||
protected ArcForward(Node origin, Node dest, float length,
|
||||
RoadInformation roadInformation, List<Point> points) {
|
||||
this.origin = origin;
|
||||
this.destination = dest;
|
||||
this.length = length;
|
||||
|
@@ -8,12 +8,10 @@ import java.util.List;
|
||||
* <p>
|
||||
* Main graph class.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* This class acts as a object-oriented <b>adjacency list</b> for a graph, i.e.,
|
||||
* it holds a list of nodes and each node holds a list of its successors.
|
||||
* This class acts as a object-oriented <b>adjacency list</b> for a graph, i.e., it
|
||||
* holds a list of nodes and each node holds a list of its successors.
|
||||
* </p>
|
||||
*
|
||||
*/
|
||||
public final class Graph {
|
||||
|
||||
@@ -31,13 +29,14 @@ public final class Graph {
|
||||
|
||||
/**
|
||||
* Create a new graph with the given ID, name, nodes and information.
|
||||
*
|
||||
* @param mapId ID of the map corresponding to this graph.
|
||||
* @param mapName Name of the map corresponding to this graph.
|
||||
* @param nodes List of nodes for this graph.
|
||||
*
|
||||
* @param mapId ID of the map corresponding to this graph.
|
||||
* @param mapName Name of the map corresponding to this graph.
|
||||
* @param nodes List of nodes for this graph.
|
||||
* @param graphStatistics Information for this graph.
|
||||
*/
|
||||
public Graph(String mapId, String mapName, List<Node> nodes, GraphStatistics graphStatistics) {
|
||||
public Graph(String mapId, String mapName, List<Node> nodes,
|
||||
GraphStatistics graphStatistics) {
|
||||
this.mapId = mapId;
|
||||
this.mapName = mapName;
|
||||
this.nodes = Collections.unmodifiableList(nodes);
|
||||
@@ -52,12 +51,9 @@ public final class Graph {
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch the node with the given ID.
|
||||
*
|
||||
* Complexity: O(1).
|
||||
*
|
||||
* Fetch the node with the given ID. Complexity: O(1).
|
||||
*
|
||||
* @param id ID of the node to fetch.
|
||||
*
|
||||
* @return Node with the given ID.
|
||||
*/
|
||||
public Node get(int id) {
|
||||
@@ -73,7 +69,6 @@ public final class Graph {
|
||||
|
||||
/**
|
||||
* @return List of nodes in this graph (unmodifiable).
|
||||
*
|
||||
* @see Collections#unmodifiableList(List)
|
||||
*/
|
||||
public List<Node> getNodes() {
|
||||
@@ -99,16 +94,17 @@ public final class Graph {
|
||||
*/
|
||||
public Graph transpose() {
|
||||
final ArrayList<Node> trNodes = new ArrayList<>(nodes.size());
|
||||
for (Node node: nodes) {
|
||||
for (Node node : nodes) {
|
||||
trNodes.add(new Node(node.getId(), node.getPoint()));
|
||||
}
|
||||
for (Node node: nodes) {
|
||||
for (Node node : nodes) {
|
||||
final Node orig = trNodes.get(node.getId());
|
||||
for (Arc arc: node.getSuccessors()) {
|
||||
for (Arc arc : node.getSuccessors()) {
|
||||
if (arc.getRoadInformation().isOneWay()) {
|
||||
final Node dest = trNodes.get(arc.getDestination().getId());
|
||||
dest.addSuccessor(new ArcBackward(new ArcForward(orig, dest, arc.getLength(),
|
||||
arc.getRoadInformation(), arc.getPoints())));
|
||||
dest.addSuccessor(
|
||||
new ArcBackward(new ArcForward(orig, dest, arc.getLength(),
|
||||
arc.getRoadInformation(), arc.getPoints())));
|
||||
}
|
||||
else if (arc instanceof ArcForward) {
|
||||
final Node dest = trNodes.get(arc.getDestination().getId());
|
||||
@@ -124,8 +120,8 @@ public final class Graph {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%s[id=%s, name=%s, #nodes=%d]", getClass().getCanonicalName(),
|
||||
getMapId(), getMapName(), size());
|
||||
return String.format("%s[id=%s, name=%s, #nodes=%d]",
|
||||
getClass().getCanonicalName(), getMapId(), getMapName(), size());
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -2,30 +2,24 @@ package org.insa.graphs.model;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Utility class that stores some statistics of graphs that are not easy to
|
||||
* access.
|
||||
* Utility class that stores some statistics of graphs that are not easy to access.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* This class is used to provide constant ({@code O(1)}) access to information
|
||||
* in graph that do not change, and that usually require linear complexity to
|
||||
* compute.
|
||||
* This class is used to provide constant ({@code O(1)}) access to information in graph
|
||||
* that do not change, and that usually require linear complexity to compute.
|
||||
* </p>
|
||||
*
|
||||
*/
|
||||
public class GraphStatistics {
|
||||
|
||||
/**
|
||||
* Special value used to indicate that the graph has no maximum speed limit
|
||||
* (some roads are not limited).
|
||||
*
|
||||
* Special value used to indicate that the graph has no maximum speed limit (some
|
||||
* roads are not limited).
|
||||
*/
|
||||
public static final int NO_MAXIMUM_SPEED = -1;
|
||||
|
||||
/**
|
||||
* Class representing a bounding box for a graph (a rectangle that contains all
|
||||
* nodes in the graph).
|
||||
*
|
||||
*/
|
||||
public static class BoundingBox {
|
||||
|
||||
@@ -34,7 +28,7 @@ public class GraphStatistics {
|
||||
/**
|
||||
* Create a new BoundingBox represented by the given top-left and bottom-right
|
||||
* points.
|
||||
*
|
||||
*
|
||||
* @param topLeft Top left corner of the bounding box.
|
||||
* @param bottomRight Bottom right corner of the bounding box.
|
||||
*/
|
||||
@@ -60,17 +54,17 @@ public class GraphStatistics {
|
||||
/**
|
||||
* Create a new bounding box by extending the current one according to the given
|
||||
* value for each side.
|
||||
*
|
||||
*
|
||||
* @param left Extra size to add to the left of the box.
|
||||
* @param top Extra size to add to the top of the box.
|
||||
* @param right Extra size to add to the right of the box.
|
||||
* @param bottom Extra size to add to the bottom of the box.
|
||||
*
|
||||
* @return New bounding box corresponding to an extension of the current one.
|
||||
*/
|
||||
public BoundingBox extend(float left, float top, float right, float bottom) {
|
||||
return new BoundingBox(
|
||||
new Point(this.topLeft.getLongitude() - left, this.topLeft.getLatitude() + top),
|
||||
new Point(this.topLeft.getLongitude() - left,
|
||||
this.topLeft.getLatitude() + top),
|
||||
new Point(this.bottomRight.getLongitude() + right,
|
||||
this.bottomRight.getLatitude() - bottom));
|
||||
}
|
||||
@@ -78,9 +72,8 @@ public class GraphStatistics {
|
||||
/**
|
||||
* Create a new bounding box by extending the current one according by the given
|
||||
* value on each side.
|
||||
*
|
||||
*
|
||||
* @param size Extra size to add to each side of this box.
|
||||
*
|
||||
* @return New bounding box corresponding to an extension of the current one.
|
||||
*/
|
||||
public BoundingBox extend(float size) {
|
||||
@@ -89,7 +82,6 @@ public class GraphStatistics {
|
||||
|
||||
/**
|
||||
* @param point Point to check
|
||||
*
|
||||
* @return true if this box contains the given point.
|
||||
*/
|
||||
public boolean contains(Point point) {
|
||||
@@ -101,7 +93,6 @@ public class GraphStatistics {
|
||||
|
||||
/**
|
||||
* @param other Box to intersect.
|
||||
*
|
||||
* @return true if this box contains the given box.
|
||||
*/
|
||||
public boolean contains(BoundingBox other) {
|
||||
@@ -110,8 +101,8 @@ public class GraphStatistics {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "BoundingBox(topLeft=" + this.topLeft + ", bottomRight=" + this.bottomRight
|
||||
+ ")";
|
||||
return "BoundingBox(topLeft=" + this.topLeft + ", bottomRight="
|
||||
+ this.bottomRight + ")";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -130,13 +121,13 @@ public class GraphStatistics {
|
||||
|
||||
/**
|
||||
* Create a new GraphStatistics instance with the given value.
|
||||
*
|
||||
*
|
||||
* @param boundingBox Bounding-box for the graph.
|
||||
* @param nbRoadOneWay Number of one-way roads in the graph.
|
||||
* @param nbRoadTwoWays Number of two-ways roads in the graph.
|
||||
* @param maximumSpeed Maximum speed of any road of the graph. You can use
|
||||
* {@link #NO_MAXIMUM_SPEED} to indicate that the graph has no maximum
|
||||
* speed limit.
|
||||
* {@link #NO_MAXIMUM_SPEED} to indicate that the graph has no maximum speed
|
||||
* limit.
|
||||
* @param maximumLength Maximum length of any arc of the graph.
|
||||
*/
|
||||
public GraphStatistics(BoundingBox boundingBox, int nbRoadOneWay, int nbRoadTwoWays,
|
||||
@@ -171,7 +162,6 @@ public class GraphStatistics {
|
||||
|
||||
/**
|
||||
* @return Number of arcs in this graph.
|
||||
*
|
||||
* @see #getOneWayRoadCount()
|
||||
* @see #getTwoWaysRoadCount()
|
||||
*/
|
||||
@@ -187,8 +177,8 @@ public class GraphStatistics {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Maximum speed of any arc in the graph, or {@link #NO_MAXIMUM_SPEED}
|
||||
* if some roads have no speed limitation.
|
||||
* @return Maximum speed of any arc in the graph, or {@link #NO_MAXIMUM_SPEED} if
|
||||
* some roads have no speed limitation.
|
||||
*/
|
||||
public int getMaximumSpeed() {
|
||||
return this.maximumSpeed;
|
||||
|
@@ -8,16 +8,13 @@ import java.util.List;
|
||||
* <p>
|
||||
* Class representing a Node in a {@link Graph}.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* This class holds information regarding nodes in the graph together with the
|
||||
* successors associated to the nodes.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* Nodes are comparable based on their ID.
|
||||
* </p>
|
||||
*
|
||||
*/
|
||||
public final class Node implements Comparable<Node> {
|
||||
|
||||
@@ -26,19 +23,17 @@ public final class Node implements Comparable<Node> {
|
||||
* Link the two given nodes with one or two arcs (depending on roadInformation),
|
||||
* with the given attributes.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* If {@code roadInformation.isOneWay()} is {@code true}, only a forward arc is
|
||||
* created (origin to destination) and added to origin. Otherwise, a
|
||||
* corresponding backward arc is created and add to destination.
|
||||
* created (origin to destination) and added to origin. Otherwise, a corresponding
|
||||
* backward arc is created and add to destination.
|
||||
* </p>
|
||||
*
|
||||
* @param origin Origin of the arc.
|
||||
* @param destination Destination of the arc.
|
||||
* @param length Length of the arc.
|
||||
*
|
||||
* @param origin Origin of the arc.
|
||||
* @param destination Destination of the arc.
|
||||
* @param length Length of the arc.
|
||||
* @param roadInformation Information corresponding to the arc.
|
||||
* @param points Points for the arc.
|
||||
*
|
||||
* @param points Points for the arc.
|
||||
* @return The newly created forward arc (origin to destination).
|
||||
*/
|
||||
public static Arc linkNodes(Node origin, Node destination, float length,
|
||||
@@ -51,12 +46,14 @@ public final class Node implements Comparable<Node> {
|
||||
else {
|
||||
final Arc d2o;
|
||||
if (origin.getId() < destination.getId()) {
|
||||
arc = new ArcForward(origin, destination, length, roadInformation, points);
|
||||
arc = new ArcForward(origin, destination, length, roadInformation,
|
||||
points);
|
||||
d2o = new ArcBackward(arc);
|
||||
}
|
||||
else {
|
||||
Collections.reverse(points);
|
||||
d2o = new ArcForward(destination, origin, length, roadInformation, points);
|
||||
d2o = new ArcForward(destination, origin, length, roadInformation,
|
||||
points);
|
||||
arc = new ArcBackward(d2o);
|
||||
}
|
||||
origin.addSuccessor(arc);
|
||||
@@ -77,8 +74,8 @@ public final class Node implements Comparable<Node> {
|
||||
/**
|
||||
* Create a new Node with the given ID corresponding to the given Point with an
|
||||
* empty list of successors.
|
||||
*
|
||||
* @param id ID of the node.
|
||||
*
|
||||
* @param id ID of the node.
|
||||
* @param point Position of the node.
|
||||
*/
|
||||
public Node(int id, Point point) {
|
||||
@@ -89,7 +86,7 @@ public final class Node implements Comparable<Node> {
|
||||
|
||||
/**
|
||||
* Add a successor to this node.
|
||||
*
|
||||
*
|
||||
* @param arc Arc to the successor.
|
||||
*/
|
||||
protected void addSuccessor(Arc arc) {
|
||||
@@ -119,7 +116,6 @@ public final class Node implements Comparable<Node> {
|
||||
|
||||
/**
|
||||
* @return List of successors of this node (unmodifiable list).
|
||||
*
|
||||
* @see Collections#unmodifiableList(List)
|
||||
*/
|
||||
public List<Arc> getSuccessors() {
|
||||
@@ -146,9 +142,8 @@ public final class Node implements Comparable<Node> {
|
||||
|
||||
/**
|
||||
* Compare the ID of this node with the ID of the given node.
|
||||
*
|
||||
*
|
||||
* @param other Node to compare this node with.
|
||||
*
|
||||
* @see java.lang.Comparable#compareTo(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
|
@@ -2,7 +2,6 @@ package org.insa.graphs.model;
|
||||
|
||||
/**
|
||||
* Class representing a point (position) on Earth.
|
||||
*
|
||||
*/
|
||||
public final class Point {
|
||||
|
||||
@@ -13,10 +12,9 @@ public final class Point {
|
||||
|
||||
/**
|
||||
* Compute the distance in meters between the two given points.
|
||||
*
|
||||
*
|
||||
* @param p1 First point.
|
||||
* @param p2 second point.
|
||||
*
|
||||
* @return Distance between the two given points (in meters).
|
||||
*/
|
||||
public static double distance(Point p1, Point p2) {
|
||||
@@ -24,7 +22,8 @@ public final class Point {
|
||||
* Math.sin(Math.toRadians(p2.getLatitude()));
|
||||
double cosLat = Math.cos(Math.toRadians(p1.getLatitude()))
|
||||
* Math.cos(Math.toRadians(p2.getLatitude()));
|
||||
double cosLong = Math.cos(Math.toRadians(p2.getLongitude() - p1.getLongitude()));
|
||||
double cosLong =
|
||||
Math.cos(Math.toRadians(p2.getLongitude() - p1.getLongitude()));
|
||||
return EARTH_RADIUS * Math.acos(sinLat + cosLat * cosLong);
|
||||
}
|
||||
|
||||
@@ -33,7 +32,7 @@ public final class Point {
|
||||
|
||||
/**
|
||||
* Create a new point corresponding to the given (longitude, latitude) position.
|
||||
*
|
||||
*
|
||||
* @param longitude Longitude of the point (in degrees).
|
||||
* @param latitude Latitude of the point (in degrees).
|
||||
*/
|
||||
@@ -58,9 +57,8 @@ public final class Point {
|
||||
|
||||
/**
|
||||
* Compute the distance from this point to the given point
|
||||
*
|
||||
*
|
||||
* @param target Target point to compute distance to.
|
||||
*
|
||||
* @return Distance between this point and the target point, in meters.
|
||||
*/
|
||||
public double distanceTo(Point target) {
|
||||
|
@@ -4,41 +4,22 @@ package org.insa.graphs.model;
|
||||
* <p>
|
||||
* Class containing information for road that may be shared by multiple arcs.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* Sharing information between arcs reduces memory footprints of the program (a
|
||||
* long road is often split into multiple arcs at each intersection).
|
||||
* Sharing information between arcs reduces memory footprints of the program (a long
|
||||
* road is often split into multiple arcs at each intersection).
|
||||
* </p>
|
||||
*
|
||||
*/
|
||||
public class RoadInformation {
|
||||
|
||||
/**
|
||||
* Enumeration for road types.
|
||||
*
|
||||
*
|
||||
* @see <a href=
|
||||
* "https://wiki.openstreetmap.org/wiki/Key:highway#Values">OpenStreetMap
|
||||
* reference for road types.</a>
|
||||
*/
|
||||
public enum RoadType {
|
||||
MOTORWAY,
|
||||
TRUNK,
|
||||
PRIMARY,
|
||||
SECONDARY,
|
||||
MOTORWAY_LINK,
|
||||
TRUNK_LINK,
|
||||
PRIMARY_LINK,
|
||||
SECONDARY_LINK,
|
||||
TERTIARY,
|
||||
TRACK,
|
||||
RESIDENTIAL,
|
||||
UNCLASSIFIED,
|
||||
LIVING_STREET,
|
||||
SERVICE,
|
||||
ROUNDABOUT,
|
||||
PEDESTRIAN,
|
||||
CYCLEWAY,
|
||||
COASTLINE
|
||||
MOTORWAY, TRUNK, PRIMARY, SECONDARY, MOTORWAY_LINK, TRUNK_LINK, PRIMARY_LINK, SECONDARY_LINK, TERTIARY, TRACK, RESIDENTIAL, UNCLASSIFIED, LIVING_STREET, SERVICE, ROUNDABOUT, PEDESTRIAN, CYCLEWAY, COASTLINE
|
||||
}
|
||||
|
||||
// Type of the road (see above).
|
||||
@@ -58,16 +39,15 @@ public class RoadInformation {
|
||||
|
||||
/**
|
||||
* Create a new RoadInformation instance containing the given parameters.
|
||||
*
|
||||
*
|
||||
* @param roadType Type of the road (see {@link RoadType}).
|
||||
* @param access Access restrictions for the road (see
|
||||
* {@link AccessRestrictions}).
|
||||
* @param access Access restrictions for the road (see {@link AccessRestrictions}).
|
||||
* @param isOneWay true if this road is a one way road, false otherwise.
|
||||
* @param maxSpeed Maximum speed for the road (in kilometers-per-hour).
|
||||
* @param name Name of the road.
|
||||
*/
|
||||
public RoadInformation(RoadType roadType, AccessRestrictions access, boolean isOneWay,
|
||||
int maxSpeed, String name) {
|
||||
public RoadInformation(RoadType roadType, AccessRestrictions access,
|
||||
boolean isOneWay, int maxSpeed, String name) {
|
||||
this.type = roadType;
|
||||
this.access = access;
|
||||
this.oneway = isOneWay;
|
||||
@@ -119,8 +99,8 @@ public class RoadInformation {
|
||||
if (getType() == RoadType.MOTORWAY) {
|
||||
typeAsString = "highway";
|
||||
}
|
||||
return typeAsString + " : " + getName() + " " + (isOneWay() ? " (oneway) " : "") + maxSpeed
|
||||
+ " km/h (max.)";
|
||||
return typeAsString + " : " + getName() + " " + (isOneWay() ? " (oneway) " : "")
|
||||
+ maxSpeed + " km/h (max.)";
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -5,12 +5,11 @@ import java.io.IOException;
|
||||
/**
|
||||
* Exception thrown when a format-error is detected when reading a graph (e.g.,
|
||||
* non-matching check bytes).
|
||||
*
|
||||
*/
|
||||
public class BadFormatException extends IOException {
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1270945933549613579L;
|
||||
|
||||
@@ -23,7 +22,7 @@ public class BadFormatException extends IOException {
|
||||
|
||||
/**
|
||||
* Create a new format exception with the given message.
|
||||
*
|
||||
*
|
||||
* @param message Message for the exception.
|
||||
*/
|
||||
public BadFormatException(String message) {
|
||||
|
@@ -1,14 +1,12 @@
|
||||
package org.insa.graphs.model.io;
|
||||
|
||||
/**
|
||||
* Exception thrown when there is a mismatch between expected and actual magic
|
||||
* number.
|
||||
*
|
||||
* Exception thrown when there is a mismatch between expected and actual magic number.
|
||||
*/
|
||||
public class BadMagicNumberException extends BadFormatException {
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = -2176603967548838864L;
|
||||
|
||||
@@ -18,13 +16,13 @@ public class BadMagicNumberException extends BadFormatException {
|
||||
/**
|
||||
* Create a new BadMagicNumberException with the given expected and actual magic
|
||||
* number.
|
||||
*
|
||||
*
|
||||
* @param actualNumber Actual magic number (read from a file).
|
||||
* @param expectedNumber Expected magic number.
|
||||
*/
|
||||
public BadMagicNumberException(int actualNumber, int expectedNumber) {
|
||||
super(String.format("Magic number mismatch, expected %#X, got %#X.", expectedNumber,
|
||||
actualNumber));
|
||||
super(String.format("Magic number mismatch, expected %#X, got %#X.",
|
||||
expectedNumber, actualNumber));
|
||||
this.actualNumber = actualNumber;
|
||||
this.expectedNumber = expectedNumber;
|
||||
}
|
||||
|
@@ -1,14 +1,12 @@
|
||||
package org.insa.graphs.model.io;
|
||||
|
||||
/**
|
||||
* Exception thrown when the version of the file is not at least the expected
|
||||
* one.
|
||||
*
|
||||
* Exception thrown when the version of the file is not at least the expected one.
|
||||
*/
|
||||
public class BadVersionException extends BadFormatException {
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 7776317018302386042L;
|
||||
|
||||
@@ -16,7 +14,6 @@ public class BadVersionException extends BadFormatException {
|
||||
private int actualVersion, expectedVersion;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param actualVersion Actual version of the file.
|
||||
* @param expectedVersion Expected version of the file.
|
||||
*/
|
||||
|
@@ -20,7 +20,6 @@ import org.insa.graphs.model.RoadInformation.RoadType;
|
||||
|
||||
/**
|
||||
* Implementation of {@link GraphReader} to read graph in binary format.
|
||||
*
|
||||
*/
|
||||
public class BinaryGraphReader extends BinaryReader implements GraphReader {
|
||||
|
||||
@@ -36,9 +35,8 @@ public class BinaryGraphReader extends BinaryReader implements GraphReader {
|
||||
|
||||
/**
|
||||
* Parse the given long value into a new instance of AccessRestrictions.
|
||||
*
|
||||
*
|
||||
* @param access The value to parse.
|
||||
*
|
||||
* @return New instance of access restrictions parsed from the given value.
|
||||
*/
|
||||
protected static AccessRestrictions toAccessInformation(final long access) {
|
||||
@@ -50,22 +48,25 @@ public class BinaryGraphReader extends BinaryReader implements GraphReader {
|
||||
// the order correspond to the 4 bits value (i.e. FORBIDDEN is 0 or PRIVATE is
|
||||
// 2) - UKNOWN is not included because value above 6 (FORESTRY) are all
|
||||
// considered unknown.
|
||||
final AccessRestriction[] allRestrictions = new AccessRestriction[] {
|
||||
AccessRestriction.FORBIDDEN, AccessRestriction.ALLOWED, AccessRestriction.PRIVATE,
|
||||
AccessRestriction.DESTINATION, AccessRestriction.DELIVERY,
|
||||
AccessRestriction.CUSTOMERS, AccessRestriction.FORESTRY };
|
||||
final AccessRestriction[] allRestrictions =
|
||||
new AccessRestriction[] { AccessRestriction.FORBIDDEN,
|
||||
AccessRestriction.ALLOWED, AccessRestriction.PRIVATE,
|
||||
AccessRestriction.DESTINATION, AccessRestriction.DELIVERY,
|
||||
AccessRestriction.CUSTOMERS, AccessRestriction.FORESTRY };
|
||||
|
||||
// The order of values inside this array is VERY IMPORTANT: The order is such
|
||||
// that each 4-bits group of the long value is processed in the correct order,
|
||||
// i.e. FOOT is processed first (4 lowest bits), and so on.
|
||||
final AccessMode[] allModes = new AccessMode[] { AccessMode.FOOT, null, AccessMode.BICYCLE,
|
||||
AccessMode.SMALL_MOTORCYCLE, AccessMode.AGRICULTURAL, AccessMode.MOTORCYCLE,
|
||||
AccessMode.MOTORCAR, AccessMode.HEAVY_GOODS, null, AccessMode.PUBLIC_TRANSPORT };
|
||||
final AccessMode[] allModes = new AccessMode[] { AccessMode.FOOT, null,
|
||||
AccessMode.BICYCLE, AccessMode.SMALL_MOTORCYCLE,
|
||||
AccessMode.AGRICULTURAL, AccessMode.MOTORCYCLE, AccessMode.MOTORCAR,
|
||||
AccessMode.HEAVY_GOODS, null, AccessMode.PUBLIC_TRANSPORT };
|
||||
|
||||
// fill maps...
|
||||
final EnumMap<AccessMode, AccessRestriction> restrictions = new EnumMap<>(AccessMode.class);
|
||||
final EnumMap<AccessMode, AccessRestriction> restrictions =
|
||||
new EnumMap<>(AccessMode.class);
|
||||
long copyAccess = access;
|
||||
for (AccessMode mode: allModes) {
|
||||
for (AccessMode mode : allModes) {
|
||||
if (mode == null) {
|
||||
continue; // filling cells
|
||||
}
|
||||
@@ -84,58 +85,56 @@ public class BinaryGraphReader extends BinaryReader implements GraphReader {
|
||||
|
||||
/**
|
||||
* Convert a character to its corresponding road type.
|
||||
*
|
||||
*
|
||||
* @param ch Character to convert.
|
||||
*
|
||||
* @return Road type corresponding to the given character.
|
||||
*
|
||||
*/
|
||||
protected static RoadType toRoadType(char ch) {
|
||||
switch (ch) {
|
||||
case 'a':
|
||||
return RoadType.MOTORWAY;
|
||||
case 'b':
|
||||
return RoadType.TRUNK;
|
||||
case 'c':
|
||||
return RoadType.PRIMARY;
|
||||
case 'd':
|
||||
return RoadType.SECONDARY;
|
||||
case 'e':
|
||||
return RoadType.MOTORWAY_LINK;
|
||||
case 'f':
|
||||
return RoadType.TRUNK_LINK;
|
||||
case 'g':
|
||||
return RoadType.PRIMARY_LINK;
|
||||
case 'h':
|
||||
return RoadType.SECONDARY_LINK;
|
||||
case 'i':
|
||||
return RoadType.TERTIARY;
|
||||
case 'j':
|
||||
return RoadType.RESIDENTIAL;
|
||||
case 'k':
|
||||
case 'l':
|
||||
return RoadType.UNCLASSIFIED;
|
||||
case 'm':
|
||||
return RoadType.LIVING_STREET;
|
||||
case 'n':
|
||||
return RoadType.SERVICE;
|
||||
case 'o':
|
||||
return RoadType.ROUNDABOUT;
|
||||
case 'p':
|
||||
return RoadType.PEDESTRIAN;
|
||||
case 'r':
|
||||
return RoadType.CYCLEWAY;
|
||||
case 's':
|
||||
return RoadType.TRACK;
|
||||
case 'z':
|
||||
return RoadType.COASTLINE;
|
||||
case 'a':
|
||||
return RoadType.MOTORWAY;
|
||||
case 'b':
|
||||
return RoadType.TRUNK;
|
||||
case 'c':
|
||||
return RoadType.PRIMARY;
|
||||
case 'd':
|
||||
return RoadType.SECONDARY;
|
||||
case 'e':
|
||||
return RoadType.MOTORWAY_LINK;
|
||||
case 'f':
|
||||
return RoadType.TRUNK_LINK;
|
||||
case 'g':
|
||||
return RoadType.PRIMARY_LINK;
|
||||
case 'h':
|
||||
return RoadType.SECONDARY_LINK;
|
||||
case 'i':
|
||||
return RoadType.TERTIARY;
|
||||
case 'j':
|
||||
return RoadType.RESIDENTIAL;
|
||||
case 'k':
|
||||
case 'l':
|
||||
return RoadType.UNCLASSIFIED;
|
||||
case 'm':
|
||||
return RoadType.LIVING_STREET;
|
||||
case 'n':
|
||||
return RoadType.SERVICE;
|
||||
case 'o':
|
||||
return RoadType.ROUNDABOUT;
|
||||
case 'p':
|
||||
return RoadType.PEDESTRIAN;
|
||||
case 'r':
|
||||
return RoadType.CYCLEWAY;
|
||||
case 's':
|
||||
return RoadType.TRACK;
|
||||
case 'z':
|
||||
return RoadType.COASTLINE;
|
||||
}
|
||||
return RoadType.UNCLASSIFIED;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new BinaryGraphReader that read from the given input stream.
|
||||
*
|
||||
*
|
||||
* @param dis Input stream to read from.
|
||||
*/
|
||||
public BinaryGraphReader(DataInputStream dis) {
|
||||
@@ -181,8 +180,10 @@ public class BinaryGraphReader extends BinaryReader implements GraphReader {
|
||||
final ArrayList<Node> nodes = new ArrayList<Node>(nbNodes);
|
||||
|
||||
// Read nodes.
|
||||
float minLongitude = Float.POSITIVE_INFINITY, minLatitude = Float.POSITIVE_INFINITY,
|
||||
maxLongitude = Float.NEGATIVE_INFINITY, maxLatitude = Float.NEGATIVE_INFINITY;
|
||||
float minLongitude = Float.POSITIVE_INFINITY,
|
||||
minLatitude = Float.POSITIVE_INFINITY,
|
||||
maxLongitude = Float.NEGATIVE_INFINITY,
|
||||
maxLatitude = Float.NEGATIVE_INFINITY;
|
||||
observers.forEach((observer) -> observer.notifyStartReadingNodes(nbNodes));
|
||||
for (int node = 0; node < nbNodes; ++node) {
|
||||
// Read longitude / latitude.
|
||||
@@ -230,7 +231,8 @@ public class BinaryGraphReader extends BinaryReader implements GraphReader {
|
||||
float maxLength = 0;
|
||||
final int copyNbTotalSuccesors = nbTotalSuccessors; // Stupid Java...
|
||||
int nbOneWayRoad = 0;
|
||||
observers.forEach((observer) -> observer.notifyStartReadingArcs(copyNbTotalSuccesors));
|
||||
observers.forEach(
|
||||
(observer) -> observer.notifyStartReadingArcs(copyNbTotalSuccesors));
|
||||
for (int node = 0; node < nbNodes; ++node) {
|
||||
for (int succ = 0; succ < nbSuccessors[node]; ++succ) {
|
||||
|
||||
@@ -250,8 +252,9 @@ public class BinaryGraphReader extends BinaryReader implements GraphReader {
|
||||
}
|
||||
maxLength = Math.max(length, maxLength);
|
||||
|
||||
length = Math.max(length, (float) Point.distance(nodes.get(node).getPoint(),
|
||||
nodes.get(destNode).getPoint()));
|
||||
length = Math.max(length,
|
||||
(float) Point.distance(nodes.get(node).getPoint(),
|
||||
nodes.get(destNode).getPoint()));
|
||||
|
||||
// Number of segments.
|
||||
final int nbSegments = dis.readUnsignedShort();
|
||||
@@ -294,14 +297,14 @@ public class BinaryGraphReader extends BinaryReader implements GraphReader {
|
||||
new GraphStatistics(
|
||||
new BoundingBox(new Point(minLongitude, maxLatitude),
|
||||
new Point(maxLongitude, minLatitude)),
|
||||
nbOneWayRoad, nbTotalSuccessors - nbOneWayRoad, maxSpeed, maxLength));
|
||||
nbOneWayRoad, nbTotalSuccessors - nbOneWayRoad, maxSpeed,
|
||||
maxLength));
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the next road information from the stream.
|
||||
*
|
||||
*
|
||||
* @return The next RoadInformation in the stream.
|
||||
*
|
||||
* @throws IOException if an error occurs while reading from the stream.
|
||||
*/
|
||||
private RoadInformation readRoadInformation() throws IOException {
|
||||
@@ -315,8 +318,8 @@ public class BinaryGraphReader extends BinaryReader implements GraphReader {
|
||||
// TODO: Try to create something...
|
||||
dis.readUnsignedShort();
|
||||
}
|
||||
return new RoadInformation(toRoadType(type), access, (x & 0x80) > 0, (x & 0x7F) * 5,
|
||||
dis.readUTF());
|
||||
return new RoadInformation(toRoadType(type), access, (x & 0x80) > 0,
|
||||
(x & 0x7F) * 5, dis.readUTF());
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -10,7 +10,6 @@ import org.insa.graphs.model.Path;
|
||||
|
||||
/**
|
||||
* Implementation of {@link PathReader} to read paths in binary format.
|
||||
*
|
||||
*/
|
||||
public class BinaryPathReader extends BinaryReader implements PathReader {
|
||||
|
||||
@@ -20,7 +19,7 @@ public class BinaryPathReader extends BinaryReader implements PathReader {
|
||||
|
||||
/**
|
||||
* Create a new BinaryPathReader that read from the given input stream.
|
||||
*
|
||||
*
|
||||
* @param dis Input stream to read from.
|
||||
*/
|
||||
public BinaryPathReader(DataInputStream dis) {
|
||||
@@ -35,7 +34,8 @@ public class BinaryPathReader extends BinaryReader implements PathReader {
|
||||
checkVersionOrThrow(dis.readInt());
|
||||
|
||||
// Read map ID and check against graph.
|
||||
final String mapId = readFixedLengthString(BinaryGraphReader.MAP_ID_FIELD_LENGTH, "UTF-8");
|
||||
final String mapId =
|
||||
readFixedLengthString(BinaryGraphReader.MAP_ID_FIELD_LENGTH, "UTF-8");
|
||||
|
||||
if (!mapId.equals(graph.getMapId())) {
|
||||
throw new MapMismatchException(mapId, graph.getMapId());
|
||||
@@ -59,12 +59,10 @@ public class BinaryPathReader extends BinaryReader implements PathReader {
|
||||
|
||||
/**
|
||||
* Read a node from the input stream and returns it.
|
||||
*
|
||||
*
|
||||
* @param graph Graph containing the nodes.
|
||||
*
|
||||
* @return The next node in the input stream.
|
||||
*
|
||||
* @throws IOException if something occurs while reading the note.
|
||||
* @throws IOException if something occurs while reading the note.
|
||||
* @throws IndexOutOfBoundsException if the node is not in the graph.
|
||||
*/
|
||||
protected Node readNode(Graph graph) throws IOException {
|
||||
|
@@ -9,13 +9,12 @@ import org.insa.graphs.model.Path;
|
||||
|
||||
/**
|
||||
* Implementation of {@link PathWriter} to write paths in binary format.
|
||||
*
|
||||
*/
|
||||
public class BinaryPathWriter extends BinaryWriter implements PathWriter {
|
||||
|
||||
/**
|
||||
* Create a new BinaryPathWriter that writes to the given output stream.
|
||||
*
|
||||
*
|
||||
* @param dos Output stream to write to.
|
||||
*/
|
||||
public BinaryPathWriter(DataOutputStream dos) {
|
||||
@@ -43,7 +42,7 @@ public class BinaryPathWriter extends BinaryWriter implements PathWriter {
|
||||
|
||||
// Write nodes.
|
||||
dos.writeInt(path.getOrigin().getId());
|
||||
for (Arc arc: path.getArcs()) {
|
||||
for (Arc arc : path.getArcs()) {
|
||||
dos.writeInt(arc.getDestination().getId());
|
||||
}
|
||||
|
||||
|
@@ -6,7 +6,6 @@ import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Base class for writing binary file.
|
||||
*
|
||||
*/
|
||||
public abstract class BinaryReader implements AutoCloseable, Closeable {
|
||||
|
||||
@@ -19,12 +18,12 @@ public abstract class BinaryReader implements AutoCloseable, Closeable {
|
||||
protected final DataInputStream dis;
|
||||
|
||||
/**
|
||||
* Create a new BinaryReader that reads from the given stream and that expected
|
||||
* the given magic number and at least the given minimum version.
|
||||
*
|
||||
* Create a new BinaryReader that reads from the given stream and that expected the
|
||||
* given magic number and at least the given minimum version.
|
||||
*
|
||||
* @param magicNumber Magic number of files to be read.
|
||||
* @param minVersion Minimum version of files to be read.
|
||||
* @param dis Input stream from which to read.
|
||||
* @param minVersion Minimum version of files to be read.
|
||||
* @param dis Input stream from which to read.
|
||||
*/
|
||||
protected BinaryReader(int magicNumber, int minVersion, DataInputStream dis) {
|
||||
this.magicNumber = magicNumber;
|
||||
@@ -38,13 +37,12 @@ public abstract class BinaryReader implements AutoCloseable, Closeable {
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the given version is greater than the minimum version, and update
|
||||
* the current version if it is.
|
||||
*
|
||||
* Check if the given version is greater than the minimum version, and update the
|
||||
* current version if it is.
|
||||
*
|
||||
* @param version Version to check.
|
||||
*
|
||||
* @throws BadVersionException if the given version is not greater than the
|
||||
* minimum version.
|
||||
* @throws BadVersionException if the given version is not greater than the minimum
|
||||
* version.
|
||||
*/
|
||||
protected void checkVersionOrThrow(int version) throws BadVersionException {
|
||||
if (version < this.minVersion) {
|
||||
@@ -62,25 +60,23 @@ public abstract class BinaryReader implements AutoCloseable, Closeable {
|
||||
|
||||
/**
|
||||
* Check if the given number matches the expected magic number.
|
||||
*
|
||||
*
|
||||
* @param magicNumber The magic number to check.
|
||||
*
|
||||
* @throws BadMagicNumberException If the two magic numbers are not equal.
|
||||
*/
|
||||
protected void checkMagicNumberOrThrow(int magicNumber) throws BadMagicNumberException {
|
||||
protected void checkMagicNumberOrThrow(int magicNumber)
|
||||
throws BadMagicNumberException {
|
||||
if (this.magicNumber != magicNumber) {
|
||||
throw new BadMagicNumberException(magicNumber, this.magicNumber);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the next byte in the input stream correspond to the given byte.
|
||||
*
|
||||
* This function consumes the next byte in the input stream.
|
||||
*
|
||||
* Check if the next byte in the input stream correspond to the given byte. This
|
||||
* function consumes the next byte in the input stream.
|
||||
*
|
||||
* @param b Byte to check.
|
||||
*
|
||||
* @throws IOException if an error occurs while reading the byte.
|
||||
* @throws IOException if an error occurs while reading the byte.
|
||||
* @throws BadFormatException if the byte read is not the expected one.
|
||||
*/
|
||||
protected void checkByteOrThrow(int b) throws IOException {
|
||||
@@ -90,17 +86,16 @@ public abstract class BinaryReader implements AutoCloseable, Closeable {
|
||||
}
|
||||
|
||||
/**
|
||||
* Read a byte array of fixed length from the input and convert it to a string
|
||||
* using the given charset, removing any trailing '\0'.
|
||||
*
|
||||
* @param length Number of bytes to read.
|
||||
* Read a byte array of fixed length from the input and convert it to a string using
|
||||
* the given charset, removing any trailing '\0'.
|
||||
*
|
||||
* @param length Number of bytes to read.
|
||||
* @param charset Charset to use to convert the bytes into a string.
|
||||
*
|
||||
* @return The convert string.
|
||||
*
|
||||
* @throws IOException if an error occurs while reading or converting.
|
||||
*/
|
||||
protected String readFixedLengthString(int length, String charset) throws IOException {
|
||||
protected String readFixedLengthString(int length, String charset)
|
||||
throws IOException {
|
||||
byte[] bytes = new byte[length];
|
||||
this.dis.read(bytes);
|
||||
return new String(bytes, "UTF-8").trim();
|
||||
@@ -109,9 +104,8 @@ public abstract class BinaryReader implements AutoCloseable, Closeable {
|
||||
/**
|
||||
* Read 24 bits in BigEndian order from the stream and return the corresponding
|
||||
* integer value.
|
||||
*
|
||||
*
|
||||
* @return Integer value read from the next 24 bits of the stream.
|
||||
*
|
||||
* @throws IOException if an error occurs while reading from the stream.
|
||||
*/
|
||||
protected int read24bits() throws IOException {
|
||||
|
@@ -6,7 +6,6 @@ import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Base class for writing binary file.
|
||||
*
|
||||
*/
|
||||
public abstract class BinaryWriter implements AutoCloseable, Closeable {
|
||||
|
||||
@@ -15,7 +14,7 @@ public abstract class BinaryWriter implements AutoCloseable, Closeable {
|
||||
|
||||
/**
|
||||
* Create a new BinaryWriter that writes to the given output stream.
|
||||
*
|
||||
*
|
||||
* @param dos Stream to write to.
|
||||
*/
|
||||
protected BinaryWriter(DataOutputStream dos) {
|
||||
@@ -29,9 +28,8 @@ public abstract class BinaryWriter implements AutoCloseable, Closeable {
|
||||
|
||||
/**
|
||||
* Write a 24-bits integer in BigEndian order to the output stream.
|
||||
*
|
||||
*
|
||||
* @param value Value to write.
|
||||
*
|
||||
* @throws IOException if an error occurs while writing to the stream.
|
||||
*/
|
||||
protected void write24bits(int value) throws IOException {
|
||||
|
@@ -7,32 +7,28 @@ import org.insa.graphs.model.Graph;
|
||||
|
||||
/**
|
||||
* Base interface for classes that can read graph.
|
||||
*
|
||||
*/
|
||||
public interface GraphReader extends AutoCloseable, Closeable {
|
||||
|
||||
/**
|
||||
* Add a new observer to this graph reader.
|
||||
*
|
||||
*
|
||||
* @param observer Observer to add.
|
||||
*/
|
||||
public void addObserver(GraphReaderObserver observer);
|
||||
|
||||
/**
|
||||
* Read a graph an returns it.
|
||||
*
|
||||
*
|
||||
* @return The graph read.
|
||||
*
|
||||
* @throws IOException if an exception occurs while reading the graph.
|
||||
*
|
||||
*/
|
||||
public Graph read() throws IOException;
|
||||
|
||||
/**
|
||||
* Close this graph reader.
|
||||
*
|
||||
*
|
||||
* @throws IOException if an exception occurs while closing the reader.
|
||||
*
|
||||
*/
|
||||
public void close() throws IOException;
|
||||
|
||||
|
@@ -5,16 +5,15 @@ import org.insa.graphs.model.Node;
|
||||
import org.insa.graphs.model.RoadInformation;
|
||||
|
||||
/**
|
||||
* Base interface that should be implemented by classes that want to observe the
|
||||
* reading of a graph by a {@link GraphReader}.
|
||||
*
|
||||
* Base interface that should be implemented by classes that want to observe the reading
|
||||
* of a graph by a {@link GraphReader}.
|
||||
*/
|
||||
public interface GraphReaderObserver {
|
||||
|
||||
/**
|
||||
* Notify observer about information on the graph, this method is always the
|
||||
* first called
|
||||
*
|
||||
* Notify observer about information on the graph, this method is always the first
|
||||
* called
|
||||
*
|
||||
* @param mapId ID of the graph.
|
||||
*/
|
||||
public void notifyStartReading(String mapId);
|
||||
@@ -26,42 +25,42 @@ public interface GraphReaderObserver {
|
||||
|
||||
/**
|
||||
* Notify that the reader is starting to read node.
|
||||
*
|
||||
*
|
||||
* @param nNodes Number of nodes to read.
|
||||
*/
|
||||
public void notifyStartReadingNodes(int nNodes);
|
||||
|
||||
/**
|
||||
* Notify that a new nodes has been read.
|
||||
*
|
||||
*
|
||||
* @param node read.
|
||||
*/
|
||||
public void notifyNewNodeRead(Node node);
|
||||
|
||||
/**
|
||||
* Notify that the reader is starting to read descriptor/road informations.
|
||||
*
|
||||
*
|
||||
* @param nDesc Number of descriptors to read.
|
||||
*/
|
||||
public void notifyStartReadingDescriptors(int nDesc);
|
||||
|
||||
/**
|
||||
* Notify that a new descriptor has been read.
|
||||
*
|
||||
*
|
||||
* @param desc Descriptor read.
|
||||
*/
|
||||
public void notifyNewDescriptorRead(RoadInformation desc);
|
||||
|
||||
/**
|
||||
* Notify that the reader is starting to read arcs.
|
||||
*
|
||||
*
|
||||
* @param nArcs Number of arcs to read (!= number of arcs in the graph).
|
||||
*/
|
||||
public void notifyStartReadingArcs(int nArcs);
|
||||
|
||||
/**
|
||||
* Notify that a new arc has been read.
|
||||
*
|
||||
*
|
||||
* @param arc Arc read.
|
||||
*/
|
||||
public void notifyNewArcRead(Arc arc);
|
||||
|
@@ -3,14 +3,13 @@ package org.insa.graphs.model.io;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Exception thrown when there is mismatch between the expected map ID and the
|
||||
* actual map ID when reading a graph.
|
||||
*
|
||||
* Exception thrown when there is mismatch between the expected map ID and the actual
|
||||
* map ID when reading a graph.
|
||||
*/
|
||||
public class MapMismatchException extends IOException {
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@@ -19,8 +18,8 @@ public class MapMismatchException extends IOException {
|
||||
|
||||
/**
|
||||
* Create a new MapMismatchException with the given IDs.
|
||||
*
|
||||
* @param actualMapId Actual map ID found when reading the path.
|
||||
*
|
||||
* @param actualMapId Actual map ID found when reading the path.
|
||||
* @param expectedMapId Expected map ID from the graph.
|
||||
*/
|
||||
public MapMismatchException(String actualMapId, String expectedMapId) {
|
||||
|
@@ -8,26 +8,22 @@ import org.insa.graphs.model.Path;
|
||||
|
||||
/**
|
||||
* Base interface that should be implemented by class used to read paths.
|
||||
*
|
||||
*/
|
||||
public interface PathReader extends AutoCloseable, Closeable {
|
||||
|
||||
/**
|
||||
* Read a path of the given graph and returns it.
|
||||
*
|
||||
*
|
||||
* @param graph Graph of the path.
|
||||
*
|
||||
* @return Path read.
|
||||
*
|
||||
* @throws IOException When an error occurs while reading the path.
|
||||
*/
|
||||
public Path readPath(Graph graph) throws IOException;
|
||||
|
||||
/**
|
||||
* Close this graph reader.
|
||||
*
|
||||
*
|
||||
* @throws IOException if an exception occurs while closing the reader.
|
||||
*
|
||||
*/
|
||||
public void close() throws IOException;
|
||||
|
||||
|
@@ -7,24 +7,21 @@ import org.insa.graphs.model.Path;
|
||||
|
||||
/**
|
||||
* Base interface that should be implemented by class used to write paths.
|
||||
*
|
||||
*/
|
||||
public interface PathWriter extends AutoCloseable, Closeable {
|
||||
|
||||
/**
|
||||
* Write the given path.
|
||||
*
|
||||
*
|
||||
* @param path Path to write.
|
||||
*
|
||||
* @throws IOException When an error occurs while writing the path.
|
||||
*/
|
||||
public void writePath(Path path) throws IOException;
|
||||
|
||||
/**
|
||||
* Close this graph reader.
|
||||
*
|
||||
*
|
||||
* @throws IOException if an exception occurs while closing the reader.
|
||||
*
|
||||
*/
|
||||
public void close() throws IOException;
|
||||
|
||||
|
@@ -39,23 +39,29 @@ public class GraphTest {
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, false, 1, null),
|
||||
new ArrayList<>());
|
||||
Node.linkNodes(nodes[0], nodes[4], 0,
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, true, 1, null), new ArrayList<>());
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, true, 1, null),
|
||||
new ArrayList<>());
|
||||
Node.linkNodes(nodes[1], nodes[2], 0,
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, false, 1, null),
|
||||
new ArrayList<>());
|
||||
Node.linkNodes(nodes[2], nodes[3], 0,
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, true, 1, null), new ArrayList<>());
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, true, 1, null),
|
||||
new ArrayList<>());
|
||||
Node.linkNodes(nodes[2], nodes[3], 0,
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, true, 1, null), new ArrayList<>());
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, true, 1, null),
|
||||
new ArrayList<>());
|
||||
Node.linkNodes(nodes[2], nodes[3], 0,
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, true, 1, null), new ArrayList<>());
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, true, 1, null),
|
||||
new ArrayList<>());
|
||||
Node.linkNodes(nodes[3], nodes[0], 0,
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, false, 1, null),
|
||||
new ArrayList<>());
|
||||
Node.linkNodes(nodes[3], nodes[4], 0,
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, true, 1, null), new ArrayList<>());
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, true, 1, null),
|
||||
new ArrayList<>());
|
||||
Node.linkNodes(nodes[4], nodes[0], 0,
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, true, 1, null), new ArrayList<>());
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, true, 1, null),
|
||||
new ArrayList<>());
|
||||
|
||||
graph = new Graph("ID", "", Arrays.asList(nodes), null);
|
||||
|
||||
@@ -66,7 +72,7 @@ public class GraphTest {
|
||||
*/
|
||||
private List<Arc> getArcsBetween(Node a, Node b) {
|
||||
List<Arc> arcs = new ArrayList<>();
|
||||
for (Arc arc: a.getSuccessors()) {
|
||||
for (Arc arc : a.getSuccessors()) {
|
||||
if (arc.getDestination().equals(b)) {
|
||||
arcs.add(arc);
|
||||
}
|
||||
|
@@ -33,23 +33,29 @@ public class NodeTest {
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, false, 1, null),
|
||||
new ArrayList<>());
|
||||
Node.linkNodes(nodes[0], nodes[4], 0,
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, true, 1, null), new ArrayList<>());
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, true, 1, null),
|
||||
new ArrayList<>());
|
||||
Node.linkNodes(nodes[1], nodes[2], 0,
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, false, 1, null),
|
||||
new ArrayList<>());
|
||||
Node.linkNodes(nodes[2], nodes[3], 0,
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, true, 1, null), new ArrayList<>());
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, true, 1, null),
|
||||
new ArrayList<>());
|
||||
Node.linkNodes(nodes[2], nodes[3], 0,
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, true, 1, null), new ArrayList<>());
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, true, 1, null),
|
||||
new ArrayList<>());
|
||||
Node.linkNodes(nodes[2], nodes[3], 0,
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, true, 1, null), new ArrayList<>());
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, true, 1, null),
|
||||
new ArrayList<>());
|
||||
Node.linkNodes(nodes[3], nodes[0], 0,
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, false, 1, null),
|
||||
new ArrayList<>());
|
||||
Node.linkNodes(nodes[3], nodes[4], 0,
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, true, 1, null), new ArrayList<>());
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, true, 1, null),
|
||||
new ArrayList<>());
|
||||
Node.linkNodes(nodes[4], nodes[0], 0,
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, true, 1, null), new ArrayList<>());
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, true, 1, null),
|
||||
new ArrayList<>());
|
||||
|
||||
}
|
||||
|
||||
@@ -57,7 +63,7 @@ public class NodeTest {
|
||||
* @return The first arc between from a to b, or null.
|
||||
*/
|
||||
private Arc getFirstArcBetween(Node a, Node b) {
|
||||
for (Arc arc: a.getSuccessors()) {
|
||||
for (Arc arc : a.getSuccessors()) {
|
||||
if (arc.getDestination().equals(b)) {
|
||||
return arc;
|
||||
}
|
||||
|
@@ -30,14 +30,15 @@ public class PathTest {
|
||||
private static Arc a2b, a2c, a2e, b2c, c2d_1, c2d_2, c2d_3, c2a, d2a, d2e, e2d;
|
||||
|
||||
// Some paths...
|
||||
private static Path emptyPath, singleNodePath, shortPath, longPath, loopPath, longLoopPath,
|
||||
invalidPath;
|
||||
private static Path emptyPath, singleNodePath, shortPath, longPath, loopPath,
|
||||
longLoopPath, invalidPath;
|
||||
|
||||
@BeforeClass
|
||||
public static void initAll() throws IOException {
|
||||
|
||||
// 10 and 20 meters per seconds
|
||||
RoadInformation speed10 = new RoadInformation(RoadType.MOTORWAY, null, true, 36, ""),
|
||||
RoadInformation speed10 =
|
||||
new RoadInformation(RoadType.MOTORWAY, null, true, 36, ""),
|
||||
speed20 = new RoadInformation(RoadType.MOTORWAY, null, true, 72, "");
|
||||
|
||||
// Create nodes
|
||||
@@ -65,8 +66,8 @@ public class PathTest {
|
||||
shortPath = new Path(graph, Arrays.asList(new Arc[] { a2b, b2c, c2d_1 }));
|
||||
longPath = new Path(graph, Arrays.asList(new Arc[] { a2b, b2c, c2d_1, d2e }));
|
||||
loopPath = new Path(graph, Arrays.asList(new Arc[] { a2b, b2c, c2d_1, d2a }));
|
||||
longLoopPath = new Path(graph,
|
||||
Arrays.asList(new Arc[] { a2b, b2c, c2d_1, d2a, a2c, c2d_3, d2a, a2b, b2c }));
|
||||
longLoopPath = new Path(graph, Arrays
|
||||
.asList(new Arc[] { a2b, b2c, c2d_1, d2a, a2c, c2d_3, d2a, a2b, b2c }));
|
||||
invalidPath = new Path(graph, Arrays.asList(new Arc[] { a2b, c2d_1, d2e }));
|
||||
|
||||
}
|
||||
@@ -184,7 +185,8 @@ public class PathTest {
|
||||
}
|
||||
|
||||
// Trap construction!
|
||||
path = Path.createFastestPathFromNodes(graph, Arrays.asList(new Node[] { nodes[1] }));
|
||||
path = Path.createFastestPathFromNodes(graph,
|
||||
Arrays.asList(new Node[] { nodes[1] }));
|
||||
assertEquals(nodes[1], path.getOrigin());
|
||||
assertEquals(0, path.getArcs().size());
|
||||
|
||||
@@ -219,7 +221,8 @@ public class PathTest {
|
||||
}
|
||||
|
||||
// Trap construction!
|
||||
path = Path.createShortestPathFromNodes(graph, Arrays.asList(new Node[] { nodes[1] }));
|
||||
path = Path.createShortestPathFromNodes(graph,
|
||||
Arrays.asList(new Node[] { nodes[1] }));
|
||||
assertEquals(nodes[1], path.getOrigin());
|
||||
assertEquals(0, path.getArcs().size());
|
||||
|
||||
@@ -232,12 +235,14 @@ public class PathTest {
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testCreateFastestPathFromNodesException() {
|
||||
Path.createFastestPathFromNodes(graph, Arrays.asList(new Node[] { nodes[1], nodes[0] }));
|
||||
Path.createFastestPathFromNodes(graph,
|
||||
Arrays.asList(new Node[] { nodes[1], nodes[0] }));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testCreateShortestPathFromNodesException() {
|
||||
Path.createShortestPathFromNodes(graph, Arrays.asList(new Node[] { nodes[1], nodes[0] }));
|
||||
Path.createShortestPathFromNodes(graph,
|
||||
Arrays.asList(new Node[] { nodes[1], nodes[0] }));
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user