use of org.opentripplanner.model.TripPattern in project OpenTripPlanner by opentripplanner.
the class TripPatternForDateMapper method map.
/**
* This method is THREAD SAFE.
*
* @param timetable The timetable to be mapped to TripPatternForDate - READ ONLY
* @param serviceDate The date to map the TripPatternForDate for - READ ONLY
* @return TripPatternForDate for this timetable and serviceDate
*/
public TripPatternForDate map(Timetable timetable, ServiceDate serviceDate) {
TIntSet serviceCodesRunning = serviceCodesRunningForDate.get(serviceDate);
TripPattern oldTripPattern = timetable.pattern;
List<TripTimes> times = new ArrayList<>();
// The TripTimes are not sorted by departure time in the source timetable because
// OTP1 performs a simple/ linear search. Raptor results depend on trips being
// sorted. We reuse the same timetables many times on different days, so cache the
// sorted versions to avoid repeated compute-intensive sorting. Anecdotally this
// reduces mapping time by more than half, but it is still rather slow. NL Mapping
// takes 32 seconds sorting every timetable, 9 seconds with cached sorting, and 6
// seconds with no timetable sorting at all.
List<TripTimes> sortedTripTimes = sortedTripTimesForTimetable.computeIfAbsent(timetable, TransitLayerMapper::getSortedTripTimes);
for (TripTimes tripTimes : sortedTripTimes) {
if (!serviceCodesRunning.contains(tripTimes.serviceCode)) {
continue;
}
if (tripTimes.getRealTimeState() == RealTimeState.CANCELED) {
continue;
}
times.add(tripTimes);
}
if (times.isEmpty()) {
if (timetable.serviceDate == serviceDate) {
LOG.debug("Tried to update TripPattern {}, but no service codes are valid for date {}", timetable.pattern.getId(), serviceDate);
}
return null;
}
return new TripPatternForDate(newTripPatternForOld.get(oldTripPattern), times.toArray(TripTimes[]::new), ServiceCalendarMapper.localDateFromServiceDate(serviceDate));
}
use of org.opentripplanner.model.TripPattern in project OpenTripPlanner by opentripplanner.
the class OtpTransitServiceBuilderLimitPeriodTest method createTripPattern.
private TripPattern createTripPattern(Collection<Trip> trips) {
TripPattern p = new TripPattern(route, STOP_PATTERN);
p.setId(new FeedScopedId(FEED_ID, trips.stream().map(t -> t.getId().getId()).collect(Collectors.joining(":"))));
p.name = "Pattern";
for (Trip trip : trips) {
p.add(new TripTimes(trip, STOP_TIMES, DEDUPLICATOR));
}
return p;
}
use of org.opentripplanner.model.TripPattern in project OpenTripPlanner by opentripplanner.
the class OtpTransitServiceBuilderLimitPeriodTest method testLimitPeriod.
@Test
public void testLimitPeriod() {
// Assert the test is set up as expected
assertEquals(2, subject.getCalendars().size());
assertEquals(2, subject.getCalendarDates().size());
assertEquals(4, subject.getTripsById().size());
assertEquals(3, subject.getTripPatterns().get(STOP_PATTERN).size());
assertEquals(2, patternInT1.getTrips().size());
assertEquals(2, patternInT1.scheduledTimetable.tripTimes.size());
assertEquals(1, patternInT2.getTrips().size());
assertEquals(1, patternInT2.scheduledTimetable.tripTimes.size());
// Limit service to last half of month
subject.limitServiceDays(new ServiceDateInterval(D2, D3));
// Verify calendar
List<ServiceCalendar> calendars = subject.getCalendars();
assertEquals(calendars.toString(), 1, calendars.size());
assertEquals(calendars.toString(), SERVICE_C_IN, calendars.get(0).getServiceId());
// Verify calendar dates
List<ServiceCalendarDate> dates = subject.getCalendarDates();
assertEquals(dates.toString(), 1, dates.size());
assertEquals(dates.toString(), SERVICE_D_IN, dates.get(0).getServiceId());
// Verify trips
EntityById<FeedScopedId, Trip> trips = subject.getTripsById();
assertEquals(trips.toString(), 2, trips.size());
assertTrue(trips.toString(), trips.containsKey(tripCSIn.getId()));
assertTrue(trips.toString(), trips.containsKey(tripCSDIn.getId()));
// Verify patterns
Collection<TripPattern> patterns = subject.getTripPatterns().get(STOP_PATTERN);
assertEquals(2, patterns.size());
assertTrue(patterns.toString(), patterns.contains(patternInT1));
assertTrue(patterns.toString(), patterns.contains(patternInT2));
// Verify trips in pattern (one trip is removed from patternInT1)
assertEquals(1, patternInT1.getTrips().size());
assertEquals(tripCSIn, patternInT1.getTrips().get(0));
// Verify trips in pattern is unchanged (one trip)
assertEquals(1, patternInT2.getTrips().size());
// Verify scheduledTimetable trips (one trip is removed from patternInT1)
assertEquals(1, patternInT1.scheduledTimetable.tripTimes.size());
assertEquals(tripCSIn, patternInT1.scheduledTimetable.tripTimes.get(0).trip);
// Verify scheduledTimetable trips in pattern is unchanged (one trip)
assertEquals(1, patternInT2.scheduledTimetable.tripTimes.size());
}
use of org.opentripplanner.model.TripPattern in project OpenTripPlanner by opentripplanner.
the class TimetableSnapshotSourceTest method testHandleCanceledTrip.
@Test
public void testHandleCanceledTrip() throws InvalidProtocolBufferException {
final FeedScopedId tripId = new FeedScopedId(feedId, "1.1");
final FeedScopedId tripId2 = new FeedScopedId(feedId, "1.2");
final Trip trip = graph.index.getTripForId().get(tripId);
final TripPattern pattern = graph.index.getPatternForTrip().get(trip);
final int tripIndex = pattern.scheduledTimetable.getTripIndex(tripId);
final int tripIndex2 = pattern.scheduledTimetable.getTripIndex(tripId2);
updater.applyTripUpdates(graph, fullDataset, Arrays.asList(TripUpdate.parseFrom(cancellation)), feedId);
final TimetableSnapshot snapshot = updater.getTimetableSnapshot();
final Timetable forToday = snapshot.resolve(pattern, 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));
final TripTimes tripTimes = forToday.getTripTimes(tripIndex);
for (int i = 0; i < tripTimes.getNumStops(); i++) {
assertEquals(TripTimes.UNAVAILABLE, tripTimes.getDepartureTime(i));
assertEquals(TripTimes.UNAVAILABLE, tripTimes.getArrivalTime(i));
}
assertEquals(RealTimeState.CANCELED, tripTimes.getRealTimeState());
}
use of org.opentripplanner.model.TripPattern in project OpenTripPlanner by opentripplanner.
the class TimetableSnapshotSourceTest method testPurgeExpiredData.
@Test
public void testPurgeExpiredData() throws InvalidProtocolBufferException {
final FeedScopedId tripId = new FeedScopedId(feedId, "1.1");
// Just to be safe...
final ServiceDate previously = serviceDate.previous().previous();
final Trip trip = graph.index.getTripForId().get(tripId);
final TripPattern pattern = graph.index.getPatternForTrip().get(trip);
updater.maxSnapshotFrequency = 0;
updater.purgeExpiredData = false;
updater.applyTripUpdates(graph, fullDataset, Arrays.asList(TripUpdate.parseFrom(cancellation)), feedId);
final TimetableSnapshot snapshotA = updater.getTimetableSnapshot();
updater.purgeExpiredData = true;
final TripDescriptor.Builder tripDescriptorBuilder = TripDescriptor.newBuilder();
tripDescriptorBuilder.setTripId("1.1");
tripDescriptorBuilder.setScheduleRelationship(TripDescriptor.ScheduleRelationship.CANCELED);
tripDescriptorBuilder.setStartDate(previously.asCompactString());
final TripUpdate.Builder tripUpdateBuilder = TripUpdate.newBuilder();
tripUpdateBuilder.setTrip(tripDescriptorBuilder);
final TripUpdate tripUpdate = tripUpdateBuilder.build();
updater.applyTripUpdates(graph, fullDataset, Arrays.asList(tripUpdate), feedId);
final TimetableSnapshot snapshotB = updater.getTimetableSnapshot();
assertNotSame(snapshotA, snapshotB);
assertSame(snapshotA.resolve(pattern, null), snapshotB.resolve(pattern, null));
assertSame(snapshotA.resolve(pattern, serviceDate), snapshotB.resolve(pattern, serviceDate));
assertNotSame(snapshotA.resolve(pattern, null), snapshotA.resolve(pattern, serviceDate));
assertSame(snapshotB.resolve(pattern, null), snapshotB.resolve(pattern, previously));
}
Aggregations