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 d89d599..57b5273 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 @@ -3,6 +3,7 @@ package org.insa.graphs.model; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Iterator; /** *

@@ -29,13 +30,42 @@ 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 { List arcs = new ArrayList(); - // TODO: + if (nodes.size() == 0) { + return new Path(graph); + } else if (nodes.size() == 1) { + return new Path(graph, nodes.get(0)); + } else { + Iterator iterateur= nodes.iterator(); + Node nodeOrigin; + Node nodeDest = iterateur.next(); + + while (iterateur.hasNext()) { + + nodeOrigin = nodeDest; + nodeDest = iterateur.next(); + + List successeurs = nodeOrigin.getSuccessors(); + + Arc shortestArc = null; + double minTravelTime = Float.MAX_VALUE; + for (Arc arc : successeurs) { + if (nodeDest.equals(arc.getDestination()) && (arc.getMinimumTravelTime() < minTravelTime)) { + minTravelTime = arc.getMinimumTravelTime(); + shortestArc = arc; + } + } + + if (shortestArc == null) { + throw new IllegalArgumentException(); + } else { + arcs.add(shortestArc); + } + } + } return new Path(graph, arcs); } @@ -50,14 +80,43 @@ 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 { List arcs = new ArrayList(); - // TODO: - return new Path(graph, arcs); + if (nodes.size() == 0) { + return new Path(graph); + } else if (nodes.size() == 1) { + return new Path(graph, nodes.get(0)); + } else { + Iterator iterateur= nodes.iterator(); + Node nodeOrigin; + Node nodeDest = iterateur.next(); + + while (iterateur.hasNext()) { + + nodeOrigin = nodeDest; + nodeDest = iterateur.next(); + + List successeurs = nodeOrigin.getSuccessors(); + + Arc shortestArc = null; + float minLength = Float.MAX_VALUE; + for (Arc arc : successeurs) { + if (nodeDest.equals(arc.getDestination()) && (arc.getLength() < minLength)) { + minLength = arc.getLength(); + shortestArc = arc; + } + } + + if (shortestArc == null) { + throw new IllegalArgumentException(); + } else { + arcs.add(shortestArc); + } + } + return new Path(graph, arcs); + } } /**