Search in sources :

Example 1 with ServiceCalendarSummary

use of org.onebusaway.gtfs_transformer.updates.CalendarSimplicationLibrary.ServiceCalendarSummary in project onebusaway-gtfs-modules by OneBusAway.

the class CalendarSimplicationLibraryTest method test.

@Test
public void test() throws IOException {
    MockGtfs gtfs = MockGtfs.create();
    gtfs.putAgencies(1, "agency_timezone=America/New_York");
    gtfs.putRoutes(1);
    gtfs.putTrips(1, "r0", "sid0");
    gtfs.putStops(1);
    gtfs.putStopTimes("t0", "s0");
    gtfs.putCalendarDates("sid0=20120305,20120306,20120307,20120308,20120309," + "20120312,20120313,20120314,20120315,20120316,20120319,20120320,20120321,20120323," + "20120326,20120327,20120328,20120329,20120330");
    GtfsMutableRelationalDao dao = gtfs.read();
    CalendarService calendarService = CalendarServiceDataFactoryImpl.createService(dao);
    AgencyAndId originalId = new AgencyAndId("a0", "sid0");
    AgencyAndId updatedId = new AgencyAndId("a0", "sidX");
    ServiceCalendarSummary summary = _library.getSummaryForServiceDates(calendarService.getServiceDatesForServiceId(originalId));
    List<Object> newEntities = new ArrayList<Object>();
    _library.computeSimplifiedCalendar(updatedId, summary, newEntities);
    List<ServiceCalendar> calendars = getEntities(newEntities, ServiceCalendar.class);
    assertEquals(1, calendars.size());
    ServiceCalendar calendar = calendars.get(0);
    assertEquals(updatedId, calendar.getServiceId());
    assertEquals(new ServiceDate(2012, 03, 05), calendar.getStartDate());
    assertEquals(new ServiceDate(2012, 03, 30), calendar.getEndDate());
    assertEquals(1, calendar.getMonday());
    assertEquals(1, calendar.getTuesday());
    assertEquals(1, calendar.getWednesday());
    assertEquals(1, calendar.getThursday());
    assertEquals(1, calendar.getFriday());
    assertEquals(0, calendar.getSaturday());
    assertEquals(0, calendar.getSunday());
    List<ServiceCalendarDate> calendarDates = getEntities(newEntities, ServiceCalendarDate.class);
    assertEquals(1, calendarDates.size());
    ServiceCalendarDate date = calendarDates.get(0);
    assertEquals(updatedId, date.getServiceId());
    assertEquals(new ServiceDate(2012, 03, 22), date.getDate());
    assertEquals(ServiceCalendarDate.EXCEPTION_TYPE_REMOVE, date.getExceptionType());
}
Also used : GtfsMutableRelationalDao(org.onebusaway.gtfs.services.GtfsMutableRelationalDao) ServiceCalendarDate(org.onebusaway.gtfs.model.ServiceCalendarDate) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) MockGtfs(org.onebusaway.gtfs.services.MockGtfs) ArrayList(java.util.ArrayList) ServiceCalendarSummary(org.onebusaway.gtfs_transformer.updates.CalendarSimplicationLibrary.ServiceCalendarSummary) CalendarService(org.onebusaway.gtfs.services.calendar.CalendarService) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) ServiceCalendar(org.onebusaway.gtfs.model.ServiceCalendar) Test(org.junit.Test)

Example 2 with ServiceCalendarSummary

use of org.onebusaway.gtfs_transformer.updates.CalendarSimplicationLibrary.ServiceCalendarSummary in project onebusaway-gtfs-modules by OneBusAway.

the class ShiftNegativeStopTimesUpdateStrategy method run.

