Skip to content

Commit 0eb0b2a

Browse files
committed
Merge branch 'fix-late-cancellations' into dev-2.x
2 parents d5e3895 + a3d0e67 commit 0eb0b2a

File tree

9 files changed

+269
-31
lines changed

9 files changed

+269
-31
lines changed

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

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public class TransitLayer {
2222
/**
2323
* Transit data required for routing, indexed by each local date(Graph TimeZone) it runs through.
2424
* A Trip "runs through" a date if any of its arrivals or departures is happening on that date.
25+
* The same trip pattern can therefore have multiple running dates and trip pattern is not
26+
* required to "run" on its service date.
2527
*/
2628
private final HashMap<LocalDate, List<TripPatternForDate>> tripPatternsRunningOnDate;
2729

@@ -94,7 +96,12 @@ public StopLocation getStopByIndex(int stop) {
9496
return stop == -1 ? null : this.stopModel.stopByIndex(stop);
9597
}
9698

97-
public Collection<TripPatternForDate> getTripPatternsForDate(LocalDate date) {
99+
/**
100+
* Returns trip patterns for the given running date. Running date is not necessarily the same
101+
* as the service date. A Trip "runs through" a date if any of its arrivals or departures is
102+
* happening on that date. Trip pattern can have multiple running dates.
103+
*/
104+
public Collection<TripPatternForDate> getTripPatternsForRunningDate(LocalDate date) {
98105
return tripPatternsRunningOnDate.getOrDefault(date, List.of());
99106
}
100107

@@ -112,16 +119,29 @@ public int getStopCount() {
112119
return stopModel.stopIndexSize();
113120
}
114121

122+
/**
123+
* Returns a copy of the list of trip patterns for the given running date. Running date is not
124+
* necessarily the same as the service date. A Trip "runs through" a date if any of its arrivals
125+
* or departures is happening on that date. Trip pattern can have multiple running dates.
126+
*/
115127
public List<TripPatternForDate> getTripPatternsRunningOnDateCopy(LocalDate runningPeriodDate) {
116128
List<TripPatternForDate> tripPatternForDate = tripPatternsRunningOnDate.get(runningPeriodDate);
117129
return tripPatternForDate != null ? new ArrayList<>(tripPatternForDate) : new ArrayList<>();
118130
}
119131

120-
public List<TripPatternForDate> getTripPatternsStartingOnDateCopy(LocalDate date) {
121-
List<TripPatternForDate> tripPatternsRunningOnDate = getTripPatternsRunningOnDateCopy(date);
122-
return tripPatternsRunningOnDate
132+
/**
133+
* Returns a copy of the list of trip patterns for the given service date. Service date is not
134+
* necessarily the same as any of the trip pattern's running dates.
135+
*/
136+
public List<TripPatternForDate> getTripPatternsOnServiceDateCopy(LocalDate date) {
137+
List<TripPatternForDate> tripPatternsRunningOnDates = getTripPatternsRunningOnDateCopy(date);
138+
// Trip pattern can run only after midnight. Therefore, we need to get the trip pattern's for
139+
// the next running date as well and filter out duplicates.
140+
tripPatternsRunningOnDates.addAll(getTripPatternsRunningOnDateCopy(date.plusDays(1)));
141+
return tripPatternsRunningOnDates
123142
.stream()
124-
.filter(t -> t.getLocalDate().equals(date))
143+
.filter(t -> t.getServiceDate().equals(date))
144+
.distinct()
125145
.collect(Collectors.toList());
126146
}
127147

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

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,36 +43,36 @@ public class TripPatternForDate implements Comparable<TripPatternForDate> {
4343
*/
4444
private final FrequencyEntry[] frequencies;
4545

46-
/** The date for which the filtering was performed. */
47-
private final LocalDate localDate;
46+
/** The service date of the trip pattern. */
47+
private final LocalDate serviceDate;
4848

4949
/**
50-
* The date on which the first trip departs.
50+
* The running date on which the first trip departs. Not necessarily the same as the service date.
5151
*/
5252
private final LocalDate startOfRunningPeriod;
5353

5454
/**
55-
* The date on which the last trip arrives.
55+
* The running date on which the last trip arrives.
5656
*/
5757
private final LocalDate endOfRunningPeriod;
5858

5959
public TripPatternForDate(
6060
RoutingTripPattern tripPattern,
6161
List<TripTimes> tripTimes,
6262
List<FrequencyEntry> frequencies,
63-
LocalDate localDate
63+
LocalDate serviceDate
6464
) {
6565
this.tripPattern = tripPattern;
6666
this.tripTimes = tripTimes.toArray(new TripTimes[0]);
6767
this.frequencies = frequencies.toArray(new FrequencyEntry[0]);
68-
this.localDate = localDate;
68+
this.serviceDate = serviceDate;
6969

7070
// TODO: We expect a pattern only containing trips or frequencies, fix ability to merge
7171
if (hasFrequencies()) {
7272
this.startOfRunningPeriod =
7373
ServiceDateUtils
7474
.asDateTime(
75-
localDate,
75+
serviceDate,
7676
frequencies
7777
.stream()
7878
.mapToInt(frequencyEntry -> frequencyEntry.startTime)
@@ -84,7 +84,7 @@ public TripPatternForDate(
8484
this.endOfRunningPeriod =
8585
ServiceDateUtils
8686
.asDateTime(
87-
localDate,
87+
serviceDate,
8888
frequencies
8989
.stream()
9090
.mapToInt(frequencyEntry -> frequencyEntry.endTime)
@@ -96,11 +96,11 @@ public TripPatternForDate(
9696
// These depend on the tripTimes array being sorted
9797
var first = tripTimes.get(0);
9898
this.startOfRunningPeriod =
99-
ServiceDateUtils.asDateTime(localDate, first.getDepartureTime(0)).toLocalDate();
99+
ServiceDateUtils.asDateTime(serviceDate, first.getDepartureTime(0)).toLocalDate();
100100
var last = tripTimes.get(tripTimes.size() - 1);
101101
this.endOfRunningPeriod =
102102
ServiceDateUtils
103-
.asDateTime(localDate, last.getArrivalTime(last.getNumStops() - 1))
103+
.asDateTime(serviceDate, last.getArrivalTime(last.getNumStops() - 1))
104104
.toLocalDate();
105105
assertValidRunningPeriod(startOfRunningPeriod, endOfRunningPeriod, first, last);
106106
}
@@ -126,18 +126,31 @@ public TripTimes getTripTimes(int i) {
126126
return tripTimes[i];
127127
}
128128

129-
public LocalDate getLocalDate() {
130-
return localDate;
129+
/**
130+
* The service date for which the trip pattern belongs to. Not necessarily the same as the start
131+
* of the running period in cases where the trip pattern only runs after midnight.
132+
*/
133+
public LocalDate getServiceDate() {
134+
return serviceDate;
131135
}
132136

133137
public int numberOfTripSchedules() {
134138
return tripTimes.length;
135139
}
136140

141+
/**
142+
* The start of the running period. This is determined by the first departure time for this
143+
* pattern. Not necessarily the same as the service date if the pattern runs after midnight.
144+
*/
137145
public LocalDate getStartOfRunningPeriod() {
138146
return startOfRunningPeriod;
139147
}
140148

149+
/**
150+
* Returns the running dates. A Trip "runs through" a date if any of its arrivals or departures is
151+
* happening on that date. The same trip pattern can therefore have multiple running dates and
152+
* trip pattern is not required to "run" on its service date.
153+
*/
141154
public List<LocalDate> getRunningPeriodDates() {
142155
// Add one day to ensure last day is included
143156
return startOfRunningPeriod
@@ -151,14 +164,14 @@ public boolean hasFrequencies() {
151164

152165
@Override
153166
public int compareTo(TripPatternForDate other) {
154-
return localDate.compareTo(other.localDate);
167+
return serviceDate.compareTo(other.serviceDate);
155168
}
156169

157170
@Override
158171
public int hashCode() {
159172
return Objects.hash(
160173
tripPattern,
161-
localDate,
174+
serviceDate,
162175
Arrays.hashCode(tripTimes),
163176
Arrays.hashCode(frequencies)
164177
);
@@ -176,15 +189,17 @@ public boolean equals(Object o) {
176189

177190
return (
178191
tripPattern.equals(that.tripPattern) &&
179-
localDate.equals(that.localDate) &&
192+
serviceDate.equals(that.serviceDate) &&
180193
Arrays.equals(tripTimes, that.tripTimes) &&
181194
Arrays.equals(frequencies, that.frequencies)
182195
);
183196
}
184197

185198
@Override
186199
public String toString() {
187-
return "TripPatternForDate{" + "tripPattern=" + tripPattern + ", localDate=" + localDate + '}';
200+
return (
201+
"TripPatternForDate{" + "tripPattern=" + tripPattern + ", serviceDate=" + serviceDate + '}'
202+
);
188203
}
189204

190205
@Nullable
@@ -214,7 +229,7 @@ public TripPatternForDate newWithFilteredTripTimes(Predicate<TripTimes> filter)
214229
return this;
215230
}
216231

217-
return new TripPatternForDate(tripPattern, filteredTripTimes, filteredFrequencies, localDate);
232+
return new TripPatternForDate(tripPattern, filteredTripTimes, filteredFrequencies, serviceDate);
218233
}
219234

220235
private static void assertValidRunningPeriod(

src/main/java/org/opentripplanner/routing/algorithm/raptoradapter/transit/frequency/TripFrequencyAlightSearch.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public RaptorBoardOrAlightEvent<T> search(
5555
arrivalTime + headway,
5656
headway,
5757
offset,
58-
pattern.getLocalDate()
58+
pattern.getServiceDate()
5959
);
6060
}
6161
}

src/main/java/org/opentripplanner/routing/algorithm/raptoradapter/transit/frequency/TripFrequencyBoardSearch.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public RaptorBoardOrAlightEvent<T> search(
5454
departureTime - headway,
5555
headway,
5656
offset,
57-
pattern.getLocalDate()
57+
pattern.getServiceDate()
5858
);
5959
}
6060
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public void update(
9696

9797
if (!tripPatternsStartingOnDateMapCache.containsKey(date)) {
9898
Map<TripPattern, TripPatternForDate> map = realtimeTransitLayer
99-
.getTripPatternsStartingOnDateCopy(date)
99+
.getTripPatternsOnServiceDateCopy(date)
100100
.stream()
101101
.collect(Collectors.toMap(t -> t.getTripPattern().getPattern(), t -> t));
102102
tripPatternsStartingOnDateMapCache.put(date, map);
@@ -146,7 +146,7 @@ public void update(
146146
} else {
147147
LOG.debug(
148148
"NEW TripPatternForDate: {} - {}",
149-
newTripPatternForDate.getLocalDate(),
149+
newTripPatternForDate.getServiceDate(),
150150
newTripPatternForDate.getTripPattern().debugInfo()
151151
);
152152
}
@@ -179,7 +179,7 @@ public void update(
179179
}
180180

181181
for (TripPatternForDate tripPatternForDate : previouslyUsedPatterns) {
182-
if (tripPatternForDate.getLocalDate().equals(date)) {
182+
if (tripPatternForDate.getServiceDate().equals(date)) {
183183
TripPattern pattern = tripPatternForDate.getTripPattern().getPattern();
184184
if (!pattern.isCreatedByRealtimeUpdater()) {
185185
continue;

src/main/java/org/opentripplanner/routing/algorithm/raptoradapter/transit/request/RaptorRoutingRequestTransitDataCreator.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ static List<TripPatternForDates> merge(
120120
// Calculate offsets per date
121121
int[] offsets = new int[patternsSorted.length];
122122
for (int i = 0; i < patternsSorted.length; i++) {
123-
LocalDate serviceDate = patternsSorted[i].getLocalDate();
123+
LocalDate serviceDate = patternsSorted[i].getServiceDate();
124124
if (offsetCache.containsKey(serviceDate)) {
125125
offsets[i] = offsetCache.get(serviceDate);
126126
} else {
@@ -185,7 +185,9 @@ private static List<TripPatternForDate> filterActiveTripPatterns(
185185
filter.tripTimesPredicate(tripTimes, filter.hasSubModeFilters());
186186
Predicate<TripTimes> tripTimesWithoutSubmodesPredicate = tripTimes ->
187187
filter.tripTimesPredicate(tripTimes, false);
188-
Collection<TripPatternForDate> tripPatternsForDate = transitLayer.getTripPatternsForDate(date);
188+
Collection<TripPatternForDate> tripPatternsForDate = transitLayer.getTripPatternsForRunningDate(
189+
date
190+
);
189191
List<TripPatternForDate> result = new ArrayList<>(tripPatternsForDate.size());
190192
for (TripPatternForDate p : tripPatternsForDate) {
191193
if (firstDay || p.getStartOfRunningPeriod().equals(date)) {

src/main/java/org/opentripplanner/routing/algorithm/raptoradapter/transit/request/TripScheduleWithOffset.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ private void findTripTimes() {
124124

125125
if (index < numSchedules) {
126126
this.tripTimes = tripPatternForDate.getTripTimes(index);
127-
this.serviceDate = tripPatternForDate.getLocalDate();
127+
this.serviceDate = tripPatternForDate.getServiceDate();
128128
this.secondsOffset = pattern.tripPatternForDateOffsets(i);
129129
return;
130130
}

0 commit comments

Comments
 (0)