Search in sources :

Example 6 with CalendarService

use of org.onebusaway.gtfs.services.calendar.CalendarService 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 7 with CalendarService

use of org.onebusaway.gtfs.services.calendar.CalendarService 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 8 with CalendarService

use of org.onebusaway.gtfs.services.calendar.CalendarService in project onebusaway-gtfs-modules by OneBusAway.

the class CalendarExtensionStrategyTest method testCalendarDateExtension.

@Test
public void testCalendarDateExtension() throws IOException {
    _gtfs.putCalendarDates("sid0=20121217,20121218,20121219,20121220,20121221," + "20121224,20121225,20121226,20121227,20121228", "sid1=20121222,20121223,20121229,20121230");
    GtfsMutableRelationalDao dao = _gtfs.read();
    ServiceDate endDate = new ServiceDate(2013, 01, 06);
    _strategy.setEndDate(endDate);
    _strategy.run(_context, dao);
    CalendarService service = CalendarServiceDataFactoryImpl.createService(dao);
    {
        Set<ServiceDate> dates = service.getServiceDatesForServiceId(new AgencyAndId("a0", "sid0"));
        assertEquals(15, dates.size());
        assertTrue(dates.contains(new ServiceDate(2012, 12, 31)));
        assertTrue(dates.contains(new ServiceDate(2013, 01, 01)));
        assertTrue(dates.contains(new ServiceDate(2013, 01, 02)));
        assertTrue(dates.contains(new ServiceDate(2013, 01, 03)));
        assertTrue(dates.contains(new ServiceDate(2013, 01, 04)));
    }
    {
        Set<ServiceDate> dates = service.getServiceDatesForServiceId(new AgencyAndId("a0", "sid1"));
        assertEquals(6, dates.size());
        assertTrue(dates.contains(new ServiceDate(2013, 01, 05)));
        assertTrue(dates.contains(new ServiceDate(2013, 01, 06)));
    }
}
Also used : GtfsMutableRelationalDao(org.onebusaway.gtfs.services.GtfsMutableRelationalDao) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) Set(java.util.Set) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) CalendarService(org.onebusaway.gtfs.services.calendar.CalendarService) Test(org.junit.Test)

Aggregations

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