diff --git a/be-graphes-model/src/main/java/org/insa/graphs/model/Path.java b/be-graphes-model/src/main/java/org/insa/graphs/model/Path.java index 3076e37..a3b6fba 100644 --- a/be-graphes-model/src/main/java/org/insa/graphs/model/Path.java +++ b/be-graphes-model/src/main/java/org/insa/graphs/model/Path.java @@ -30,12 +30,38 @@ public class Path { * @throws IllegalArgumentException If the list of nodes is not valid, i.e. two * consecutive nodes in the list are not connected in the graph. * - * @deprecated Need to be implemented. */ public static Path createFastestPathFromNodes(Graph graph, List nodes) throws IllegalArgumentException { + if (nodes.size() == 1) { + return new Path(graph, nodes.get(0)); + } + if (nodes.size() == 1) { + return new Path(graph); + } List arcs = new ArrayList(); - // TODO: + for (int i = 0 ; i < nodes.size(); i++) { + Node currNode = nodes.get(i); + if (i < nodes.size() -1) { + Node nextNode = nodes.get(i+1); + if (currNode.hasSuccessors()) { + Arc better = null; + for (Arc j : currNode.getSuccessors()) { + if (j.getDestination() == nextNode) { + if (better == null || better.getMinimumTravelTime() > j.getMinimumTravelTime()) { + better = j; + } + } + } + if (better == null) { + throw (new IllegalArgumentException()); + } else { + arcs.add(better); + } + } + } + } + return new Path(graph, arcs); } @@ -51,12 +77,38 @@ public class Path { * @throws IllegalArgumentException If the list of nodes is not valid, i.e. two * consecutive nodes in the list are not connected in the graph. * - * @deprecated Need to be implemented. */ public static Path createShortestPathFromNodes(Graph graph, List nodes) throws IllegalArgumentException { + if (nodes.size() == 1) { + return new Path(graph, nodes.get(0)); + } + if (nodes.size() == 1) { + return new Path(graph); + } List arcs = new ArrayList(); - // TODO: + for (int i = 0 ; i < nodes.size(); i++) { + Node currNode = nodes.get(i); + if (i < nodes.size() -1) { + Node nextNode = nodes.get(i+1); + if (currNode.hasSuccessors()) { + Arc better = null; + for (Arc j : currNode.getSuccessors()) { + if (j.getDestination() == nextNode) { + if (better == null || better.getLength() > j.getLength()) { + better = j; + } + } + } + if (better == null) { + throw (new IllegalArgumentException()); + } else { + arcs.add(better); + } + } + } + } + return new Path(graph, arcs); }