Search in sources :

Example 16 with ServiceDate

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

the class ExtendedCalendarServiceImpl method computeServiceDatesForServiceIds.

private List<Date> computeServiceDatesForServiceIds(ServiceIdActivation serviceIds, Date lowerBounds, Date upperBounds) {
    Set<Date> serviceDates = null;
    for (LocalizedServiceId lsid : serviceIds.getActiveServiceIds()) {
        List<Date> dates = _calendarService.getDatesForLocalizedServiceId(lsid);
        if (dates == null)
            dates = Collections.emptyList();
        if (serviceDates == null)
            serviceDates = new HashSet<Date>(dates);
        else
            serviceDates.retainAll(dates);
    }
    for (LocalizedServiceId lsid : serviceIds.getInactiveServiceIds()) {
        List<Date> dates = _calendarService.getDatesForLocalizedServiceId(lsid);
        if (serviceDates != null)
            serviceDates.removeAll(dates);
    }
    List<Date> dates = new ArrayList<Date>();
    if (serviceDates != null) {
        for (Date serviceDate : serviceDates) {
            if ((lowerBounds == null || lowerBounds.before(serviceDate)) && (upperBounds == null || serviceDate.before(upperBounds)))
                dates.add(serviceDate);
        }
    }
    Collections.sort(dates);
    return dates;
}
Also used : LocalizedServiceId(org.onebusaway.gtfs.model.calendar.LocalizedServiceId) ArrayList(java.util.ArrayList) Date(java.util.Date) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) HashSet(java.util.HashSet)

Example 17 with ServiceDate

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

the class BlockConfigurationEntriesFactoryTest method before.

@Before
public void before() {
    /**
     **
     * Calendar Service
     ***
     */
    _calendarService = new CalendarServiceImpl();
    CalendarServiceData data = new CalendarServiceData();
    _calendarService.setData(data);
    addServiceDates(data, "sA", new ServiceDate(2010, 9, 10), new ServiceDate(2010, 9, 11));
    addServiceDates(data, "sB", new ServiceDate(2010, 9, 11), new ServiceDate(2010, 9, 12));
    /**
     **
     * Service Id Cache
     ***
     */
    ServiceIdOverlapCache serviceIdOverlapCache = new ServiceIdOverlapCache();
    serviceIdOverlapCache.setCalendarService(_calendarService);
    /**
     **
     * ShapePointsService
     ***
     */
    ShapePointHelper shapePointService = Mockito.mock(ShapePointHelper.class);
    ShapePointsFactory shapePointsFactory = new ShapePointsFactory();
    shapePointsFactory.addPoint(0, 0);
    Mockito.when(shapePointService.getShapePointsForShapeId((AgencyAndId) Mockito.any())).thenReturn(shapePointsFactory.create());
    /**
     **
     * Factory
     ***
     */
    _factory = new BlockConfigurationEntriesFactory();
    _factory.setServiceIdOverlapCache(serviceIdOverlapCache);
    _factory.setShapePointHelper(shapePointService);
}
Also used : CalendarServiceData(org.onebusaway.gtfs.model.calendar.CalendarServiceData) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) ShapePointsFactory(org.onebusaway.transit_data_federation.model.ShapePointsFactory) CalendarServiceImpl(org.onebusaway.gtfs.impl.calendar.CalendarServiceImpl) ShapePointHelper(org.onebusaway.transit_data_federation.bundle.tasks.ShapePointHelper) Before(org.junit.Before)

Example 18 with ServiceDate

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

the class ServiceIdController method index.

@RequestMapping()
public ModelAndView index(@RequestParam() String serviceId) {
    AgencyAndId id = AgencyAndIdLibrary.convertFromString(serviceId);
    List<ServiceDate> serviceDates = new ArrayList<ServiceDate>(_calendarService.getServiceDatesForServiceId(id));
    Collections.sort(serviceDates);
    List<Date> dates = new ArrayList<Date>();
    for (ServiceDate serviceDate : serviceDates) dates.add(serviceDate.getAsDate());
    ModelAndView mv = new ModelAndView("service-id.jsp");
    mv.addObject("serviceId", serviceId);
    mv.addObject("dates", dates);
    return mv;
}
Also used : ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) ArrayList(java.util.ArrayList) ModelAndView(org.springframework.web.servlet.ModelAndView) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) Date(java.util.Date) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 19 with ServiceDate

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

the class TimetableSnapshotSource method purgeExpiredData.

