Search in sources :

Example 6 with TimetableSnapshot

use of org.opentripplanner.model.TimetableSnapshot in project OpenTripPlanner by opentripplanner.

the class TimetableSnapshotSourceTest method testGetSnapshot.

@Test
public void testGetSnapshot() throws InvalidProtocolBufferException {
    updater.applyTripUpdates(graph, fullDataset, Arrays.asList(TripUpdate.parseFrom(cancellation)), feedId);
    final TimetableSnapshot snapshot = updater.getTimetableSnapshot();
    assertNotNull(snapshot);
    assertSame(snapshot, updater.getTimetableSnapshot());
    updater.applyTripUpdates(graph, fullDataset, Arrays.asList(TripUpdate.parseFrom(cancellation)), feedId);
    assertSame(snapshot, updater.getTimetableSnapshot());
    updater.maxSnapshotFrequency = (-1);
    final TimetableSnapshot newSnapshot = updater.getTimetableSnapshot();
    assertNotNull(newSnapshot);
    assertNotSame(snapshot, newSnapshot);
}
Also used : TimetableSnapshot(org.opentripplanner.model.TimetableSnapshot) Test(org.junit.Test)

Example 7 with TimetableSnapshot

use of org.opentripplanner.model.TimetableSnapshot in project OpenTripPlanner by opentripplanner.

the class TimetableSnapshotSourceTest method testHandleAddedTrip.

@Test
public void testHandleAddedTrip() throws ParseException {
    // GIVEN
    // Get service date of today because old dates will be purged after applying updates
    final ServiceDate serviceDate = new ServiceDate(Calendar.getInstance());
    final String addedTripId = "added_trip";
    TripUpdate tripUpdate;
    {
        final TripDescriptor.Builder tripDescriptorBuilder = TripDescriptor.newBuilder();
        tripDescriptorBuilder.setTripId(addedTripId);
        tripDescriptorBuilder.setScheduleRelationship(TripDescriptor.ScheduleRelationship.ADDED);
        tripDescriptorBuilder.setStartDate(serviceDate.asCompactString());
        final Calendar calendar = serviceDate.getAsCalendar(graph.getTimeZone());
        final long midnightSecondsSinceEpoch = calendar.getTimeInMillis() / 1000;
        final TripUpdate.Builder tripUpdateBuilder = TripUpdate.newBuilder();
        tripUpdateBuilder.setTrip(tripDescriptorBuilder);
        {
            // Stop A
            final StopTimeUpdate.Builder stopTimeUpdateBuilder = tripUpdateBuilder.addStopTimeUpdateBuilder();
            stopTimeUpdateBuilder.setScheduleRelationship(StopTimeUpdate.ScheduleRelationship.SCHEDULED);
            stopTimeUpdateBuilder.setStopId("A");
            {
                // Arrival
                final StopTimeEvent.Builder arrivalBuilder = stopTimeUpdateBuilder.getArrivalBuilder();
                arrivalBuilder.setTime(midnightSecondsSinceEpoch + (8 * 3600) + (30 * 60));
                arrivalBuilder.setDelay(0);
            }
            {
                // Departure
                final StopTimeEvent.Builder departureBuilder = stopTimeUpdateBuilder.getDepartureBuilder();
                departureBuilder.setTime(midnightSecondsSinceEpoch + (8 * 3600) + (30 * 60));
                departureBuilder.setDelay(0);
            }
        }
        {
            // Stop C
            final StopTimeUpdate.Builder stopTimeUpdateBuilder = tripUpdateBuilder.addStopTimeUpdateBuilder();
            stopTimeUpdateBuilder.setScheduleRelationship(StopTimeUpdate.ScheduleRelationship.SCHEDULED);
            stopTimeUpdateBuilder.setStopId("C");
            {
                // Arrival
                final StopTimeEvent.Builder arrivalBuilder = stopTimeUpdateBuilder.getArrivalBuilder();
                arrivalBuilder.setTime(midnightSecondsSinceEpoch + (8 * 3600) + (40 * 60));
                arrivalBuilder.setDelay(0);
            }
            {
                // Departure
                final StopTimeEvent.Builder departureBuilder = stopTimeUpdateBuilder.getDepartureBuilder();
                departureBuilder.setTime(midnightSecondsSinceEpoch + (8 * 3600) + (45 * 60));
                departureBuilder.setDelay(0);
            }
        }
        {
            // Stop E
            final StopTimeUpdate.Builder stopTimeUpdateBuilder = tripUpdateBuilder.addStopTimeUpdateBuilder();
            stopTimeUpdateBuilder.setScheduleRelationship(StopTimeUpdate.ScheduleRelationship.SCHEDULED);
            stopTimeUpdateBuilder.setStopId("E");
            {
                // Arrival
                final StopTimeEvent.Builder arrivalBuilder = stopTimeUpdateBuilder.getArrivalBuilder();
                arrivalBuilder.setTime(midnightSecondsSinceEpoch + (8 * 3600) + (55 * 60));
                arrivalBuilder.setDelay(0);
            }
            {
                // Departure
                final StopTimeEvent.Builder departureBuilder = stopTimeUpdateBuilder.getDepartureBuilder();
                departureBuilder.setTime(midnightSecondsSinceEpoch + (8 * 3600) + (55 * 60));
                departureBuilder.setDelay(0);
            }
        }
        tripUpdate = tripUpdateBuilder.build();
    }
    // WHEN
    updater.applyTripUpdates(graph, fullDataset, Arrays.asList(tripUpdate), feedId);
    // THEN
    // Find new pattern in graph starting from stop A
    Stop stopA = graph.index.getStopForId(new FeedScopedId(feedId, "A"));
    // Get trip pattern of last (most recently added) outgoing edge
    // FIXME create a new test to see that add-trip realtime updates work
    TripPattern tripPattern = null;
    assertNotNull("Added trip pattern should be found", tripPattern);
    final TimetableSnapshot snapshot = updater.getTimetableSnapshot();
    final Timetable forToday = snapshot.resolve(tripPattern, serviceDate);
    final Timetable schedule = snapshot.resolve(tripPattern, null);
    assertNotSame(forToday, schedule);
    final int forTodayAddedTripIndex = forToday.getTripIndex(addedTripId);
    assertTrue("Added trip should be found in time table for service date", forTodayAddedTripIndex > -1);
    assertEquals(RealTimeState.ADDED, forToday.getTripTimes(forTodayAddedTripIndex).getRealTimeState());
    final int scheduleTripIndex = schedule.getTripIndex(addedTripId);
    assertEquals("Added trip should not be found in scheduled time table", -1, scheduleTripIndex);
}
Also used : Timetable(org.opentripplanner.model.Timetable) TripUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate) Stop(org.opentripplanner.model.Stop) GtfsContextBuilder.contextBuilder(org.opentripplanner.gtfs.GtfsContextBuilder.contextBuilder) Calendar(java.util.Calendar) TimetableSnapshot(org.opentripplanner.model.TimetableSnapshot) TripPattern(org.opentripplanner.model.TripPattern) ServiceDate(org.opentripplanner.model.calendar.ServiceDate) StopTimeUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeUpdate) FeedScopedId(org.opentripplanner.model.FeedScopedId) Test(org.junit.Test)

