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 7.5KB

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