diff --git a/src/main/java/fr/u_paris/gla/project/itinerary/Graph.java b/src/main/java/fr/u_paris/gla/project/itinerary/Graph.java index 9f263e8..8ca90af 100644 --- a/src/main/java/fr/u_paris/gla/project/itinerary/Graph.java +++ b/src/main/java/fr/u_paris/gla/project/itinerary/Graph.java @@ -1,5 +1,6 @@ package fr.u_paris.gla.project.itinerary; +import java.util.HashMap; import java.util.Map; import java.util.Set; diff --git a/src/main/java/fr/u_paris/gla/project/itinerary/GraphNode.java b/src/main/java/fr/u_paris/gla/project/itinerary/GraphNode.java index 2b9e2a6..625e24c 100644 --- a/src/main/java/fr/u_paris/gla/project/itinerary/GraphNode.java +++ b/src/main/java/fr/u_paris/gla/project/itinerary/GraphNode.java @@ -2,6 +2,6 @@ package fr.u_paris.gla.project.itinerary; import java.util.List; -public interface GraphNode{ +public interface GraphNode { int getId(); } diff --git a/src/main/java/fr/u_paris/gla/project/itinerary/Main.java b/src/main/java/fr/u_paris/gla/project/itinerary/Main.java index f141637..eb0266d 100644 --- a/src/main/java/fr/u_paris/gla/project/itinerary/Main.java +++ b/src/main/java/fr/u_paris/gla/project/itinerary/Main.java @@ -48,88 +48,50 @@ public class Main{ return new double[] {Double.parseDouble(stringCoords[0]), Double.parseDouble(stringCoords[1])}; } - private static List getStops(HashSet nodes, String name, String gps) { - double []coords = getCoords(gps); - return nodes.stream().filter( - stop -> stop.getName().equals(name) - && stop.getLatitude() == coords[0] - && stop.getLongitude() == coords[1] - ).toList(); - } - - /* - private static Stop getOrCreateStop(HashSet nodes, String name, String gps, String lineId) { - List stops = getStops(nodes, name, gps); - if (stops.isEmpty()) { + private static Stop getOrCreateStop(HashSet nodes, HashMap tmp, String name, String gps, String lineId) { + Stop stop = tmp.get(gps); + if (stop == null) { double[] coords = getCoords(gps); Stop newStop = new Stop(lineId, name, coords[0], coords[1]); nodes.add(newStop); + tmp.put(gps, newStop); return newStop; } - else if (stops.size() == 1) { - stops.get(0).addLine(lineId); - return stops.get(0); - } else { - LOGGER.severe("Error in graph creation"); - return null; + stop.addLine(lineId); + return stop; } } public static int lineNB = 0; - private static void addLine(String[] line, HashSet nodes, HashMap> connections) { - // TODO Cas particulier ou une ligne a le même nom - - Stop fromStop = getOrCreateStop(nodes, line[IDFM_TRACE_FROM_INDEX], line[IDFM_TRACE_FROM_GPS_INDEX], line[IDFM_TRACE_ID_INDEX]); - Stop toStop = getOrCreateStop(nodes, line[IDFM_TRACE_TO_INDEX], line[IDFM_TRACE_TO_GPS_INDEX], line[IDFM_TRACE_ID_INDEX]); - - if (fromStop == null || toStop == null) - return; + private static void addLine(String[] line, HashSet nodes, HashMap tmp, HashMap> connections) { + Stop fromStop = getOrCreateStop(nodes, tmp, line[IDFM_TRACE_FROM_INDEX], line[IDFM_TRACE_FROM_GPS_INDEX], line[IDFM_TRACE_ID_INDEX]); + Stop toStop = getOrCreateStop(nodes, tmp, line[IDFM_TRACE_TO_INDEX], line[IDFM_TRACE_TO_GPS_INDEX], line[IDFM_TRACE_ID_INDEX]); String[] timeString = line[IDFM_TRACE_TIME_INDEX].split(":"); int time = Integer.parseInt(timeString[0]) * 60 + Integer.parseInt(timeString[1]); - Connection connection = new Connection(toStop.getId(), line[IDFM_TRACE_ID_INDEX], Double.parseDouble(line[IDFM_TRACE_DISTANCE_INDEX]), time); - - connections.computeIfAbsent(fromStop.getId(), k -> new HashSet<>()).add(connection); - -/* - Set linesConnections = connections.get(fromStop.getId()); - - String[] timeString = line[IDFM_TRACE_TIME_INDEX].split(":"); - int time = Integer.parseInt(timeString[0]) * 60 + Integer.parseInt(timeString[1]); - Connection connection = new Connection(toStop.getId(), line[IDFM_TRACE_ID_INDEX], Double.parseDouble(line[IDFM_TRACE_DISTANCE_INDEX]), time); - if (linesConnections == null) - connections.put(fromStop.getId(), new HashSet<>(List.of(connection))); - else - linesConnections.add(connection); - - if (lineNB++ % 1000 == 0) { - LOGGER.log(Level.INFO,"Added line " + line[IDFM_TRACE_ID_INDEX] + " from " + line[IDFM_TRACE_FROM_INDEX] + " to " + line[IDFM_TRACE_TO_INDEX] + ", line " + lineNB); - } + Connection connection = new Connection(toStop, line[IDFM_TRACE_ID_INDEX], Double.parseDouble(line[IDFM_TRACE_DISTANCE_INDEX]), time); + connections.computeIfAbsent(fromStop, k -> new HashSet<>()).add(connection); } - */ public static void main(String[] args){ - // TODO ajouter option du nom en argument if (args.length != 0) { LOGGER.severe("Invalid command line. Target file names are in the main file for now."); return; } - /* try { HashSet nodes = new HashSet<>(); - HashMap> connections = new HashMap<>(); + HashMap> connections = new HashMap<>(); + HashMap tmp = new HashMap<>(); CSVTools.readCSVFromFile(TRACE_FILE_NAME, - (String[] line) -> addLine(line, nodes, connections)); + (String[] line) -> addLine(line, nodes, tmp, connections)); Graph graph = new Graph<>(nodes, connections); } catch (IOException e) { LOGGER.log(Level.SEVERE, "Error while reading the line paths", e); } - - */ } } diff --git a/src/main/java/fr/u_paris/gla/project/itinerary/Stop.java b/src/main/java/fr/u_paris/gla/project/itinerary/Stop.java index 2cbd2db..fc9473e 100644 --- a/src/main/java/fr/u_paris/gla/project/itinerary/Stop.java +++ b/src/main/java/fr/u_paris/gla/project/itinerary/Stop.java @@ -9,8 +9,11 @@ public class Stop implements GraphNode { private final int id; private final Set lines; + private final String name; + private final double latitude; + private final double longitude; public Stop(String line, String name, double latitude, double longitude) {