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 e654c12..73d364c 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 @@ -57,7 +57,41 @@ public class Path { public static Path createShortestPathFromNodes(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++) + { + Node origin = nodes.get(i); + Node destination = nodes.get(i+1); + + List successors = origin.getSuccessors(); + Arc bestArc = null; + float lengthMin = Float.MAX_VALUE; + for(Arc successor : successors) + { + float legnthSuccessor = successor.getLength(); + if(successor.getDestination().equals(destination)) + { + if (legnthSuccessor < lengthMin) + { + bestArc = successor; + lengthMin = successor.getLength(); + } + } + } + if (bestArc == null) + { + throw new IllegalArgumentException("No arc from " + origin.getId()+ "to " + destination.getId()); + } + arcs.add(bestArc); + } + return new Path(graph, arcs); } @@ -196,7 +230,35 @@ public class Path { * @deprecated Need to be implemented. */ public boolean isValid() { - // TODO: + //it is empty + if(this.isEmpty()) return true; + //it contains a single node (without arcs) + else if((this.getArcs().isEmpty())) return true; + + else{ + List arcs = this.getArcs(); + + //nombre d arcs dans une variable + int sizeArcs = arcs.size(); + + if(arcs == null || arcs.size() < 1) return false; + + //the first arc has for origin the origin of the path + if(arcs.get(0).getOrigin().equals(origin)) + { + /*for two consecutive + * arcs, the destination of the first one is the origin of the second one */ + for(int i = 0; i < sizeArcs-1; i++) + { + + if(!(arcs.get(i).getDestination().equals(arcs.get(i+1).getOrigin()))) return false; + } + + return true; + } + } + + return false; } @@ -224,13 +286,8 @@ public class Path { * @deprecated Need to be implemented. */ public double getTravelTime(double speed) { -<<<<<<< HEAD return getLength() * 3600.0 / (speed * 1000.0); -======= - // TODO: - return getLength() * 3600.0 / (speed * 1000.0); ->>>>>>> refs/remotes/origin/main } /**