Search in sources :

Example 6 with LocalizedServiceId

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

the class CalendarServiceDataFactoryImplSyntheticTest method test.

@Test
public void test() {
    CalendarServiceDataFactoryImpl factory = new CalendarServiceDataFactoryImpl();
    Agency agencyA = agency("A", "America/Los_Angeles");
    Agency agencyB = agency("B", "America/Denver");
    AgencyAndId serviceIdA1 = new AgencyAndId("A", "1");
    AgencyAndId serviceIdA2 = new AgencyAndId("A", "2");
    AgencyAndId serviceIdB1 = new AgencyAndId("B", "1");
    ServiceDate dStart = new ServiceDate(2010, 2, 10);
    ServiceDate dEnd = new ServiceDate(2010, 2, 24);
    ServiceCalendar c1 = calendar(serviceIdA1, dStart, dEnd, "1111100");
    ServiceCalendar c2 = calendar(serviceIdA2, dStart, dEnd, "1111111");
    ServiceCalendar c3 = calendar(serviceIdB1, dStart, dEnd, "0000011");
    ServiceCalendarDate cd1 = calendarDate(serviceIdA2, new ServiceDate(2010, 2, 15), ServiceCalendarDate.EXCEPTION_TYPE_REMOVE);
    ServiceCalendarDate cd2 = calendarDate(serviceIdA2, new ServiceDate(2010, 2, 26), ServiceCalendarDate.EXCEPTION_TYPE_ADD);
    Trip t1 = trip("A", "1", serviceIdA1);
    Trip t2 = trip("A", "2", serviceIdA1);
    Trip t3 = trip("A", "3", serviceIdA2);
    Trip t4 = trip("B", "4", serviceIdA2);
    Trip t5 = trip("B", "5", serviceIdB1);
    Trip t6 = trip("B", "6", serviceIdB1);
    GtfsRelationalDaoImpl dao = new GtfsRelationalDaoImpl();
    factory.setGtfsDao(dao);
    saveEntities(dao, agencyA, agencyB);
    saveEntities(dao, c1, c2, c3);
    saveEntities(dao, cd1, cd2);
    saveEntities(dao, t1, t2, t3, t4, t5, t6);
    CalendarServiceData data = factory.createData();
    TimeZone tzA = TimeZone.getTimeZone("America/Los_Angeles");
    TimeZone tzB = TimeZone.getTimeZone("America/Denver");
    assertEquals(tzA, data.getTimeZoneForAgencyId("A"));
    assertEquals(tzB, data.getTimeZoneForAgencyId("B"));
    assertNull(data.getTimeZoneForAgencyId("DNE"));
    Set<AgencyAndId> serviceIds = data.getServiceIds();
    assertEquals(3, serviceIds.size());
    assertTrue(serviceIds.contains(serviceIdA1));
    assertTrue(serviceIds.contains(serviceIdA2));
    assertTrue(serviceIds.contains(serviceIdB1));
    // M-F only for this service id
    List<ServiceDate> serviceDates = data.getServiceDatesForServiceId(serviceIdA1);
    assertEquals(11, serviceDates.size());
    assertEquals(dStart, serviceDates.get(0));
    assertEquals(new ServiceDate(2010, 2, 16), serviceDates.get(4));
    assertEquals(dEnd, serviceDates.get(10));
    // 7-days a week, with some calendar modifications for this service id
    serviceDates = data.getServiceDatesForServiceId(serviceIdA2);
    assertEquals(15, serviceDates.size());
    assertEquals(dStart, serviceDates.get(0));
    assertEquals(new ServiceDate(2010, 2, 14), serviceDates.get(4));
    // 2010-02-15 should be excluded
    assertEquals(new ServiceDate(2010, 2, 16), serviceDates.get(5));
    // 2010-02-26 should be added
    assertEquals(new ServiceDate(2010, 2, 26), serviceDates.get(14));
    // Just weekends for this service id
    serviceDates = data.getServiceDatesForServiceId(serviceIdB1);
    assertEquals(4, serviceDates.size());
    assertEquals(new ServiceDate(2010, 2, 13), serviceDates.get(0));
    assertEquals(new ServiceDate(2010, 2, 14), serviceDates.get(1));
    assertEquals(new ServiceDate(2010, 2, 20), serviceDates.get(2));
    assertEquals(new ServiceDate(2010, 2, 21), serviceDates.get(3));
    // Service id does not exist
    serviceDates = data.getServiceDatesForServiceId(new AgencyAndId("DNE", "DNE"));
    assertNull(serviceDates);
    // Service ids for dates
    Set<AgencyAndId> serviceIdsForDate = data.getServiceIdsForDate(new ServiceDate(2010, 2, 10));
    assertEquals(2, serviceIdsForDate.size());
    assertTrue(serviceIdsForDate.contains(serviceIdA1));
    assertTrue(serviceIdsForDate.contains(serviceIdA2));
    serviceIdsForDate = data.getServiceIdsForDate(new ServiceDate(2010, 2, 13));
    assertEquals(2, serviceIdsForDate.size());
    assertTrue(serviceIdsForDate.contains(serviceIdA2));
    assertTrue(serviceIdsForDate.contains(serviceIdB1));
    serviceIdsForDate = data.getServiceIdsForDate(new ServiceDate(2010, 2, 2));
    assertEquals(0, serviceIdsForDate.size());
    // Localized service dates
    List<Date> dates = data.getDatesForLocalizedServiceId(new LocalizedServiceId(serviceIdA1, tzA));
    assertEquals(11, dates.size());
    assertEquals(DateSupport.date("2010-02-10 00:00 Pacific Standard Time"), dates.get(0));
    assertEquals(DateSupport.date("2010-02-24 00:00 Pacific Standard Time"), dates.get(10));
    dates = data.getDatesForLocalizedServiceId(new LocalizedServiceId(serviceIdA1, tzB));
    assertNull(dates);
    dates = data.getDatesForLocalizedServiceId(new LocalizedServiceId(serviceIdA2, tzA));
    assertEquals(15, dates.size());
    assertEquals(DateSupport.date("2010-02-10 00:00 Pacific Standard Time"), dates.get(0));
    assertEquals(DateSupport.date("2010-02-26 00:00 Pacific Standard Time"), dates.get(14));
    dates = data.getDatesForLocalizedServiceId(new LocalizedServiceId(serviceIdA2, tzB));
    assertEquals(15, dates.size());
    assertEquals(DateSupport.date("2010-02-10 00:00 Mountain Standard Time"), dates.get(0));
    assertEquals(DateSupport.date("2010-02-26 00:00 Mountain Standard Time"), dates.get(14));
    dates = data.getDatesForLocalizedServiceId(new LocalizedServiceId(serviceIdB1, tzA));
    assertNull(dates);
    dates = data.getDatesForLocalizedServiceId(new LocalizedServiceId(serviceIdB1, tzB));
    assertEquals(4, dates.size());
    assertEquals(DateSupport.date("2010-02-13 00:00 Mountain Standard Time"), dates.get(0));
    assertEquals(DateSupport.date("2010-02-21 00:00 Mountain Standard Time"), dates.get(3));
}
Also used : ServiceCalendarDate(org.onebusaway.gtfs.model.ServiceCalendarDate) Trip(org.onebusaway.gtfs.model.Trip) Agency(org.onebusaway.gtfs.model.Agency) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) Date(java.util.Date) ServiceCalendarDate(org.onebusaway.gtfs.model.ServiceCalendarDate) CalendarServiceData(org.onebusaway.gtfs.model.calendar.CalendarServiceData) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) TimeZone(java.util.TimeZone) LocalizedServiceId(org.onebusaway.gtfs.model.calendar.LocalizedServiceId) GtfsRelationalDaoImpl(org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl) ServiceCalendar(org.onebusaway.gtfs.model.ServiceCalendar) Test(org.junit.Test)

