Search in sources :

Example 1 with Timetable

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

the class SiriTimetableSnapshotSource method handleTripPatternUpdate.

private boolean handleTripPatternUpdate(Graph graph, TripPattern pattern, VehicleActivityStructure activity, Trip trip, ServiceDate serviceDate) {
    // Apply update on the *scheduled* time table and set the updated trip times in the buffer
    Timetable currentTimetable = getCurrentTimetable(pattern, serviceDate);
    final TripTimes updatedTripTimes = createUpdatedTripTimes(currentTimetable, graph, activity, timeZone, trip.getId());
    if (updatedTripTimes == null) {
        return false;
    }
    final boolean success = buffer.update(pattern, updatedTripTimes, serviceDate);
    return success;
}
Also used : Timetable(org.opentripplanner.model.Timetable) TimetableHelper.createUpdatedTripTimes(org.opentripplanner.ext.siri.TimetableHelper.createUpdatedTripTimes) TripTimes(org.opentripplanner.routing.trippattern.TripTimes)

Example 2 with Timetable

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

the class SiriTimetableSnapshotSource method cancelPreviouslyAddedTrip.

/**
 * Cancel previously added trip from buffer if there is a previously added trip with given trip
 * id (without agency id) on service date
 *
 * @param feedId feed id the trip id belongs to
 * @param tripId trip id without agency id
 * @param serviceDate service date
 * @return true if a previously added trip was cancelled
 */
private boolean cancelPreviouslyAddedTrip(final String feedId, final String tripId, final ServiceDate serviceDate) {
    boolean success = false;
    final TripPattern pattern = buffer.getLastAddedTripPattern(new FeedScopedId(feedId, tripId), serviceDate);
    if (pattern != null) {
        // Cancel trip times for this trip in this pattern
        final Timetable timetable = buffer.resolve(pattern, serviceDate);
        final int tripIndex = timetable.getTripIndex(tripId);
        if (tripIndex == -1) {
            LOG.warn("Could not cancel previously added trip {}", tripId);
        } else {
            final TripTimes newTripTimes = new TripTimes(timetable.getTripTimes(tripIndex));
            newTripTimes.cancel();
            buffer.update(pattern, newTripTimes, serviceDate);
            // buffer.removeLastAddedTripPattern(feedId, tripId, serviceDate);
            success = true;
        }
    }
    return success;
}
Also used : Timetable(org.opentripplanner.model.Timetable) FeedScopedId(org.opentripplanner.model.FeedScopedId) TimetableHelper.createUpdatedTripTimes(org.opentripplanner.ext.siri.TimetableHelper.createUpdatedTripTimes) TripTimes(org.opentripplanner.routing.trippattern.TripTimes) TripPattern(org.opentripplanner.model.TripPattern)

Example 3 with Timetable

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

the class SiriTimetableSnapshotSource method cancelScheduledTrip.

/**
 * Cancel scheduled trip in buffer given trip id (without agency id) on service date
 *
 * @param tripId trip id without agency id
 * @param serviceDate service date
 * @return true if scheduled trip was cancelled
 */
private boolean cancelScheduledTrip(String feedId, String tripId, final ServiceDate serviceDate) {
    boolean success = false;
    final TripPattern pattern = getPatternForTripId(feedId, tripId);
    if (pattern != null) {
        // Cancel scheduled trip times for this trip in this pattern
        final Timetable timetable = pattern.scheduledTimetable;
        final int tripIndex = timetable.getTripIndex(tripId);
        if (tripIndex == -1) {
            LOG.warn("Could not cancel scheduled trip {}", tripId);
        } else {
            final TripTimes newTripTimes = new TripTimes(timetable.getTripTimes(tripIndex));
            newTripTimes.cancel();
            buffer.update(pattern, newTripTimes, serviceDate);
            success = true;
        }
    }
    return success;
}
Also used : Timetable(org.opentripplanner.model.Timetable) TimetableHelper.createUpdatedTripTimes(org.opentripplanner.ext.siri.TimetableHelper.createUpdatedTripTimes) TripTimes(org.opentripplanner.routing.trippattern.TripTimes) TripPattern(org.opentripplanner.model.TripPattern)

Example 4 with Timetable

use of org.opentripplanner.model.Timetable 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 5 with Timetable

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

Aggregations

Timetable (org.opentripplanner.model.Timetable)16 TripPattern (org.opentripplanner.model.TripPattern)13 TripTimes (org.opentripplanner.routing.trippattern.TripTimes)12 TimetableSnapshot (org.opentripplanner.model.TimetableSnapshot)6 Trip (org.opentripplanner.model.Trip)6 FeedScopedId (org.opentripplanner.model.FeedScopedId)5 Stop (org.opentripplanner.model.Stop)5 Test (org.junit.Test)4 TimetableHelper.createUpdatedTripTimes (org.opentripplanner.ext.siri.TimetableHelper.createUpdatedTripTimes)4 ServiceDate (org.opentripplanner.model.calendar.ServiceDate)4 TripUpdate (com.google.transit.realtime.GtfsRealtime.TripUpdate)3 StopTimeUpdate (com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeUpdate)3 ArrayList (java.util.ArrayList)3 ServiceDay (org.opentripplanner.routing.core.ServiceDay)3 Calendar (java.util.Calendar)2 GtfsContextBuilder.contextBuilder (org.opentripplanner.gtfs.GtfsContextBuilder.contextBuilder)2 TripTimeShort (org.opentripplanner.model.TripTimeShort)2 TripDescriptor (com.google.transit.realtime.GtfsRealtime.TripDescriptor)1 StopTimeEvent (com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeEvent)1 LocalDate (java.time.LocalDate)1