use of org.onebusaway.gtfs_transformer.services.TransformContext in project onebusaway-gtfs-modules by OneBusAway.
the class UpdateStopNameFromParentStationIfInvalidStrategyTest method test.
@Test
public void test() throws IOException {
_gtfs.putAgencies(1);
_gtfs.putLines("stops.txt", "stop_id,stop_name,stop_lat,stop_lon,location_type,parent_station,platform_code", "stop0,2,1,1,0,,", "stop1,Station A Platform 1,0,0,0,station0,1", "stop2,2,0,0,0,station0,2", "station0,Station A,0,0,1,,");
_gtfs.putCalendars(1);
_gtfs.putRoutes(1);
_gtfs.putTrips(2, "r0", "sid0");
_gtfs.putStopTimes("t0", "stop0,stop1");
_gtfs.putStopTimes("t0", "stop2,stop0");
GtfsMutableRelationalDao dao = _gtfs.read();
TransformContext tc = new TransformContext();
tc.setDefaultAgencyId("a0");
GtfsTransformStrategy strategy = new UpdateStopNameFromParentStationIfInvalidStrategy();
strategy.run(tc, dao);
UpdateLibrary.clearDaoCache(dao);
assertEquals("2", getStopName(dao, "stop0"));
assertEquals("Station A Platform 1", getStopName(dao, "stop1"));
assertEquals("Station A", getStopName(dao, "stop2"));
assertEquals("Station A", getStopName(dao, "station0"));
}
use of org.onebusaway.gtfs_transformer.services.TransformContext in project onebusaway-gtfs-modules by OneBusAway.
the class AnomalyCheckFutureTripCounts method run.
@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
Collection<Date> datesToIgnore;
SetListener datesToIgnoreListener = new SetListener();
datesToIgnoreListener = (SetListener) readCsvFrom(datesToIgnoreListener, datesToIgnoreUrl, datesToIgnoreFile);
datesToIgnore = datesToIgnoreListener.returnContents();
Collection<Date> holidays;
SetListener holidaysListener = new SetListener();
holidaysListener = (SetListener) readCsvFrom(holidaysListener, holidaysUrl, holidaysFile);
holidays = holidaysListener.returnContents();
Map<String, Double> dayAvgTripsMap = new HashMap<String, Double>();
MapListener mapListener = new MapListener();
mapListener = (MapListener) readCsvFrom(mapListener, dayAvgTripMapUrl, dayAvgTripMapFile);
dayAvgTripsMap = mapListener.returnContents();
SimpleDateFormat dayOfWeekFormat = new SimpleDateFormat("EEEE");
for (Date date : holidays) {
_log.info("holiday: " + date.toString() + " on a " + dayOfWeekFormat.format(date));
}
for (Date date : datesToIgnore) {
_log.info("ignore: " + date.toString());
}
for (String key : dayAvgTripsMap.keySet()) {
_log.info("key: " + key + " value: " + dayAvgTripsMap.get(key));
}
ExternalServices es = new ExternalServicesBridgeFactory().getExternalServices();
String[] days = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday", "Holiday" };
Map<Date, Integer> dateTripMap = getDateTripMap(dao);
int dayCounter = 0;
Map<String, ArrayList<Double>> dayAvgTripsMapUpdate = new HashMap<String, ArrayList<Double>>();
for (String day : days) {
dayAvgTripsMapUpdate.put(day, new ArrayList<Double>());
}
Date dateDay = removeTime(addDays(new Date(), dayCounter));
while (dateTripMap.get(dateDay) != null) {
int tripCount = dateTripMap.get(dateDay);
if (dayAvgTripMapFile == null && dayAvgTripMapUrl == null) {
_log.info("On {} there are {} trips", dateDay.toString(), tripCount);
} else {
if ((tripCount < dayAvgTripsMap.get(dayOfWeekFormat.format(dateDay)) * (1 + percentageMatch / 100)) && (tripCount > dayAvgTripsMap.get(dayOfWeekFormat.format(dateDay)) * (1 - percentageMatch / 100)) && !holidays.contains(dateDay)) {
_log.info(dateDay + " has " + tripCount + " trips, and that's within reasonable expections");
dayAvgTripsMapUpdate.get(dayOfWeekFormat.format(dateDay)).add((double) tripCount);
} else if (holidays.contains(dateDay) & (tripCount < dayAvgTripsMap.get("Holiday") * (1 + percentageMatch / 100) && (tripCount > dayAvgTripsMap.get("Holiday") * (1 - percentageMatch / 100)))) {
_log.info(dateDay + " has " + tripCount + " trips, is a holiday, and that's within reasonable expections");
dayAvgTripsMapUpdate.get("Holiday").add((double) tripCount);
} else if (datesToIgnore.contains(dateDay)) {
_log.info(dateDay + " has " + tripCount + " trips, and we are ignoring this possible anomoly");
} else {
_log.info(dateDay + " has " + tripCount + " trips, this may indicate a problem.");
if (!silentMode) {
es.publishMessage(CloudContextService.getTopic(), dateDay.toString() + " has " + tripCount + " trips, this may indicate a problem.");
}
}
}
dayCounter++;
dateDay = removeTime(addDays(new Date(), dayCounter));
}
String out = "";
for (String stringDay : days) {
double tripsUpcoming;
try {
tripsUpcoming = dayAvgTripsMapUpdate.get(stringDay).stream().mapToDouble(a -> a).average().getAsDouble();
} catch (NoSuchElementException exception) {
tripsUpcoming = dayAvgTripsMap.get(stringDay);
}
double currentAvg = dayAvgTripsMap.get(stringDay);
int aprox_hours_per_month = 28 * 24;
double suggestedAvg = currentAvg + ((tripsUpcoming - currentAvg) / (aprox_hours_per_month));
out += stringDay + "," + suggestedAvg + "\n";
}
_log.info(out);
try {
Files.deleteIfExists(Paths.get(dayAvgTripMapFile));
Files.write(Paths.get(dayAvgTripMapFile), out.getBytes());
} catch (IOException io) {
_log.error(io.getMessage());
}
}
use of org.onebusaway.gtfs_transformer.services.TransformContext in project onebusaway-gtfs-modules by OneBusAway.
the class RemoveNonRevenueStopsStrategyTest method test.
@Test
public void test() throws IOException {
RemoveNonRevenueStopsStrategy _strategy = new RemoveNonRevenueStopsStrategy();
_gtfs.putAgencies(1);
_gtfs.putStops(3);
_gtfs.putRoutes(1);
_gtfs.putCalendars(1, "start_date=20120903", "end_date=20121016", "mask=1111100");
_gtfs.putTrips(1, "r0", "sid0");
_gtfs.putLines("stop_times.txt", "trip_id,stop_id,stop_sequence,arrival_time,departure_time,drop_off_type,pickup_type", "t0,s0,0,01:00:00,01:05:00,1,1", "t0,s1,1,01:30:00,01:30:00,0,0", "t0,s2,2,02:30:00,02:30:00,1,1", "t0,s0,3,03:00:00,03:00:00,0,0", "t0,s2,4,03:30:00,03:30:00,1,1");
GtfsMutableRelationalDao dao = _gtfs.read();
_strategy.run(new TransformContext(), dao);
Trip trip = dao.getTripForId(_gtfs.id("t0"));
assertEquals("sid0", trip.getServiceId().getId());
List<StopTime> stopTimes = dao.getStopTimesForTrip(trip);
assertEquals(2, stopTimes.size());
assertEquals(0, stopTimes.get(1).getPickupType());
assertEquals(0, stopTimes.get(0).getPickupType());
}
use of org.onebusaway.gtfs_transformer.services.TransformContext in project onebusaway-gtfs-modules by OneBusAway.
the class TransformFactoryTest method testReplaceValueInUpdate.
@Test
public void testReplaceValueInUpdate() throws IOException, TransformSpecificationException {
_factory.addModificationsFromString("{'op':'update', " + "'match':{'file':'trips.txt'}, " + "'update':{'trip_headsign': 's/Downtown/Uptown/'}}");
GtfsTransformStrategy transform = _transformer.getLastTransform();
TransformContext context = new TransformContext();
GtfsMutableRelationalDao dao = new GtfsRelationalDaoImpl();
Trip trip = new Trip();
trip.setId(new AgencyAndId("1", "1"));
trip.setTripHeadsign("Downtown Express");
dao.saveEntity(trip);
transform.run(context, dao);
assertEquals("Uptown Express", trip.getTripHeadsign());
}
use of org.onebusaway.gtfs_transformer.services.TransformContext 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());
}
Aggregations