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