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 7580981..d2ad83b 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 @@ -17,7 +17,7 @@ import java.util.List; public class Path { /** - * Create a new path that goes through the given list of nodes (in order), choosing + * Create a new path that goes through the given list of nodes in order, choosing * the fastest route if multiple are available. * * @param graph Graph containing the nodes in the list. @@ -25,11 +25,41 @@ public class Path { * @return A path that goes through the given list of nodes. * @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 { List arcs = new ArrayList(); + if (nodes.size() == 0) { + return new Path(graph); + } + if (nodes.size() == 1) { + return new Path(graph, nodes.get(0)); + } + for (int i = 1; i < nodes.size(); i++) { + Node depart = nodes.get(i - 1); + Node dest = nodes.get(i); + List test = depart.getSuccessors(); + List chemins = new ArrayList(); + Boolean erreur = true; + for (Arc a : test) { + if (a.getDestination().equals(dest)) { + chemins.add(a); + erreur = false; + } + } + if (erreur) { + throw new IllegalArgumentException(); + } + double min = chemins.get(0).getMinimumTravelTime(); + int index = 0; + for (int j = 1; j < chemins.size(); j++) { + if (min > chemins.get(j).getMinimumTravelTime()) { + index = j; + min = chemins.get(j).getMinimumTravelTime(); + } + } + arcs.add(chemins.get(index)); + } // TODO: return new Path(graph, arcs); } @@ -43,11 +73,41 @@ public class Path { * @return A path that goes through the given list of nodes. * @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 { List arcs = new ArrayList(); + if (nodes.size() == 0) { + return new Path(graph); + } + if (nodes.size() == 1) { + return new Path(graph, nodes.get(0)); + } + for (int i = 1; i < nodes.size(); i++) { + Node depart = nodes.get(i - 1); + Node dest = nodes.get(i); + List test = depart.getSuccessors(); + List chemins = new ArrayList(); + Boolean erreur = true; + for (Arc a : test) { + if (a.getDestination().equals(dest)) { + chemins.add(a); + erreur = false; + } + } + if (erreur) { + throw new IllegalArgumentException(); + } + double min = chemins.get(0).getLength(); + int index = 0; + for (int j = 1; j < chemins.size(); j++) { + if (min > chemins.get(j).getLength()) { + index = j; + min = chemins.get(j).getLength(); + } + } + arcs.add(chemins.get(index)); + } // TODO: return new Path(graph, arcs); } @@ -184,11 +244,26 @@ public class Path { * * * @return true if the path is valid, false otherwise. - * @deprecated Need to be implemented. */ public boolean isValid() { + if (this.isEmpty()) { + return true; + } + if (this.size() < 2) { + return true; + } + if (!this.origin.equals(this.arcs.get(0).getOrigin())) { + return false; + } + for (int i = 0; i < this.arcs.size() - 1; i++) { + Node dest = this.arcs.get(i).getDestination(); + Node depart = this.arcs.get(i + 1).getOrigin(); + if (!dest.equals(depart)) { + return false; + } + } // TODO: - return false; + return true; } /** @@ -198,8 +273,12 @@ public class Path { * @deprecated Need to be implemented. */ public float getLength() { + float l = 0; + for (Arc a : this.arcs) { + l += a.getLength(); + } // TODO: - return 0; + return l; } /** @@ -211,8 +290,12 @@ public class Path { * @deprecated Need to be implemented. */ public double getTravelTime(double speed) { + double l = 0; + for (Arc a : this.arcs) { + l += a.getTravelTime(speed); + } // TODO: - return 0; + return l; } /** @@ -224,7 +307,11 @@ public class Path { */ public double getMinimumTravelTime() { // TODO: - return 0; + double l = 0; + for (Arc a : this.arcs) { + l += a.getMinimumTravelTime(); + } + return l; } }