From 86704ff6019c7f938989c4df44bb9ce6bb211b9f Mon Sep 17 00:00:00 2001 From: Matteo Date: Fri, 11 Apr 2025 10:35:56 +0200 Subject: [PATCH] ajout de createShortestPathFromNodes --- .../main/java/org/insa/graphs/model/Path.java | 93 +++++++++++++++++-- 1 file changed, 83 insertions(+), 10 deletions(-) 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 d32835d..d6940a4 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 @@ -1,6 +1,8 @@ package org.insa.graphs.model; -import java.awt.RenderingHints; +// import java.awt.RenderingHints; commenté parce qu'il soulevait une erreur (doesn't +// exist) +import java.io.ObjectInputStream; import java.util.*; /** @@ -25,7 +27,7 @@ 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. */ - // TODO : faut faire en fct du maxspeed ds road information + // public static Path createFastestPathFromNodes(Graph graph, List nodes) @@ -86,16 +88,87 @@ 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(); - // TODO: en fonction des mètres - Boolean fini = false; + List arcsFinaux = new ArrayList(); - return new Path(graph, arcs); + if (nodes.size() == 1) { + System.out.println("Attention liste avec un seul node\n"); + return new Path(graph, nodes.get(0)); + } + else { + + for (Node nodeE : nodes) { + if (nodes.indexOf(nodeE) == nodes.size() - 1) { // on ne regarde pas le + // dernier node qui est + // la + // destination + + + } // sinon on fait notre recherche + else if (nodeE.hasSuccessors()) { + List temporaryArcs = nodeE.getSuccessors(); + List temporaryCorrespondingArc = new ArrayList<>(); + for (Arc arcE : temporaryArcs) { + if (arcE.getDestination() + .equals(nodes.get(nodes.indexOf(nodeE) + 1))) { // si + // la + // destination + // de + // l'arc + // est + // égal + // au + // prochain + // node + + temporaryCorrespondingArc.add(arcE);// ajouter cet arc dans + // la + // liste + // temporaryCorrespondingArcs + + } + } + + // la liste temporaryCorrespondingArc devrait être remplie + if (temporaryCorrespondingArc.isEmpty()) { // si elle est vide les 2 + // nodes ne sont pas + // connectés + throw new IllegalArgumentException( + "les nodes de la liste ne sont pas connectés \n"); + } + + Arc ArcPlusCourt = temporaryCorrespondingArc.get(0); + for (Arc ArcTemporaire : temporaryCorrespondingArc) {// comparer ts + // les + // arcs de la + // liste + // et prendre + // le + // plus rapide + if (ArcTemporaire.getLength() < ArcPlusCourt.getLength()) { + ArcPlusCourt = ArcTemporaire; + } + } + arcsFinaux.add(ArcPlusCourt);// l'ajouter dans la liste finale qui + // est + // arcsFinaux + + } + else if (!nodeE.hasSuccessors()) { + System.out.println("node has no successors \n"); + } + else { + System.out.println( + "on ne devrait pas pouvoir être ici gros problème\n"); + } + } + + + return new Path(graph, arcsFinaux); + } } /** @@ -263,9 +336,9 @@ public class Path { * @return Total length of the path (in meters). */ public float getLength() { - float pathLength = 0f; // sinon error si on met O.O - for (Arc element : this.arcs) { - pathLength = pathLength + ((float) element.getLength()); + float pathLength=0f; // sinon error si on met O.O + for (Arc element : this.arcs) { + pathLength=pathLength+((float) element.getLength()); } return pathLength; }