Search in sources :

Example 21 with ServiceDate

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

the class IndexAPI method getStoptimesForStopAndDate.

/**
 * Return upcoming vehicle arrival/departure times at the given stop.
 * @param date in YYYYMMDD format
 */
@GET
@Path("/stops/{stopId}/stoptimes/{date}")
public Response getStoptimesForStopAndDate(@PathParam("stopId") String stopIdString, @PathParam("date") String date, @QueryParam("omitNonPickups") boolean omitNonPickups) {
    Stop stop = index.stopForId.get(GtfsLibrary.convertIdFromString(stopIdString));
    if (stop == null)
        return Response.status(Status.NOT_FOUND).entity(MSG_404).build();
    ServiceDate sd;
    try {
        sd = ServiceDate.parseString(date);
    } catch (ParseException e) {
        return Response.status(Status.BAD_REQUEST).entity(MSG_400).build();
    }
    List<StopTimesInPattern> ret = index.getStopTimesForStop(stop, sd, omitNonPickups);
    return Response.status(Status.OK).entity(ret).build();
}
Also used : ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) TransitStop(org.opentripplanner.routing.vertextype.TransitStop) Stop(org.onebusaway.gtfs.model.Stop) ParseException(java.text.ParseException) StopTimesInPattern(org.opentripplanner.index.model.StopTimesInPattern) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 22 with ServiceDate

use of org.onebusaway.gtfs.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.getAsString());
        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 AgencyAndId tripId = new AgencyAndId(feedId, modifiedTripId);
        final Trip trip = graph.index.tripForId.get(tripId);
        final TripPattern originalTripPattern = graph.index.patternForTrip.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(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.routing.edgetype.Timetable) TripUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate) Calendar(java.util.Calendar) TimetableSnapshot(org.opentripplanner.routing.edgetype.TimetableSnapshot) TripPattern(org.opentripplanner.routing.edgetype.TripPattern) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) StopTimeUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeUpdate) TripTimes(org.opentripplanner.routing.trippattern.TripTimes) Test(org.junit.Test)

Example 23 with ServiceDate

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

the class TimetableSnapshotSourceTest method testPurgeExpiredData.

@Test
public void testPurgeExpiredData() throws InvalidProtocolBufferException {
    final AgencyAndId tripId = new AgencyAndId(feedId, "1.1");
    // Just to be safe...
    final ServiceDate previously = serviceDate.previous().previous();
    final Trip trip = graph.index.tripForId.get(tripId);
    final TripPattern pattern = graph.index.patternForTrip.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.getAsString());
    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.onebusaway.gtfs.model.calendar.ServiceDate) TripUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate) TripDescriptor(com.google.transit.realtime.GtfsRealtime.TripDescriptor) TimetableSnapshot(org.opentripplanner.routing.edgetype.TimetableSnapshot) TripPattern(org.opentripplanner.routing.edgetype.TripPattern) Test(org.junit.Test)

Example 24 with ServiceDate

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

the class GraphIndex method currentUpdatedTimetableForTripPattern.

/**
 * Get the most up-to-date timetable for the given TripPattern, as of right now.
 * There should probably be a less awkward way to do this that just gets the latest entry from the resolver without
 * making a fake routing request.
 */
public Timetable currentUpdatedTimetableForTripPattern(TripPattern tripPattern) {
    RoutingRequest req = new RoutingRequest();
    req.setRoutingContext(graph, (Vertex) null, (Vertex) null);
    // The timetableSnapshot will be null if there's no real-time data being applied.
    if (req.rctx.timetableSnapshot == null)
        return tripPattern.scheduledTimetable;
    // Get the updated times for right now, which is the only reasonable default since no date is supplied.
    Calendar calendar = Calendar.getInstance();
    ServiceDate serviceDate = new ServiceDate(calendar.getTime());
    return req.rctx.timetableSnapshot.resolve(tripPattern, serviceDate);
}
Also used : ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) Calendar(java.util.Calendar) RoutingRequest(org.opentripplanner.routing.core.RoutingRequest)

Example 25 with ServiceDate

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

the class CalendarServiceDataFactoryImpl method getServiceDatesForServiceId.

public Set<ServiceDate> getServiceDatesForServiceId(AgencyAndId serviceId, TimeZone serviceIdTimeZone) {
    Set<ServiceDate> activeDates = new HashSet<ServiceDate>();
    ServiceCalendar c = _dao.getCalendarForServiceId(serviceId);
    if (c != null) {
        addDatesFromCalendar(c, serviceIdTimeZone, activeDates);
    }
    for (ServiceCalendarDate cd : _dao.getCalendarDatesForServiceId(serviceId)) {
        addAndRemoveDatesFromCalendarDate(cd, serviceIdTimeZone, activeDates);
    }
    return activeDates;
}
Also used : ServiceCalendarDate(org.onebusaway.gtfs.model.ServiceCalendarDate) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) HashSet(java.util.HashSet) ServiceCalendar(org.onebusaway.gtfs.model.ServiceCalendar)

Aggregations

ServiceDate (org.onebusaway.gtfs.model.calendar.ServiceDate)134 Test (org.junit.Test)49 Date (java.util.Date)46 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)45 ArrayList (java.util.ArrayList)23 ServiceCalendar (org.onebusaway.gtfs.model.ServiceCalendar)23 Calendar (java.util.Calendar)22 ServiceCalendarDate (org.onebusaway.gtfs.model.ServiceCalendarDate)22 HashSet (java.util.HashSet)17 ServiceIdActivation (org.onebusaway.transit_data_federation.services.transit_graph.ServiceIdActivation)14 CalendarServiceData (org.onebusaway.gtfs.model.calendar.CalendarServiceData)13 CalendarService (org.onebusaway.gtfs.services.calendar.CalendarService)13 Set (java.util.Set)11 Trip (org.onebusaway.gtfs.model.Trip)11 TimeZone (java.util.TimeZone)10 GtfsMutableRelationalDao (org.onebusaway.gtfs.services.GtfsMutableRelationalDao)10 TripUpdate (com.google.transit.realtime.GtfsRealtime.TripUpdate)9 LocalizedServiceId (org.onebusaway.gtfs.model.calendar.LocalizedServiceId)9 Agency (org.onebusaway.gtfs.model.Agency)8 TripDescriptor (com.google.transit.realtime.GtfsRealtime.TripDescriptor)7