fix csv export + basic hours

This commit is contained in:
Mylloon 2024-03-29 18:48:34 +01:00
parent 682fca4f9c
commit 4661eef08f
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
3 changed files with 71 additions and 54 deletions

View file

@ -36,15 +36,15 @@ public final class CSVStreamProvider {
/** Distance to reach maximal speed in km */ /** Distance to reach maximal speed in km */
private static final double TWO_ACCELERATION_DISTANCE = 0.2; private static final double TWO_ACCELERATION_DISTANCE = 0.2;
private String[] line = new String[NetworkFormat.NUMBER_COLUMNS]; private final String[] line = new String[NetworkFormat.NUMBER_COLUMNS];
private Iterator<TraceEntry> currentTrace; private final Iterator<TraceEntry> currentTrace;
private Iterator<List<StopEntry>> currentPath = Collections.emptyIterator(); private Iterator<List<StopEntry>> currentPath = Collections.emptyIterator();
private Iterator<StopEntry> currentSegmentStart = Collections.emptyIterator(); private Iterator<StopEntry> currentSegmentStart = Collections.emptyIterator();
private Iterator<StopEntry> currentSegmentEnd = Collections.emptyIterator(); private Iterator<StopEntry> currentSegmentEnd = Collections.emptyIterator();
Map<StopEntry, Set<StopEntry>> lineSegments = new HashMap<>(); Map<StopEntry, Set<StopEntry>> lineSegments = new HashMap<>();
// The transport id with its value // The transport id with its value
private Map<String, Transport> transports; private final Map<String, Transport> transports;
List <TraceDescription> descriptions = new ArrayList<>(); List <TraceDescription> descriptions = new ArrayList<>();
@ -111,10 +111,12 @@ public final class CSVStreamProvider {
return; return;
} }
TraceEntry trace = this.currentTrace.next(); TraceEntry trace = this.currentTrace.next();
traceId = trace.id;
traceType = trace.type; this.traceId = trace.id;
descriptions.clear(); this.traceType = trace.type;
descriptions.addAll(trace.descriptions); this.descriptions.clear();
this.descriptions.addAll(trace.descriptions);
this.currentPath = trace.getPaths().iterator(); this.currentPath = trace.getPaths().iterator();
this.line[NetworkFormat.LINE_INDEX] = trace.lname; this.line[NetworkFormat.LINE_INDEX] = trace.lname;
this.lineSegments.clear(); this.lineSegments.clear();
@ -128,7 +130,7 @@ public final class CSVStreamProvider {
} while (currentSegmentStart == null); } while (currentSegmentStart == null);
} }
public String[] next() { public String[][] next() {
if (!this.onNext) { if (!this.onNext) {
skipToNext(); skipToNext();
} }
@ -146,7 +148,7 @@ public final class CSVStreamProvider {
this.line[NetworkFormat.VARIANT_INDEX] = Integer this.line[NetworkFormat.VARIANT_INDEX] = Integer
.toString(bifurcation); .toString(bifurcation);
fillTransports(bifurcation); fillTransports(bifurcation);
return Arrays.copyOf(this.line, this.line.length); return new String[][]{Arrays.copyOf(this.line, this.line.length)};
} }
/** @param stop1 /** @param stop1

View file

@ -15,13 +15,14 @@ public class CSVStreamSchedulesProvider {
private final String[] line = new String[ScheduleFormat.NUMBER_COLUMNS]; private final String[] line = new String[ScheduleFormat.NUMBER_COLUMNS];
private final Iterator<TraceEntry> currentTrace; private final Iterator<TraceEntry> currentTrace;
private final Map<String, Transport> transports;
Map<StopEntry, Set<StopEntry>> lineSegments = new HashMap<>();
private Iterator<List<StopEntry>> currentPath = Collections.emptyIterator(); private Iterator<List<StopEntry>> currentPath = Collections.emptyIterator();
private Iterator<StopEntry> currentSegmentStart = Collections.emptyIterator(); private Iterator<StopEntry> currentSegmentStart = Collections.emptyIterator();
private Iterator<StopEntry> currentSegmentEnd = Collections.emptyIterator(); private Iterator<StopEntry> currentSegmentEnd = Collections.emptyIterator();
Map<StopEntry, Set<StopEntry>> lineSegments = new HashMap<>();
private final Map<String, Transport> transports;
private StopEntry start = null; private StopEntry start = null;
private StopEntry end = null; private StopEntry end = null;
private boolean hasNext = false; private boolean hasNext = false;
private boolean onNext = false; private boolean onNext = false;
@ -61,7 +62,7 @@ public class CSVStreamSchedulesProvider {
this.lineSegments.putIfAbsent(this.start, new HashSet<>()); this.lineSegments.putIfAbsent(this.start, new HashSet<>());
this.end = this.currentSegmentEnd.next(); this.end = this.currentSegmentEnd.next();
} while (this.lineSegments.get(this.start).contains(this.end) } while (this.lineSegments.get(this.start).contains(this.end)
&& this.currentSegmentEnd.hasNext()); && this.currentSegmentEnd.hasNext());
if (!this.lineSegments.get(this.start).contains(this.end)) { if (!this.lineSegments.get(this.start).contains(this.end)) {
this.lineSegments.get(this.start).add(this.end); this.lineSegments.get(this.start).add(this.end);
this.onNext = true; this.onNext = true;
@ -84,36 +85,8 @@ public class CSVStreamSchedulesProvider {
} }
TraceEntry trace = this.currentTrace.next(); TraceEntry trace = this.currentTrace.next();
this.currentPath = trace.getPaths().iterator(); this.currentPath = trace.getPaths().iterator();
// Retrieve transports informations
Transport transport = this.transports.get(trace.id);
// Build bifurcations
transport.buildBifurcation();
// Iterate over possibilites
transport.descriptions.forEach(desc -> {
// TODO: On doit ajouter toutes les horaires au lieu de mettre que la première
Arrays.stream(new String[]{desc.first}).forEach(time -> {
// Write line name
this.line[ScheduleFormat.LINE_INDEX] = trace.lname;
// Write bifurcation
this.line[ScheduleFormat.TRIP_SEQUENCE_INDEX] = desc.bifurcation.toString();
// Write terminus
this.line[ScheduleFormat.TERMINUS_INDEX] = desc.from;
// Write time TODO: Pourquoi ça marche pas ??
this.line[ScheduleFormat.TIME_INDEX] = time;
// Test
// System.out.println("TEST CSV: " + Arrays.toString(this.line));
this.lineSegments.clear();
});
});
} }
List<StopEntry> path = this.currentPath.next(); List<StopEntry> path = this.currentPath.next();
this.currentSegmentEnd = path.iterator(); this.currentSegmentEnd = path.iterator();
if (this.currentSegmentEnd.hasNext()) { if (this.currentSegmentEnd.hasNext()) {
@ -123,22 +96,63 @@ public class CSVStreamSchedulesProvider {
} while (currentSegmentStart == null); } while (currentSegmentStart == null);
} }
public String[] next() { public String[][] next() {
if (!this.onNext) { if (!this.onNext) {
skipToNext(); skipToNext();
} }
this.onNext = false; this.onNext = false;
this.line[ScheduleFormat.TIME_INDEX] = null; /**/
/* if (!this.currentTrace.hasNext()) {
* this.line[ScheduleFormat.DISTANCE_INDEX] = return new String[][]{Arrays.copyOf(this.line, this.line.length)};
* NumberFormat.getInstance(Locale.ENGLISH) }
* .format(distance); TraceEntry trace = this.currentTrace.next();
* this.line[ScheduleFormat.VARIANT_INDEX] = Integer
* .toString(this.lineSegments.get(this.start).size() - 1);
*/
return Arrays.copyOf(this.line, this.line.length); // Retrieve transports informations
Transport transport = this.transports.get(trace.id);
// Build bifurcations
transport.buildBifurcation();
// Store all lines for this iteration
List<String[]> allLines = new ArrayList<>();
// Iterate over possibilites
transport.descriptions.forEach(desc -> {
// TODO: On doit ajouter toutes les horaires au lieu de mettre que la première
Arrays.stream(findTimes(desc.first, desc.last)).forEach(time -> {
// Create a new line array
String[] newLine = Arrays.copyOf(this.line, this.line.length);
// Write line name
newLine[ScheduleFormat.LINE_INDEX] = trace.lname;
// Write bifurcation
newLine[ScheduleFormat.TRIP_SEQUENCE_INDEX] = desc.bifurcation.toString();
// Write terminus
newLine[ScheduleFormat.TERMINUS_INDEX] = desc.from;
// Write time
newLine[ScheduleFormat.TIME_INDEX] = time;
// Add the new line to the list of lines for this iteration
allLines.add(Arrays.copyOf(newLine, newLine.length));
});
this.lineSegments.clear();
});
return allLines.toArray(new String[0][]);
}
private String[] findTimes(String start, String last) {
return new String[]{
start,
/* TODO: fill with random values each n minutes */
last,
};
} }
} }

View file

@ -11,6 +11,7 @@ import java.io.InputStreamReader;
import java.io.Reader; import java.io.Reader;
import java.net.URL; import java.net.URL;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -53,11 +54,11 @@ public final class CSVTools {
} }
public static void writeCSVToFile(String filename, public static void writeCSVToFile(String filename,
Stream<String[]> contentLineConsumer) throws IOException { Stream<String[][]> contentLinesConsumer) throws IOException {
try (FileWriter writer = new FileWriter(filename, StandardCharsets.UTF_8)) { try (FileWriter writer = new FileWriter(filename, StandardCharsets.UTF_8)) {
CSVWriterBuilder wBuilder = new CSVWriterBuilder(writer).withSeparator(';'); CSVWriterBuilder wBuilder = new CSVWriterBuilder(writer).withSeparator(';');
try (ICSVWriter csv = wBuilder.build()) { try (ICSVWriter csv = wBuilder.build()) {
contentLineConsumer.forEachOrdered(csv::writeNext); contentLinesConsumer.forEachOrdered(line -> Arrays.stream(line).forEach(csv::writeNext));
} }
} }
} }