Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/dev-2.x' into netex-parking
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardehrenfried committed Jul 19, 2024
2 parents 990ce7c + c3c1ea0 commit 6181512
Show file tree
Hide file tree
Showing 15 changed files with 514 additions and 767 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
<jersey.version>3.1.7</jersey.version>
<junit.version>5.10.3</junit.version>
<micrometer.version>1.13.2</micrometer.version>
<netcdf4.version>5.5.3</netcdf4.version>
<netcdf4.version>5.6.0</netcdf4.version>
<logback.version>1.5.6</logback.version>
<lucene.version>9.11.1</lucene.version>
<slf4j.version>2.0.13</slf4j.version>
Expand Down Expand Up @@ -247,7 +247,7 @@
<dependency>
<groupId>me.fabriciorby</groupId>
<artifactId>maven-surefire-junit5-tree-reporter</artifactId>
<version>1.2.1</version>
<version>1.3.0</version>
</dependency>
</dependencies>
<configuration>
Expand Down Expand Up @@ -321,7 +321,7 @@
but we need the Maven project version as well, so we perform substitution. -->
<groupId>io.github.git-commit-id</groupId>
<artifactId>git-commit-id-maven-plugin</artifactId>
<version>9.0.0</version>
<version>9.0.1</version>
<executions>
<execution>
<goals>
Expand Down
1 change: 1 addition & 0 deletions renovate.json5
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
"org.mockito:mockito-core",
"com.tngtech.archunit:archunit",
"org.apache.maven.plugins:maven-surefire-plugin",
"me.fabriciorby:maven-surefire-junit5-tree-reporter",
"org.jacoco:jacoco-maven-plugin", // coverage plugin
"org.apache.commons:commons-compress", // only used by tests
// maven plugins
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package org.opentripplanner.ext.siri;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.opentripplanner.updater.spi.UpdateResultAssertions.assertFailure;

import java.util.Set;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.opentripplanner.transit.model.timetable.RealTimeState;
import org.opentripplanner.updater.spi.UpdateError;
import org.opentripplanner.updater.spi.UpdateResult;
import org.opentripplanner.updater.trip.RealtimeTestEnvironment;

