Skip to content

Commit 095ffe6

Browse files
committed
Merge branch 'fix-realtime-update-service-days' into dev-2.x
2 parents 4d1f2a6 + 6150f21 commit 095ffe6

4 files changed

Lines changed: 90 additions & 4 deletions

File tree

src/main/java/org/opentripplanner/routing/algorithm/raptoradapter/transit/TripPatternForDate.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@
1212
import org.opentripplanner.transit.model.network.RoutingTripPattern;
1313
import org.opentripplanner.transit.model.timetable.FrequencyEntry;
1414
import org.opentripplanner.transit.model.timetable.TripTimes;
15+
import org.slf4j.Logger;
16+
import org.slf4j.LoggerFactory;
1517

1618
/**
1719
* A TripPattern with its TripSchedules filtered by validity on a particular date. This is to avoid
1820
* having to do any filtering by date during the search itself.
1921
*/
2022
public class TripPatternForDate implements Comparable<TripPatternForDate> {
2123

24+
private static final Logger LOG = LoggerFactory.getLogger(TripPatternForDate.class);
25+
2226
/**
2327
* The original TripPattern whose TripSchedules were filtered to produce this.tripSchedules. Its
2428
* TripSchedules remain unchanged.
@@ -98,6 +102,28 @@ public TripPatternForDate(
98102
.asDateTime(localDate, last.getArrivalTime(last.getNumStops() - 1))
99103
.toLocalDate();
100104
}
105+
if (startOfRunningPeriod.isAfter(endOfRunningPeriod)) {
106+
if (hasFrequencies()) {
107+
var tripId = frequencies.get(0).tripTimes.getTrip().getId();
108+
LOG.warn(
109+
"Could not construct as start of the running period {} is after the end {} in frequency trip {}",
110+
startOfRunningPeriod,
111+
endOfRunningPeriod,
112+
tripId
113+
);
114+
} else {
115+
var tripId = tripTimes.get(0).getTrip().getId();
116+
LOG.warn(
117+
"Could not construct as start of the running period {} is after the end {} in trip {}",
118+
startOfRunningPeriod,
119+
endOfRunningPeriod,
120+
tripId
121+
);
122+
}
123+
throw new IllegalArgumentException(
124+
"Start of the running period is after end of the running period"
125+
);
126+
}
101127
}
102128

103129
public List<TripTimes> tripTimes() {

src/main/java/org/opentripplanner/routing/algorithm/raptoradapter/transit/mappers/TransitLayerUpdater.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,14 @@ public void update(
112112
datesToBeUpdated.addAll(oldTripPatternForDate.getRunningPeriodDates());
113113
}
114114

115-
TripPatternForDate newTripPatternForDate = tripPatternForDateMapper.map(
116-
timetable,
117-
timetable.getServiceDate()
118-
);
115+
TripPatternForDate newTripPatternForDate;
116+
117+
try {
118+
newTripPatternForDate = tripPatternForDateMapper.map(timetable, timetable.getServiceDate());
119+
} catch (IllegalArgumentException exception) {
120+
// There is some issue with finding the correct running period, using old pattern instead
121+
newTripPatternForDate = oldTripPatternForDate;
122+
}
119123

120124
if (newTripPatternForDate != null) {
121125
tripPatternsStartingOnDateMapCache.get(date).put(tripPattern, newTripPatternForDate);

src/main/java/org/opentripplanner/updater/trip/TimetableSnapshotSource.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,19 @@ private Result<UpdateSuccess, UpdateError> handleScheduledTrip(
413413
return UpdateError.result(tripId, NO_UPDATES);
414414
}
415415

416+
final FeedScopedId serviceId = transitService.getTripForId(tripId).getServiceId();
417+
final Set<LocalDate> serviceDates = transitService
418+
.getCalendarService()
419+
.getServiceDatesForServiceId(serviceId);
420+
if (!serviceDates.contains(serviceDate)) {
421+
debug(
422+
tripId,
423+
"SCHEDULED trip has service date {} for which trip's service is not valid, skipping.",
424+
serviceDate.toString()
425+
);
426+
return UpdateError.result(tripId, NO_SERVICE_ON_DATE);
427+
}
428+
416429
// If this trip_id has been used for previously ADDED/MODIFIED trip message (e.g. when the
417430
// sequence of stops has changed, and is now changing back to the originally scheduled one),
418431
// mark that previously created trip as DELETED.

src/test/java/org/opentripplanner/updater/trip/TimetableSnapshotSourceTest.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import static org.junit.jupiter.api.Assertions.assertNull;
1212
import static org.junit.jupiter.api.Assertions.assertSame;
1313
import static org.junit.jupiter.api.Assertions.assertTrue;
14+
import static org.opentripplanner.model.UpdateError.UpdateErrorType.NO_SERVICE_ON_DATE;
1415
import static org.opentripplanner.updater.trip.BackwardsDelayPropagationType.REQUIRED_NO_DATA;
1516
import static org.opentripplanner.updater.trip.TimetableSnapshotSourceTest.SameAssert.NotSame;
1617
import static org.opentripplanner.updater.trip.TimetableSnapshotSourceTest.SameAssert.Same;
@@ -574,6 +575,48 @@ public void scheduled() {
574575
assertEquals(90, originalTripTimesForToday.getDepartureDelay(2));
575576
}
576577

578+
/**
579+
* This test just asserts that trip with start date that is outside the service period doesn't
580+
* throw an exception and is ignored instead.
581+
*/
582+
@Test
583+
public void invalidTripDate() {
584+
// GIVEN
585+
586+
String scheduledTripId = "1.1";
587+
588+
var serviceDateOutsideService = SERVICE_DATE.minusYears(10);
589+
var builder = new TripUpdateBuilder(
590+
scheduledTripId,
591+
serviceDateOutsideService,
592+
SCHEDULED,
593+
transitModel.getTimeZone()
594+
)
595+
.addDelayedStopTime(1, 0)
596+
.addDelayedStopTime(2, 60, 80)
597+
.addDelayedStopTime(3, 90, 90);
598+
599+
var tripUpdate = builder.build();
600+
601+
var updater = defaultUpdater();
602+
603+
// WHEN
604+
var result = updater.applyTripUpdates(
605+
TRIP_MATCHER_NOOP,
606+
REQUIRED_NO_DATA,
607+
fullDataset,
608+
List.of(tripUpdate),
609+
feedId
610+
);
611+
612+
// THEN
613+
final TimetableSnapshot snapshot = updater.getTimetableSnapshot();
614+
assertNull(snapshot);
615+
assertEquals(1, result.failed());
616+
var errors = result.failures();
617+
assertEquals(1, errors.get(NO_SERVICE_ON_DATE).size());
618+
}
619+
577620
@Test
578621
public void scheduledTripWithSkippedAndNoData() {
579622
// GIVEN

0 commit comments

Comments
 (0)