@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
    Set<ShiftedServiceCalendar> shiftedIds = new HashSet<ShiftedServiceCalendar>();
    for (Trip trip : dao.getAllTrips()) {
        List<StopTime> stopTimes = dao.getStopTimesForTrip(trip);
        int minTime = getMinStopTime(stopTimes);
        if (minTime >= 0) {
            continue;
        }
        int dayShift = getDayShiftForNegativeStopTime(minTime);
        shiftStopTimes(stopTimes, dayShift * SECONDS_IN_DAY);
        ShiftedServiceCalendar shifted = new ShiftedServiceCalendar(trip.getServiceId(), -dayShift);
        shiftedIds.add(shifted);
        trip.setServiceId(shifted.getShiftedServiceId());
    }
    CalendarService calendarService = CalendarServiceDataFactoryImpl.createService(dao);
    CalendarSimplicationLibrary library = new CalendarSimplicationLibrary();
    for (ShiftedServiceCalendar shifted : shiftedIds) {
        Set<ServiceDate> allServiceDates = calendarService.getServiceDatesForServiceId(shifted.getOriginalServiceId());
        Set<ServiceDate> shiftedServiceDates = shiftServiceDates(allServiceDates, shifted.getDayOffset());
        ServiceCalendarSummary summary = library.getSummaryForServiceDates(shiftedServiceDates);
        List<Object> newEntities = new ArrayList<Object>();
        library.computeSimplifiedCalendar(shifted.getShiftedServiceId(), summary, newEntities);
        for (Object newEntity : newEntities) {
            dao.saveEntity(newEntity);
        }
    }
    UpdateLibrary.clearDaoCache(dao);
}
Also used : Trip(org.onebusaway.gtfs.model.Trip) ArrayList(java.util.ArrayList) ServiceCalendarSummary(org.onebusaway.gtfs_transformer.updates.CalendarSimplicationLibrary.ServiceCalendarSummary) CalendarService(org.onebusaway.gtfs.services.calendar.CalendarService) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) HashSet(java.util.HashSet) StopTime(org.onebusaway.gtfs.model.StopTime)

Example 3 with ServiceCalendarSummary

use of org.onebusaway.gtfs_transformer.updates.CalendarSimplicationLibrary.ServiceCalendarSummary in project onebusaway-gtfs-modules by OneBusAway.

the class CalendarSimplicationStrategy method run.

@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
    RemoveEntityLibrary removeEntityLibrary = new RemoveEntityLibrary();
    Map<Set<AgencyAndId>, AgencyAndId> serviceIdsToUpdatedServiceId = new HashMap<Set<AgencyAndId>, AgencyAndId>();
    Map<AgencyAndId, List<AgencyAndId>> mergeToolIdMapping = computeMergeToolIdMapping(dao);
    for (Route route : dao.getAllRoutes()) {
        Map<TripKey, List<Trip>> tripsByKey = TripKey.groupTripsForRouteByKey(dao, route);
        Map<Set<AgencyAndId>, List<TripKey>> tripKeysByServiceIds = _library.groupTripKeysByServiceIds(tripsByKey);
        for (Set<AgencyAndId> serviceIds : tripKeysByServiceIds.keySet()) {
            AgencyAndId updatedServiceId = createUpdatedServiceId(serviceIdsToUpdatedServiceId, serviceIds);
            for (TripKey tripKey : tripKeysByServiceIds.get(serviceIds)) {
                List<Trip> tripsForKey = tripsByKey.get(tripKey);
                Trip tripToKeep = tripsForKey.get(0);
                tripToKeep.setServiceId(updatedServiceId);
                for (int i = 1; i < tripsForKey.size(); i++) {
                    Trip trip = tripsForKey.get(i);
                    removeEntityLibrary.removeTrip(dao, trip);
                }
                if (undoGoogleTransitDataFeedMergeTool) {
                    AgencyAndId updatedTripId = computeUpdatedTripIdForMergedTripsIfApplicable(mergeToolIdMapping, tripsForKey);
                    if (updatedTripId != null) {
                        tripToKeep.setId(updatedTripId);
                    }
                }
            }
        }
    }
    CalendarService calendarService = CalendarServiceDataFactoryImpl.createService(dao);
    List<Object> newEntities = new ArrayList<Object>();
    for (Map.Entry<Set<AgencyAndId>, AgencyAndId> entry : serviceIdsToUpdatedServiceId.entrySet()) {
        Set<ServiceDate> allServiceDates = getServiceDatesForServiceIds(calendarService, entry.getKey());
        ServiceCalendarSummary summary = _library.getSummaryForServiceDates(allServiceDates);
        _library.computeSimplifiedCalendar(entry.getValue(), summary, newEntities);
    }
    saveUpdatedCalendarEntities(dao, newEntities);
}
Also used : Trip(org.onebusaway.gtfs.model.Trip) HashSet(java.util.HashSet) Set(java.util.Set) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ServiceCalendarSummary(org.onebusaway.gtfs_transformer.updates.CalendarSimplicationLibrary.ServiceCalendarSummary) CalendarService(org.onebusaway.gtfs.services.calendar.CalendarService) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) ArrayList(java.util.ArrayList) List(java.util.List) RemoveEntityLibrary(org.onebusaway.gtfs_transformer.impl.RemoveEntityLibrary) HashMap(java.util.HashMap) Map(java.util.Map) FactoryMap(org.onebusaway.collections.FactoryMap) Route(org.onebusaway.gtfs.model.Route)

