use of org.onebusaway.gtfs.model.Trip in project onebusaway-gtfs-modules by OneBusAway.
the class TransformFactoryTest method testReplaceIdInUpdateComplex.
@Test
public void testReplaceIdInUpdateComplex() throws IOException, TransformSpecificationException {
_factory.addModificationsFromString("{'op':'update', " + "'match':{'file':'trips.txt'}, " + "'update':{'trip_id': 's/^([^_]*)_([0-9]*).*/$2/'}}");
GtfsTransformStrategy transform = _transformer.getLastTransform();
TransformContext context = new TransformContext();
context.setDefaultAgencyId("2");
GtfsMutableRelationalDao dao = new GtfsRelationalDaoImpl();
Trip trip = new Trip();
trip.setId(new AgencyAndId("2", "1234-this-text-to-remove"));
dao.saveEntity(trip);
transform.run(context, dao);
assertEquals("1234", trip.getId().getId());
}
use of org.onebusaway.gtfs.model.Trip in project onebusaway-gtfs-modules by OneBusAway.
the class TransformFactoryTest method testPathInUpdate.
@Test
public void testPathInUpdate() throws IOException, TransformSpecificationException {
_factory.addModificationsFromString("{'op':'update', " + "'match':{'file':'trips.txt'}, " + "'update':{'trip_headsign': 'path(route.longName)'}}");
GtfsTransformStrategy transform = _transformer.getLastTransform();
TransformContext context = new TransformContext();
GtfsMutableRelationalDao dao = new GtfsRelationalDaoImpl();
Route route = new Route();
route.setLongName("long cat");
Trip trip = new Trip();
trip.setId(new AgencyAndId("1", "1"));
trip.setRoute(route);
dao.saveEntity(trip);
transform.run(context, dao);
assertEquals("long cat", trip.getTripHeadsign());
}
use of org.onebusaway.gtfs.model.Trip 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.gtfs.model.Trip 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.gtfs.model.Trip in project onebusaway-gtfs-modules by OneBusAway.
the class DeduplicateRoutesStrategy method run.
@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
Map<AgencyAndId, List<Route>> routesById = new FactoryMap<AgencyAndId, List<Route>>(new ArrayList<Route>());
for (Route route : dao.getAllRoutes()) {
AgencyAndId aid = route.getId();
String id = aid.getId();
int index = id.indexOf('_');
if (index == -1)
continue;
String commonId = id.substring(index + 1);
AgencyAndId commonIdFull = new AgencyAndId(aid.getAgencyId(), commonId);
routesById.get(commonIdFull).add(route);
}
for (Map.Entry<AgencyAndId, List<Route>> entry : routesById.entrySet()) {
AgencyAndId routeId = entry.getKey();
List<Route> routes = entry.getValue();
if (routes.size() == 1)
continue;
// Remove the route with the old id
Route route = routes.get(0);
dao.removeEntity(route);
// Add the route with the new id
route.setId(routeId);
dao.saveEntity(route);
for (int i = 1; i < routes.size(); i++) {
Route duplicateRoute = routes.get(i);
dao.removeEntity(duplicateRoute);
List<Trip> trips = dao.getTripsForRoute(duplicateRoute);
for (Trip trip : trips) trip.setRoute(route);
}
}
UpdateLibrary.clearDaoCache(dao);
}
Aggregations