Skip to content

Commit ce28ab5

Browse files
Merge pull request opentripplanner#6685 from leonardehrenfried/remove-validation
Move validation of TripPatternForDate into RealTimeRaptorTransitDataUpdater
2 parents 1308a7c + f7cbc6c commit ce28ab5

3 files changed

Lines changed: 32 additions & 14 deletions

File tree

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

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ public TripPatternForDate(
9494
serviceDate,
9595
last.getArrivalTime(last.getNumStops() - 1)
9696
).toLocalDate();
97-
assertValidRunningPeriod(startOfRunningPeriod, endOfRunningPeriod, first, last);
9897
}
9998
}
10099

@@ -224,23 +223,22 @@ public TripPatternForDate newWithFilteredTripTimes(Predicate<TripTimes> filter)
224223
return new TripPatternForDate(tripPattern, filteredTripTimes, filteredFrequencies, serviceDate);
225224
}
226225

227-
private static void assertValidRunningPeriod(
228-
LocalDate startOfRunningPeriod,
229-
LocalDate endOfRunningPeriod,
230-
TripTimes first,
231-
TripTimes last
232-
) {
233-
if (first.getTrip().getRoute().getFlexibleLineType() != null) {
234-
// do not validate running period for flexible trips
235-
return;
236-
}
226+
/**
227+
* Asserts that the running period is valid and throws an {@link IllegalArgumentException} if it
228+
* is not.
229+
* This validation is only needed for real-time updates and should not be applied to flex trips
230+
* where it would fail.
231+
*/
232+
public void assertValidRunningPeriod() throws IllegalArgumentException {
237233
if (startOfRunningPeriod.isAfter(endOfRunningPeriod)) {
234+
var firstTrip = tripTimes[0].getTrip();
235+
var lastTrip = tripTimes[tripTimes.length - 1].getTrip();
238236
LOG.warn(
239237
"Could not construct as start of the running period {} in trip {} is after the end {} in trip {}",
240238
startOfRunningPeriod,
241-
first.getTrip().getId(),
239+
firstTrip.getId(),
242240
endOfRunningPeriod,
243-
last.getTrip().getId()
241+
lastTrip.getId()
244242
);
245243
throw new IllegalArgumentException(
246244
"Start of the running period is after end of the running period"

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,10 @@ public void update(
122122
TripPatternForDate newTripPatternForDate;
123123

124124
try {
125-
newTripPatternForDate = tripPatternForDateMapper.map(timetable, timetable.getServiceDate());
125+
newTripPatternForDate = tripPatternForDateMapper.mapAndValidate(
126+
timetable,
127+
timetable.getServiceDate()
128+
);
126129
} catch (IllegalArgumentException exception) {
127130
// There is some issue with finding the correct running period, using old pattern instead
128131
newTripPatternForDate = oldTripPatternForDate;

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,21 @@ public TripPatternForDate map(Timetable timetable, LocalDate serviceDate) {
101101
serviceDate
102102
);
103103
}
104+
105+
/**
106+
* Calls {@link TripPatternForDateMapper#map(Timetable, LocalDate)} and validates that the result
107+
* is valid. Since it would cause exceptions with flex trips during start up this method should
108+
* be used during real-time updates as flex trip cannot have real-time (as of now).
109+
*
110+
* @throws IllegalArgumentException
111+
*/
112+
@Nullable
113+
public TripPatternForDate mapAndValidate(Timetable timetable, LocalDate serviceDate)
114+
throws IllegalArgumentException {
115+
var result = map(timetable, serviceDate);
116+
if (result != null) {
117+
result.assertValidRunningPeriod();
118+
}
119+
return result;
120+
}
104121
}

0 commit comments

Comments
 (0)