Example 8 with TimetableSnapshot

use of org.opentripplanner.model.TimetableSnapshot in project OpenTripPlanner by opentripplanner.

the class StopTimesHelper method stopTimesForStop.

/**
 * Get a list of all trips that pass through a stop during a single ServiceDate. Useful when creating complete stop
 * timetables for a single day.
 *
 * @param stop Stop object to perform the search for
 * @param serviceDate Return all departures for the specified date
 */
public static List<StopTimesInPattern> stopTimesForStop(RoutingService routingService, Stop stop, ServiceDate serviceDate, boolean omitNonPickups) {
    List<StopTimesInPattern> ret = new ArrayList<>();
    Collection<TripPattern> patternsForStop = routingService.getPatternsForStop(stop, true);
    for (TripPattern pattern : patternsForStop) {
        StopTimesInPattern stopTimes = new StopTimesInPattern(pattern);
        Timetable tt;
        TimetableSnapshot timetableSnapshot = routingService.getTimetableSnapshot();
        if (timetableSnapshot != null) {
            tt = timetableSnapshot.resolve(pattern, serviceDate);
        } else {
            tt = pattern.scheduledTimetable;
        }
        ServiceDay sd = new ServiceDay(routingService.getServiceCodes(), serviceDate, routingService.getCalendarService(), pattern.route.getAgency().getId());
        int sidx = 0;
        for (Stop currStop : pattern.stopPattern.stops) {
            if (currStop == stop) {
                if (omitNonPickups && pattern.stopPattern.pickups[sidx] == StopPattern.PICKDROP_NONE)
                    continue;
                for (TripTimes t : tt.tripTimes) {
                    if (!sd.serviceRunning(t.serviceCode))
                        continue;
                    stopTimes.times.add(new TripTimeShort(t, sidx, stop, sd));
                }
            }
            sidx++;
        }
        ret.add(stopTimes);
    }
    return ret;
}
Also used : Timetable(org.opentripplanner.model.Timetable) TripTimeShort(org.opentripplanner.model.TripTimeShort) ServiceDay(org.opentripplanner.routing.core.ServiceDay) Stop(org.opentripplanner.model.Stop) ArrayList(java.util.ArrayList) TripTimes(org.opentripplanner.routing.trippattern.TripTimes) TimetableSnapshot(org.opentripplanner.model.TimetableSnapshot) TripPattern(org.opentripplanner.model.TripPattern) StopTimesInPattern(org.opentripplanner.model.StopTimesInPattern)

Aggregations

TimetableSnapshot (org.opentripplanner.model.TimetableSnapshot)8 TripPattern (org.opentripplanner.model.TripPattern)7 Test (org.junit.Test)6 Timetable (org.opentripplanner.model.Timetable)6 FeedScopedId (org.opentripplanner.model.FeedScopedId)5 TripUpdate (com.google.transit.realtime.GtfsRealtime.TripUpdate)4 Trip (org.opentripplanner.model.Trip)4 TripTimes (org.opentripplanner.routing.trippattern.TripTimes)4 StopTimeUpdate (com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeUpdate)3 ServiceDate (org.opentripplanner.model.calendar.ServiceDate)3 TripDescriptor (com.google.transit.realtime.GtfsRealtime.TripDescriptor)2 ArrayList (java.util.ArrayList)2 Calendar (java.util.Calendar)2 GtfsContextBuilder.contextBuilder (org.opentripplanner.gtfs.GtfsContextBuilder.contextBuilder)2 Stop (org.opentripplanner.model.Stop)2 ServiceDay (org.opentripplanner.routing.core.ServiceDay)2 StopTimeEvent (com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeEvent)1 StopTimesInPattern (org.opentripplanner.model.StopTimesInPattern)1 TripTimeShort (org.opentripplanner.model.TripTimeShort)1