Schedule parser
This commit is contained in:
parent
318a915b84
commit
37ced08d10
3 changed files with 121 additions and 5 deletions
|
@ -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<Integer> 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<Integer> getSchedules() {
|
||||
return this.schedules;
|
||||
}
|
||||
|
||||
public int getBifurcation() {
|
||||
return this.bifurcation;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -61,19 +61,23 @@ public class ItineraryCalculator {
|
|||
*/
|
||||
private static Stop getOrCreateStop(HashSet<Stop> nodes, HashMap<String, ArrayList<Stop>> tmp, String name, String gps, String lineId) {
|
||||
ArrayList<Stop> 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<Integer> bifurcations, int time, HashMap<String, ArrayList<Stop>> stopsHashSet, HashMap<Stop, Set<Connection>> connections, HashSet<Stop> processed){
|
||||
time = time%86400;
|
||||
//If the stop has already been processed, it is not reprocessed.
|
||||
if(processed.contains(current)) {return;}
|
||||
processed.add(current);
|
||||
|
||||
Set<Connection> neighborhood = connections.get(current);
|
||||
if(neighborhood == null) {return;}
|
||||
|
||||
|
||||
ArrayList<Connection> 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<String, ArrayList<Stop>> stopsHashSet, HashMap<Stop, Set<Connection>> connections) {
|
||||
|
||||
String line = input[0];
|
||||
|
||||
ArrayList<Integer> 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<Stop> 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);
|
||||
|
|
|
@ -85,4 +85,6 @@ public class Stop implements GraphNode {
|
|||
public void addLine(String s){
|
||||
lines.add(s);
|
||||
}
|
||||
|
||||
public Set<String> getLines() { return this.lines; }
|
||||
}
|
||||
|
|
Reference in a new issue