-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Addressing review feedback: Implementing changes #6
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
050da21
ENH: NAV-14 - Add presence to GTFS file type
munterfi 6273663
ENH: NAV-14 - Add support for extended route types
munterfi a3085ac
STYLE: NAV-14 - Rename test methods, adjust visibility and remove whi…
munterfi 45921e3
REFACTOR: NAV-14 - Introduce test builder pattern for GTFS module
munterfi 2b1cdc2
ENH: NAV-14 - Improve testing of GTFS schedule
munterfi eac4f5c
ORG: NAV-6 - Set code style for project
munterfi f2479d3
ENH: NAV-19 - Partition routes into sub-routes with the same stop seq…
munterfi 5958bad
ENH: NAV-14 - Remove supported field from HierarchicalVehicleType
munterfi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
105 changes: 105 additions & 0 deletions
105
src/main/java/ch/naviqore/raptor/GtfsRoutePartitioner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package ch.naviqore.raptor; | ||
|
||
import ch.naviqore.gtfs.schedule.model.*; | ||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.log4j.Log4j2; | ||
|
||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Splits the routes of a GTFS schedule into sub-routes. In a GTFS schedule, a route can have multiple trips with | ||
* different stop sequences. This class groups trips with the same stop sequence into sub-routes and assigns them the | ||
* parent route. | ||
* | ||
* @author munterfi | ||
*/ | ||
@Log4j2 | ||
public class GtfsRoutePartitioner { | ||
private final Map<Route, Map<String, SubRoute>> subRoutes = new HashMap<>(); | ||
|
||
public GtfsRoutePartitioner(GtfsSchedule schedule) { | ||
log.info("Partitioning GTFS schedule with {} routes into sub-routes", schedule.getRoutes().size()); | ||
schedule.getRoutes().values().forEach(this::processRoute); | ||
log.info("Found {} sub-routes in schedule", subRoutes.values().stream().mapToInt(Map::size).sum()); | ||
} | ||
|
||
private void processRoute(Route route) { | ||
Map<String, SubRoute> sequenceKeyToSubRoute = new HashMap<>(); | ||
route.getTrips().forEach(trip -> { | ||
String key = generateStopSequenceKey(trip); | ||
SubRoute subRoute = sequenceKeyToSubRoute.computeIfAbsent(key, | ||
s -> new SubRoute(String.format("%s_sr%d", route.getId(), sequenceKeyToSubRoute.size() + 1), route, | ||
key, extractStopSequence(trip))); | ||
subRoute.addTrip(trip); | ||
}); | ||
subRoutes.put(route, sequenceKeyToSubRoute); | ||
log.debug("Route {} split into {} sub-routes", route.getId(), sequenceKeyToSubRoute.size()); | ||
} | ||
|
||
private String generateStopSequenceKey(Trip trip) { | ||
return trip.getStopTimes().stream().map(t -> t.stop().getId()).collect(Collectors.joining("-")); | ||
} | ||
|
||
private List<Stop> extractStopSequence(Trip trip) { | ||
List<Stop> sequence = new ArrayList<>(); | ||
for (StopTime stopTime : trip.getStopTimes()) { | ||
sequence.add(stopTime.stop()); | ||
} | ||
return sequence; | ||
} | ||
|
||
public List<SubRoute> getSubRoutes(Route route) { | ||
Map<String, SubRoute> currentSubRoutes = subRoutes.get(route); | ||
if (currentSubRoutes == null) { | ||
throw new IllegalArgumentException("Route " + route.getId() + " not found in schedule"); | ||
} | ||
return new ArrayList<>(currentSubRoutes.values()); | ||
} | ||
|
||
public SubRoute getSubRoute(Trip trip) { | ||
Map<String, SubRoute> currentSubRoutes = subRoutes.get(trip.getRoute()); | ||
if (currentSubRoutes == null) { | ||
throw new IllegalArgumentException("Trip " + trip.getId() + " not found in schedule"); | ||
} | ||
String key = generateStopSequenceKey(trip); | ||
return currentSubRoutes.get(key); | ||
} | ||
|
||
/** | ||
* A sub-route belongs to a route, but has a unique stop sequence. | ||
*/ | ||
@RequiredArgsConstructor | ||
@Getter | ||
public static class SubRoute { | ||
private final String id; | ||
private final Route route; | ||
@Getter(AccessLevel.NONE) | ||
private final String stopSequenceKey; | ||
private final List<Stop> stopsSequence; | ||
private final List<Trip> trips = new ArrayList<>(); | ||
|
||
private void addTrip(Trip trip) { | ||
trips.add(trip); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (obj == this) return true; | ||
if (obj == null || obj.getClass() != this.getClass()) return false; | ||
var that = (SubRoute) obj; | ||
return Objects.equals(this.id, that.id); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(id); | ||
} | ||
|
||
public String toString() { | ||
return "SubRoute[" + "id=" + id + ", " + "route=" + route + ", " + "stopSequence=" + stopSequenceKey + ']'; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/test/java/ch/naviqore/raptor/GtfsRoutePartitionerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package ch.naviqore.raptor; | ||
|
||
import ch.naviqore.gtfs.schedule.model.*; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@ExtendWith(GtfsScheduleTestExtension.class) | ||
class GtfsRoutePartitionerTest { | ||
|
||
private GtfsSchedule schedule; | ||
private GtfsRoutePartitioner partitioner; | ||
|
||
@BeforeEach | ||
void setUp(GtfsScheduleTestBuilder builder) { | ||
schedule = builder.withAddAgency() | ||
.withAddCalendars() | ||
.withAddCalendarDates() | ||
.withAddInterCity() | ||
.withAddUnderground() | ||
.withAddBus() | ||
.build(); | ||
partitioner = new GtfsRoutePartitioner(schedule); | ||
} | ||
|
||
@Test | ||
void getSubRoutes() { | ||
assertThat(partitioner.getSubRoutes(schedule.getRoutes().get("route1"))).as("SubRoutes").hasSize(2); | ||
assertThat(partitioner.getSubRoutes(schedule.getRoutes().get("route2"))).as("SubRoutes").hasSize(1); | ||
assertThat(partitioner.getSubRoutes(schedule.getRoutes().get("route3"))).as("SubRoutes").hasSize(2); | ||
} | ||
|
||
@Test | ||
void getSubRoute() { | ||
for (Route route : schedule.getRoutes().values()) { | ||
for (Trip trip : route.getTrips()) { | ||
GtfsRoutePartitioner.SubRoute subRoute = partitioner.getSubRoute(trip); | ||
assertThat(subRoute).as("SubRoute for trip ID " + trip.getId() + " in route " + route.getId()) | ||
.isNotNull(); | ||
} | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe add test, to make sure that the expected number of trips are found in each (or specific cases) subroute instance. |
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use @BeforeAll and make
setUp
,schedule
,partitioner
static. Since this data is not manipulated by any test, I don't see why whe should rebuild the GTFS Schedule instance everytime.