Search in sources :

Example 71 with ServiceDate

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

the class RoutingContextTest method testSetServiceDays.

@Test
public void testSetServiceDays() throws Exception {
    String feedId = "FEED";
    String agencyId = "AGENCY";
    Agency agency = new Agency();
    agency.setId(agencyId);
    Graph graph = mock(Graph.class);
    RoutingRequest routingRequest = mock(RoutingRequest.class);
    CalendarService calendarService = mock(CalendarService.class);
    // You're probably not supposed to do this to mocks (access their fields directly)
    // But I know of no other way to do this since the mock object has only action-free stub methods.
    routingRequest.modes = new TraverseModeSet("WALK,TRANSIT");
    when(graph.getTimeZone()).thenReturn(TimeZone.getTimeZone("Europe/Budapest"));
    when(graph.getCalendarService()).thenReturn(calendarService);
    when(graph.getFeedIds()).thenReturn(Collections.singletonList("FEED"));
    when(graph.getAgencies(feedId)).thenReturn(Collections.singletonList(agency));
    when(calendarService.getTimeZoneForAgencyId(agencyId)).thenReturn(TimeZone.getTimeZone("Europe/Budapest"));
    when(routingRequest.getSecondsSinceEpoch()).thenReturn(1393750800L, 1396126800L, /* 2014-03-29T22:00:00+01:00 */
    1396132200L, /* 2014-03-29T23:30:00+01:00 */
    1396135800L, 1401696000L, 1414272600L, /* 2014-10-25T23:30:00+02:00 */
    1414276200L, /* 2014-10-26T00:30:00+02:00 */
    1414279800L);
    verifyServiceDays(routingRequest, graph, new ServiceDate(2014, 3, 1), new ServiceDate(2014, 3, 2), new ServiceDate(2014, 3, 3));
    verifyServiceDays(routingRequest, graph, new ServiceDate(2014, 3, 28), new ServiceDate(2014, 3, 29), new ServiceDate(2014, 3, 30));
    verifyServiceDays(routingRequest, graph, new ServiceDate(2014, 3, 28), new ServiceDate(2014, 3, 29), new ServiceDate(2014, 3, 30));
    verifyServiceDays(routingRequest, graph, new ServiceDate(2014, 3, 29), new ServiceDate(2014, 3, 30), new ServiceDate(2014, 3, 31));
    verifyServiceDays(routingRequest, graph, new ServiceDate(2014, 6, 1), new ServiceDate(2014, 6, 2), new ServiceDate(2014, 6, 3));
    verifyServiceDays(routingRequest, graph, new ServiceDate(2014, 10, 24), new ServiceDate(2014, 10, 25), new ServiceDate(2014, 10, 26));
    verifyServiceDays(routingRequest, graph, new ServiceDate(2014, 10, 25), new ServiceDate(2014, 10, 26), new ServiceDate(2014, 10, 27));
    verifyServiceDays(routingRequest, graph, new ServiceDate(2014, 10, 25), new ServiceDate(2014, 10, 26), new ServiceDate(2014, 10, 27));
}
Also used : ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) Agency(org.onebusaway.gtfs.model.Agency) Graph(org.opentripplanner.routing.graph.Graph) CalendarService(org.onebusaway.gtfs.services.calendar.CalendarService) Test(org.junit.Test)

Example 72 with ServiceDate

use of org.onebusaway.gtfs.model.calendar.ServiceDate 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.getAsString());
        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.stopForId.get(new AgencyAndId(feedId, "A"));
    TransitStopDepart transitStopDepartA = graph.index.stopVertexForStop.get(stopA).departVertex;
    // Get trip pattern of last (most recently added) outgoing edge
    final List<Edge> outgoingEdges = (List<Edge>) transitStopDepartA.getOutgoing();
    final TripPattern tripPattern = ((TransitBoardAlight) outgoingEdges.get(outgoingEdges.size() - 1)).getPattern();
    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.routing.edgetype.Timetable) TripUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate) TransitBoardAlight(org.opentripplanner.routing.edgetype.TransitBoardAlight) 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) List(java.util.List) Edge(org.opentripplanner.routing.graph.Edge) TransitStopDepart(org.opentripplanner.routing.vertextype.TransitStopDepart) Test(org.junit.Test)

