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