-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Import experimental GTFS notices, add API #7726
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
Open
leonardehrenfried
wants to merge
8
commits into
opentripplanner:dev-2.x
Choose a base branch
from
ibi-group:notices
base: dev-2.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4ccd463
Map notices into internal model
leonardehrenfried 96c9bad
Add test for notices
leonardehrenfried 628a336
Add schema docs
leonardehrenfried 373452c
Improve mapper
leonardehrenfried ec3011a
Use released version of OBA
leonardehrenfried 0c86cb1
Refine tests
leonardehrenfried 866f5ef
Add comment to schema
leonardehrenfried 3ba10fd
Apply review feedback
leonardehrenfried 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
This file contains hidden or 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 hidden or 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
18 changes: 18 additions & 0 deletions
18
application/src/main/java/org/opentripplanner/apis/gtfs/datafetchers/NoticeImpl.java
This file contains hidden or 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,18 @@ | ||
| package org.opentripplanner.apis.gtfs.datafetchers; | ||
|
|
||
| import graphql.schema.DataFetcher; | ||
| import graphql.schema.DataFetchingEnvironment; | ||
| import org.opentripplanner.apis.gtfs.generated.GraphQLDataFetchers; | ||
| import org.opentripplanner.transit.model.basic.Notice; | ||
|
|
||
| public class NoticeImpl implements GraphQLDataFetchers.GraphQLNotice { | ||
|
|
||
| @Override | ||
| public DataFetcher<String> text() { | ||
| return env -> source(env).text(); | ||
| } | ||
|
|
||
| private static Notice source(DataFetchingEnvironment env) { | ||
| return env.getSource(); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
97 changes: 97 additions & 0 deletions
97
application/src/main/java/org/opentripplanner/gtfs/mapping/NoticeAssignmentMapper.java
This file contains hidden or 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,97 @@ | ||
| package org.opentripplanner.gtfs.mapping; | ||
|
|
||
| import com.google.common.collect.ArrayListMultimap; | ||
| import com.google.common.collect.Multimap; | ||
| import java.util.Collection; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.function.Function; | ||
| import java.util.stream.Collectors; | ||
| import org.onebusaway.gtfs.model.NoticeAssignment; | ||
| import org.opentripplanner.core.model.id.FeedScopedId; | ||
| import org.opentripplanner.graph_builder.issue.api.DataImportIssueStore; | ||
| import org.opentripplanner.transit.model.basic.Notice; | ||
| import org.opentripplanner.transit.model.framework.AbstractTransitEntity; | ||
| import org.opentripplanner.transit.model.network.Route; | ||
| import org.opentripplanner.transit.model.timetable.Trip; | ||
|
|
||
| /** | ||
| * Maps GTFS notice_assignments.txt entries to OTP notice assignments, connecting each | ||
| * {@link Notice} to its target {@link Route} or {@link Trip}. | ||
| */ | ||
| class NoticeAssignmentMapper { | ||
|
|
||
| private final IdFactory idFactory; | ||
| private final DataImportIssueStore issueStore; | ||
| private final NoticeMapper noticeMapper; | ||
| private final RouteMapper routeMapper; | ||
| private final TripMapper tripMapper; | ||
|
|
||
| NoticeAssignmentMapper( | ||
| IdFactory idFactory, | ||
| DataImportIssueStore issueStore, | ||
| NoticeMapper noticeMapper, | ||
| TripMapper tripMapper, | ||
| RouteMapper routeMapper | ||
| ) { | ||
| this.idFactory = idFactory; | ||
| this.issueStore = issueStore; | ||
| this.noticeMapper = noticeMapper; | ||
| this.routeMapper = routeMapper; | ||
| this.tripMapper = tripMapper; | ||
| } | ||
|
|
||
| Multimap<AbstractTransitEntity, Notice> map(Collection<NoticeAssignment> assignments) { | ||
| Multimap<AbstractTransitEntity, Notice> result = ArrayListMultimap.create(); | ||
| var trips = tripMapper | ||
| .getMappedTrips() | ||
| .stream() | ||
| .collect(Collectors.toMap(Trip::getId, Function.identity())); | ||
| var routes = routeMapper.mappedRoutes(); | ||
| for (var assignment : assignments) { | ||
| mapOne(assignment, noticeMapper.mappedNotices(), trips, routes).ifPresent(entry -> | ||
| result.put(entry.getKey(), entry.getValue()) | ||
| ); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| private Optional<Map.Entry<AbstractTransitEntity, Notice>> mapOne( | ||
| NoticeAssignment assignment, | ||
| Map<FeedScopedId, Notice> notices, | ||
| Map<FeedScopedId, Trip> trips, | ||
| Map<FeedScopedId, Route> routes | ||
| ) { | ||
| var notice = notices.get( | ||
| idFactory.createId(assignment.getNoticeId(), "notice_id in notice assignment") | ||
| ); | ||
| if (notice == null) { | ||
| issueStore.add( | ||
| "NoticeAssignmentWithoutNotice", | ||
| "Notice in notice assignment is missing for assignment %s", | ||
| assignment | ||
| ); | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| var recordId = idFactory.createId(assignment.getRecordId(), "NoticeAssignment.recordId"); | ||
|
|
||
| AbstractTransitEntity entity = switch (assignment.getTableName()) { | ||
| case routes -> routes.get(recordId); | ||
| case trips -> trips.get(recordId); | ||
| }; | ||
|
|
||
| if (entity == null) { | ||
| issueStore.add( | ||
| "NoticeAssignmentWithUnknownEntity", | ||
| "Could not map notice assignment %s for %s with id %s", | ||
| assignment.getId(), | ||
| assignment.getTableName(), | ||
| assignment.getRecordId() | ||
| ); | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| return Optional.of(Map.entry(entity, notice)); | ||
| } | ||
| } | ||
37 changes: 37 additions & 0 deletions
37
application/src/main/java/org/opentripplanner/gtfs/mapping/NoticeMapper.java
This file contains hidden or 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,37 @@ | ||
| package org.opentripplanner.gtfs.mapping; | ||
|
|
||
| import java.util.Collection; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import org.opentripplanner.core.model.id.FeedScopedId; | ||
| import org.opentripplanner.transit.model.basic.Notice; | ||
|
|
||
| /** | ||
| * Responsible for mapping onebusaway GTFS Notice into the OTP model. | ||
| * Caches mapped instances so that the same GTFS notice produces the same OTP notice object. | ||
| */ | ||
| class NoticeMapper { | ||
|
|
||
| private final IdFactory idFactory; | ||
| private final Map<FeedScopedId, Notice> cache = new HashMap<>(); | ||
|
|
||
| NoticeMapper(IdFactory idFactory) { | ||
| this.idFactory = idFactory; | ||
| } | ||
|
|
||
| Collection<Notice> map(Collection<org.onebusaway.gtfs.model.Notice> notices) { | ||
| return notices.stream().map(this::map).toList(); | ||
| } | ||
|
|
||
| Notice map(org.onebusaway.gtfs.model.Notice gtfsNotice) { | ||
| var notice = Notice.of(idFactory.createId(gtfsNotice.getId(), "Notice")) | ||
| .withText(gtfsNotice.getDisplayText()) | ||
| .build(); | ||
| return cache.computeIfAbsent(notice.getId(), _ -> notice); | ||
| } | ||
|
|
||
| Map<FeedScopedId, Notice> mappedNotices() { | ||
| return Collections.unmodifiableMap(cache); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
20 changes: 20 additions & 0 deletions
20
application/src/test/java/org/opentripplanner/apis/gtfs/datafetchers/NoticeImplTest.java
This file contains hidden or 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,20 @@ | ||
| package org.opentripplanner.apis.gtfs.datafetchers; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.opentripplanner.apis.support.graphql.DataFetchingSupport.dataFetchingEnvironment; | ||
| import static org.opentripplanner.core.model.id.FeedScopedIdForTestFactory.id; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.opentripplanner.transit.model.basic.Notice; | ||
|
|
||
| class NoticeImplTest { | ||
|
|
||
| private static final NoticeImpl SUBJECT = new NoticeImpl(); | ||
| private static final Notice NOTICE = Notice.of(id(1)).withText("AAA").build(); | ||
|
|
||
| @Test | ||
| void text() throws Exception { | ||
| var env = dataFetchingEnvironment(NOTICE); | ||
| assertEquals("AAA", SUBJECT.text().get(env)); | ||
| } | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.