Example 4 with ServiceCalendarSummary

use of org.onebusaway.gtfs_transformer.updates.CalendarSimplicationLibrary.ServiceCalendarSummary in project onebusaway-gtfs-modules by OneBusAway.

the class CalendarExtensionStrategy method run.

@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
    CalendarService service = CalendarServiceDataFactoryImpl.createService(dao);
    CalendarSimplicationLibrary simplication = new CalendarSimplicationLibrary();
    for (AgencyAndId serviceId : dao.getAllServiceIds()) {
        ServiceCalendarSummary summary = simplication.getSummaryForServiceDates(service.getServiceDatesForServiceId(serviceId));
        /**
         * If a service id has no active dates at all, we don't extend it.
         */
        if (summary.allServiceDates.isEmpty()) {
            continue;
        }
        ServiceCalendar calendar = dao.getCalendarForServiceId(serviceId);
        if (calendar == null) {
            ServiceDate lastDate = summary.serviceDatesInOrder.get(summary.serviceDatesInOrder.size() - 1);
            if (lastDate.compareTo(inactiveCalendarCutoff) < 0) {
                continue;
            }
            /**
             * We only want days of the week that are in service past our stale
             * calendar cutoff.
             */
            Set<Integer> daysOfTheWeekToUse = getDaysOfTheWeekToUse(summary);
            if (daysOfTheWeekToUse.isEmpty()) {
                continue;
            }
            ServiceDate firstMissingDate = lastDate.next();
            for (ServiceDate serviceDate = firstMissingDate; serviceDate.compareTo(endDate) <= 0; serviceDate = serviceDate.next()) {
                Calendar serviceDateAsCalendar = serviceDate.getAsCalendar(_utcTimeZone);
                // Move the calendar forward to "noon" to mitigate the effects of DST
                // (though the shouldn't be a problem for UTC?)
                serviceDateAsCalendar.add(Calendar.HOUR_OF_DAY, 12);
                int dayOfWeek = serviceDateAsCalendar.get(Calendar.DAY_OF_WEEK);
                if (daysOfTheWeekToUse.contains(dayOfWeek)) {
                    ServiceCalendarDate scd = new ServiceCalendarDate();
                    scd.setDate(serviceDate);
                    scd.setExceptionType(ServiceCalendarDate.EXCEPTION_TYPE_ADD);
                    scd.setServiceId(serviceId);
                    dao.saveEntity(scd);
                }
            }
        } else {
            if (calendar.getEndDate().compareTo(inactiveCalendarCutoff) >= 0) {
                calendar.setEndDate(endDate);
            }
        }
    }
    UpdateLibrary.clearDaoCache(dao);
}
Also used : ServiceCalendarDate(org.onebusaway.gtfs.model.ServiceCalendarDate) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) ServiceCalendar(org.onebusaway.gtfs.model.ServiceCalendar) Calendar(java.util.Calendar) ServiceCalendarSummary(org.onebusaway.gtfs_transformer.updates.CalendarSimplicationLibrary.ServiceCalendarSummary) CalendarService(org.onebusaway.gtfs.services.calendar.CalendarService) ServiceCalendar(org.onebusaway.gtfs.model.ServiceCalendar)

Aggregations

ServiceDate (org.onebusaway.gtfs.model.calendar.ServiceDate)4 CalendarService (org.onebusaway.gtfs.services.calendar.CalendarService)4 ServiceCalendarSummary (org.onebusaway.gtfs_transformer.updates.CalendarSimplicationLibrary.ServiceCalendarSummary)4 ArrayList (java.util.ArrayList)3 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)3 HashSet (java.util.HashSet)2 ServiceCalendar (org.onebusaway.gtfs.model.ServiceCalendar)2 ServiceCalendarDate (org.onebusaway.gtfs.model.ServiceCalendarDate)2 Trip (org.onebusaway.gtfs.model.Trip)2 Calendar (java.util.Calendar)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Test (org.junit.Test)1 FactoryMap (org.onebusaway.collections.FactoryMap)1 Route (org.onebusaway.gtfs.model.Route)1 StopTime (org.onebusaway.gtfs.model.StopTime)1 GtfsMutableRelationalDao (org.onebusaway.gtfs.services.GtfsMutableRelationalDao)1 MockGtfs (org.onebusaway.gtfs.services.MockGtfs)1