Search in sources :

Example 1 with FactoryMap

use of org.onebusaway.collections.FactoryMap 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);
}
Also used : FactoryMap(org.onebusaway.collections.FactoryMap) ServiceCalendarDate(org.onebusaway.gtfs.model.ServiceCalendarDate) Trip(org.onebusaway.gtfs.model.Trip) Set(java.util.Set) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) HashMap(java.util.HashMap) CalendarService(org.onebusaway.gtfs.services.calendar.CalendarService) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) ArrayList(java.util.ArrayList) List(java.util.List) ServiceCalendar(org.onebusaway.gtfs.model.ServiceCalendar)

Example 2 with FactoryMap

use of org.onebusaway.collections.FactoryMap in project onebusaway-gtfs-modules by OneBusAway.

the class DeduplicateTripsStrategy method run.

@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
    Map<String, List<Trip>> tripsByCommonId = new FactoryMap<String, List<Trip>>(new ArrayList<Trip>());
    int total = 0;
    int badIds = 0;
    for (Trip trip : dao.getAllTrips()) {
        AgencyAndId aid = trip.getId();
        String id = aid.getId();
        int index = id.indexOf('_');
        if (index != -1) {
            String commonId = id.substring(index + 1);
            tripsByCommonId.get(commonId).add(trip);
        } else {
            badIds++;
        }
    }
    _log.info("trips=" + total + " badIds=" + badIds);
    int weird = 0;
    int pairs = 0;
    int patternMismatch = 0;
    int propertyMismatch = 0;
    for (List<Trip> trips : tripsByCommonId.values()) {
        if (trips.size() == 1)
            continue;
        if (trips.size() != 2) {
            System.out.println("weird: " + trips);
            weird++;
            continue;
        }
        pairs++;
        Collections.sort(trips, _tripComparator);
        Trip tripA = trips.get(0);
        Trip tripB = trips.get(1);
        List<StopTime> stopTimesA = dao.getStopTimesForTrip(tripA);
        List<StopTime> stopTimesB = dao.getStopTimesForTrip(tripB);
        StopSequencePattern patternA = StopSequencePattern.getPatternForStopTimes(stopTimesA);
        StopSequencePattern patternB = StopSequencePattern.getPatternForStopTimes(stopTimesB);
        if (!patternA.equals(patternB)) {
            System.out.println("  pattern: " + tripA.getId() + " " + tripB.getId());
            patternMismatch++;
            continue;
        }
        String property = areTripsEquivalent(tripA, tripB);
        if (property != null) {
            System.out.println("  property: " + tripA.getId() + " " + tripB.getId() + " " + property);
            propertyMismatch++;
            continue;
        }
    }
    _log.info("weird=" + weird + " pairs=" + pairs + " patternMismatch=" + patternMismatch + " propertyMismatch=" + propertyMismatch);
}
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) StopTime(org.onebusaway.gtfs.model.StopTime)

Example 3 with FactoryMap

use of org.onebusaway.collections.FactoryMap 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 4 with FactoryMap

use of org.onebusaway.collections.FactoryMap 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)

Example 5 with FactoryMap

use of org.onebusaway.collections.FactoryMap in project onebusaway-gtfs-modules by OneBusAway.

the class TripKey method groupTripsForRouteByKey.

public static Map<TripKey, List<Trip>> groupTripsForRouteByKey(GtfsMutableRelationalDao dao, Route route) {
    List<Trip> trips = dao.getTripsForRoute(route);
    Map<TripKey, List<Trip>> tripsByKey = new FactoryMap<TripKey, List<Trip>>(new ArrayList<Trip>());
    for (Trip trip : trips) {
        TripKey key = getTripKeyForTrip(dao, trip);
        tripsByKey.get(key).add(trip);
    }
    return tripsByKey;
}
Also used : FactoryMap(org.onebusaway.collections.FactoryMap) Trip(org.onebusaway.gtfs.model.Trip) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

ArrayList (java.util.ArrayList)6 List (java.util.List)6 FactoryMap (org.onebusaway.collections.FactoryMap)6 Trip (org.onebusaway.gtfs.model.Trip)6 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)4 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 StopTime (org.onebusaway.gtfs.model.StopTime)2 Map (java.util.Map)1 ServiceCalendar (org.onebusaway.gtfs.model.ServiceCalendar)1 ServiceCalendarDate (org.onebusaway.gtfs.model.ServiceCalendarDate)1 ServiceDate (org.onebusaway.gtfs.model.calendar.ServiceDate)1 CalendarService (org.onebusaway.gtfs.services.calendar.CalendarService)1