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 */
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<StopEntry> currentSegmentStart = Collections.emptyIterator();
private Iterator<StopEntry> currentSegmentEnd = Collections.emptyIterator();
Map<StopEntry, Set<StopEntry>> lineSegments = new HashMap<>();
// The transport id with its value
private Map<String, Transport> transports;
private final Map<String, Transport> transports;
List <TraceDescription> 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

View file

@ -15,11 +15,12 @@ public class CSVStreamSchedulesProvider {
private final String[] line = new String[ScheduleFormat.NUMBER_COLUMNS];
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<StopEntry> currentSegmentStart = 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 end = null;
private boolean hasNext = false;
@ -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<StopEntry> 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<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.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<String[]> contentLineConsumer) throws IOException {
Stream<String[][]> 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));
}
}
}