Skip to content

Commit

Permalink
fix(MergeFeedUtils): Use simplified stop times comparison (MTD req)
Browse files Browse the repository at this point in the history
  • Loading branch information
binh-dam-ibigroup committed Nov 15, 2021
1 parent cba9141 commit c1224bf
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public class MergeFeedsJob extends FeedSourceJob {
@JsonIgnore @BsonIgnore
public Set<String> sharedTripIdsWithConsistentSignature = new HashSet<>();
@JsonIgnore @BsonIgnore
public Set<String> serviceIdsToCloneAndRename = new HashSet<>();
public Set<String> serviceIdsToCloneRenameAndExtend = new HashSet<>();
@JsonIgnore @BsonIgnore
public Set<String> serviceIdsToTerminateEarly = new HashSet<>();

Expand Down Expand Up @@ -507,24 +507,29 @@ private void determineMergeStrategy() {
// Build the set of calendars to be cloned/renamed/extended from trip ids present
// in both active/future feeds and that have consistent signature.
// These trips will be linked to the new service_ids.
serviceIdsToCloneAndRename.addAll(
sharedTripIdsWithConsistentSignature.stream()
.map(tripId -> activeFeed.trips.get(tripId).service_id)
.collect(Collectors.toList())
serviceIdsToCloneRenameAndExtend.addAll(
getActiveServiceIds(this.sharedTripIdsWithConsistentSignature)
);

// Build the set of calendars to be shortened to the day before the future feed start date
// from trips in the active feed but not in the future feed.
serviceIdsToTerminateEarly.addAll(
feedMergeContext.getActiveTripIdsNotInFutureFeed().stream()
.map(tripId -> activeFeed.trips.get(tripId).service_id)
.collect(Collectors.toList())
getActiveServiceIds(feedMergeContext.getActiveTripIdsNotInFutureFeed())
);

mergeFeedsResult.mergeStrategy = CHECK_STOP_TIMES;
}
}

/**
* Obtains the service ids corresponding to the provided trip ids.
*/
private List<String> getActiveServiceIds(Set<String> tripIds) {
return tripIds.stream()
.map(tripId -> feedMergeContext.activeFeed.trips.get(tripId).service_id)
.collect(Collectors.toList());
}

/**
* Compare stop times for the given tripId between the future and active feeds. The comparison will inform whether
* trip and/or service IDs should be modified in the output merged feed.
Expand All @@ -537,15 +542,15 @@ private void compareStopTimesAndCollectTripAndServiceIds(String tripId, Feed fut
List<StopTime> activeStopTimes = Lists.newArrayList(activeFeed.stopTimes.getOrdered(tripId));
String activeServiceId = activeFeed.trips.get(tripId).service_id;
String futureServiceId = futureFeed.trips.get(tripId).service_id;
if (!stopTimesMatch(futureStopTimes, activeStopTimes)) {
if (!stopTimesMatchSimplified(futureStopTimes, activeStopTimes)) {
// If stop_times or services do not match, merge will fail and no other action will be taken.
sharedTripIdsWithInconsistentSignature.add(tripId);
} else {
// If the trip's stop_times are an exact match, we can safely include just the
// future trip and exclude the active one. Also, mark the service_id for cloning,
// the cloned service id will need to be extended to the full time range.
sharedTripIdsWithConsistentSignature.add(tripId);
serviceIdsToCloneAndRename.add(futureServiceId);
serviceIdsToCloneRenameAndExtend.add(futureServiceId);
sharedConsistentTripAndCalendarIds.add(new TripAndCalendars(tripId, activeServiceId, futureServiceId));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ public void initializeRowValues() {
public void addClonedServiceId() throws IOException {
if (table.name.equals("calendar")) {
String originalServiceId = rowValues[keyFieldIndex];
if (job.serviceIdsToCloneAndRename.contains(originalServiceId)) {
if (job.serviceIdsToCloneRenameAndExtend.contains(originalServiceId)) {
// FIXME: Do we need to worry about calendar_dates?
String[] clonedValues = rowValues.clone();
String newServiceId = clonedValues[keyFieldIndex] = String.join(":", idScope, originalServiceId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,24 @@ public static String getTableScopedValue(Table table, String prefix, String id)
}

/**
* Checks whether the future and active stop_times for a particular trip_id are an exact match.
* Checks whether the future and active stop_times for a particular trip_id are an exact match,
* using these criteria only: arrival_time, departure_time, stop_id, and stop_sequence
* instead of StopTime::equals (Revised MTC feed merge requirement).
*/
public static boolean stopTimesMatch(List<StopTime> futureStopTimes, List<StopTime> activeStopTimes) {
public static boolean stopTimesMatchSimplified(List<StopTime> futureStopTimes, List<StopTime> activeStopTimes) {
if (futureStopTimes.size() != activeStopTimes.size()) {
return false;
}
for (int i = 0; i < activeStopTimes.size(); i++) {
if (!activeStopTimes.get(i).equals(futureStopTimes.get(i))) {
StopTime activeTime = activeStopTimes.get(i);
StopTime futureTime = futureStopTimes.get(i);

if (
activeTime.arrival_time != futureTime.arrival_time ||
activeTime.departure_time != futureTime.departure_time ||
activeTime.stop_sequence != futureTime.stop_sequence ||
!activeTime.stop_id.equals(futureTime.stop_id)
) {
return false;
}
}
Expand Down

0 comments on commit c1224bf

Please sign in to comment.