Skip to content

Commit

Permalink
refactor: Extract TripTimes interface.
Browse files Browse the repository at this point in the history
This creates a new interface TripTimes and rename the old class to RealTimeTripTimes and
let the ScheduledTripTimes also implement this interface. This will allow us to wait for
an update before an accusal RealTime object is created.
  • Loading branch information
t2gran committed Oct 16, 2023
1 parent e2259be commit 207bb3f
Show file tree
Hide file tree
Showing 20 changed files with 710 additions and 587 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ private static TripPattern delay(TripPattern pattern1, int seconds) {
}

private static TripTimes delay(TripTimes tt, int seconds) {
var delayed = tt.copyOfScheduledTimes();
var delayed = tt.copyScheduledTimes();
IntStream
.range(0, delayed.getNumStops())
.forEach(i -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import org.opentripplanner.transit.model.site.RegularStop;
import org.opentripplanner.transit.model.site.Station;
import org.opentripplanner.transit.model.timetable.OccupancyStatus;
import org.opentripplanner.transit.model.timetable.RealTimeTripTimes;
import org.opentripplanner.transit.model.timetable.Trip;
import org.opentripplanner.transit.model.timetable.TripTimes;
import org.opentripplanner.transit.model.timetable.TripTimesFactory;
import uk.org.siri.siri20.OccupancyEnumeration;

Expand All @@ -45,7 +45,7 @@ public class TimetableHelperTest {
ZoneIds.CET
);

private TripTimes tripTimes;
private RealTimeTripTimes tripTimes;

@BeforeEach
public void setUp() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import org.opentripplanner.transit.model.organization.Operator;
import org.opentripplanner.transit.model.site.RegularStop;
import org.opentripplanner.transit.model.timetable.RealTimeState;
import org.opentripplanner.transit.model.timetable.RealTimeTripTimes;
import org.opentripplanner.transit.model.timetable.Trip;
import org.opentripplanner.transit.model.timetable.TripTimes;
import org.opentripplanner.transit.model.timetable.TripTimesFactory;
import org.opentripplanner.transit.service.TransitModel;
import org.opentripplanner.updater.spi.DataValidationExceptionMapper;
Expand Down Expand Up @@ -203,14 +203,14 @@ Result<TripUpdate, UpdateError> build() {
.withStopPattern(stopPattern)
.build();

TripTimes tripTimes = TripTimesFactory.tripTimes(
RealTimeTripTimes tripTimes = TripTimesFactory.tripTimes(
trip,
aimedStopTimes,
transitModel.getDeduplicator()
);
tripTimes.setServiceCode(transitModel.getServiceCodes().get(trip.getServiceId()));
pattern.add(tripTimes);
TripTimes updatedTripTimes = tripTimes.copyOfScheduledTimes();
RealTimeTripTimes updatedTripTimes = tripTimes.copyScheduledTimes();

// Loop through calls again and apply updates
for (int stopSequence = 0; stopSequence < calls.size(); stopSequence++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.opentripplanner.transit.model.site.RegularStop;
import org.opentripplanner.transit.model.site.StopLocation;
import org.opentripplanner.transit.model.timetable.RealTimeState;
import org.opentripplanner.transit.model.timetable.RealTimeTripTimes;
import org.opentripplanner.transit.model.timetable.TripTimes;
import org.opentripplanner.updater.spi.DataValidationExceptionMapper;
import org.opentripplanner.updater.spi.UpdateError;
Expand Down Expand Up @@ -94,7 +95,7 @@ public ModifiedTripBuilder(
* in form the SIRI-ET update.
*/
public Result<TripUpdate, UpdateError> build() {
TripTimes newTimes = existingTripTimes.copyOfScheduledTimes();
RealTimeTripTimes newTimes = existingTripTimes.copyScheduledTimes();

StopPattern stopPattern = createStopPattern(pattern, calls, entityResolver);

Expand Down Expand Up @@ -145,7 +146,7 @@ public Result<TripUpdate, UpdateError> build() {
/**
* Applies real-time updates from the calls into newTimes.
*/
private void applyUpdates(TripTimes newTimes) {
private void applyUpdates(RealTimeTripTimes newTimes) {
ZonedDateTime startOfService = ServiceDateUtils.asStartOfService(serviceDate, zoneId);
Set<CallWrapper> alreadyVisited = new HashSet<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.opentripplanner.transit.model.framework.DataValidationException;
import org.opentripplanner.transit.model.framework.Result;
import org.opentripplanner.transit.model.network.TripPattern;
import org.opentripplanner.transit.model.timetable.RealTimeTripTimes;
import org.opentripplanner.transit.model.timetable.Trip;
import org.opentripplanner.transit.model.timetable.TripTimes;
import org.opentripplanner.transit.service.DefaultTransitService;
Expand Down Expand Up @@ -401,7 +402,7 @@ private boolean markScheduledTripAsDeleted(Trip trip, final LocalDate serviceDat
if (tripTimes == null) {
LOG.warn("Could not mark scheduled trip as deleted {}", trip.getId());
} else {
final TripTimes newTripTimes = tripTimes.copyOfScheduledTimes();
final RealTimeTripTimes newTripTimes = tripTimes.copyScheduledTimes();
newTripTimes.deleteTrip();
buffer.update(pattern, newTripTimes, serviceDate);
success = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.opentripplanner.ext.siri.mapper.OccupancyMapper;
import org.opentripplanner.framework.i18n.NonLocalizedString;
import org.opentripplanner.framework.time.ServiceDateUtils;
import org.opentripplanner.transit.model.timetable.TripTimes;
import org.opentripplanner.transit.model.timetable.RealTimeTripTimes;
import uk.org.siri.siri20.NaturalLanguageStringStructure;
import uk.org.siri.siri20.OccupancyEnumeration;

Expand Down Expand Up @@ -52,7 +52,7 @@ private static int handleMissingRealtime(int... times) {

public static void applyUpdates(
ZonedDateTime departureDate,
TripTimes tripTimes,
RealTimeTripTimes tripTimes,
int index,
boolean isLastStop,
boolean isJourneyPredictionInaccurate,
Expand Down

This file was deleted.

7 changes: 4 additions & 3 deletions src/main/java/org/opentripplanner/model/Timetable.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.opentripplanner.transit.model.network.TripPattern;
import org.opentripplanner.transit.model.timetable.Direction;
import org.opentripplanner.transit.model.timetable.FrequencyEntry;
import org.opentripplanner.transit.model.timetable.RealTimeTripTimes;
import org.opentripplanner.transit.model.timetable.Trip;
import org.opentripplanner.transit.model.timetable.TripTimes;
import org.opentripplanner.updater.GtfsRealtimeMapper;
Expand Down Expand Up @@ -204,7 +205,7 @@ public Result<TripTimesPatch, UpdateError> createUpdatedTripTimes(
LOG.trace("tripId {} found at index {} in timetable.", tripId, tripIndex);
}

TripTimes newTimes = getTripTimes(tripIndex).copyOfScheduledTimes();
RealTimeTripTimes newTimes = getTripTimes(tripIndex).copyScheduledTimes();
List<Integer> skippedStopIndices = new ArrayList<>();

// The GTFS-RT reference specifies that StopTimeUpdates are sorted by stop_sequence.
Expand Down Expand Up @@ -413,12 +414,12 @@ public boolean isValidFor(LocalDate serviceDate) {
// TODO maybe put this is a more appropriate place
public void setServiceCodes(Map<FeedScopedId, Integer> serviceCodes) {
for (TripTimes tt : this.tripTimes) {
tt.setServiceCode(serviceCodes.get(tt.getTrip().getServiceId()));
((RealTimeTripTimes) tt).setServiceCode(serviceCodes.get(tt.getTrip().getServiceId()));
}
// Repeated code... bad sign...
for (FrequencyEntry freq : this.frequencyEntries) {
TripTimes tt = freq.tripTimes;
tt.setServiceCode(serviceCodes.get(tt.getTrip().getServiceId()));
((RealTimeTripTimes) tt).setServiceCode(serviceCodes.get(tt.getTrip().getServiceId()));
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/main/java/org/opentripplanner/model/TripTimesPatch.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.opentripplanner.model;

import java.util.List;
import org.opentripplanner.transit.model.timetable.RealTimeTripTimes;
import org.opentripplanner.transit.model.timetable.TripTimes;

/**
Expand All @@ -9,15 +10,15 @@
*/
public class TripTimesPatch {

private final TripTimes tripTimes;
private final RealTimeTripTimes tripTimes;
private final List<Integer> skippedStopIndices;

public TripTimesPatch(TripTimes tripTimes, List<Integer> skippedStopIndices) {
public TripTimesPatch(RealTimeTripTimes tripTimes, List<Integer> skippedStopIndices) {
this.tripTimes = tripTimes;
this.skippedStopIndices = skippedStopIndices;
}

public TripTimes getTripTimes() {
public RealTimeTripTimes getTripTimes() {
return this.tripTimes;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.Objects;
import java.util.stream.Collectors;
import org.opentripplanner.graph_builder.issue.api.DataImportIssueStore;
import org.opentripplanner.model.StopTime;
import org.opentripplanner.netex.index.api.ReadOnlyHierarchicalMap;
import org.opentripplanner.netex.index.api.ReadOnlyHierarchicalMapById;
import org.opentripplanner.netex.mapping.support.FeedScopedIdFactory;
Expand Down Expand Up @@ -360,19 +361,16 @@ private org.opentripplanner.transit.model.network.Route lookupRoute(

private void createTripTimes(List<Trip> trips, TripPattern tripPattern) {
for (Trip trip : trips) {
if (result.tripStopTimes.get(trip).size() == 0) {
List<StopTime> stopTimes = result.tripStopTimes.get(trip);
if (stopTimes.size() == 0) {
issueStore.add(
"TripWithoutTripTimes",
"Trip %s does not contain any trip times.",
trip.getId()
);
} else {
try {
TripTimes tripTimes = TripTimesFactory.tripTimes(
trip,
result.tripStopTimes.get(trip),
deduplicator
);
TripTimes tripTimes = TripTimesFactory.tripTimes(trip, stopTimes, deduplicator);
tripPattern.add(tripTimes);
} catch (DataValidationException e) {
issueStore.add(e.error());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
/**
* Uses a TripTimes to represent multiple trips following the same template at regular intervals.
* (see GTFS frequencies.txt)
* <p>
* Refactor this to inherit {@link TripTimes}
*/
public class FrequencyEntry implements Serializable {

Expand Down Expand Up @@ -116,11 +118,13 @@ public int prevArrivalTime(int stop, int t) {
* Returns a disposable TripTimes for this frequency entry in which the vehicle passes the given
* stop index (not stop sequence number) at the given time. This allows us to separate the
* departure/arrival search process from actually instantiating a TripTimes, to avoid making too
* many short-lived clones. This delegation is a sign that maybe FrequencyEntry should subclass
* many short-lived clones. This delegation is a sign that maybe FrequencyEntry should implement
* TripTimes.
*/
public TripTimes materialize(int stop, int time, boolean depart) {
return tripTimes.timeShift(stop, time, depart);
// TODO: The cast should be changed, the internal type should be scheduledTripTimes and the
// shift method should partially be inlined here
return ((RealTimeTripTimes) tripTimes).timeShift(stop, time, depart);
}

/** Used in debugging / dumping times. */
Expand Down
Loading

0 comments on commit 207bb3f

Please sign in to comment.