From 4661eef08f1fd130cdfe6e648870f44211262ad3 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Fri, 29 Mar 2024 18:48:34 +0100 Subject: [PATCH] fix csv export + basic hours --- .../gla/project/idfm/CSVStreamProvider.java | 20 ++-- .../idfm/CSVStreamSchedulesProvider.java | 100 ++++++++++-------- .../u_paris/gla/project/utils/CSVTools.java | 5 +- 3 files changed, 71 insertions(+), 54 deletions(-) diff --git a/src/main/java/fr/u_paris/gla/project/idfm/CSVStreamProvider.java b/src/main/java/fr/u_paris/gla/project/idfm/CSVStreamProvider.java index 432f12c..3a48c1a 100644 --- a/src/main/java/fr/u_paris/gla/project/idfm/CSVStreamProvider.java +++ b/src/main/java/fr/u_paris/gla/project/idfm/CSVStreamProvider.java @@ -36,15 +36,15 @@ public final class CSVStreamProvider { /** Distance to reach maximal speed in km */ 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 currentTrace; + private final Iterator currentTrace; private Iterator> currentPath = Collections.emptyIterator(); private Iterator currentSegmentStart = Collections.emptyIterator(); private Iterator currentSegmentEnd = Collections.emptyIterator(); Map> lineSegments = new HashMap<>(); // The transport id with its value - private Map transports; + private final Map transports; List descriptions = new ArrayList<>(); @@ -111,10 +111,12 @@ public final class CSVStreamProvider { return; } TraceEntry trace = this.currentTrace.next(); - traceId = trace.id; - traceType = trace.type; - descriptions.clear(); - descriptions.addAll(trace.descriptions); + + this.traceId = trace.id; + this.traceType = trace.type; + this.descriptions.clear(); + this.descriptions.addAll(trace.descriptions); + this.currentPath = trace.getPaths().iterator(); this.line[NetworkFormat.LINE_INDEX] = trace.lname; this.lineSegments.clear(); @@ -128,7 +130,7 @@ public final class CSVStreamProvider { } while (currentSegmentStart == null); } - public String[] next() { + public String[][] next() { if (!this.onNext) { skipToNext(); } @@ -146,7 +148,7 @@ public final class CSVStreamProvider { this.line[NetworkFormat.VARIANT_INDEX] = Integer .toString(bifurcation); fillTransports(bifurcation); - return Arrays.copyOf(this.line, this.line.length); + return new String[][]{Arrays.copyOf(this.line, this.line.length)}; } /** @param stop1 diff --git a/src/main/java/fr/u_paris/gla/project/idfm/CSVStreamSchedulesProvider.java b/src/main/java/fr/u_paris/gla/project/idfm/CSVStreamSchedulesProvider.java index 36fe27c..eb64831 100644 --- a/src/main/java/fr/u_paris/gla/project/idfm/CSVStreamSchedulesProvider.java +++ b/src/main/java/fr/u_paris/gla/project/idfm/CSVStreamSchedulesProvider.java @@ -15,13 +15,14 @@ public class CSVStreamSchedulesProvider { private final String[] line = new String[ScheduleFormat.NUMBER_COLUMNS]; private final Iterator currentTrace; - private final Map transports; - Map> lineSegments = new HashMap<>(); private Iterator> currentPath = Collections.emptyIterator(); private Iterator currentSegmentStart = Collections.emptyIterator(); private Iterator currentSegmentEnd = Collections.emptyIterator(); + Map> lineSegments = new HashMap<>(); + private final Map transports; + private StopEntry start = null; - private StopEntry end = null; + private StopEntry end = null; private boolean hasNext = false; private boolean onNext = false; @@ -61,7 +62,7 @@ public class CSVStreamSchedulesProvider { this.lineSegments.putIfAbsent(this.start, new HashSet<>()); this.end = this.currentSegmentEnd.next(); } while (this.lineSegments.get(this.start).contains(this.end) - && this.currentSegmentEnd.hasNext()); + && this.currentSegmentEnd.hasNext()); if (!this.lineSegments.get(this.start).contains(this.end)) { this.lineSegments.get(this.start).add(this.end); this.onNext = true; @@ -84,36 +85,8 @@ public class CSVStreamSchedulesProvider { } TraceEntry trace = this.currentTrace.next(); 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 path = this.currentPath.next(); this.currentSegmentEnd = path.iterator(); if (this.currentSegmentEnd.hasNext()) { @@ -123,22 +96,63 @@ public class CSVStreamSchedulesProvider { } while (currentSegmentStart == null); } - public String[] next() { + public String[][] next() { if (!this.onNext) { skipToNext(); } this.onNext = false; - this.line[ScheduleFormat.TIME_INDEX] = null; + /**/ - /* - * this.line[ScheduleFormat.DISTANCE_INDEX] = - * NumberFormat.getInstance(Locale.ENGLISH) - * .format(distance); - * this.line[ScheduleFormat.VARIANT_INDEX] = Integer - * .toString(this.lineSegments.get(this.start).size() - 1); - */ + if (!this.currentTrace.hasNext()) { + return new String[][]{Arrays.copyOf(this.line, this.line.length)}; + } + TraceEntry trace = this.currentTrace.next(); - 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 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, + }; } } diff --git a/src/main/java/fr/u_paris/gla/project/utils/CSVTools.java b/src/main/java/fr/u_paris/gla/project/utils/CSVTools.java index b3194f9..93a3ef3 100644 --- a/src/main/java/fr/u_paris/gla/project/utils/CSVTools.java +++ b/src/main/java/fr/u_paris/gla/project/utils/CSVTools.java @@ -11,6 +11,7 @@ import java.io.InputStreamReader; import java.io.Reader; import java.net.URL; import java.nio.charset.StandardCharsets; +import java.util.Arrays; import java.util.function.Consumer; import java.util.stream.Stream; @@ -53,11 +54,11 @@ public final class CSVTools { } public static void writeCSVToFile(String filename, - Stream contentLineConsumer) throws IOException { + Stream contentLinesConsumer) throws IOException { try (FileWriter writer = new FileWriter(filename, StandardCharsets.UTF_8)) { CSVWriterBuilder wBuilder = new CSVWriterBuilder(writer).withSeparator(';'); try (ICSVWriter csv = wBuilder.build()) { - contentLineConsumer.forEachOrdered(csv::writeNext); + contentLinesConsumer.forEachOrdered(line -> Arrays.stream(line).forEach(csv::writeNext)); } } }