From ce8a801322a81f263989045a3a04de218571f08a Mon Sep 17 00:00:00 2001 From: AngeHerman Date: Mon, 8 Apr 2024 13:54:28 +0200 Subject: [PATCH] Comments added for java doc --- .../fr/u_paris/gla/project/idfm/BifStop.java | 11 +- .../project/idfm/CSVSchedulesProvider.java | 54 +++-- .../fr/u_paris/gla/project/idfm/Stop.java | 44 ++-- .../gla/project/idfm/TraceDescription.java | 15 +- .../u_paris/gla/project/idfm/Transport.java | 218 +++++++++--------- 5 files changed, 179 insertions(+), 163 deletions(-) diff --git a/src/main/java/fr/u_paris/gla/project/idfm/BifStop.java b/src/main/java/fr/u_paris/gla/project/idfm/BifStop.java index cc81298..098337f 100644 --- a/src/main/java/fr/u_paris/gla/project/idfm/BifStop.java +++ b/src/main/java/fr/u_paris/gla/project/idfm/BifStop.java @@ -1,11 +1,18 @@ package fr.u_paris.gla.project.idfm; +/** + * A representation of a stop with the bifurcation that is needed. All stops + * have a list of bifstop called connected. BifStop is just composed of a + * connected stop and the bifurcation used to go from the first stop to the + * connected one. + */ public class BifStop { + // The bifurcation public int bifurc; public Stop stop; - public BifStop(int bif, Stop s){ + public BifStop(int bif, Stop stop){ bifurc = bif; - stop = s; + this.stop = stop; } } diff --git a/src/main/java/fr/u_paris/gla/project/idfm/CSVSchedulesProvider.java b/src/main/java/fr/u_paris/gla/project/idfm/CSVSchedulesProvider.java index b06035b..9f5f907 100644 --- a/src/main/java/fr/u_paris/gla/project/idfm/CSVSchedulesProvider.java +++ b/src/main/java/fr/u_paris/gla/project/idfm/CSVSchedulesProvider.java @@ -10,16 +10,9 @@ import java.time.LocalDateTime; import java.text.NumberFormat; import java.util.*; -import org.apache.commons.lang3.ObjectUtils.Null; public class CSVSchedulesProvider { private static final DateTimeFormatter HOUR_MINUTE_FORMATTER = ScheduleFormat.getTimeFormatter(); - // private static final HashMap timings = new HashMap(){{ - // put("Bus", new int[]{45, 50}); - // put("Funicular", new int[]{120, 70}); - // put("Tram", new int[]{72, 60}); - // put("Rail", new int[]{120, 60,50}); - // put("Subway", new int[]{50, 45}); - // }}; + private static final HashMap timings = new HashMap(){{ put("Bus", new int[]{11, 8, 7}); put("Funicular", new int[]{15, 25, 20}); @@ -28,8 +21,8 @@ public class CSVSchedulesProvider { put("Subway", new int[]{4, 2, 6,3,3,4}); }}; + // Time between 2 passages for the transports with a new type we don't know yet private static int DEFAULT_TIMING = 6; - // private static int MINUTES_BEFORE_LAST_DEPARTURE = 6; private static final NumberFormat MINUTES_SECOND_FORMATTER = NumberFormat .getInstance(Locale.ENGLISH); @@ -75,16 +68,15 @@ public class CSVSchedulesProvider { } + /** + * Move to the the nextDescription of a Transport line + */ private void skipToNextDescription() { if (this.currentDescription.hasNext()) { TraceDescription description = this.currentDescription.next(); - // hours_count = 0; - // current_time = description.first; - // last_time = description.last; + currentHour = convertIntoLocalDateTime(description.first); lastHour = convertIntoLocalDateTime(description.last); - // System.out.println("** SkipTOnextD: last hour est "+lastHour+" first est "+currentHour+" desc.last est "+description.last+ " et desc.first est "+description.first); - if(lastHour.compareTo(currentHour) <= 0){ lastHour = lastHour.plusDays(1); @@ -98,18 +90,18 @@ public class CSVSchedulesProvider { } } + + /** + * Move to the next Transport line + */ private void skipToNextTransport() { if (this.currentTransport.hasNext()) { Transport transport = this.currentTransport.next(); this.line[ScheduleFormat.LINE_INDEX] = transport.name; - // System.out.println("Changement de transport"+transport.name); - // transport.buildBifurcation(); current_tansport_type = transport.type; this.currentDescription = transport.descriptions.iterator(); skipToNextDescription(); - } else { - } } @@ -122,6 +114,10 @@ public class CSVSchedulesProvider { } + /** + * Add random minutes for the next passage of a transport. + * The random minutes depends on the type of the transport + */ private void addRandomMinutes() { // System.out.println("** addM: AVANT: "+currentHour); currentHour = currentHour.plusMinutes(pickMinute(current_tansport_type)); @@ -131,14 +127,11 @@ public class CSVSchedulesProvider { // if(debut == 7) throw new IllegalArgumentException(); } - private static long calculateDuration(String debut, String dernier) { - LocalTime debutTime = LocalTime.parse(debut, HOUR_MINUTE_FORMATTER); - LocalTime dernierTime = LocalTime.parse(dernier, HOUR_MINUTE_FORMATTER); - - Duration difference = Duration.between(debutTime, dernierTime); - return difference.toMinutes(); - } - + /** + * + * @param transportType the type of a transport + * @return a random minute depending on the type of the transport + */ public static int pickMinute(String transportType) { if (!timings.containsKey(transportType)) { return DEFAULT_TIMING; @@ -149,9 +142,14 @@ public class CSVSchedulesProvider { return temps[indexAleatoire]; } - public static LocalDateTime convertIntoLocalDateTime(String heureMinute) { + /** + * + * @param hourMinute hour and minute representation. Ex: "14:03" + * @return a datetime of today but using hourMinute + */ + public static LocalDateTime convertIntoLocalDateTime(String hourMinute) { LocalDateTime aujourdHui = LocalDateTime.now(); - LocalTime time = LocalTime.parse(heureMinute, HOUR_MINUTE_FORMATTER); + LocalTime time = LocalTime.parse(hourMinute, HOUR_MINUTE_FORMATTER); return aujourdHui.withHour(time.getHour()).withMinute(time.getMinute()).withSecond(0).withNano(0); } diff --git a/src/main/java/fr/u_paris/gla/project/idfm/Stop.java b/src/main/java/fr/u_paris/gla/project/idfm/Stop.java index eabed68..0a0a2fa 100644 --- a/src/main/java/fr/u_paris/gla/project/idfm/Stop.java +++ b/src/main/java/fr/u_paris/gla/project/idfm/Stop.java @@ -5,45 +5,41 @@ import java.util.Map; import java.util.ArrayList; import java.util.HashMap; +/** + * A representation of a stop with its connected stop and which bifurcation + * is needed to go to each connected stop + */ public class Stop { Map connected = new HashMap<>(); public String name; - public Stop(String n){ - name = n; + public Stop(String name){ + this.name = name; } - // public boolean isStopConnected(String stopName) { - // // for (BifStop b : connected) { - // // if (b.stop.name.equals(stopName)) { - // // return true; - // // } - // // } - // // return false; - // return getStop(stopName) != null; - // } - - // public Stop getStop(String stopName) { - // // if (this.name.equals(stopName)) { - // // return this; - // // } - // // for (BifStop b : connected) { - // // if (b.stop.name.equals(stopName)) { - // // return b.stop; - // // } - // // } - // // return null; - // return transport.getStopFromIndex(stopName); - // } + /** + * Checks is stopName is connected to this one + * @param stopName + * @return True if stopName is connected to the current stop + */ public boolean isStopConnected(String stopName) { return connected.containsKey(stopName); } + /** + * Add Connected stop + * @param stop connected stop with the bifurcation needed + */ public void addConnectedStop(BifStop stop) { connected.put(stop.stop.name, stop); } + /** + * Return the connected stop with the name : stopName + * @param stopName + * @return the connected stop with the name : stopName + */ public BifStop getConnectedStop(String stopName) { return connected.get(stopName); } diff --git a/src/main/java/fr/u_paris/gla/project/idfm/TraceDescription.java b/src/main/java/fr/u_paris/gla/project/idfm/TraceDescription.java index 5f86d26..2de7424 100644 --- a/src/main/java/fr/u_paris/gla/project/idfm/TraceDescription.java +++ b/src/main/java/fr/u_paris/gla/project/idfm/TraceDescription.java @@ -3,6 +3,11 @@ package fr.u_paris.gla.project.idfm; import java.util.List; import java.util.ArrayList; +/** + * A representation of a transport description encompansing its first and last + * stop in all of its direction, the first and last schedule and all the + * bifurcation that direction takes. The description comes from the fourth column * of the stop csv file. + */ public class TraceDescription { public String from; public String to; @@ -10,11 +15,11 @@ public class TraceDescription { public String last; List bifurcation = new ArrayList<>(); - public TraceDescription(String fr,String t, String f, String l){ - from = fr; - to = t; - first = f; - last = l; + public TraceDescription(String from,String to, String first, String last){ + this.from = from; + this.to = to; + this.first = first; + this.last = last; } @Override diff --git a/src/main/java/fr/u_paris/gla/project/idfm/Transport.java b/src/main/java/fr/u_paris/gla/project/idfm/Transport.java index 5dd2404..31fe9ed 100644 --- a/src/main/java/fr/u_paris/gla/project/idfm/Transport.java +++ b/src/main/java/fr/u_paris/gla/project/idfm/Transport.java @@ -9,30 +9,39 @@ import java.util.HashSet; import java.util.LinkedHashMap; import java.util.AbstractMap.SimpleEntry; - +/** + * Representation of a line with its description and stops + */ public class Transport { + //All the stops of the line Map stopsMap = new HashMap<>(); public String name; public String type; + + //All the line descriptions (directions and schedules) List descriptions = new ArrayList<>(); - public Transport(String n,String t_type){ - name = n; - type = t_type; + public Transport(String name,String type){ + this.name = name; + this.type = type; } + + /** + * Build the bifurcation for all the descriptions + */ public void buildBifurcation(){ - int found = 0; + // int found = 0; for(TraceDescription d : descriptions){ // System.out.println("Debut est "+d.first); Stop debut = stopsMap.get(d.from); Stop fin = stopsMap.get(d.to); if (debut != null && fin != null) { - SimpleEntry> sol = roadToLast(debut.name, debut.name, fin.name, new ArrayList(), new ArrayList()); + SimpleEntry> sol = roadToLast(debut.name, fin.name, new ArrayList(), new ArrayList()); if (sol.getKey()) { - found++; + // found++; d.bifurcation = sol.getValue(); } } @@ -40,23 +49,33 @@ public class Transport { // System.out.println("J'en ai trouvé "+found); } + + /** + * Build the bifurcation for all the descriptions but optimized + */ public void buildBifurcationOptimzed() { - int found = 0; - for (TraceDescription d : descriptions) { - Stop debut = stopsMap.get(d.from); - Stop fin = stopsMap.get(d.to); - if (debut != null && fin != null) { - Set alreadyVisited = new HashSet<>(); // Utiliser un HashSet pour les arrêts déjà visités - SimpleEntry> sol = roadToLastOptimized(debut.name, debut.name, fin.name, alreadyVisited, new ArrayList()); - if (sol.getKey()) { - found++; - d.bifurcation = sol.getValue(); + // int found = 0; + for (TraceDescription d : descriptions) { + Stop debut = stopsMap.get(d.from); + Stop fin = stopsMap.get(d.to); + if (debut != null && fin != null) { + Set alreadyVisited = new HashSet<>(); + SimpleEntry> sol = roadToLastOptimized(debut.name, fin.name, alreadyVisited, new ArrayList()); + if (sol.getKey()) { + // found++; + d.bifurcation = sol.getValue(); + } } } + // System.out.println("J'en ai trouvé " + found); } - // System.out.println("J'en ai trouvé " + found); -} + + /** + * Check if the stop is a terminus + * @param stop the name of a Stop + * @return True if the stop is a terminus + */ public boolean isTerminus(String stop){ for(TraceDescription t: descriptions){ if(stop.equals(t.first) || stop.equals(t.last)) @@ -65,15 +84,26 @@ public class Transport { return false; } - public SimpleEntry > roadToLast(String currentStop,String first, String last, List alreadyVisited, List bif){ + + /** + * Find the road from the currentStop to the last stop + * @param currentStop the current stop we are visiting + * @param last The last stop we are trying to go to + * @param alreadyVisited All the stop we already have visisted + * @param bif All the bifurcation encountered from the first stop to the current + * one + * @return True and the bifurcation if we found our road to the last stop and + * false if we didn't + */ + public SimpleEntry > roadToLast(String currentStop, String last, List alreadyVisited, List bifurcation){ if(currentStop.equals(last)){ - return new SimpleEntry<>(true,bif); + return new SimpleEntry<>(true,bifurcation); } //Checker if the current stop is the bad terminus if(isTerminus(currentStop)){ - return new SimpleEntry<>(false,bif); + return new SimpleEntry<>(false,null); } List visitedCopy = new ArrayList<>(alreadyVisited); visitedCopy.add(currentStop); @@ -82,13 +112,13 @@ public class Transport { List > > solutions = new ArrayList<>(); for(BifStop b: current.connected.values()){ if(!visitedCopy.contains(b.stop.name)){ - List bifCopy = new ArrayList<>(bif); + List bifCopy = new ArrayList<>(bifurcation); if(b.bifurc!= 0) bifCopy.add(b.bifurc); - solutions.add(roadToLast(b.stop.name, first, last, visitedCopy, bifCopy)); + solutions.add(roadToLast(b.stop.name, last, visitedCopy, bifCopy)); } } - //Todo: Send a list on list of integer in case there is a lot of path for the same direction + //TODo: Send a list on list of integer in case there is a lot of path for the same direction List bifSol = new ArrayList<>(); boolean trouve = false; for(SimpleEntry> se: solutions){ @@ -100,47 +130,64 @@ public class Transport { return new SimpleEntry<>(trouve,bifSol) ; } - public SimpleEntry> roadToLastOptimized(String currentStop, String first, String last, Set alreadyVisited, List bif) { - if (currentStop.equals(last)) { - return new SimpleEntry<>(true, bif); - } - // Checker if the current stop is the bad terminus - if (isTerminus(currentStop)) { - return new SimpleEntry<>(false, bif); - } + /** + * Find the road from the currentStop to the last stop + * @param currentStop the current stop we are visiting + * @param last The last stop we are trying to go to + * @param alreadyVisited All the stop we already have visisted + * @param bif All the bifurcation encountered from the first stop to the current + * one + * @return True and the bifurcation if we found our road to the last stop and + * false if we didn't + */ + public SimpleEntry> roadToLastOptimized(String currentStop, String last, Set alreadyVisited, List bifurcation) { + if (currentStop.equals(last)) { + return new SimpleEntry<>(true, bifurcation); + } - alreadyVisited.add(currentStop); + // Checker if the current stop is the bad terminus + if (isTerminus(currentStop)) { + return new SimpleEntry<>(false, null); + } - Stop current = stopsMap.get(currentStop); - List>> solutions = new ArrayList<>(); - for (BifStop b : current.connected.values()) { - if (!alreadyVisited.contains(b.stop.name)) { - List bifCopy = new ArrayList<>(bif); - if (b.bifurc != 0) { - bifCopy.add(b.bifurc); + alreadyVisited.add(currentStop); + + Stop current = stopsMap.get(currentStop); + List>> solutions = new ArrayList<>(); + for (BifStop b : current.connected.values()) { + if (!alreadyVisited.contains(b.stop.name)) { + List bifCopy = new ArrayList<>(bifurcation); + if (b.bifurc != 0) { + bifCopy.add(b.bifurc); + } + solutions.add(roadToLastOptimized(b.stop.name, last, alreadyVisited, bifCopy)); } - solutions.add(roadToLastOptimized(b.stop.name, first, last, alreadyVisited, bifCopy)); } + + // Todo: Send a list on list of integer in case there is a lot of path for the same direction + List bifSol = new ArrayList<>(); + boolean trouve = false; + for (SimpleEntry> se : solutions) { + if (se.getKey()) { + trouve = true; + bifSol = se.getValue(); + break; // Exit loop if a solution is found + } + } + + alreadyVisited.remove(currentStop); // Remove current stop from visited after processing + + return new SimpleEntry<>(trouve, bifSol); } - // Todo: Send a list on list of integer in case there is a lot of path for the same direction - List bifSol = new ArrayList<>(); - boolean trouve = false; - for (SimpleEntry> se : solutions) { - if (se.getKey()) { - trouve = true; - bifSol = se.getValue(); - break; // Exit loop if a solution is found - } - } - - alreadyVisited.remove(currentStop); // Remove current stop from visited after processing - - return new SimpleEntry<>(trouve, bifSol); -} - + /** + * Connect 2 stops (start, end) and add them in stopMap if they are not already in + * @param start The name of a stop + * @param end The name of the stop connected to the start + * @param bifurcation The bifurcation taken to go from start stop to end stop + */ public void addStop(String start, String end, int bifurcation){ Stop startStop = stopsMap.computeIfAbsent(start, Stop::new); Stop endStop = stopsMap.computeIfAbsent(end, Stop::new); @@ -149,58 +196,21 @@ public class Transport { startStop.addConnectedStop(connectedStop); } - // public void addStop(String start,String end,int bifurcation){ - // Stop start_stop; - // Stop end_stop; - // if (!stopsMap.containsKey(start)){ - // start_stop = new Stop(start); - // stopsMap.put(start, start_stop); - // }else{ - // start_stop = stopsMap.get(start); - // } - - // if (!stopsMap.containsKey(end)){ - // end_stop = new Stop(end); - // stopsMap.put(end, end_stop); - // }else{ - // end_stop = stopsMap.get(end); - // } - // start_stop.connected.add( new BifStop(bifurcation,end_stop) ); - - // } - - /** - * - * @param stopsMap - * @param start - * @param end - * @return true if the start stop has been added before the end stop + * Print every stops of the line and its connections */ - public boolean startBeforeEnd( String start, String end) { - for (String key : stopsMap.keySet()) { - if (key.equals(start)) { - return true; - } - if (key.equals(end)) { - return false; + public void printAllConnectionStops() { + System.out.println("Affichage des couples (stop, next du stop):"); + for (Map.Entry entry : stopsMap.entrySet()) { + Stop stop = entry.getValue(); + System.out.println("Stop: " + stop.name); + System.out.println("Next:"); + for (BifStop nextEntry : stop.connected.values()) { + System.out.println(nextEntry.bifurc + ": " + nextEntry.stop.name); } } - return false; } - // public void printAllConnectionStops() { - // System.out.println("Affichage des couples (stop, next du stop):"); - // for (Map.Entry entry : stopsMap.entrySet()) { - // Stop stop = entry.getValue(); - // System.out.println("Stop: " + stop.name); - // System.out.println("Next:"); - // for (BifStop nextEntry : stop.connected) { - // System.out.println(nextEntry.bifurc + ": " + nextEntry.stop.name); - // } - // } - // } - /** * Add all the description to the current one