horraires done

This commit is contained in:
AngeHerman 2024-04-02 22:21:17 +02:00
parent 51a63e0606
commit 80d31927ae

View file

@ -2,9 +2,24 @@ package fr.u_paris.gla.project.idfm;
import fr.u_paris.gla.project.io.ScheduleFormat; import fr.u_paris.gla.project.io.ScheduleFormat;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.Duration;
import java.time.LocalDateTime;
// import java.time.format.ResolverStyle;
import java.text.NumberFormat; import java.text.NumberFormat;
import java.util.*; import java.util.*;
import org.apache.commons.lang3.ObjectUtils.Null;
public class CSVSchedulesProvider { public class CSVSchedulesProvider {
private static final DateTimeFormatter HOUR_MINUTE_FORMATTER = ScheduleFormat.getTimeFormatter();
// private static final HashMap<String, int[]> timings = new HashMap<String, int[]>(){{
// 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<String, int[]> timings = new HashMap<String, int[]>(){{ private static final HashMap<String, int[]> timings = new HashMap<String, int[]>(){{
put("Bus", new int[]{11, 8, 7}); put("Bus", new int[]{11, 8, 7});
put("Funicular", new int[]{15, 25, 20}); put("Funicular", new int[]{15, 25, 20});
@ -12,6 +27,9 @@ public class CSVSchedulesProvider {
put("Rail", new int[]{10, 11,15,12}); put("Rail", new int[]{10, 11,15,12});
put("Subway", new int[]{4, 2, 6,3,3,4}); put("Subway", new int[]{4, 2, 6,3,3,4});
}}; }};
private static int DEFAULT_TIMING = 6;
// private static int MINUTES_BEFORE_LAST_DEPARTURE = 6;
private static final NumberFormat MINUTES_SECOND_FORMATTER = NumberFormat private static final NumberFormat MINUTES_SECOND_FORMATTER = NumberFormat
.getInstance(Locale.ENGLISH); .getInstance(Locale.ENGLISH);
@ -23,12 +41,13 @@ public class CSVSchedulesProvider {
private final Iterator<Transport> currentTransport; private final Iterator<Transport> currentTransport;
private Iterator<TraceDescription> currentDescription = Collections.emptyIterator(); private Iterator<TraceDescription> currentDescription = Collections.emptyIterator();
// private Stop start = null; private String current_tansport_type = "";
// private Stop end = null; private LocalDateTime currentHour = null;
// private boolean hasNext = false; private LocalDateTime lastHour = null;
// private boolean onNext = false; private int debut = 0;
private int hours_count = 0;
private String last_hour = "";
/** /**
* Create the stream provider * Create the stream provider
@ -42,12 +61,16 @@ public class CSVSchedulesProvider {
} }
private void skipToNext() { private void skipToNext() {
if (hours_count == 2){ if(currentHour == null || lastHour == null){
skipToNextTransport();
}else if(currentHour.compareTo(lastHour) < 0){
// System.out.println("**Skip: Le current hour est plus petit "+currentHour+"|||"+lastHour);
addRandomMinutes();
}else if (currentHour.compareTo(lastHour) >= 0) {
// System.out.println("**Skip: Le current hour est plus grand "+currentHour+"|||"+lastHour);
skipToNextDescription(); skipToNextDescription();
hours_count = 0; }
}else if(hours_count == 1){ else if (!this.currentDescription.hasNext()) {
this.line[ScheduleFormat.TIME_INDEX] = last_hour;
}else if (!this.currentDescription.hasNext()) {
skipToNextTransport(); skipToNextTransport();
} }
@ -56,15 +79,21 @@ public class CSVSchedulesProvider {
private void skipToNextDescription() { private void skipToNextDescription() {
if (this.currentDescription.hasNext()) { if (this.currentDescription.hasNext()) {
TraceDescription description = this.currentDescription.next(); TraceDescription description = this.currentDescription.next();
hours_count = 0; // hours_count = 0;
// this.start = new Stop(description.from); // current_time = description.first;
// this.end = new Stop(description.to); // 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);
}
this.line[ScheduleFormat.TERMINUS_INDEX] = description.from; this.line[ScheduleFormat.TERMINUS_INDEX] = description.from;
this.line[ScheduleFormat.TRIP_SEQUENCE_INDEX] = description.bifurcation.toString(); this.line[ScheduleFormat.TRIP_SEQUENCE_INDEX] = description.bifurcation.toString();
this.line[ScheduleFormat.TIME_INDEX] = description.first; this.line[ScheduleFormat.TIME_INDEX] = currentHour.format(HOUR_MINUTE_FORMATTER);
last_hour = description.last;
// this.onNext = true;
// this.hasNext = true;
}else{ }else{
skipToNextTransport(); skipToNextTransport();
} }
@ -74,12 +103,14 @@ public class CSVSchedulesProvider {
if (this.currentTransport.hasNext()) { if (this.currentTransport.hasNext()) {
Transport transport = this.currentTransport.next(); Transport transport = this.currentTransport.next();
this.line[ScheduleFormat.LINE_INDEX] = transport.name; this.line[ScheduleFormat.LINE_INDEX] = transport.name;
// System.out.println("Changement de transport"+transport.name);
// transport.buildBifurcation(); // transport.buildBifurcation();
current_tansport_type = transport.type;
this.currentDescription = transport.descriptions.iterator(); this.currentDescription = transport.descriptions.iterator();
skipToNextDescription(); skipToNextDescription();
} else { } else {
// this.onNext = true;
// this.hasNext = false;
} }
} }
@ -88,35 +119,41 @@ public class CSVSchedulesProvider {
return null; return null;
} }
skipToNext(); skipToNext();
hours_count ++;
return new String[][]{Arrays.copyOf(this.line, this.line.length)}; return new String[][]{Arrays.copyOf(this.line, this.line.length)};
// if (!this.currentTransport.hasNext()) {
// return new String[][]{Arrays.copyOf(this.line, this.line.length)};
// }
// Transport transport = this.currentTransport.next();
// // Store all lines for this iteration
// List<String[]> allLines = new ArrayList<>();
// transport.descriptions.forEach(desc -> {
// Arrays.stream(findTimes(desc.first, desc.last)).forEach(time -> {
// String[] newLine = Arrays.copyOf(this.line, this.line.length);
// newLine[ScheduleFormat.LINE_INDEX] = transport.name;
// newLine[ScheduleFormat.TRIP_SEQUENCE_INDEX] = desc.bifurcation.toString();
// newLine[ScheduleFormat.TERMINUS_INDEX] = desc.to;
// newLine[ScheduleFormat.TIME_INDEX] = time;
// allLines.add(Arrays.copyOf(newLine, newLine.length));
// });
// });
// return allLines.toArray(new String[0][]);
} }
private String[] findTimes(String start, String last) { private void addRandomMinutes() {
return new String[]{ // System.out.println("** addM: AVANT: "+currentHour);
start, currentHour = currentHour.plusMinutes(pickMinute(current_tansport_type));
last, this.line[ScheduleFormat.TIME_INDEX] = currentHour.format(HOUR_MINUTE_FORMATTER);
}; // System.out.println("** addM: APRES: "+currentHour);
// debut ++;
// 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();
}
public static int pickMinute(String transportType) {
if (!timings.containsKey(transportType)) {
return DEFAULT_TIMING;
}
int[] temps = timings.get(transportType);
Random random = new Random();
int indexAleatoire = random.nextInt(temps.length);
return temps[indexAleatoire];
}
public static LocalDateTime convertIntoLocalDateTime(String heureMinute) {
LocalDateTime aujourdHui = LocalDateTime.now();
LocalTime time = LocalTime.parse(heureMinute, HOUR_MINUTE_FORMATTER);
return aujourdHui.withHour(time.getHour()).withMinute(time.getMinute()).withSecond(0).withNano(0);
} }
} }