Search in sources :

Example 96 with ServiceDate

use of org.onebusaway.gtfs.model.calendar.ServiceDate 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 97 with ServiceDate

use of org.onebusaway.gtfs.model.calendar.ServiceDate 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 98 with ServiceDate

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

the class ThirtyDayCalendarExtensionStrategy method ensureActiveCalendar.

private void ensureActiveCalendar(int daysFromNow, Map<Date, List<ServiceCalendar>> serviceIdsByDate, GtfsMutableRelationalDao dao) {
    Date date = removeTime(new Date(System.currentTimeMillis() + daysFromNow * milisPerDay));
    List<ServiceCalendar> activeServiceIds = getLastActiveCalendar(daysFromNow, serviceIdsByDate);
    // update service ids in dao
    for (ServiceCalendar serviceId : activeServiceIds) {
        ServiceCalendar serviceCalendar = serviceId;
        serviceCalendar.setEndDate(new ServiceDate(date));
        dao.saveOrUpdateEntity(serviceCalendar);
        dao.flush();
    }
}
Also used : ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) ServiceCalendarDate(org.onebusaway.gtfs.model.ServiceCalendarDate) ServiceCalendar(org.onebusaway.gtfs.model.ServiceCalendar)

Example 99 with ServiceDate

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

the class VerifyBusService method constructDate.

private Date constructDate(ServiceDate date) {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.YEAR, date.getYear());
    calendar.set(Calendar.MONTH, date.getMonth() - 1);
    calendar.set(Calendar.DATE, date.getDay());
    Date date1 = calendar.getTime();
    date1 = removeTime(date1);
    return date1;
}
Also used : Calendar(java.util.Calendar) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) Date(java.util.Date) ServiceCalendarDate(org.onebusaway.gtfs.model.ServiceCalendarDate)

Example 100 with ServiceDate

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

the class VerifyBusService method run.

@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
    GtfsMutableRelationalDao reference = (GtfsMutableRelationalDao) context.getReferenceReader().getEntityStore();
    ExternalServices es = new ExternalServicesBridgeFactory().getExternalServices();
    String feed = CloudContextService.getLikelyFeedName(dao);
    CalendarService refCalendarService = CalendarServiceDataFactoryImpl.createService(reference);
    AgencyAndId refAgencyAndId = reference.getAllTrips().iterator().next().getId();
    int curSerRoute = 0;
    int alarmingRoutes = 0;
    Date today = removeTime(new Date());
    // list of all routes in ATIS
    Set<String> ATISrouteIds = new HashSet<>();
    // check for route specific current service
    for (Route route : dao.getAllRoutes()) {
        ATISrouteIds.add(route.getId().getId());
        _log.info("Adding route: {}", route.getId().getId());
        curSerRoute = 0;
        triploop: for (Trip trip1 : dao.getTripsForRoute(route)) {
            for (ServiceCalendarDate calDate : dao.getCalendarDatesForServiceId(trip1.getServiceId())) {
                Date date = constructDate(calDate.getDate());
                if (calDate.getExceptionType() == 1 && date.equals(today)) {
                    _log.info("ATIS has current service for route: {}", route.getId().getId());
                    curSerRoute++;
                    break triploop;
                }
            }
        }
        if (curSerRoute == 0) {
            _log.error("No current service for {}", route.getId().getId());
            // if there is no current service, check that it should have service
            // there are certain routes that don't run on the weekend or won't have service in reference
            ServiceDate sToday = createServiceDate(today);
            Route refRoute = reference.getRouteForId(new AgencyAndId(refAgencyAndId.getAgencyId(), route.getId().getId()));
            reftriploop: for (Trip refTrip : reference.getTripsForRoute(refRoute)) {
                Set<ServiceDate> activeDates = refCalendarService.getServiceDatesForServiceId(refTrip.getServiceId());
                if (activeDates.contains(sToday)) {
                    _log.info("Reference has service for this bus route today but ATIS does not: {}", route.getId());
                    // This would be the site to add to a bulk metric, missingBus:
                    alarmingRoutes++;
                }
            }
        }
    }
    es.publishMetric(CloudContextService.getNamespace(), "RoutesMissingTripsFromAtisButInRefToday", "feed", feed, alarmingRoutes);
    es.publishMetric(CloudContextService.getNamespace(), "RoutesContainingTripsToday", "feed", feed, curSerRoute);
}
Also used : GtfsMutableRelationalDao(org.onebusaway.gtfs.services.GtfsMutableRelationalDao) ServiceCalendarDate(org.onebusaway.gtfs.model.ServiceCalendarDate) Trip(org.onebusaway.gtfs.model.Trip) ExternalServicesBridgeFactory(org.onebusaway.cloud.api.ExternalServicesBridgeFactory) Set(java.util.Set) HashSet(java.util.HashSet) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) Date(java.util.Date) ServiceCalendarDate(org.onebusaway.gtfs.model.ServiceCalendarDate) CalendarService(org.onebusaway.gtfs.services.calendar.CalendarService) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) ExternalServices(org.onebusaway.cloud.api.ExternalServices) Route(org.onebusaway.gtfs.model.Route) HashSet(java.util.HashSet)

Aggregations

ServiceDate (org.onebusaway.gtfs.model.calendar.ServiceDate)134 Test (org.junit.Test)49 Date (java.util.Date)46 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)45 ArrayList (java.util.ArrayList)23 ServiceCalendar (org.onebusaway.gtfs.model.ServiceCalendar)23 Calendar (java.util.Calendar)22 ServiceCalendarDate (org.onebusaway.gtfs.model.ServiceCalendarDate)22 HashSet (java.util.HashSet)17 ServiceIdActivation (org.onebusaway.transit_data_federation.services.transit_graph.ServiceIdActivation)14 CalendarServiceData (org.onebusaway.gtfs.model.calendar.CalendarServiceData)13 CalendarService (org.onebusaway.gtfs.services.calendar.CalendarService)13 Set (java.util.Set)11 Trip (org.onebusaway.gtfs.model.Trip)11 TimeZone (java.util.TimeZone)10 GtfsMutableRelationalDao (org.onebusaway.gtfs.services.GtfsMutableRelationalDao)10 TripUpdate (com.google.transit.realtime.GtfsRealtime.TripUpdate)9 LocalizedServiceId (org.onebusaway.gtfs.model.calendar.LocalizedServiceId)9 Agency (org.onebusaway.gtfs.model.Agency)8 TripDescriptor (com.google.transit.realtime.GtfsRealtime.TripDescriptor)7