Example 73 with ServiceDate

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

the class TimetableSnapshotTest method testUpdate.

@Test(expected = ConcurrentModificationException.class)
public void testUpdate() {
    ServiceDate today = new ServiceDate();
    ServiceDate yesterday = today.previous();
    TripPattern pattern = patternIndex.get(new AgencyAndId("agency", "1.1"));
    TimetableSnapshot resolver = new TimetableSnapshot();
    Timetable origNow = resolver.resolve(pattern, today);
    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();
    // new timetable for today
    updateResolver(resolver, pattern, tripUpdate, "agency", today);
    Timetable updatedNow = resolver.resolve(pattern, today);
    assertNotSame(origNow, updatedNow);
    // reuse timetable for today
    updateResolver(resolver, pattern, tripUpdate, "agency", today);
    assertEquals(updatedNow, resolver.resolve(pattern, today));
    // create new timetable for tomorrow
    updateResolver(resolver, pattern, tripUpdate, "agency", yesterday);
    assertNotSame(origNow, resolver.resolve(pattern, yesterday));
    assertNotSame(updatedNow, resolver.resolve(pattern, yesterday));
    // exception if we try to modify a snapshot
    TimetableSnapshot snapshot = resolver.commit();
    updateResolver(snapshot, pattern, tripUpdate, "agency", yesterday);
}
Also used : ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) TripUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) TripDescriptor(com.google.transit.realtime.GtfsRealtime.TripDescriptor) Test(org.junit.Test)

Example 74 with ServiceDate

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

the class Graph method updateTransitFeedValidity.

// Infer the time period covered by the transit feed
public void updateTransitFeedValidity(CalendarServiceData data) {
    long now = new Date().getTime() / 1000;
    final long SEC_IN_DAY = 24 * 60 * 60;
    HashSet<String> agenciesWithFutureDates = new HashSet<String>();
    HashSet<String> agencies = new HashSet<String>();
    for (AgencyAndId sid : data.getServiceIds()) {
        agencies.add(sid.getAgencyId());
        for (ServiceDate sd : data.getServiceDatesForServiceId(sid)) {
            // Adjust for timezone, assuming there is only one per graph.
            long t = sd.getAsDate(getTimeZone()).getTime() / 1000;
            if (t > now) {
                agenciesWithFutureDates.add(sid.getAgencyId());
            }
            // assume feed is unreliable after midnight on last service day
            long u = t + SEC_IN_DAY;
            if (t < this.transitServiceStarts)
                this.transitServiceStarts = t;
            if (u > this.transitServiceEnds)
                this.transitServiceEnds = u;
        }
    }
    for (String agency : agencies) {
        if (!agenciesWithFutureDates.contains(agency)) {
            LOG.warn(this.addBuilderAnnotation(new NoFutureDates(agency)));
        }
    }
}
Also used : ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) NoFutureDates(org.opentripplanner.graph_builder.annotation.NoFutureDates) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate)

Example 75 with ServiceDate

use of org.onebusaway.gtfs.model.calendar.ServiceDate in project onebusaway-gtfs-modules by OneBusAway.

the class MockGtfsTest method testPutCalendarDates.

@Test
public void testPutCalendarDates() throws IOException {
    _gtfs.putMinimal();
    _gtfs.putCalendarDates("sid0=20120901,-20120902", "sid1=20120903");
    GtfsRelationalDao dao = _gtfs.read();
    List<ServiceCalendarDate> dates = dao.getCalendarDatesForServiceId(new AgencyAndId("a0", "sid0"));
    assertEquals(2, dates.size());
    assertEquals(new ServiceDate(2012, 9, 1), dates.get(0).getDate());
    assertEquals(1, dates.get(0).getExceptionType());
    assertEquals(new ServiceDate(2012, 9, 2), dates.get(1).getDate());
    assertEquals(2, dates.get(1).getExceptionType());
    dates = dao.getCalendarDatesForServiceId(new AgencyAndId("a0", "sid1"));
    assertEquals(1, dates.size());
    assertEquals(new ServiceDate(2012, 9, 3), dates.get(0).getDate());
    assertEquals(1, dates.get(0).getExceptionType());
}
Also used : ServiceCalendarDate(org.onebusaway.gtfs.model.ServiceCalendarDate) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) Test(org.junit.Test)

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