Search in sources :

Example 1 with TransformContext

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"));
}
Also used : GtfsMutableRelationalDao(org.onebusaway.gtfs.services.GtfsMutableRelationalDao) TransformContext(org.onebusaway.gtfs_transformer.services.TransformContext) GtfsTransformStrategy(org.onebusaway.gtfs_transformer.services.GtfsTransformStrategy) Test(org.junit.Test)

Example 2 with TransformContext

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());
    }
}
Also used : ExternalServices(org.onebusaway.cloud.api.ExternalServices) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) GtfsMutableRelationalDao(org.onebusaway.gtfs.services.GtfsMutableRelationalDao) java.util(java.util) CsvField(org.onebusaway.csv_entities.schema.annotations.CsvField) ServiceCalendar(org.onebusaway.gtfs.model.ServiceCalendar) Logger(org.slf4j.Logger) Files(java.nio.file.Files) URL(java.net.URL) LoggerFactory(org.slf4j.LoggerFactory) GtfsTransformStrategy(org.onebusaway.gtfs_transformer.services.GtfsTransformStrategy) SimpleDateFormat(java.text.SimpleDateFormat) Trip(org.onebusaway.gtfs.model.Trip) CSVLibrary(org.onebusaway.csv_entities.CSVLibrary) CSVListener(org.onebusaway.csv_entities.CSVListener) TransformContext(org.onebusaway.gtfs_transformer.services.TransformContext) java.io(java.io) Paths(java.nio.file.Paths) ExternalServicesBridgeFactory(org.onebusaway.cloud.api.ExternalServicesBridgeFactory) ServiceCalendarDate(org.onebusaway.gtfs.model.ServiceCalendarDate) CloudContextService(org.onebusaway.gtfs_transformer.services.CloudContextService) ExternalServicesBridgeFactory(org.onebusaway.cloud.api.ExternalServicesBridgeFactory) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) ServiceCalendarDate(org.onebusaway.gtfs.model.ServiceCalendarDate) ExternalServices(org.onebusaway.cloud.api.ExternalServices) SimpleDateFormat(java.text.SimpleDateFormat)

Example 3 with TransformContext

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());
}
Also used : GtfsMutableRelationalDao(org.onebusaway.gtfs.services.GtfsMutableRelationalDao) Trip(org.onebusaway.gtfs.model.Trip) TransformContext(org.onebusaway.gtfs_transformer.services.TransformContext) StopTime(org.onebusaway.gtfs.model.StopTime) Test(org.junit.Test)

Example 4 with TransformContext

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());
}
Also used : GtfsMutableRelationalDao(org.onebusaway.gtfs.services.GtfsMutableRelationalDao) Trip(org.onebusaway.gtfs.model.Trip) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) TransformContext(org.onebusaway.gtfs_transformer.services.TransformContext) GtfsTransformStrategy(org.onebusaway.gtfs_transformer.services.GtfsTransformStrategy) GtfsRelationalDaoImpl(org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl) Test(org.junit.Test)

Example 5 with TransformContext

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());
}
Also used : GtfsMutableRelationalDao(org.onebusaway.gtfs.services.GtfsMutableRelationalDao) Trip(org.onebusaway.gtfs.model.Trip) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) TransformContext(org.onebusaway.gtfs_transformer.services.TransformContext) GtfsTransformStrategy(org.onebusaway.gtfs_transformer.services.GtfsTransformStrategy) GtfsRelationalDaoImpl(org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl) Test(org.junit.Test)

Aggregations

TransformContext (org.onebusaway.gtfs_transformer.services.TransformContext)12 Test (org.junit.Test)11 Trip (org.onebusaway.gtfs.model.Trip)11 GtfsMutableRelationalDao (org.onebusaway.gtfs.services.GtfsMutableRelationalDao)11 GtfsTransformStrategy (org.onebusaway.gtfs_transformer.services.GtfsTransformStrategy)7 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)6 GtfsRelationalDaoImpl (org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl)5 StopTime (org.onebusaway.gtfs.model.StopTime)3 ServiceCalendar (org.onebusaway.gtfs.model.ServiceCalendar)2 ServiceDate (org.onebusaway.gtfs.model.calendar.ServiceDate)2 java.io (java.io)1 URL (java.net.URL)1 Files (java.nio.file.Files)1 Paths (java.nio.file.Paths)1 SimpleDateFormat (java.text.SimpleDateFormat)1 java.util (java.util)1 ExternalServices (org.onebusaway.cloud.api.ExternalServices)1 ExternalServicesBridgeFactory (org.onebusaway.cloud.api.ExternalServicesBridgeFactory)1 CSVLibrary (org.onebusaway.csv_entities.CSVLibrary)1 CSVListener (org.onebusaway.csv_entities.CSVListener)1