Skip to content

Commit

Permalink
Refactor GTFS parser and reader
Browse files Browse the repository at this point in the history
- Split along responsibilities.
  • Loading branch information
munterfi committed Apr 26, 2024
1 parent ccc1731 commit 7c999ec
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 27 deletions.
16 changes: 11 additions & 5 deletions src/main/java/ch/naviqore/gtfs/schedule/GtfsScheduleParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
import ch.naviqore.gtfs.schedule.type.ExceptionType;
import ch.naviqore.gtfs.schedule.type.RouteType;
import ch.naviqore.gtfs.schedule.type.ServiceDayTime;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.csv.CSVRecord;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.EnumMap;
import java.util.EnumSet;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.csv.CSVRecord;

/**
* GTFS CSV records parser
Expand All @@ -37,7 +37,14 @@ public GtfsScheduleParser(GtfsScheduleBuilder builder) {
}

public void parse(CSVRecord record, GtfsScheduleFile fileType) {
parsers.getOrDefault(fileType, r -> log.warn("Unsupported GTFS file type for parsing: {}", fileType))
Set<GtfsScheduleFile> warnings = EnumSet.noneOf(GtfsScheduleFile.class);
parsers.getOrDefault(fileType, r -> {
if (!warnings.contains(fileType)) {
log.warn("Unsupported GTFS file type for parsing: {}", fileType);
} else {
warnings.add(fileType);
}
})
.accept(record);
}

Expand Down Expand Up @@ -68,7 +75,6 @@ private void parseCalendar(CSVRecord record) {
LocalDate.parse(record.get("end_date"), DATE_FORMATTER));
}


private void parseCalendarDate(CSVRecord record) {
builder.addCalendarDate(record.get("service_id"), LocalDate.parse(record.get("date"), DATE_FORMATTER),
ExceptionType.parse(record.get("exception_type")));
Expand Down
18 changes: 12 additions & 6 deletions src/main/java/ch/naviqore/gtfs/schedule/model/Calendar.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
package ch.naviqore.gtfs.schedule.model;

import ch.naviqore.gtfs.schedule.type.ExceptionType;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
@Getter
public final class Calendar {

private final String id;
private final EnumSet<DayOfWeek> serviceDays;
private final LocalDate startDate;
private final LocalDate endDate;
private final Map<LocalDate, CalendarDate> calendarDates = new HashMap<>();
private final List<Trip> trips = new ArrayList<>();

/**
* Determines if the service is operational on a specific day, considering both regular service days and
* exceptions.
* Determines if the service is operational on a specific day, considering both regular service days and exceptions.
*
* @param date the date to check for service availability
* @return true if the service is operational on the given date, false otherwise
Expand All @@ -43,6 +45,10 @@ void addCalendarDate(CalendarDate calendarDate) {
calendarDates.put(calendarDate.date(), calendarDate);
}

void addTrip(Trip trip) {
trips.add(trip);
}

@Override
public boolean equals(Object obj) {
if (obj == this) return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public GtfsScheduleBuilder addTrip(String id, String routeId, String serviceId)
Trip trip = new Trip(id, route, calendar);
route.addTrip(trip);
trips.put(id, trip);
calendar.addTrip(trip);
return this;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ch/naviqore/raptor/model/RouteStop.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ch.naviqore.raptor.model;

public class RouteStop {
public record RouteStop() {
}
39 changes: 39 additions & 0 deletions src/main/java/ch/naviqore/raptor/model/RouteTraversalBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package ch.naviqore.raptor.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = AccessLevel.PACKAGE)
public class RouteTraversalBuilder {

private final Map<String, Integer> routes = new HashMap<>();
private final Map<String, Integer> stops = new HashMap<>();

public static RouteTraversalBuilder builder() {
return new RouteTraversalBuilder();
}

public RouteTraversalBuilder addRoute(String id) {
if (routes.containsKey(id)) {
throw new IllegalArgumentException("Route " + id + " already exists");
}
routes.put(id, routes.size());
return this;
}

public RouteTraversalBuilder addStopTime() {
return this;
}

public RouteTraversalBuilder addRouteStop(String id, String routeid) {
if (routes.containsKey(id)) {
throw new IllegalArgumentException("Route " + id + " already exists");
}
return this;
}

}
4 changes: 0 additions & 4 deletions src/main/java/ch/naviqore/raptor/model/Stop.java

This file was deleted.

4 changes: 0 additions & 4 deletions src/main/java/ch/naviqore/raptor/model/StopRoute.java

This file was deleted.

4 changes: 0 additions & 4 deletions src/main/java/ch/naviqore/raptor/model/Transfer.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import ch.naviqore.gtfs.schedule.GtfsScheduleBenchmarkData.Dataset;
import ch.naviqore.gtfs.schedule.model.GtfsSchedule;
import ch.naviqore.gtfs.schedule.model.GtfsScheduleDay;

import java.io.IOException;
import java.time.LocalDate;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

public class GtfsScheduleBenchmark {
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class GtfsScheduleBenchmark {

private static final Dataset DATASET = Dataset.SWITZERLAND;
private static final Dataset DATASET = Dataset.GERMANY;

public static void main(String[] args) throws IOException, InterruptedException {
String path = GtfsScheduleBenchmarkData.get(DATASET);
Expand Down

0 comments on commit 7c999ec

Please sign in to comment.