Search in sources :

Example 21 with TripPattern

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));
}
Also used : TIntSet(gnu.trove.set.TIntSet) ArrayList(java.util.ArrayList) TripTimes(org.opentripplanner.routing.trippattern.TripTimes) TripPattern(org.opentripplanner.model.TripPattern) TripPatternForDate(org.opentripplanner.routing.algorithm.raptor.transit.TripPatternForDate)

Example 22 with TripPattern

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;
}
Also used : FeedScopedId(org.opentripplanner.model.FeedScopedId) Trip(org.opentripplanner.model.Trip) TripPattern(org.opentripplanner.model.TripPattern) Stop(org.opentripplanner.model.Stop) Collection(java.util.Collection) ServiceCalendar(org.opentripplanner.model.calendar.ServiceCalendar) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) StopTime(org.opentripplanner.model.StopTime) Collectors(java.util.stream.Collectors) ServiceDate(org.opentripplanner.model.calendar.ServiceDate) List(java.util.List) Route(org.opentripplanner.model.Route) StopPattern(org.opentripplanner.model.StopPattern) ServiceCalendarDate(org.opentripplanner.model.calendar.ServiceCalendarDate) Deduplicator(org.opentripplanner.routing.trippattern.Deduplicator) TraverseMode(org.opentripplanner.routing.core.TraverseMode) TransitMode(org.opentripplanner.model.TransitMode) TripTimes(org.opentripplanner.routing.trippattern.TripTimes) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) ServiceDateInterval(org.opentripplanner.model.calendar.ServiceDateInterval) Trip(org.opentripplanner.model.Trip) FeedScopedId(org.opentripplanner.model.FeedScopedId) TripTimes(org.opentripplanner.routing.trippattern.TripTimes) TripPattern(org.opentripplanner.model.TripPattern)

Example 23 with TripPattern

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());
}
Also used : ServiceCalendarDate(org.opentripplanner.model.calendar.ServiceCalendarDate) Trip(org.opentripplanner.model.Trip) ServiceDateInterval(org.opentripplanner.model.calendar.ServiceDateInterval) FeedScopedId(org.opentripplanner.model.FeedScopedId) TripPattern(org.opentripplanner.model.TripPattern) ServiceCalendar(org.opentripplanner.model.calendar.ServiceCalendar) Test(org.junit.Test)

Example 24 with TripPattern

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());
}
Also used : Timetable(org.opentripplanner.model.Timetable) Trip(org.opentripplanner.model.Trip) FeedScopedId(org.opentripplanner.model.FeedScopedId) TripTimes(org.opentripplanner.routing.trippattern.TripTimes) TimetableSnapshot(org.opentripplanner.model.TimetableSnapshot) TripPattern(org.opentripplanner.model.TripPattern) Test(org.junit.Test)

Example 25 with TripPattern

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));
}
Also used : ServiceDate(org.opentripplanner.model.calendar.ServiceDate) Trip(org.opentripplanner.model.Trip) TripUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate) TripDescriptor(com.google.transit.realtime.GtfsRealtime.TripDescriptor) FeedScopedId(org.opentripplanner.model.FeedScopedId) TimetableSnapshot(org.opentripplanner.model.TimetableSnapshot) TripPattern(org.opentripplanner.model.TripPattern) Test(org.junit.Test)

Aggregations

TripPattern (org.opentripplanner.model.TripPattern)61 Trip (org.opentripplanner.model.Trip)26 FeedScopedId (org.opentripplanner.model.FeedScopedId)23 Stop (org.opentripplanner.model.Stop)23 ArrayList (java.util.ArrayList)19 TripTimes (org.opentripplanner.routing.trippattern.TripTimes)19 Timetable (org.opentripplanner.model.Timetable)13 ServiceDate (org.opentripplanner.model.calendar.ServiceDate)13 Route (org.opentripplanner.model.Route)12 Test (org.junit.Test)11 HashSet (java.util.HashSet)8 RoutingService (org.opentripplanner.routing.RoutingService)8 List (java.util.List)7 StopPattern (org.opentripplanner.model.StopPattern)7 TimetableSnapshot (org.opentripplanner.model.TimetableSnapshot)7 GET (javax.ws.rs.GET)6 Path (javax.ws.rs.Path)6 StopTime (org.opentripplanner.model.StopTime)5 TripUpdate (com.google.transit.realtime.GtfsRealtime.TripUpdate)4 StopTimeUpdate (com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeUpdate)4