diff --git a/be-graphes-gui/src/main/java/org/insa/graphs/gui/simple/Launch.java b/be-graphes-gui/src/main/java/org/insa/graphs/gui/simple/Launch.java index aa7ea04..0a8e01e 100644 --- a/be-graphes-gui/src/main/java/org/insa/graphs/gui/simple/Launch.java +++ b/be-graphes-gui/src/main/java/org/insa/graphs/gui/simple/Launch.java @@ -14,6 +14,7 @@ import org.insa.graphs.gui.drawing.components.BasicDrawing; import org.insa.graphs.model.Graph; import org.insa.graphs.model.Path; import org.insa.graphs.model.io.BinaryGraphReader; +import org.insa.graphs.model.io.BinaryPathReader; import org.insa.graphs.model.io.GraphReader; import org.insa.graphs.model.io.PathReader; @@ -57,23 +58,26 @@ public class Launch { try (final GraphReader reader = new BinaryGraphReader(new DataInputStream( new BufferedInputStream(new FileInputStream(mapName))))) { - // TODO: read the graph - graph = null; + // DONE: read the graph + graph = reader.read(); } // create the drawing final Drawing drawing = createDrawing(); - // TODO: draw the graph on the drawing + // DONE: draw the graph on the drawing + drawing.drawGraph(graph); - // TODO: create a path reader - try (final PathReader pathReader = null) { + // DONE: create a path reader + try (final PathReader pathReader = new BinaryPathReader(new DataInputStream( + new BufferedInputStream(new FileInputStream(pathName))))) { - // TODO: read the path - path = null; + // DONE: read the path + path = pathReader.readPath(graph); } - // TODO: draw the path on the drawing + // DONE: draw the path on the drawing + drawing.drawPath(path); } } 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 d6a570e..3758400 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 @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; +import java.util.Iterator; /** *

@@ -26,12 +27,32 @@ 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(); - // TODO: + + if (nodes.size() == 1) { return new Path(graph, nodes.get(0)); } + + for (int i = 0; i < nodes.size() - 1; i++) { + Arc bestArc = null; + double bestSpeed = Double.MAX_VALUE; + for (Arc arcsucc : nodes.get(i).getSuccessors()) { + if (arcsucc.getDestination().equals(nodes.get(i+1))) { + int succspeed = arcsucc.getRoadInformation().getMaximumSpeed(); + if (arcsucc.getTravelTime(succspeed) < bestSpeed) { + bestArc = arcsucc; + bestSpeed = arcsucc.getTravelTime(succspeed); + } + } + } + if (bestArc == null) { + throw new IllegalArgumentException(); + } else { + arcs.add(bestArc); + } + } + return new Path(graph, arcs); } @@ -193,11 +214,21 @@ public class Path { * * * @return true if the path is valid, false otherwise. - * @deprecated Need to be implemented. */ public boolean isValid() { - // TODO: - return false; + + if (this.isEmpty()) { return true; } + if (this.size() == 1 && this.arcs.isEmpty()) { return true; } + if (!this.arcs.get(0).getOrigin().equals(this.origin)) { return false; } + + Boolean good = true; + for (int i = 0; i < this.arcs.size() - 1; i++) { + if (!this.arcs.get(i).getDestination().equals(this.arcs.get(i+1).getOrigin())) { + good = false; + } + } + + return good; } /**