Example 7 with LocalizedServiceId

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

the class CalendarServiceDataFactoryImplTest method testIslandGtfs.

@Test
public void testIslandGtfs() throws IOException {
    GtfsRelationalDaoImpl dao = new GtfsRelationalDaoImpl();
    GtfsTestData.readGtfs(dao, GtfsTestData.getIslandGtfs(), "26");
    CalendarServiceDataFactoryImpl factory = new CalendarServiceDataFactoryImpl();
    factory.setGtfsDao(dao);
    CalendarServiceData data = factory.createData();
    TimeZone tzExpected = TimeZone.getTimeZone("America/Los_Angeles");
    TimeZone tzActual = data.getTimeZoneForAgencyId("26");
    assertEquals(tzExpected, tzActual);
    Set<AgencyAndId> serviceIds = data.getServiceIds();
    assertEquals(6, serviceIds.size());
    assertTrue(serviceIds.contains(new AgencyAndId("26", "23")));
    assertTrue(serviceIds.contains(new AgencyAndId("26", "24")));
    assertTrue(serviceIds.contains(new AgencyAndId("26", "25")));
    assertTrue(serviceIds.contains(new AgencyAndId("26", "26")));
    assertTrue(serviceIds.contains(new AgencyAndId("26", "27")));
    assertTrue(serviceIds.contains(new AgencyAndId("26", "28")));
    AgencyAndId serviceId = new AgencyAndId("26", "23");
    List<ServiceDate> serviceDates = data.getServiceDatesForServiceId(serviceId);
    assertEquals(239, serviceDates.size());
    assertEquals(new ServiceDate(2008, 10, 27), serviceDates.get(0));
    assertEquals(new ServiceDate(2008, 10, 28), serviceDates.get(1));
    assertEquals(new ServiceDate(2009, 9, 24), serviceDates.get(serviceDates.size() - 2));
    assertEquals(new ServiceDate(2009, 9, 25), serviceDates.get(serviceDates.size() - 1));
    serviceIds = data.getServiceIdsForDate(new ServiceDate(2008, 01, 02));
    assertEquals(1, serviceIds.size());
    assertTrue(serviceIds.contains(new AgencyAndId("26", "25")));
    serviceIds = data.getServiceIdsForDate(new ServiceDate(2008, 1, 5));
    assertEquals(1, serviceIds.size());
    assertTrue(serviceIds.contains(new AgencyAndId("26", "26")));
    serviceIds = data.getServiceIdsForDate(new ServiceDate(2008, 5, 31));
    assertEquals(2, serviceIds.size());
    assertTrue(serviceIds.contains(new AgencyAndId("26", "26")));
    assertTrue(serviceIds.contains(new AgencyAndId("26", "27")));
    serviceIds = data.getServiceIdsForDate(new ServiceDate(2009, 1, 1));
    assertEquals(0, serviceIds.size());
    List<Date> dates = data.getDatesForLocalizedServiceId(new LocalizedServiceId(new AgencyAndId("26", "23"), tzExpected));
    assertEquals(DateSupport.date("2008-10-27 00:00 Pacific Daylight Time"), dates.get(0));
    assertEquals(DateSupport.date("2008-10-28 00:00 Pacific Daylight Time"), dates.get(1));
    assertEquals(DateSupport.date("2009-09-24 00:00 Pacific Daylight Time"), dates.get(dates.size() - 2));
    assertEquals(DateSupport.date("2009-09-25 00:00 Pacific Daylight Time"), dates.get(dates.size() - 1));
}
Also used : CalendarServiceData(org.onebusaway.gtfs.model.calendar.CalendarServiceData) TimeZone(java.util.TimeZone) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) LocalizedServiceId(org.onebusaway.gtfs.model.calendar.LocalizedServiceId) GtfsRelationalDaoImpl(org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) Date(java.util.Date) Test(org.junit.Test)

