Search in sources :

Example 1 with CalendarService

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

the class GtfsHibernateReaderExampleMain method main.

public static void main(String[] args) throws IOException {
    if (!(args.length == 1 || args.length == 2)) {
        System.err.println("usage: gtfsPath [hibernate-config.xml]");
        System.exit(-1);
    }
    String resource = "classpath:org/onebusaway/gtfs/examples/hibernate-configuration-examples.xml";
    if (args.length == 2)
        resource = args[1];
    HibernateGtfsFactory factory = createHibernateGtfsFactory(resource);
    GtfsReader reader = new GtfsReader();
    reader.setInputLocation(new File(args[0]));
    GtfsMutableRelationalDao dao = factory.getDao();
    reader.setEntityStore(dao);
    reader.run();
    Collection<Stop> stops = dao.getAllStops();
    for (Stop stop : stops) System.out.println(stop.getName());
    CalendarService calendarService = factory.getCalendarService();
    Set<AgencyAndId> serviceIds = calendarService.getServiceIds();
    for (AgencyAndId serviceId : serviceIds) {
        Set<ServiceDate> dates = calendarService.getServiceDatesForServiceId(serviceId);
        ServiceDate from = null;
        ServiceDate to = null;
        for (ServiceDate date : dates) {
            from = min(from, date);
            to = max(to, date);
        }
        System.out.println("serviceId=" + serviceId + " from=" + from + " to=" + to);
    }
}
Also used : GtfsMutableRelationalDao(org.onebusaway.gtfs.services.GtfsMutableRelationalDao) GtfsReader(org.onebusaway.gtfs.serialization.GtfsReader) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) Stop(org.onebusaway.gtfs.model.Stop) HibernateGtfsFactory(org.onebusaway.gtfs.services.HibernateGtfsFactory) File(java.io.File) CalendarService(org.onebusaway.gtfs.services.calendar.CalendarService)

Example 2 with CalendarService

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

the class DeduplicateServiceIdsStrategy method run.

@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
    CalendarService service = CalendarServiceDataFactoryImpl.createService(dao);
    Map<Set<ServiceDate>, List<AgencyAndId>> serviceIdsByServiceDates = new FactoryMap<Set<ServiceDate>, List<AgencyAndId>>(new ArrayList<AgencyAndId>());
    for (AgencyAndId serviceId : dao.getAllServiceIds()) {
        Set<ServiceDate> serviceDates = service.getServiceDatesForServiceId(serviceId);
        serviceIdsByServiceDates.get(serviceDates).add(serviceId);
    }
    Map<AgencyAndId, AgencyAndId> serviceIdMapping = new HashMap<AgencyAndId, AgencyAndId>();
    for (List<AgencyAndId> serviceIds : serviceIdsByServiceDates.values()) {
        Collections.sort(serviceIds);
        if (serviceIds.size() == 1) {
            continue;
        }
        AgencyAndId target = serviceIds.get(0);
        for (int i = 1; i < serviceIds.size(); ++i) {
            AgencyAndId source = serviceIds.get(i);
            serviceIdMapping.put(source, target);
        }
    }
    for (Trip trip : dao.getAllTrips()) {
        AgencyAndId mappedServiceId = serviceIdMapping.get(trip.getServiceId());
        if (mappedServiceId != null) {
            trip.setServiceId(mappedServiceId);
        }
    }
    for (AgencyAndId serviceId : serviceIdMapping.keySet()) {
        ServiceCalendar serviceCalendar = dao.getCalendarForServiceId(serviceId);
        if (serviceCalendar != null) {
            dao.removeEntity(serviceCalendar);
        }
        for (ServiceCalendarDate date : dao.getCalendarDatesForServiceId(serviceId)) {
            dao.removeEntity(date);
        }
    }
    _log.info("removed {} duplicate service ids", serviceIdMapping.size());
    UpdateLibrary.clearDaoCache(dao);
}
Also used : FactoryMap(org.onebusaway.collections.FactoryMap) ServiceCalendarDate(org.onebusaway.gtfs.model.ServiceCalendarDate) Trip(org.onebusaway.gtfs.model.Trip) Set(java.util.Set) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) HashMap(java.util.HashMap) CalendarService(org.onebusaway.gtfs.services.calendar.CalendarService) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) ArrayList(java.util.ArrayList) List(java.util.List) ServiceCalendar(org.onebusaway.gtfs.model.ServiceCalendar)

Example 3 with CalendarService

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

the class CalendarExtensionStrategyTest method testCalendarDateSelectiveExtension.

@Test
public void testCalendarDateSelectiveExtension() throws IOException {
    /**
     * Tuesday is only partially active for sid0
     */
    _gtfs.putCalendarDates("sid0=20121210,20121211,20121212,20121213,20121214," + "20121217,20121219,20121220,20121221," + "20121224,20121226,20121227,20121228", "sid1=20121208,20121209,20121215,20121216");
    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(17, dates.size());
        assertTrue(dates.contains(new ServiceDate(2012, 12, 31)));
        assertFalse(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(4, dates.size());
    }
}
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)

Example 4 with CalendarService

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

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