From 37ced08d103f4b25642148cce39411e9191b8096 Mon Sep 17 00:00:00 2001 From: Francois Date: Mon, 15 Apr 2024 17:11:35 +0200 Subject: [PATCH] Schedule parser --- .../gla/project/itinerary/Connection.java | 26 ++++- .../itinerary/ItineraryCalculator.java | 98 ++++++++++++++++++- .../u_paris/gla/project/itinerary/Stop.java | 2 + 3 files changed, 121 insertions(+), 5 deletions(-) diff --git a/src/main/java/fr/u_paris/gla/project/itinerary/Connection.java b/src/main/java/fr/u_paris/gla/project/itinerary/Connection.java index 1cafbec..8bb0107 100644 --- a/src/main/java/fr/u_paris/gla/project/itinerary/Connection.java +++ b/src/main/java/fr/u_paris/gla/project/itinerary/Connection.java @@ -1,5 +1,7 @@ package fr.u_paris.gla.project.itinerary; +import java.util.ArrayList; + public class Connection{ // Destination of the connection between the two stops private final Stop stop; @@ -11,11 +13,21 @@ public class Connection{ private final int time; - public Connection(Stop stop, String lineName, double distance, int time){ + private final ArrayList schedules; + + private final int bifurcation; + + public Connection(Stop stop, String lineName, double distance, int time, int bifurcation){ this.stop = stop; this.lineName=lineName; this.distance = distance; this.time = time; + this.schedules = new ArrayList<>(); + this.bifurcation = bifurcation; + } + + public Connection(Stop stop, String lineName, double distance, int time){ + this(stop, lineName, distance, time, 0); } @@ -34,4 +46,16 @@ public class Connection{ public Stop getStop() { return stop; } + + public void addSchedule(int hours) { + this.schedules.add(hours); + } + + public ArrayList getSchedules() { + return this.schedules; + } + + public int getBifurcation() { + return this.bifurcation; + } } diff --git a/src/main/java/fr/u_paris/gla/project/itinerary/ItineraryCalculator.java b/src/main/java/fr/u_paris/gla/project/itinerary/ItineraryCalculator.java index fcce253..ca72f53 100644 --- a/src/main/java/fr/u_paris/gla/project/itinerary/ItineraryCalculator.java +++ b/src/main/java/fr/u_paris/gla/project/itinerary/ItineraryCalculator.java @@ -61,19 +61,23 @@ public class ItineraryCalculator { */ private static Stop getOrCreateStop(HashSet nodes, HashMap> tmp, String name, String gps, String lineId) { ArrayList stopList = tmp.get(name); + double[] coords = getCoords(gps); + // First we search by name, and then compare the coordinates since multiple stations can have the same name. A margin of error is necessary since stops can have multiple GPS coordinates if (stopList != null) { for(Stop stop : stopList) { - double[] coords = getCoords(gps); + double dist = GPS.distance(coords[0], coords[1], stop.getLatitude(), stop.getLongitude()); - if(dist < ERROR_MARGIN) { + if(dist == 0) { stop.addLine(lineId); return stop; } + if(dist < ERROR_MARGIN) { + + } } } - double[] coords = getCoords(gps); Stop newStop = new Stop(lineId, name, coords[0], coords[1]); nodes.add(newStop); stopList = stopList == null ? new ArrayList<>() : stopList; @@ -97,11 +101,94 @@ public class ItineraryCalculator { String time0WithoutComma = timeString[0].replace(",", ""); int time = Integer.parseInt(time0WithoutComma) * 60 + Integer.parseInt(timeString[1]); - Connection connection = new Connection(toStop, line[IDFM_TRACE_ID_INDEX], Double.parseDouble(line[IDFM_TRACE_DISTANCE_INDEX]), time); + Connection connection = new Connection(toStop, line[IDFM_TRACE_ID_INDEX], Double.parseDouble(line[IDFM_TRACE_DISTANCE_INDEX]), time, Integer.parseInt(line[IDFM_TRACE_DERIV_INDEX])); connections.computeIfAbsent(fromStop, k -> new HashSet<>()).add(connection); } + private static void addScheduleRec(Stop current, Stop previous, String line, ArrayList bifurcations, int time, HashMap> stopsHashSet, HashMap> connections, HashSet processed){ + time = time%86400; + //If the stop has already been processed, it is not reprocessed. + if(processed.contains(current)) {return;} + processed.add(current); + + Set neighborhood = connections.get(current); + if(neighborhood == null) {return;} + + + ArrayList directions = new ArrayList<>(); + for(Connection n : neighborhood) { + if(n.getLineName().equals(line) + && (previous == null || !n.getStop().getName().equals(previous.getName())) + ) { + directions.add(n); + } + } + + if(directions.size() == 0) {return;} + + Stop next_stop = null; + if(directions.size() > 1) { + int bifurcation = bifurcations.size() == 0 ? 0 : bifurcations.get(0); + if(bifurcations.size() > 0) {bifurcations.remove(0);} + for(Connection d : directions) { + if(d.getBifurcation() == bifurcation) { + next_stop = d.getStop(); + break; + } + } + if(next_stop == null) { + return; + } + } + else { + next_stop = directions.get(0).getStop(); + if(directions.get(0).getBifurcation() != 0) { + if(bifurcations.size() > 0 && directions.get(0).getBifurcation() == bifurcations.get(0)){ + bifurcations.remove(0); + } + } + } + + for(Connection n : neighborhood) { + if(n.getStop() == next_stop) { + n.addSchedule(time); + time += n.getTime(); + addScheduleRec(next_stop, current, line, bifurcations, time, stopsHashSet, connections, processed); + return; + } + } + } + + private static void addSchedule(String[] input, HashMap> stopsHashSet, HashMap> connections) { + + String line = input[0]; + + ArrayList bifurcations = new ArrayList<>(); + if(!input[1].equals("[]")) { + String[] b = input[1].substring(1, input[1].length()-1).split(","); + bifurcations = new ArrayList<>(); + for(String n : b){ + bifurcations.add(Integer.parseInt(n.trim())); + } + } + + String name = input[2]; + + String[] timeString = input[3].split(":"); + int time = Integer.parseInt(timeString[0]) * 3600 + Integer.parseInt(timeString[1])*60; + + + ArrayList stops = stopsHashSet.get(name); + if(stops == null) {return;} + + for(Stop stop : stops) { + if(stop.getLines().contains(line)) { + addScheduleRec(stop, null, line, bifurcations, time, stopsHashSet, connections, new HashSet<>()); + } + } + } + public static void main(String[] args){ if (args.length != 0) { LOGGER.severe("Invalid command line. Target file names are in the main file for now."); @@ -116,6 +203,9 @@ public class ItineraryCalculator { CSVTools.readCSVFromFile(TRACE_FILE_NAME, (String[] line) -> addLine(line, nodes, tmp, connections)); + CSVTools.readCSVFromFile(HOURS_FILE_NAME, + (String[] line) -> addSchedule(line, tmp, connections)); + Stop porteivry = tmp.get("Porte d'Ivry").get(0); Stop repu = tmp.get("République").get(0); 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 98a2306..6d9169c 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 @@ -85,4 +85,6 @@ public class Stop implements GraphNode { public void addLine(String s){ lines.add(s); } + + public Set getLines() { return this.lines; } }