Example 8 with LocalizedServiceId

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

the class CalendarServiceDataFactoryImpl method createData.

@Override
public CalendarServiceData createData() {
    CalendarServiceData data = new CalendarServiceData();
    setTimeZonesForAgencies(data);
    List<AgencyAndId> serviceIds = _dao.getAllServiceIds();
    int index = 0;
    for (AgencyAndId serviceId : serviceIds) {
        index++;
        _log.info("serviceId=" + serviceId + " (" + index + "/" + serviceIds.size() + ")");
        TimeZone serviceIdTimeZone = data.getTimeZoneForAgencyId(serviceId.getAgencyId());
        if (serviceIdTimeZone == null) {
            serviceIdTimeZone = TimeZone.getDefault();
        }
        Set<ServiceDate> activeDates = getServiceDatesForServiceId(serviceId, serviceIdTimeZone);
        List<ServiceDate> serviceDates = new ArrayList<ServiceDate>(activeDates);
        Collections.sort(serviceDates);
        data.putServiceDatesForServiceId(serviceId, serviceDates);
        List<String> tripAgencyIds = _dao.getTripAgencyIdsReferencingServiceId(serviceId);
        Set<TimeZone> timeZones = new HashSet<TimeZone>();
        for (String tripAgencyId : tripAgencyIds) {
            TimeZone timeZone = data.getTimeZoneForAgencyId(tripAgencyId);
            timeZones.add(timeZone);
        }
        for (TimeZone timeZone : timeZones) {
            List<Date> dates = new ArrayList<Date>(serviceDates.size());
            for (ServiceDate serviceDate : serviceDates) dates.add(serviceDate.getAsDate(timeZone));
            LocalizedServiceId id = new LocalizedServiceId(serviceId, timeZone);
            data.putDatesForLocalizedServiceId(id, dates);
        }
    }
    return data;
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) ArrayList(java.util.ArrayList) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) Date(java.util.Date) ServiceCalendarDate(org.onebusaway.gtfs.model.ServiceCalendarDate) CalendarServiceData(org.onebusaway.gtfs.model.calendar.CalendarServiceData) TimeZone(java.util.TimeZone) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) LocalizedServiceId(org.onebusaway.gtfs.model.calendar.LocalizedServiceId) HashSet(java.util.HashSet)

