Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup trip times - Part A #5437

Merged
merged 12 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.opentripplanner.transit.model.framework.FeedScopedId;
import org.opentripplanner.transit.model.network.Route;
import org.opentripplanner.transit.model.timetable.Trip;
import org.opentripplanner.transit.model.timetable.TripTimes;
import org.opentripplanner.transit.model.timetable.TripTimesFactory;

class EmissionsTest {

Expand Down Expand Up @@ -130,7 +130,7 @@ private ScheduledTransitLeg createTransitLeg(Route route) {
.withRoute(route)
.build();
return new ScheduledTransitLegBuilder<>()
.withTripTimes(new TripTimes(trip, stopTimes, new Deduplicator()))
.withTripTimes(TripTimesFactory.tripTimes(trip, stopTimes, new Deduplicator()))
.withTripPattern(pattern)
.withBoardStopIndexInPattern(0)
.withAlightStopIndexInPattern(2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private static TripPattern delay(TripPattern pattern1, int seconds) {
}

private static TripTimes delay(TripTimes tt, int seconds) {
var delayed = new TripTimes(tt);
var delayed = tt.copyOfScheduledTimes();
IntStream
.range(0, delayed.getNumStops())
.forEach(i -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.opentripplanner.transit.model.timetable.RealTimeState;
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.DefaultTransitService;
import org.opentripplanner.transit.service.StopModel;
import org.opentripplanner.transit.service.TransitModel;
Expand Down Expand Up @@ -111,7 +112,7 @@ class ModifiedTripBuilderTest {
STOP_TIME_C_1.setStopSequence(1);
}

private static final TripTimes TRIP_TIMES = new TripTimes(
private static final TripTimes TRIP_TIMES = TripTimesFactory.tripTimes(
TRIP,
List.of(STOP_TIME_A_1, STOP_TIME_B_1, STOP_TIME_C_1),
DEDUPLICATOR
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.opentripplanner.transit.model.timetable.OccupancyStatus;
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.StopModel;
import uk.org.siri.siri20.OccupancyEnumeration;

Expand Down Expand Up @@ -74,7 +75,7 @@ public void setUp() {
.build();

Trip trip = Trip.of(new FeedScopedId(FEED_ID, "TRIP_ID")).withRoute(route).build();
tripTimes = new TripTimes(trip, List.of(stopTime), new Deduplicator());
tripTimes = TripTimesFactory.tripTimes(trip, List.of(stopTime), new Deduplicator());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.opentripplanner.transit.model.timetable.RealTimeState;
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.TripTimesValidationMapper;
import org.opentripplanner.updater.spi.UpdateError;
Expand Down Expand Up @@ -201,10 +202,14 @@ Result<TripUpdate, UpdateError> build() {
.withStopPattern(stopPattern)
.build();

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

// 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 @@ -94,7 +94,7 @@ public ModifiedTripBuilder(
* in form the SIRI-ET update.
*/
public Result<TripUpdate, UpdateError> build() {
TripTimes newTimes = new TripTimes(existingTripTimes);
TripTimes newTimes = existingTripTimes.copyOfScheduledTimes();

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,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 = new TripTimes(tripTimes);
final TripTimes newTripTimes = tripTimes.copyOfScheduledTimes();
newTripTimes.deleteTrip();
buffer.update(pattern, newTripTimes, serviceDate);
success = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ public static int[] intArray(int size, int initialValue) {
* Copy int array and shift all values by the given {@code offset}.
*/
public static int[] shiftArray(int offset, int[] array) {
int[] a = new int[array.length];
for (int i = 0; i < array.length; i++) {
a[i] = array[i] + offset;
int[] a = Arrays.copyOf(array, array.length);
for (int i = 0; i < array.length; ++i) {
a[i] += offset;
}
return a;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.util.HashMap;
import java.util.Map;
import org.opentripplanner.graph_builder.model.GraphBuilderModule;
import org.opentripplanner.model.Timetable;
import org.opentripplanner.transit.service.TransitModel;

/**
Expand Down Expand Up @@ -46,13 +45,9 @@ public void buildGraph() {
return;
}

final Timetable scheduledTimetable = pattern.getScheduledTimetable();

scheduledTimetable.getTripTimes().forEach(tripTimes -> tripTimes.timeShift(timeShift));

scheduledTimetable
.getFrequencyEntries()
.forEach(frequencyEntry -> frequencyEntry.tripTimes.timeShift(timeShift));
pattern
.getScheduledTimetable()
.updateAllTripTimes(it -> it.adjustTimesToGraphTimeZone(timeShift));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.opentripplanner.transit.model.timetable.FrequencyEntry;
import org.opentripplanner.transit.model.timetable.Trip;
import org.opentripplanner.transit.model.timetable.TripTimes;
import org.opentripplanner.transit.model.timetable.TripTimesFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -132,7 +133,7 @@ private void buildTripPatternForTrip(Trip trip) {
TripPattern tripPattern = findOrCreateTripPattern(stopPattern, trip, direction);

// Create a TripTimes object for this list of stoptimes, which form one trip.
TripTimes tripTimes = new TripTimes(trip, stopTimes, deduplicator);
TripTimes tripTimes = TripTimesFactory.tripTimes(trip, stopTimes, deduplicator);

// If this trip is referenced by one or more lines in frequencies.txt, wrap it in a FrequencyEntry.
List<Frequency> frequencies = frequenciesForTrip.get(trip);
Expand Down
22 changes: 21 additions & 1 deletion src/main/java/org/opentripplanner/model/Timetable.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.UnaryOperator;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.opentripplanner.framework.time.ServiceDateUtils;
Expand Down Expand Up @@ -202,7 +203,7 @@ public Result<TripTimesPatch, UpdateError> createUpdatedTripTimesFromGTFSRT(
LOG.trace("tripId {} found at index {} in timetable.", tripId, tripIndex);
}

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

// The GTFS-RT reference specifies that StopTimeUpdates are sorted by stop_sequence.
Expand Down Expand Up @@ -396,6 +397,25 @@ public void addTripTimes(TripTimes tt) {
tripTimes.add(tt);
}

/**
* Apply the same update to all trip-times inculuding scheduled and frequency based
* trip times.
* <p>
* THIS IS NOT THREAD-SAFE - ONLY USE THIS METHOD DURING GRAPH-BUILD!
*/
public void updateAllTripTimes(UnaryOperator<TripTimes> update) {
tripTimes.replaceAll(update);
frequencyEntries.replaceAll(it ->
new FrequencyEntry(
it.startTime,
it.endTime,
it.headway,
it.exactTimes,
update.apply(it.tripTimes)
)
);
}

/**
* Add a frequency entry to this Timetable. See addTripTimes method. Maybe Frequency Entries
* should just be TripTimes for simplicity.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.opentripplanner.transit.model.timetable.Trip;
import org.opentripplanner.transit.model.timetable.TripOnServiceDate;
import org.opentripplanner.transit.model.timetable.TripTimes;
import org.opentripplanner.transit.model.timetable.TripTimesFactory;
import org.rutebanken.netex.model.DatedServiceJourney;
import org.rutebanken.netex.model.DatedServiceJourneyRefStructure;
import org.rutebanken.netex.model.DestinationDisplay;
Expand Down Expand Up @@ -366,7 +367,11 @@ private void createTripTimes(List<Trip> trips, TripPattern tripPattern) {
trip.getId()
);
} else {
TripTimes tripTimes = new TripTimes(trip, result.tripStopTimes.get(trip), deduplicator);
TripTimes tripTimes = TripTimesFactory.tripTimes(
trip,
result.tripStopTimes.get(trip),
deduplicator
);
tripPattern.add(tripTimes);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* Does the same thing as String.intern, but for several different types. Java's String.intern uses
* perm gen space and is broken anyway.
*/
public class Deduplicator implements Serializable {
public class Deduplicator implements DeduplicatorService, Serializable {

private static final String ZERO_COUNT = sizeAndCount(0, 0);

Expand Down Expand Up @@ -50,6 +50,7 @@ public void reset() {
canonicalLists.clear();
}

@Override
@Nullable
public BitSet deduplicateBitSet(BitSet original) {
if (original == null) {
Expand All @@ -64,7 +65,7 @@ public BitSet deduplicateBitSet(BitSet original) {
return canonical;
}

/** Used to deduplicate time and stop sequence arrays. The same times may occur in many trips. */
@Override
@Nullable
public int[] deduplicateIntArray(int[] original) {
if (original == null) {
Expand All @@ -80,6 +81,7 @@ public int[] deduplicateIntArray(int[] original) {
return canonical.array;
}

@Override
@Nullable
public String deduplicateString(String original) {
if (original == null) {
Expand All @@ -90,6 +92,7 @@ public String deduplicateString(String original) {
return canonical == null ? original : canonical;
}

@Override
@Nullable
public String[] deduplicateStringArray(String[] original) {
if (original == null) {
Expand All @@ -104,10 +107,7 @@ public String[] deduplicateStringArray(String[] original) {
return canonical.array;
}

/**
* Used to deduplicate list of via places for stop destinationDisplay. Stops may have the same via
* arrays.
*/
@Override
@Nullable
public String[][] deduplicateString2DArray(String[][] original) {
if (original == null) {
Expand All @@ -122,6 +122,7 @@ public String[][] deduplicateString2DArray(String[][] original) {
return canonical.array;
}

@Override
@SuppressWarnings("unchecked")
@Nullable
public <T> T deduplicateObject(Class<T> cl, T original) {
Expand All @@ -137,6 +138,7 @@ public <T> T deduplicateObject(Class<T> cl, T original) {
return canonical == null ? original : canonical;
}

@Override
@Nullable
public <T> T[] deduplicateObjectArray(Class<T> type, T[] original) {
if (original == null) {
Expand All @@ -160,6 +162,7 @@ public <T> T[] deduplicateObjectArray(Class<T> type, T[] original) {
return canonical.array();
}

@Override
@Nullable
public <T> List<T> deduplicateImmutableList(Class<T> clazz, List<T> original) {
if (original == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.opentripplanner.transit.model.framework;

import java.util.BitSet;
import java.util.List;
import javax.annotation.Nullable;

class DeduplicatorNoop implements DeduplicatorService {

@Nullable
@Override
public BitSet deduplicateBitSet(BitSet original) {
return original;
}

@Nullable
@Override
public int[] deduplicateIntArray(int[] original) {
return original;
}

@Nullable
@Override
public String deduplicateString(String original) {
return original;
}

@Nullable
@Override
public String[] deduplicateStringArray(String[] original) {
return original;
}

@Nullable
@Override
public String[][] deduplicateString2DArray(String[][] original) {
return original;
}

@Nullable
@Override
public <T> T deduplicateObject(Class<T> cl, T original) {
return original;
}

@Nullable
@Override
public <T> T[] deduplicateObjectArray(Class<T> type, T[] original) {
return original;
}

@Nullable
@Override
public <T> List<T> deduplicateImmutableList(Class<T> clazz, List<T> original) {
return original;
}
}
Loading
Loading