No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Path.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. package org.insa.graph;
  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. import java.util.List;
  5. /**
  6. * Class representing a path between nodes in a graph.
  7. *
  8. * A path is represented as a list of {@link Arc} and not a list of {@link Node}
  9. * due to the multigraph nature of the considered graphs.
  10. *
  11. */
  12. public class Path {
  13. /**
  14. * Create a new path that goes through the given list of nodes (in order),
  15. * choosing the fastest route if multiple are available.
  16. *
  17. * @param graph Graph containing the nodes in the list.
  18. * @param nodes List of nodes to build the path.
  19. *
  20. * @return A path that goes through the given list of nodes.
  21. *
  22. * @throws IllegalArgumentException If the list of nodes is not valid, i.e. two
  23. * consecutive nodes in the list are not connected in the graph.
  24. *
  25. * @deprecated Need to be implemented.
  26. */
  27. public static Path createFastestPathFromNodes(Graph graph, List<Node> nodes)
  28. throws IllegalArgumentException {
  29. List<Arc> arcs = new ArrayList<Arc>();
  30. // TODO:
  31. return new Path(graph, arcs);
  32. }
  33. /**
  34. * Create a new path that goes through the given list of nodes (in order),
  35. * choosing the shortest route if multiple are available.
  36. *
  37. * @param graph Graph containing the nodes in the list.
  38. * @param nodes List of nodes to build the path.
  39. *
  40. * @return A path that goes through the given list of nodes.
  41. *
  42. * @throws IllegalArgumentException If the list of nodes is not valid, i.e. two
  43. * consecutive nodes in the list are not connected in the graph.
  44. *
  45. * @deprecated Need to be implemented.
  46. */
  47. public static Path createShortestPathFromNodes(Graph graph, List<Node> nodes)
  48. throws IllegalArgumentException {
  49. List<Arc> arcs = new ArrayList<Arc>();
  50. // TODO:
  51. return new Path(graph, arcs);
  52. }
  53. /**
  54. * Concatenate the given paths.
  55. *
  56. * @param paths Array of paths to concatenate.
  57. *
  58. * @return Concatenated path.
  59. *
  60. * @throws IllegalArgumentException if the paths cannot be concatenated (IDs of
  61. * map do not match, or the end of a path is not the beginning of the
  62. * next).
  63. */
  64. public static Path concatenate(Path... paths) throws IllegalArgumentException {
  65. if (paths.length == 0) {
  66. throw new IllegalArgumentException("Cannot concatenate an empty list of paths.");
  67. }
  68. final String mapId = paths[0].getGraph().getMapId();
  69. for (int i = 1; i < paths.length; ++i) {
  70. if (!paths[i].getGraph().getMapId().equals(mapId)) {
  71. throw new IllegalArgumentException(
  72. "Cannot concatenate paths from different graphs.");
  73. }
  74. }
  75. ArrayList<Arc> arcs = new ArrayList<>();
  76. for (Path path: paths) {
  77. arcs.addAll(path.getArcs());
  78. }
  79. Path path = new Path(paths[0].getGraph(), arcs);
  80. if (!path.isValid()) {
  81. throw new IllegalArgumentException(
  82. "Cannot concatenate paths that do not form a single path.");
  83. }
  84. return path;
  85. }
  86. // Graph containing this path.
  87. private final Graph graph;
  88. // Origin of the path
  89. private final Node origin;
  90. // List of arcs in this path.
  91. private final List<Arc> arcs;
  92. /**
  93. * Create an empty path corresponding to the given graph.
  94. *
  95. * @param graph Graph containing the path.
  96. */
  97. public Path(Graph graph) {
  98. this.graph = graph;
  99. this.origin = null;
  100. this.arcs = new ArrayList<>();
  101. }
  102. /**
  103. * Create a new path containing a single node.
  104. *
  105. * @param graph Graph containing the path.
  106. * @param node Single node of the path.
  107. */
  108. public Path(Graph graph, Node node) {
  109. this.graph = graph;
  110. this.origin = node;
  111. this.arcs = new ArrayList<>();
  112. }
  113. /**
  114. * Create a new path with the given list of arcs.
  115. *
  116. * @param graph Graph containing the path.
  117. * @param arcs Arcs to construct the path.
  118. */
  119. public Path(Graph graph, List<Arc> arcs) {
  120. this.graph = graph;
  121. this.arcs = arcs;
  122. this.origin = arcs.size() > 0 ? arcs.get(0).getOrigin() : null;
  123. }
  124. /**
  125. * @return Graph containing the path.
  126. */
  127. public Graph getGraph() {
  128. return graph;
  129. }
  130. /**
  131. * @return First node of the path.
  132. */
  133. public Node getOrigin() {
  134. return origin;
  135. }
  136. /**
  137. * @return Last node of the path.
  138. */
  139. public Node getDestination() {
  140. return arcs.get(arcs.size() - 1).getDestination();
  141. }
  142. /**
  143. * @return List of arcs in the path.
  144. */
  145. public List<Arc> getArcs() {
  146. return Collections.unmodifiableList(arcs);
  147. }
  148. /**
  149. * Check if this path is empty (it does not contain any node).
  150. *
  151. * @return true if this path is empty, false otherwise.
  152. */
  153. public boolean isEmpty() {
  154. return this.origin == null;
  155. }
  156. /**
  157. * Get the number of <b>nodes</b> in this path.
  158. *
  159. * @return Number of nodes in this path.
  160. */
  161. public int size() {
  162. return 1 + this.arcs.size();
  163. }
  164. /**
  165. * Check if this path is valid.
  166. *
  167. * A path is valid if any of the following is true:
  168. * <ul>
  169. * <li>it is empty;</li>
  170. * <li>it contains a single node (without arcs);</li>
  171. * <li>the first arc has for origin the origin of the path and, for two
  172. * consecutive arcs, the destination of the first one is the origin of the
  173. * second one.</li>
  174. * </ul>
  175. *
  176. * @return true if the path is valid, false otherwise.
  177. *
  178. * @deprecated Need to be implemented.
  179. */
  180. public boolean isValid() {
  181. // TODO:
  182. return false;
  183. }
  184. /**
  185. * Compute the length of this path (in meters).
  186. *
  187. * @return Total length of the path (in meters).
  188. *
  189. * @deprecated Need to be implemented.
  190. */
  191. public float getLength() {
  192. // TODO:
  193. return 0;
  194. }
  195. /**
  196. * Compute the time required to travel this path if moving at the given speed.
  197. *
  198. * @param speed Speed to compute the travel time.
  199. *
  200. * @return Time (in seconds) required to travel this path at the given speed (in
  201. * kilometers-per-hour).
  202. *
  203. * @deprecated Need to be implemented.
  204. */
  205. public double getTravelTime(double speed) {
  206. // TODO:
  207. return 0;
  208. }
  209. /**
  210. * Compute the time to travel this path if moving at the maximum allowed speed
  211. * on every arc.
  212. *
  213. * @return Minimum travel time to travel this path (in seconds).
  214. *
  215. * @deprecated Need to be implemented.
  216. */
  217. public double getMinimumTravelTime() {
  218. // TODO:
  219. return 0;
  220. }
  221. }