Example 9 with LocalizedServiceId

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

the class CalendarServiceImplSyntheticTest method testGetServiceDatesWithinRange04.

@Test
public void testGetServiceDatesWithinRange04() {
    Date from = date("2010-02-02 04:30 Pacific Standard Time");
    Date to = date("2010-02-02 04:45 Pacific Standard Time");
    Map<LocalizedServiceId, List<Date>> result = service.getServiceDateArrivalsWithinRange(intervals, from, to);
    assertEquals(1, result.size());
    List<Date> dates = result.get(lsid2);
    assertEquals(1, dates.size());
    assertTrue(dates.contains(d2.getAsDate(tz)));
}
Also used : LocalizedServiceId(org.onebusaway.gtfs.model.calendar.LocalizedServiceId) ArrayList(java.util.ArrayList) List(java.util.List) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) Date(java.util.Date) Test(org.junit.Test)

Example 10 with LocalizedServiceId

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

the class CalendarServiceImplSyntheticTest method testGetServiceDatesWithinRange02.

@Test
public void testGetServiceDatesWithinRange02() {
    Date from = date("2010-02-03 05:00 Pacific Standard Time");
    Date to = date("2010-02-03 05:30 Pacific Standard Time");
    Map<LocalizedServiceId, List<Date>> result = service.getServiceDatesWithinRange(intervals, from, to);
    assertEquals(1, result.size());
    List<Date> dates = result.get(lsid2);
    assertEquals(2, dates.size());
    assertTrue(dates.contains(d2.getAsDate(tz)));
    assertTrue(dates.contains(d3.getAsDate(tz)));
}
Also used : LocalizedServiceId(org.onebusaway.gtfs.model.calendar.LocalizedServiceId) ArrayList(java.util.ArrayList) List(java.util.List) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) Date(java.util.Date) Test(org.junit.Test)

Aggregations

Date (java.util.Date)11 LocalizedServiceId (org.onebusaway.gtfs.model.calendar.LocalizedServiceId)11 ServiceDate (org.onebusaway.gtfs.model.calendar.ServiceDate)11 ArrayList (java.util.ArrayList)9 Test (org.junit.Test)9 List (java.util.List)8 CalendarServiceData (org.onebusaway.gtfs.model.calendar.CalendarServiceData)4 TimeZone (java.util.TimeZone)3 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)3 GtfsRelationalDaoImpl (org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl)2 ServiceCalendarDate (org.onebusaway.gtfs.model.ServiceCalendarDate)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Agency (org.onebusaway.gtfs.model.Agency)1 ServiceCalendar (org.onebusaway.gtfs.model.ServiceCalendar)1 Trip (org.onebusaway.gtfs.model.Trip)1 ServiceInterval (org.onebusaway.gtfs.model.calendar.ServiceInterval)1