private boolean purgeExpiredData() {
    final ServiceDate today = new ServiceDate();
    // Just to be safe...
    final ServiceDate previously = today.previous().previous();
    if (lastPurgeDate != null && lastPurgeDate.compareTo(previously) > 0) {
        return false;
    }
    LOG.debug("purging expired realtime data");
    lastPurgeDate = previously;
    return buffer.purgeExpiredData(previously);
}
Also used : ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate)

Example 20 with ServiceDate

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

the class TimetableSnapshotSource method applyTripUpdates.

/**
 * Method to apply a trip update list to the most recent version of the timetable snapshot. A
 * GTFS-RT feed is always applied against a single static feed (indicated by feedId).
 *<<<<<<< HEAD
 *
 *=======
 *
 * However, multi-feed support is not completed and we currently assume there is only one static
 * feed when matching IDs.
 *
 *>>>>>>> 7296be8ffd532a13afb0bec263a9f436ab787022
 * @param graph graph to update (needed for adding/changing stop patterns)
 * @param fullDataset true iff the list with updates represent all updates that are active right
 *        now, i.e. all previous updates should be disregarded
 * @param updates GTFS-RT TripUpdate's that should be applied atomically
 * @param feedId
 */
public void applyTripUpdates(final Graph graph, final boolean fullDataset, final List<TripUpdate> updates, final String feedId) {
    if (updates == null) {
        LOG.warn("updates is null");
        return;
    }
    // Acquire lock on buffer
    bufferLock.lock();
    try {
        if (fullDataset) {
            // Remove all updates from the buffer
            buffer.clear(feedId);
        }
        LOG.debug("message contains {} trip updates", updates.size());
        int uIndex = 0;
        for (TripUpdate tripUpdate : updates) {
            if (fuzzyTripMatcher != null && tripUpdate.hasTrip()) {
                final TripDescriptor trip = fuzzyTripMatcher.match(feedId, tripUpdate.getTrip());
                tripUpdate = tripUpdate.toBuilder().setTrip(trip).build();
            }
            if (!tripUpdate.hasTrip()) {
                LOG.warn("Missing TripDescriptor in gtfs-rt trip update: \n{}", tripUpdate);
                continue;
            }
            ServiceDate serviceDate = new ServiceDate();
            final TripDescriptor tripDescriptor = tripUpdate.getTrip();
            if (tripDescriptor.hasStartDate()) {
                try {
                    serviceDate = ServiceDate.parseString(tripDescriptor.getStartDate());
                } catch (final ParseException e) {
                    LOG.warn("Failed to parse start date in gtfs-rt trip update: \n{}", tripUpdate);
                    continue;
                }
            } else {
            // TODO: figure out the correct service date. For the special case that a trip
            // starts for example at 40:00, yesterday would probably be a better guess.
            }
            uIndex += 1;
            LOG.debug("trip update #{} ({} updates) :", uIndex, tripUpdate.getStopTimeUpdateCount());
            LOG.trace("{}", tripUpdate);
            // Determine what kind of trip update this is
            boolean applied = false;
            final TripDescriptor.ScheduleRelationship tripScheduleRelationship = determineTripScheduleRelationship(tripUpdate);
            switch(tripScheduleRelationship) {
                case SCHEDULED:
                    applied = handleScheduledTrip(tripUpdate, feedId, serviceDate);
                    break;
                case ADDED:
                    applied = validateAndHandleAddedTrip(graph, tripUpdate, feedId, serviceDate);
                    break;
                case UNSCHEDULED:
                    applied = handleUnscheduledTrip(tripUpdate, feedId, serviceDate);
                    break;
                case CANCELED:
                    applied = handleCanceledTrip(tripUpdate, feedId, serviceDate);
                    break;
                case MODIFIED:
                    applied = validateAndHandleModifiedTrip(graph, tripUpdate, feedId, serviceDate);
                    break;
            }
            if (applied) {
                appliedBlockCount++;
            } else {
                LOG.warn("Failed to apply TripUpdate.");
                LOG.trace(" Contents: {}", tripUpdate);
            }
            if (appliedBlockCount % logFrequency == 0) {
                LOG.info("Applied {} trip updates.", appliedBlockCount);
            }
        }
        LOG.debug("end of update message");
        // Make sure that the public (locking) getTimetableSnapshot function is not called.
        if (purgeExpiredData) {
            final boolean modified = purgeExpiredData();
            getTimetableSnapshot(modified);
        } else {
            getTimetableSnapshot(false);
        }
    } finally {
        // Always release lock
        bufferLock.unlock();
    }
}
Also used : ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) TripUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate) TripDescriptor(com.google.transit.realtime.GtfsRealtime.TripDescriptor) ParseException(java.text.ParseException)

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