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);
}
}
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);
}
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());
}
}
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());
}
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);
}
Aggregations