Search in sources :

Example 16 with ServiceDate

use of org.opentripplanner.model.calendar.ServiceDate 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)

Example 17 with ServiceDate

use of org.opentripplanner.model.calendar.ServiceDate in project OpenTripPlanner by opentripplanner.

the class TimetableSnapshotSourceTest method testHandleModifiedTrip.

@Test
public void testHandleModifiedTrip() throws ParseException {
    // TODO
    // GIVEN
    // Get service date of today because old dates will be purged after applying updates
    ServiceDate serviceDate = new ServiceDate(Calendar.getInstance());
    String modifiedTripId = "10.1";
    TripUpdate tripUpdate;
    {
        final TripDescriptor.Builder tripDescriptorBuilder = TripDescriptor.newBuilder();
        tripDescriptorBuilder.setTripId(modifiedTripId);
        tripDescriptorBuilder.setScheduleRelationship(TripDescriptor.ScheduleRelationship.MODIFIED);
        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 O
            final StopTimeUpdate.Builder stopTimeUpdateBuilder = tripUpdateBuilder.addStopTimeUpdateBuilder();
            stopTimeUpdateBuilder.setScheduleRelationship(StopTimeUpdate.ScheduleRelationship.SCHEDULED);
            stopTimeUpdateBuilder.setStopId("O");
            stopTimeUpdateBuilder.setStopSequence(10);
            {
                // Arrival
                final StopTimeEvent.Builder arrivalBuilder = stopTimeUpdateBuilder.getArrivalBuilder();
                arrivalBuilder.setTime(midnightSecondsSinceEpoch + (12 * 3600) + (30 * 60));
                arrivalBuilder.setDelay(0);
            }
            {
                // Departure
                final StopTimeEvent.Builder departureBuilder = stopTimeUpdateBuilder.getDepartureBuilder();
                departureBuilder.setTime(midnightSecondsSinceEpoch + (12 * 3600) + (30 * 60));
                departureBuilder.setDelay(0);
            }
        }
        {
            // Stop C
            final StopTimeUpdate.Builder stopTimeUpdateBuilder = tripUpdateBuilder.addStopTimeUpdateBuilder();
            stopTimeUpdateBuilder.setScheduleRelationship(StopTimeUpdate.ScheduleRelationship.SCHEDULED);
            stopTimeUpdateBuilder.setStopId("C");
            stopTimeUpdateBuilder.setStopSequence(30);
            {
                // Arrival
                final StopTimeEvent.Builder arrivalBuilder = stopTimeUpdateBuilder.getArrivalBuilder();
                arrivalBuilder.setTime(midnightSecondsSinceEpoch + (12 * 3600) + (40 * 60));
                arrivalBuilder.setDelay(0);
            }
            {
                // Departure
                final StopTimeEvent.Builder departureBuilder = stopTimeUpdateBuilder.getDepartureBuilder();
                departureBuilder.setTime(midnightSecondsSinceEpoch + (12 * 3600) + (45 * 60));
                departureBuilder.setDelay(0);
            }
        }
        {
            // Stop D
            final StopTimeUpdate.Builder stopTimeUpdateBuilder = tripUpdateBuilder.addStopTimeUpdateBuilder();
            stopTimeUpdateBuilder.setScheduleRelationship(StopTimeUpdate.ScheduleRelationship.SKIPPED);
            stopTimeUpdateBuilder.setStopId("D");
            stopTimeUpdateBuilder.setStopSequence(40);
            {
                // Arrival
                final StopTimeEvent.Builder arrivalBuilder = stopTimeUpdateBuilder.getArrivalBuilder();
                arrivalBuilder.setTime(midnightSecondsSinceEpoch + (12 * 3600) + (50 * 60));
                arrivalBuilder.setDelay(0);
            }
            {
                // Departure
                final StopTimeEvent.Builder departureBuilder = stopTimeUpdateBuilder.getDepartureBuilder();
                departureBuilder.setTime(midnightSecondsSinceEpoch + (12 * 3600) + (51 * 60));
                departureBuilder.setDelay(0);
            }
        }
        {
            // Stop P
            final StopTimeUpdate.Builder stopTimeUpdateBuilder = tripUpdateBuilder.addStopTimeUpdateBuilder();
            stopTimeUpdateBuilder.setScheduleRelationship(StopTimeUpdate.ScheduleRelationship.SCHEDULED);
            stopTimeUpdateBuilder.setStopId("P");
            stopTimeUpdateBuilder.setStopSequence(50);
            {
                // Arrival
                final StopTimeEvent.Builder arrivalBuilder = stopTimeUpdateBuilder.getArrivalBuilder();
                arrivalBuilder.setTime(midnightSecondsSinceEpoch + (12 * 3600) + (55 * 60));
                arrivalBuilder.setDelay(0);
            }
            {
                // Departure
                final StopTimeEvent.Builder departureBuilder = stopTimeUpdateBuilder.getDepartureBuilder();
                departureBuilder.setTime(midnightSecondsSinceEpoch + (12 * 3600) + (55 * 60));
                departureBuilder.setDelay(0);
            }
        }
        tripUpdate = tripUpdateBuilder.build();
    }
    // WHEN
    updater.applyTripUpdates(graph, fullDataset, Arrays.asList(tripUpdate), feedId);
    // THEN
    final TimetableSnapshot snapshot = updater.getTimetableSnapshot();
    // Original trip pattern
    {
        final FeedScopedId tripId = new FeedScopedId(feedId, modifiedTripId);
        final Trip trip = graph.index.getTripForId().get(tripId);
        final TripPattern originalTripPattern = graph.index.getPatternForTrip().get(trip);
        final Timetable originalTimetableForToday = snapshot.resolve(originalTripPattern, serviceDate);
        final Timetable originalTimetableScheduled = snapshot.resolve(originalTripPattern, null);
        assertNotSame(originalTimetableForToday, originalTimetableScheduled);
        final int originalTripIndexScheduled = originalTimetableScheduled.getTripIndex(modifiedTripId);
        assertTrue("Original trip should be found in scheduled time table", originalTripIndexScheduled > -1);
        final TripTimes originalTripTimesScheduled = originalTimetableScheduled.getTripTimes(originalTripIndexScheduled);
        assertFalse("Original trip times should not be canceled in scheduled time table", originalTripTimesScheduled.isCanceled());
        assertEquals(RealTimeState.SCHEDULED, originalTripTimesScheduled.getRealTimeState());
        final int originalTripIndexForToday = originalTimetableForToday.getTripIndex(modifiedTripId);
        assertTrue("Original trip should be found in time table for service date", originalTripIndexForToday > -1);
        final TripTimes originalTripTimesForToday = originalTimetableForToday.getTripTimes(originalTripIndexForToday);
        assertTrue("Original trip times should be canceled in time table for service date", originalTripTimesForToday.isCanceled());
        assertEquals(RealTimeState.CANCELED, originalTripTimesForToday.getRealTimeState());
    }
    // New trip pattern
    {
        final TripPattern newTripPattern = snapshot.getLastAddedTripPattern(new FeedScopedId(feedId, modifiedTripId), serviceDate);
        assertNotNull("New trip pattern should be found", newTripPattern);
        final Timetable newTimetableForToday = snapshot.resolve(newTripPattern, serviceDate);
        final Timetable newTimetableScheduled = snapshot.resolve(newTripPattern, null);
        assertNotSame(newTimetableForToday, newTimetableScheduled);
        final int newTimetableForTodayModifiedTripIndex = newTimetableForToday.getTripIndex(modifiedTripId);
        assertTrue("New trip should be found in time table for service date", newTimetableForTodayModifiedTripIndex > -1);
        assertEquals(RealTimeState.MODIFIED, newTimetableForToday.getTripTimes(newTimetableForTodayModifiedTripIndex).getRealTimeState());
        assertEquals("New trip should not be found in scheduled time table", -1, newTimetableScheduled.getTripIndex(modifiedTripId));
    }
}
Also used : Timetable(org.opentripplanner.model.Timetable) Trip(org.opentripplanner.model.Trip) TripUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate) 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) TripTimes(org.opentripplanner.routing.trippattern.TripTimes) Test(org.junit.Test)

