Search in sources :

Example 46 with AgencyAndId

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

the class HibernateGtfsRelationalDaoImplTest method testStopsForStation.

@Test
public void testStopsForStation() {
    Stop stationA = new Stop();
    stationA.setId(new AgencyAndId("X", "A"));
    _dao.saveEntity(stationA);
    Stop stationB = new Stop();
    stationB.setId(new AgencyAndId("X", "B"));
    _dao.saveEntity(stationB);
    Stop stopA1 = new Stop();
    stopA1.setId(new AgencyAndId("X", "A1"));
    stopA1.setParentStation("A");
    _dao.saveEntity(stopA1);
    Stop stopA2 = new Stop();
    stopA2.setId(new AgencyAndId("X", "A2"));
    stopA2.setParentStation("A");
    _dao.saveEntity(stopA2);
    Stop stopB1 = new Stop();
    stopB1.setId(new AgencyAndId("X", "B1"));
    stopB1.setParentStation("B");
    _dao.saveEntity(stopB1);
    _dao.flush();
    Stop station2 = _dao.getStopForId(new AgencyAndId("X", "A"));
    List<Stop> stops = _dao.getStopsForStation(station2);
    assertEquals(2, stops.size());
    Set<String> ids = new HashSet<String>();
    ids.add("A1");
    ids.add("A2");
    assertTrue(ids.contains(stops.get(0).getId().getId()));
    assertTrue(ids.contains(stops.get(1).getId().getId()));
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) Stop(org.onebusaway.gtfs.model.Stop) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 47 with AgencyAndId

use of org.onebusaway.gtfs.model.AgencyAndId 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)

Example 48 with AgencyAndId

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

the class CalendarSimplicationLibrary method groupTripKeysByServiceIds.

public Map<Set<AgencyAndId>, List<TripKey>> groupTripKeysByServiceIds(Map<TripKey, List<Trip>> tripsByKey) {
    Map<Set<AgencyAndId>, List<TripKey>> tripKeysByServiceIds = new FactoryMap<Set<AgencyAndId>, List<TripKey>>(new ArrayList<TripKey>());
    for (Map.Entry<TripKey, List<Trip>> entry : tripsByKey.entrySet()) {
        TripKey key = entry.getKey();
        List<Trip> tripsForKey = entry.getValue();
        Set<AgencyAndId> serviceIds = new HashSet<AgencyAndId>();
        for (Trip trip : tripsForKey) {
            serviceIds.add(trip.getServiceId());
        }
        tripKeysByServiceIds.get(serviceIds).add(key);
    }
    return tripKeysByServiceIds;
}
Also used : FactoryMap(org.onebusaway.collections.FactoryMap) Trip(org.onebusaway.gtfs.model.Trip) Set(java.util.Set) HashSet(java.util.HashSet) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) FactoryMap(org.onebusaway.collections.FactoryMap) HashSet(java.util.HashSet)

Example 49 with AgencyAndId

use of org.onebusaway.gtfs.model.AgencyAndId 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 50 with AgencyAndId

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

the class CalendarSimplicationStrategy method computeMergeToolIdMapping.

private Map<AgencyAndId, List<AgencyAndId>> computeMergeToolIdMapping(GtfsDao dao) {
    if (!undoGoogleTransitDataFeedMergeTool)
        return Collections.emptyMap();
    Map<AgencyAndId, List<AgencyAndId>> mergedIdMapping = new FactoryMap<AgencyAndId, List<AgencyAndId>>(new ArrayList<AgencyAndId>());
    Map<AgencyAndId, List<AgencyAndId>> unmergedIdMapping = new FactoryMap<AgencyAndId, List<AgencyAndId>>(new ArrayList<AgencyAndId>());
    for (Trip trip : dao.getAllTrips()) {
        AgencyAndId tripId = trip.getId();
        AgencyAndId unmergedTripId = computeUnmergedTripId(tripId);
        if (unmergedTripId.equals(tripId)) {
            unmergedIdMapping.get(unmergedTripId).add(tripId);
        } else {
            mergedIdMapping.get(unmergedTripId).add(tripId);
        }
    }
    Set<AgencyAndId> intersection = new HashSet<AgencyAndId>(mergedIdMapping.keySet());
    intersection.retainAll(unmergedIdMapping.keySet());
    if (!intersection.isEmpty()) {
        throw new IllegalStateException("some ids appeared both in the merged and unmerged case: " + intersection);
    }
    mergedIdMapping.putAll(unmergedIdMapping);
    return mergedIdMapping;
}
Also used : FactoryMap(org.onebusaway.collections.FactoryMap) Trip(org.onebusaway.gtfs.model.Trip) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet)

Aggregations

AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)102 Test (org.junit.Test)63 Trip (org.onebusaway.gtfs.model.Trip)37 ServiceDate (org.onebusaway.gtfs.model.calendar.ServiceDate)25 Stop (org.onebusaway.gtfs.model.Stop)24 ServiceCalendar (org.onebusaway.gtfs.model.ServiceCalendar)17 ServiceCalendarDate (org.onebusaway.gtfs.model.ServiceCalendarDate)16 ArrayList (java.util.ArrayList)15 Route (org.onebusaway.gtfs.model.Route)15 StopTime (org.onebusaway.gtfs.model.StopTime)15 GtfsMutableRelationalDao (org.onebusaway.gtfs.services.GtfsMutableRelationalDao)15 Agency (org.onebusaway.gtfs.model.Agency)13 ShapePoint (org.onebusaway.gtfs.model.ShapePoint)12 GtfsRelationalDao (org.onebusaway.gtfs.services.GtfsRelationalDao)11 HashSet (java.util.HashSet)10 GtfsRelationalDaoImpl (org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl)10 List (java.util.List)9 Frequency (org.onebusaway.gtfs.model.Frequency)9 CalendarService (org.onebusaway.gtfs.services.calendar.CalendarService)7 Set (java.util.Set)6