Skip to content

Commit

Permalink
Move environment methods around, add documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardehrenfried committed May 27, 2024
1 parent 1252e78 commit b4d5495
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 61 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,56 @@
package org.opentripplanner.ext.siri;

import java.time.Duration;
import java.time.LocalDate;
import java.util.List;
import org.opentripplanner.transit.model.framework.FeedScopedId;
import org.opentripplanner.transit.model.network.TripPattern;
import org.opentripplanner.transit.model.timetable.Trip;
import org.opentripplanner.transit.model.timetable.TripTimes;
import org.opentripplanner.updater.TimetableSnapshotSourceParameters;
import org.opentripplanner.updater.spi.UpdateResult;
import org.opentripplanner.updater.trip.AbstractRealtimeTestEnvironment;
import uk.org.siri.siri20.EstimatedTimetableDeliveryStructure;

public class SiriRealtimeTestEnvironment extends AbstractRealtimeTestEnvironment {

private static final TimetableSnapshotSourceParameters PARAMETERS = new TimetableSnapshotSourceParameters(
Duration.ZERO,
false
);
private final SiriTimetableSnapshotSource source;

public SiriRealtimeTestEnvironment() {
super();
var parameters = new TimetableSnapshotSourceParameters(Duration.ZERO, false);
source = new SiriTimetableSnapshotSource(parameters, transitModel);
source = new SiriTimetableSnapshotSource(PARAMETERS, transitModel);
}

public EntityResolver getEntityResolver() {
return new EntityResolver(getTransitService(), getFeedId());
}

public TripPattern getPatternForTrip(FeedScopedId tripId) {
return getPatternForTrip(tripId, serviceDate);
}

public TripPattern getPatternForTrip(FeedScopedId tripId, LocalDate serviceDate) {
var transitService = getTransitService();
var trip = transitService.getTripOnServiceDateById(tripId);
return transitService.getPatternForTrip(trip.getTrip(), serviceDate);
}

/**
* Find the current TripTimes for a trip id on the default serviceDate
*/
public TripTimes getTripTimesForTrip(Trip trip) {
return getTripTimesForTrip(trip.getId(), serviceDate);
}

/**
* Find the current TripTimes for a trip id on the default serviceDate
*/
public TripTimes getTripTimesForTrip(String id) {
return getTripTimesForTrip(id(id), serviceDate);
}

public UpdateResult applyEstimatedTimetable(List<EstimatedTimetableDeliveryStructure> updates) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.opentripplanner.DateTimeHelper;
import org.opentripplanner.ext.siri.EntityResolver;
import org.opentripplanner.graph_builder.issue.api.DataImportIssueStore;
import org.opentripplanner.model.StopTime;
import org.opentripplanner.model.calendar.CalendarServiceData;
Expand All @@ -27,7 +26,7 @@
import org.opentripplanner.transit.service.TransitModel;
import org.opentripplanner.transit.service.TransitService;

public class AbstractRealtimeTestEnvironment {
abstract public class AbstractRealtimeTestEnvironment {

protected static final FeedScopedId CAL_ID = TransitModelForTest.id("CAL_1");
private final TransitModelForTest testModel = TransitModelForTest.of();
Expand Down Expand Up @@ -95,34 +94,6 @@ public TransitService getTransitService() {
return new DefaultTransitService(transitModel);
}

public EntityResolver getEntityResolver() {
return new EntityResolver(getTransitService(), getFeedId());
}

public TripPattern getPatternForTrip(FeedScopedId tripId) {
return getPatternForTrip(tripId, serviceDate);
}

public TripPattern getPatternForTrip(FeedScopedId tripId, LocalDate serviceDate) {
var transitService = getTransitService();
var trip = transitService.getTripOnServiceDateById(tripId);
return transitService.getPatternForTrip(trip.getTrip(), serviceDate);
}

/**
* Find the current TripTimes for a trip id on the default serviceDate
*/
public TripTimes getTripTimesForTrip(Trip trip) {
return getTripTimesForTrip(trip.getId(), serviceDate);
}

/**
* Find the current TripTimes for a trip id on the default serviceDate
*/
public TripTimes getTripTimesForTrip(String id) {
return getTripTimesForTrip(id(id), serviceDate);
}

/**
* Find the current TripTimes for a trip id on a serviceDate
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.google.transit.realtime.GtfsRealtime;
import java.time.Duration;
import java.util.List;
import org.opentripplanner.transit.model.network.TripPattern;
import org.opentripplanner.transit.model.timetable.Trip;
import org.opentripplanner.updater.TimetableSnapshotSourceParameters;
import org.opentripplanner.updater.spi.UpdateResult;

Expand All @@ -29,4 +31,8 @@ public UpdateResult applyTripUpdates(List<GtfsRealtime.TripUpdate> updates) {
getFeedId()
);
}

public TripPattern getPatternForTrip(Trip trip) {
return transitModel.getTransitModelIndex().getPatternForTrip().get(trip);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,83 @@
import static com.google.transit.realtime.GtfsRealtime.TripDescriptor.ScheduleRelationship.SCHEDULED;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertSame;

import org.junit.jupiter.api.Test;
import org.opentripplanner.model.Timetable;
import org.opentripplanner.model.TimetableSnapshot;
import org.opentripplanner.transit.model.network.TripPattern;
import org.opentripplanner.transit.model.timetable.RealTimeState;
import org.opentripplanner.updater.trip.GtfsRealtimeTestEnvironment;
import org.opentripplanner.updater.trip.TripUpdateBuilder;

/**
* Delays should be applied to the first trip but should leave the second trip untouched.
*/
public class DelayedTest {

private static final int DELAY = 1;
private static final int STOP_SEQUENCE = 1;

@Test
public void delayed() {
var env = new GtfsRealtimeTestEnvironment();

final TripPattern pattern = env.transitModel
.getTransitModelIndex()
.getPatternForTrip()
.get(env.trip1);
final int tripIndex = pattern.getScheduledTimetable().getTripIndex(env.trip1.getId());
//final int tripIndex2 = pattern.getScheduledTimetable().getTripIndex(env.trip2.getId());

var tripUpdateBuilder = new TripUpdateBuilder(
var tripUpdate = new TripUpdateBuilder(
env.trip1.getId().getId(),
env.serviceDate,
SCHEDULED,
env.timeZone
);

int stopSequence = 1;
int delay = 1;
tripUpdateBuilder.addDelayedStopTime(stopSequence, delay);

var tripUpdate = tripUpdateBuilder.build();
)
.addDelayedStopTime(STOP_SEQUENCE, DELAY)
.build();

var result = env.applyTripUpdates(tripUpdate);

assertEquals(1, result.successful());

final TimetableSnapshot snapshot = env.source.getTimetableSnapshot();
final Timetable forToday = snapshot.resolve(pattern, env.serviceDate);
final Timetable schedule = snapshot.resolve(pattern, null);
assertNotSame(forToday, schedule);
assertNotSame(forToday.getTripTimes(tripIndex), schedule.getTripTimes(tripIndex));
//assertSame(forToday.getTripTimes(tripIndex2), schedule.getTripTimes(tripIndex2));
assertEquals(1, forToday.getTripTimes(tripIndex).getArrivalDelay(1));
assertEquals(1, forToday.getTripTimes(tripIndex).getDepartureDelay(1));

assertEquals(RealTimeState.SCHEDULED, schedule.getTripTimes(tripIndex).getRealTimeState());
assertEquals(RealTimeState.UPDATED, forToday.getTripTimes(tripIndex).getRealTimeState());
//assertEquals(RealTimeState.SCHEDULED, schedule.getTripTimes(tripIndex2).getRealTimeState());
//assertEquals(RealTimeState.SCHEDULED, forToday.getTripTimes(tripIndex2).getRealTimeState());
// trip1 should be modified
{
var pattern1 = env.getPatternForTrip(env.trip1);
final int trip1Index = pattern1.getScheduledTimetable().getTripIndex(env.trip1.getId());

final TimetableSnapshot snapshot = env.source.getTimetableSnapshot();
final Timetable trip1Realtime = snapshot.resolve(pattern1, env.serviceDate);
final Timetable trip1Scheduled = snapshot.resolve(pattern1, null);

assertNotSame(trip1Realtime, trip1Scheduled);
assertNotSame(
trip1Realtime.getTripTimes(trip1Index),
trip1Scheduled.getTripTimes(trip1Index)
);
assertEquals(1, trip1Realtime.getTripTimes(trip1Index).getArrivalDelay(STOP_SEQUENCE));
assertEquals(1, trip1Realtime.getTripTimes(trip1Index).getDepartureDelay(STOP_SEQUENCE));

assertEquals(
RealTimeState.SCHEDULED,
trip1Scheduled.getTripTimes(trip1Index).getRealTimeState()
);
assertEquals(
RealTimeState.UPDATED,
trip1Realtime.getTripTimes(trip1Index).getRealTimeState()
);
}

// trip2 should keep the scheduled information
{
var pattern = env.getPatternForTrip(env.trip2);
final int tripIndex = pattern.getScheduledTimetable().getTripIndex(env.trip2.getId());

final TimetableSnapshot snapshot = env.source.getTimetableSnapshot();
final Timetable realtime = snapshot.resolve(pattern, env.serviceDate);
final Timetable scheduled = snapshot.resolve(pattern, null);

assertSame(realtime, scheduled);
assertSame(realtime.getTripTimes(tripIndex), scheduled.getTripTimes(tripIndex));
assertEquals(0, realtime.getTripTimes(tripIndex).getArrivalDelay(STOP_SEQUENCE));
assertEquals(0, realtime.getTripTimes(tripIndex).getDepartureDelay(STOP_SEQUENCE));

assertEquals(RealTimeState.SCHEDULED, scheduled.getTripTimes(tripIndex).getRealTimeState());
assertEquals(RealTimeState.SCHEDULED, realtime.getTripTimes(tripIndex).getRealTimeState());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
import org.opentripplanner.updater.trip.GtfsRealtimeTestEnvironment;
import org.opentripplanner.updater.trip.TripUpdateBuilder;

/**
* A mixture of delayed and skipped stops should result in both delayed and cancelled stops.
*/
public class SkippedTest {

@Test
Expand Down

0 comments on commit b4d5495

Please sign in to comment.