Example 18 with ServiceDate

use of org.opentripplanner.model.calendar.ServiceDate in project OpenTripPlanner by opentripplanner.

the class StopTimesHelper method stopTimesForStop.

/**
 * Fetch upcoming vehicle departures from a stop.
 * It goes though all patterns passing the stop for the previous, current and next service date.
 * It uses a priority queue to keep track of the next departures. The queue is shared between all
 * dates, as services from the previous service date can visit the stop later than the current
 * service date's services. This happens eg. with sleeper trains.
 *
 * TODO: Add frequency based trips
 * @param stop Stop object to perform the search for
 * @param startTime Start time for the search. Seconds from UNIX epoch
 * @param timeRange Searches forward for timeRange seconds from startTime
 * @param numberOfDepartures Number of departures to fetch per pattern
 * @param omitNonPickups If true, do not include vehicles that will not pick up passengers.
 */
public static List<StopTimesInPattern> stopTimesForStop(RoutingService routingService, TimetableSnapshot timetableSnapshot, Stop stop, long startTime, int timeRange, int numberOfDepartures, boolean omitNonPickups) {
    if (startTime == 0) {
        startTime = System.currentTimeMillis() / 1000;
    }
    List<StopTimesInPattern> ret = new ArrayList<>();
    Date date = new Date(startTime * 1000);
    ServiceDate[] serviceDates = { new ServiceDate(date).previous(), new ServiceDate(date), new ServiceDate(date).next() };
    Collection<TripPattern> patternsForStop = routingService.getPatternsForStop(stop, true);
    for (TripPattern pattern : patternsForStop) {
        Queue<TripTimeShort> pq = listTripTimeShortsForPatternAtStop(routingService, timetableSnapshot, stop, pattern, startTime, timeRange, numberOfDepartures, omitNonPickups, serviceDates);
        if (pq.size() != 0) {
            StopTimesInPattern stopTimes = new StopTimesInPattern(pattern);
            while (pq.size() != 0) {
                stopTimes.times.add(0, pq.poll());
            }
            ret.add(stopTimes);
        }
    }
    return ret;
}
Also used : TripTimeShort(org.opentripplanner.model.TripTimeShort) ServiceDate(org.opentripplanner.model.calendar.ServiceDate) ArrayList(java.util.ArrayList) Date(java.util.Date) ServiceDate(org.opentripplanner.model.calendar.ServiceDate) TripPattern(org.opentripplanner.model.TripPattern) StopTimesInPattern(org.opentripplanner.model.StopTimesInPattern)

