Skip to content

Refactor: Separate the Siri TripUpdate into AddedTrip and ModifiedTrip classes #6489

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

Draft
wants to merge 1 commit into
base: dev-2.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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 @@ -163,7 +163,7 @@ class AddedTripBuilder {
this.dataSource = dataSource;
}

Result<TripUpdate, UpdateError> build() {
Result<SiriTripUpdate.SiriAddTrip, UpdateError> build() {
if (calls.size() < 2) {
return UpdateError.result(tripId, TOO_FEW_STOPS, dataSource);
}
Expand Down Expand Up @@ -228,7 +228,7 @@ Result<TripUpdate, UpdateError> build() {
tripTimes.validateNonIncreasingTimes();
tripTimes.setServiceCode(transitService.getServiceCode(trip.getServiceId()));

TripPattern pattern = TripPattern
TripPattern addedTripPattern = TripPattern
.of(getTripPatternId.apply(trip))
.withRoute(trip.getRoute())
.withMode(trip.getMode())
Expand Down Expand Up @@ -273,12 +273,12 @@ Result<TripUpdate, UpdateError> build() {
.build();

return Result.success(
new TripUpdate(
new SiriTripUpdate.SiriAddTrip(
stopPattern,
updatedTripTimes,
serviceDate,
tripOnServiceDate,
pattern,
addedTripPattern,
isAddedRoute,
dataSource
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public ModifiedTripBuilder(
* Create a new StopPattern and TripTimes for the trip based on the calls, and other fields read
* in form the SIRI-ET update.
*/
public Result<TripUpdate, UpdateError> build() {
public Result<SiriTripUpdate.SiriModifyTrip, UpdateError> build() {
RealTimeTripTimes newTimes = existingTripTimes.copyScheduledTimes();

var stopPattern = createStopPattern(pattern, calls, entityResolver);
Expand All @@ -108,7 +108,12 @@ public Result<TripUpdate, UpdateError> build() {
LOG.debug("Trip is cancelled");
newTimes.cancelTrip();
return Result.success(
new TripUpdate(pattern.getStopPattern(), newTimes, serviceDate, dataSource)
new SiriTripUpdate.SiriModifyTrip(
pattern.getStopPattern(),
newTimes,
serviceDate,
dataSource
)
);
}

Expand Down Expand Up @@ -147,7 +152,9 @@ public Result<TripUpdate, UpdateError> build() {
}

LOG.debug("A valid TripUpdate object was applied using the Timetable class update method.");
return Result.success(new TripUpdate(stopPattern, newTimes, serviceDate, dataSource));
return Result.success(
new SiriTripUpdate.SiriModifyTrip(stopPattern, newTimes, serviceDate, dataSource)
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ private Result<UpdateSuccess, UpdateError> apply(
boolean shouldAddNewTrip = false;
try {
shouldAddNewTrip = shouldAddNewTrip(journey, entityResolver);
Result<TripUpdate, UpdateError> result;
Result<? extends SiriTripUpdate, UpdateError> result;
if (shouldAddNewTrip) {
result =
new AddedTripBuilder(
Expand Down Expand Up @@ -188,7 +188,7 @@ private Timetable getCurrentTimetable(TripPattern tripPattern, LocalDate service
return snapshotManager.getTimetableSnapshotBuffer().resolve(tripPattern, serviceDate);
}

private Result<TripUpdate, UpdateError> handleModifiedTrip(
private Result<SiriTripUpdate.SiriModifyTrip, UpdateError> handleModifiedTrip(
@Nullable SiriFuzzyTripMatcher fuzzyTripMatcher,
EntityResolver entityResolver,
EstimatedVehicleJourney estimatedVehicleJourney
Expand Down Expand Up @@ -274,31 +274,46 @@ private Result<TripUpdate, UpdateError> handleModifiedTrip(
/**
* Add a (new) trip to the timetableRepository and the buffer
*/
private Result<UpdateSuccess, UpdateError> addTripToGraphAndBuffer(TripUpdate tripUpdate) {
private Result<UpdateSuccess, UpdateError> addTripToGraphAndBuffer(SiriTripUpdate tripUpdate) {
Trip trip = tripUpdate.tripTimes().getTrip();
LocalDate serviceDate = tripUpdate.serviceDate();

final TripPattern pattern;
if (tripUpdate.tripPatternCreation()) {
pattern = tripUpdate.addedTripPattern();
} else {
// Get cached trip pattern or create one if it doesn't exist yet
pattern =
tripPatternCache.getOrCreateTripPattern(tripUpdate.stopPattern(), trip, serviceDate);
}
final RealTimeTripUpdate realTimeTripUpdate;
switch (tripUpdate) {
case SiriTripUpdate.SiriAddTrip addTrip -> {
// Add new trip times to buffer, making protective copies as needed. Bubble success/error up.
realTimeTripUpdate =
new RealTimeTripUpdate(
addTrip.addedTripPattern(),
addTrip.tripTimes(),
addTrip.serviceDate(),
addTrip.addedTripOnServiceDate(),
true,
addTrip.routeCreation(),
addTrip.dataSource()
);
}
case SiriTripUpdate.SiriModifyTrip modifyTrip -> {
// Get cached trip pattern or create one if it doesn't exist yet
var tripPattern = tripPatternCache.getOrCreateTripPattern(
modifyTrip.stopPattern(),
trip,
modifyTrip.serviceDate()
);

// Add new trip times to buffer, making protective copies as needed. Bubble success/error up.
RealTimeTripUpdate realTimeTripUpdate = new RealTimeTripUpdate(
pattern,
tripUpdate.tripTimes(),
serviceDate,
tripUpdate.addedTripOnServiceDate(),
tripUpdate.tripCreation(),
tripUpdate.routeCreation(),
tripUpdate.dataSource()
);
realTimeTripUpdate =
new RealTimeTripUpdate(
tripPattern,
modifyTrip.tripTimes(),
modifyTrip.serviceDate(),
null,
false,
false,
modifyTrip.dataSource()
);
}
}
var result = snapshotManager.updateBuffer(realTimeTripUpdate);
LOG.debug("Applied real-time data for trip {} on {}", trip, serviceDate);
LOG.debug("Applied real-time data for trip {} on {}", trip, tripUpdate.serviceDate());
return result;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.opentripplanner.updater.trip.siri;

import java.time.LocalDate;
import javax.annotation.Nullable;
import org.opentripplanner.transit.model.network.StopPattern;
import org.opentripplanner.transit.model.network.TripPattern;
import org.opentripplanner.transit.model.timetable.RealTimeTripTimes;
import org.opentripplanner.transit.model.timetable.TripOnServiceDate;
import org.opentripplanner.transit.model.timetable.TripTimes;

/**
* This is a DTO used internally in the SIRI code for holding on to realtime trip update information
*/
public sealed interface SiriTripUpdate
permits SiriTripUpdate.SiriAddTrip, SiriTripUpdate.SiriModifyTrip {
TripTimes tripTimes();
LocalDate serviceDate();

/**
* A message containing information for modifying an existing trip.
*
* @param stopPattern The stop pattern for the modified trip.
* @param tripTimes The updated trip times for the modified trip.
* @param serviceDate The service date for which this update applies (updates are valid
* only for one service date)
* @param dataSource The dataSource of the real-time update.
*/
record SiriModifyTrip(
StopPattern stopPattern,
RealTimeTripTimes tripTimes,
LocalDate serviceDate,
@Nullable String dataSource
)
implements SiriTripUpdate {}

/**
* A message with information for adding a new trip
*
* @param stopPattern The stop pattern to which belongs the created trip.
* @param tripTimes The trip times for the created trip.
* @param serviceDate The service date for which this update applies (updates are valid
* only for one service date)
* @param addedTripOnServiceDate TripOnServiceDate corresponding to the new trip.
* @param addedTripPattern The new trip pattern for the new trip.
* @param routeCreation true if an added trip cannot be registered under an existing route
* and a new route must be created.
* @param dataSource The dataSource of the real-time update.
*/
record SiriAddTrip(
StopPattern stopPattern,
TripTimes tripTimes,
LocalDate serviceDate,
TripOnServiceDate addedTripOnServiceDate,
TripPattern addedTripPattern,
boolean routeCreation,
@Nullable String dataSource
)
implements SiriTripUpdate {}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ void testAddedTrip() {

assertTrue(addedTrip.isSuccess(), "Trip creation should succeed");

TripUpdate tripUpdate = addedTrip.successValue();
var tripUpdate = addedTrip.successValue();
// Assert trip
Trip trip = tripUpdate.tripTimes().getTrip();
assertEquals(TRIP_ID, trip.getId(), "Trip should be mapped");
Expand All @@ -164,8 +164,6 @@ void testAddedTrip() {

assertTrue(tripUpdate.routeCreation(), "The route is marked as created by real time updater");

assertTrue(tripUpdate.tripCreation(), "The trip is marked as created by real time updater");

TripPattern pattern = tripUpdate.addedTripPattern();
assertNotNull(pattern);
assertEquals(route, pattern.getRoute());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ void testUpdateNoCalls() {

assertTrue(result.isSuccess(), "Update should succeed");

TripUpdate tripUpdate = result.successValue();
var tripUpdate = result.successValue();
assertEquals(PATTERN.getStopPattern(), tripUpdate.stopPattern());
TripTimes updatedTimes = tripUpdate.tripTimes();
assertEquals(STOP_TIME_A_1.getArrivalTime(), updatedTimes.getArrivalTime(0));
Expand Down Expand Up @@ -211,7 +211,7 @@ void testUpdateCancellation() {

assertTrue(result.isSuccess(), "Update should succeed");

TripUpdate tripUpdate = result.successValue();
var tripUpdate = result.successValue();
assertEquals(PATTERN.getStopPattern(), tripUpdate.stopPattern());
TripTimes updatedTimes = tripUpdate.tripTimes();
assertEquals(RealTimeState.CANCELED, updatedTimes.getRealTimeState());
Expand Down Expand Up @@ -256,7 +256,7 @@ void testUpdateSameStops() {

assertTrue(result.isSuccess(), "Update should succeed");

TripUpdate tripUpdate = result.successValue();
var tripUpdate = result.successValue();
assertEquals(PATTERN.getStopPattern(), tripUpdate.stopPattern());
TripTimes updatedTimes = tripUpdate.tripTimes();
assertEquals(secondsInDay(10, 1), updatedTimes.getArrivalTime(0));
Expand Down Expand Up @@ -356,7 +356,7 @@ void testUpdateSameStopsDepartEarly() {

assertTrue(result.isSuccess(), "Update should succeed");

TripUpdate tripUpdate = result.successValue();
var tripUpdate = result.successValue();
assertEquals(PATTERN.getStopPattern(), tripUpdate.stopPattern());
TripTimes updatedTimes = tripUpdate.tripTimes();
assertEquals(secondsInDay(9, 58), updatedTimes.getArrivalTime(0));
Expand Down Expand Up @@ -407,7 +407,7 @@ void testUpdateUpdatedStop() {

assertTrue(result.isSuccess(), "Update should succeed");

TripUpdate tripUpdate = result.successValue();
var tripUpdate = result.successValue();
StopPattern stopPattern = tripUpdate.stopPattern();
assertNotEquals(PATTERN.getStopPattern(), stopPattern);
assertEquals(STOP_A_2, stopPattern.getStop(0));
Expand Down Expand Up @@ -508,7 +508,7 @@ void testUpdateCascading() {
)
.build();

TripUpdate tripUpdate = secondResult.successValue();
var tripUpdate = secondResult.successValue();
StopPattern stopPattern = tripUpdate.stopPattern();
assertEquals(PATTERN.getStopPattern(), stopPattern);

Expand Down
Loading