class SiriTimetableSnapshotSourceTest {
Expand Down Expand Up @@ -415,10 +415,6 @@ void testExtraUnknownStop() {
assertFailure(UpdateError.UpdateErrorType.INVALID_STOP_SEQUENCE, result);
}

private void assertFailure(UpdateError.UpdateErrorType expectedError, UpdateResult result) {
assertEquals(Set.of(expectedError), result.failures().keySet());
}

private static SiriEtBuilder updatedJourneyBuilder(RealtimeTestEnvironment env) {
return new SiriEtBuilder(env.getDateTimeHelper())
.withEstimatedCalls(builder ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,17 +331,6 @@ public boolean isBoardAndAlightAt(int stopIndex, PickDrop value) {

/* METHODS THAT DELEGATE TO THE SCHEDULED TIMETABLE */

// TODO: These should probably be deprecated. That would require grabbing the scheduled timetable,
// and would avoid mistakes where real-time updates are accidentally not taken into account.

public boolean stopPatternIsEqual(TripPattern other) {
return stopPattern.equals(other.stopPattern);
}

public Trip getTrip(int tripIndex) {
return scheduledTimetable.getTripTimes(tripIndex).getTrip();
}

// TODO OTP2 this method modifies the state, it will be refactored in a subsequent step
/**
* Add the given tripTimes to this pattern's scheduled timetable, recording the corresponding trip
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.opentripplanner.updater.trip;

import static com.google.transit.realtime.GtfsRealtime.TripDescriptor.ScheduleRelationship.SCHEDULED;
import static org.opentripplanner.updater.spi.UpdateError.UpdateErrorType.INVALID_ARRIVAL_TIME;
import static org.opentripplanner.updater.spi.UpdateError.UpdateErrorType.INVALID_DEPARTURE_TIME;
import static org.opentripplanner.updater.spi.UpdateError.UpdateErrorType.INVALID_INPUT_STRUCTURE;
import static org.opentripplanner.updater.spi.UpdateError.UpdateErrorType.NOT_IMPLEMENTED_DUPLICATED;
import static org.opentripplanner.updater.spi.UpdateError.UpdateErrorType.NOT_IMPLEMENTED_UNSCHEDULED;
import static org.opentripplanner.updater.spi.UpdateError.UpdateErrorType.NO_SERVICE_ON_DATE;
Expand All @@ -19,6 +21,7 @@
import com.google.common.collect.Multimaps;
import com.google.transit.realtime.GtfsRealtime;
import com.google.transit.realtime.GtfsRealtime.TripDescriptor;
import com.google.transit.realtime.GtfsRealtime.TripDescriptor.ScheduleRelationship;
import com.google.transit.realtime.GtfsRealtime.TripUpdate;
import com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeUpdate;
import de.mfdz.MfdzRealtimeExtensions;
Expand Down Expand Up @@ -150,7 +153,7 @@ public UpdateResult applyTripUpdates(
return UpdateResult.empty();
}

Map<TripDescriptor.ScheduleRelationship, Integer> failuresByRelationship = new HashMap<>();
Map<ScheduleRelationship, Integer> failuresByRelationship = new HashMap<>();
List<Result<UpdateSuccess, UpdateError>> results = new ArrayList<>();

snapshotManager.withLock(() -> {
Expand All @@ -176,6 +179,7 @@ public UpdateResult applyTripUpdates(

if (!tripDescriptor.hasTripId() || tripDescriptor.getTripId().isBlank()) {
debug(feedId, "", "No trip id found for gtfs-rt trip update: \n{}", tripUpdate);
results.add(Result.failure(UpdateError.noTripId(INVALID_INPUT_STRUCTURE)));
continue;
}

Expand All @@ -199,11 +203,12 @@ public UpdateResult applyTripUpdates(
serviceDate = localDateNow.get();
}
// Determine what kind of trip update this is
final TripDescriptor.ScheduleRelationship tripScheduleRelationship = determineTripScheduleRelationship(
tripDescriptor
var scheduleRelationship = Objects.requireNonNullElse(
tripDescriptor.getScheduleRelationship(),
SCHEDULED
);
if (updateIncrementality == DIFFERENTIAL) {
purgePatternModifications(tripScheduleRelationship, tripId, serviceDate);
purgePatternModifications(scheduleRelationship, tripId, serviceDate);
}

uIndex += 1;
Expand All @@ -213,7 +218,7 @@ public UpdateResult applyTripUpdates(
Result<UpdateSuccess, UpdateError> result;
try {
result =
switch (tripScheduleRelationship) {
switch (scheduleRelationship) {
case SCHEDULED -> handleScheduledTrip(
tripUpdate,
tripId,
Expand Down Expand Up @@ -255,11 +260,11 @@ public UpdateResult applyTripUpdates(
if (result.isFailure()) {
debug(tripId, "Failed to apply TripUpdate.");
LOG.trace(" Contents: {}", tripUpdate);
if (failuresByRelationship.containsKey(tripScheduleRelationship)) {
var c = failuresByRelationship.get(tripScheduleRelationship);
failuresByRelationship.put(tripScheduleRelationship, ++c);
if (failuresByRelationship.containsKey(scheduleRelationship)) {
var c = failuresByRelationship.get(scheduleRelationship);
failuresByRelationship.put(scheduleRelationship, ++c);
} else {
failuresByRelationship.put(tripScheduleRelationship, 1);
failuresByRelationship.put(scheduleRelationship, 1);
}
}
}
Expand All @@ -282,16 +287,16 @@ public UpdateResult applyTripUpdates(
* added trip pattern.
*/
private void purgePatternModifications(
TripDescriptor.ScheduleRelationship tripScheduleRelationship,
ScheduleRelationship tripScheduleRelationship,
FeedScopedId tripId,
LocalDate serviceDate
) {
final TripPattern pattern = snapshotManager.getRealtimeAddedTripPattern(tripId, serviceDate);
if (
!isPreviouslyAddedTrip(tripId, pattern, serviceDate) ||
(
tripScheduleRelationship != TripDescriptor.ScheduleRelationship.CANCELED &&
tripScheduleRelationship != TripDescriptor.ScheduleRelationship.DELETED
tripScheduleRelationship != ScheduleRelationship.CANCELED &&
tripScheduleRelationship != ScheduleRelationship.DELETED
)
) {
// Remove previous realtime updates for this trip. This is necessary to avoid previous
Expand Down Expand Up @@ -327,7 +332,7 @@ public TimetableSnapshot getTimetableSnapshot() {

private static void logUpdateResult(
String feedId,
Map<TripDescriptor.ScheduleRelationship, Integer> failuresByRelationship,
Map<ScheduleRelationship, Integer> failuresByRelationship,
UpdateResult updateResult
) {
ResultLogger.logUpdateResult(feedId, "gtfs-rt-trip-updates", updateResult);
Expand All @@ -345,27 +350,6 @@ private static void logUpdateResult(
});
}

/**
* Determine how the trip update should be handled.
*
* @param tripDescriptor trip descriptor
* @return TripDescriptor.ScheduleRelationship indicating how the trip update should be handled
*/
private TripDescriptor.ScheduleRelationship determineTripScheduleRelationship(
final TripDescriptor tripDescriptor
) {
// Assume default value
TripDescriptor.ScheduleRelationship tripScheduleRelationship =
TripDescriptor.ScheduleRelationship.SCHEDULED;

// If trip update contains schedule relationship, use it
if (tripDescriptor.hasScheduleRelationship()) {
tripScheduleRelationship = tripDescriptor.getScheduleRelationship();
}

return tripScheduleRelationship;
}

private Result<UpdateSuccess, UpdateError> handleScheduledTrip(
TripUpdate tripUpdate,
FeedScopedId tripId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,8 @@ void testInterline(
void staySeatedNotAllowed() {
var transferService = new DefaultTransferService();

var fromTrip = patterns.get(0).getTrip(0);
var toTrip = patterns.get(1).getTrip(0);

var fromTrip = patterns.get(0).getScheduledTimetable().getTripTimes().get(0).getTrip();
var toTrip = patterns.get(1).getScheduledTimetable().getTripTimes().get(0).getTrip();
var notAllowed = new StaySeatedNotAllowed(fromTrip, toTrip);

var calendarService = new CalendarServiceData();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.opentripplanner.updater.spi;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Set;

public class UpdateResultAssertions {

public static void assertFailure(UpdateError.UpdateErrorType expectedError, UpdateResult result) {
assertEquals(Set.of(expectedError), result.failures().keySet());
}

public static void assertSuccess(UpdateResult updateResult) {
var errorCodes = updateResult.failures().keySet();
assertEquals(
Set.of(),
errorCodes,
"Update result should have no error codes but had %s".formatted(errorCodes)
);
assertTrue(updateResult.successful() > 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.opentripplanner.ext.siri.SiriFuzzyTripMatcher;
import org.opentripplanner.ext.siri.SiriTimetableSnapshotSource;
import org.opentripplanner.ext.siri.updater.EstimatedTimetableHandler;
import org.opentripplanner.framework.i18n.I18NString;
import org.opentripplanner.graph_builder.issue.api.DataImportIssueStore;
import org.opentripplanner.model.StopTime;
import org.opentripplanner.model.TimetableSnapshot;
Expand Down Expand Up @@ -55,16 +56,19 @@ public final class RealtimeTestEnvironment {
);
public static final LocalDate SERVICE_DATE = LocalDate.of(2024, 5, 8);
public static final FeedScopedId SERVICE_ID = TransitModelForTest.id("CAL_1");
public static final String STOP_A1_ID = "A1";
public static final String STOP_B1_ID = "B1";
public static final String STOP_C1_ID = "C1";
private final TransitModelForTest testModel = TransitModelForTest.of();
public final ZoneId timeZone = ZoneId.of(TransitModelForTest.TIME_ZONE_ID);
public final Station stationA = testModel.station("A").build();
public final Station stationB = testModel.station("B").build();
public final Station stationC = testModel.station("C").build();
public final Station stationD = testModel.station("D").build();
public final RegularStop stopA1 = testModel.stop("A1").withParentStation(stationA).build();
public final RegularStop stopB1 = testModel.stop("B1").withParentStation(stationB).build();
public final RegularStop stopA1 = testModel.stop(STOP_A1_ID).withParentStation(stationA).build();
public final RegularStop stopB1 = testModel.stop(STOP_B1_ID).withParentStation(stationB).build();
public final RegularStop stopB2 = testModel.stop("B2").withParentStation(stationB).build();
public final RegularStop stopC1 = testModel.stop("C1").withParentStation(stationC).build();
public final RegularStop stopC1 = testModel.stop(STOP_C1_ID).withParentStation(stationC).build();
public final RegularStop stopD1 = testModel.stop("D1").withParentStation(stationD).build();
public final StopModel stopModel = testModel
.stopModelBuilder()
Expand Down Expand Up @@ -110,12 +114,20 @@ private RealtimeTestEnvironment(SourceType sourceType) {
Route route1 = TransitModelForTest.route(route1Id).build();

trip1 =
createTrip("TestTrip1", route1, List.of(new Stop(stopA1, 10, 11), new Stop(stopB1, 20, 21)));
createTrip(
"TestTrip1",
route1,
List.of(new StopCall(stopA1, 10, 11), new StopCall(stopB1, 20, 21))
);
trip2 =
createTrip(
"TestTrip2",
route1,
List.of(new Stop(stopA1, 60, 61), new Stop(stopB1, 70, 71), new Stop(stopC1, 80, 81))
List.of(
new StopCall(stopA1, 60, 61),
new StopCall(stopB1, 70, 71),
new StopCall(stopC1, 80, 81)
)
);

CalendarServiceData calendarServiceData = new CalendarServiceData();
Expand Down Expand Up @@ -291,8 +303,13 @@ private UpdateResult applyEstimatedTimetable(
return getEstimatedTimetableHandler(fuzzyMatching).applyUpdate(updates, DIFFERENTIAL);
}

private Trip createTrip(String id, Route route, List<Stop> stops) {
var trip = Trip.of(id(id)).withRoute(route).withServiceId(SERVICE_ID).build();
private Trip createTrip(String id, Route route, List<StopCall> stops) {
var trip = Trip
.of(id(id))
.withRoute(route)
.withHeadsign(I18NString.of("Headsign of %s".formatted(id)))
.withServiceId(SERVICE_ID)
.build();

var tripOnServiceDate = TripOnServiceDate
.of(trip.getId())
Expand All @@ -314,7 +331,7 @@ private Trip createTrip(String id, Route route, List<Stop> stops) {

final TripPattern pattern = TransitModelForTest
.tripPattern(id + "Pattern", route)
.withStopPattern(TransitModelForTest.stopPattern(stops.stream().map(Stop::stop).toList()))
.withStopPattern(TransitModelForTest.stopPattern(stops.stream().map(StopCall::stop).toList()))
.build();
pattern.add(tripTimes);

Expand All @@ -339,5 +356,5 @@ private StopTime createStopTime(
return st;
}

protected record Stop(RegularStop stop, int arrivalTime, int departureTime) {}
private record StopCall(RegularStop stop, int arrivalTime, int departureTime) {}
}
Loading

0 comments on commit 6181512

Please sign in to comment.