Example 19 with ServiceDate

use of org.opentripplanner.model.calendar.ServiceDate in project OpenTripPlanner by opentripplanner.

the class ServiceDay method init.

private void init(Map<FeedScopedId, Integer> serviceCodes, CalendarService cs, TimeZone timeZone) {
    Date d = serviceDate.getAsDate(timeZone);
    this.midnight = d.getTime() / 1000;
    serviceIdsRunning = new BitSet(cs.getServiceIds().size());
    for (FeedScopedId serviceId : cs.getServiceIdsOnDate(serviceDate)) {
        int n = serviceCodes.get(serviceId);
        if (n < 0)
            continue;
        serviceIdsRunning.set(n);
    }
}
Also used : BitSet(java.util.BitSet) FeedScopedId(org.opentripplanner.model.FeedScopedId) Date(java.util.Date) ServiceDate(org.opentripplanner.model.calendar.ServiceDate)

Example 20 with ServiceDate

use of org.opentripplanner.model.calendar.ServiceDate in project OpenTripPlanner by opentripplanner.

the class TimetableSnapshotTest method testPurge.

@Test
public void testPurge() {
    ServiceDate today = new ServiceDate();
    ServiceDate yesterday = today.previous();
    TripPattern pattern = patternIndex.get(new FeedScopedId("agency", "1.1"));
    TripDescriptor.Builder tripDescriptorBuilder = TripDescriptor.newBuilder();
    tripDescriptorBuilder.setTripId("1.1");
    tripDescriptorBuilder.setScheduleRelationship(ScheduleRelationship.CANCELED);
    TripUpdate.Builder tripUpdateBuilder = TripUpdate.newBuilder();
    tripUpdateBuilder.setTrip(tripDescriptorBuilder);
    TripUpdate tripUpdate = tripUpdateBuilder.build();
    TimetableSnapshot resolver = new TimetableSnapshot();
    updateResolver(resolver, pattern, tripUpdate, "agency", today);
    updateResolver(resolver, pattern, tripUpdate, "agency", yesterday);
    assertNotSame(resolver.resolve(pattern, yesterday), resolver.resolve(pattern, null));
    assertNotSame(resolver.resolve(pattern, today), resolver.resolve(pattern, null));
    assertNotNull(resolver.commit());
    assertFalse(resolver.isDirty());
    assertTrue(resolver.purgeExpiredData(yesterday));
    assertFalse(resolver.purgeExpiredData(yesterday));
    assertEquals(resolver.resolve(pattern, yesterday), resolver.resolve(pattern, null));
    assertNotSame(resolver.resolve(pattern, today), resolver.resolve(pattern, null));
    assertNull(resolver.commit());
    assertFalse(resolver.isDirty());
}
Also used : ServiceDate(org.opentripplanner.model.calendar.ServiceDate) TripUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate) TripDescriptor(com.google.transit.realtime.GtfsRealtime.TripDescriptor) Test(org.junit.Test)

Aggregations

ServiceDate (org.opentripplanner.model.calendar.ServiceDate)40 FeedScopedId (org.opentripplanner.model.FeedScopedId)15 ArrayList (java.util.ArrayList)11 TripPattern (org.opentripplanner.model.TripPattern)10 Test (org.junit.Test)9 Stop (org.opentripplanner.model.Stop)9 Trip (org.opentripplanner.model.Trip)9 TripUpdate (com.google.transit.realtime.GtfsRealtime.TripUpdate)8 Date (java.util.Date)8 HashSet (java.util.HashSet)7 TripDescriptor (com.google.transit.realtime.GtfsRealtime.TripDescriptor)6 ZonedDateTime (java.time.ZonedDateTime)6 TripTimeShort (org.opentripplanner.model.TripTimeShort)6 TripTimes (org.opentripplanner.routing.trippattern.TripTimes)6 Calendar (java.util.Calendar)5 Timetable (org.opentripplanner.model.Timetable)4 ServiceCalendarDate (org.opentripplanner.model.calendar.ServiceCalendarDate)4 EstimatedCall (uk.org.siri.siri20.EstimatedCall)4 TimetableHelper.createUpdatedTripTimes (org.opentripplanner.ext.siri.TimetableHelper.createUpdatedTripTimes)3 Route (org.opentripplanner.model.Route)3