Search in sources :

Example 6 with GtfsRelationalDaoImpl

use of org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl in project onebusaway-gtfs-modules by OneBusAway.

the class SubsectionTripTransformStrategy method run.

@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
    List<Trip> tripsToAdd = new ArrayList<Trip>();
    List<StopTime> stopTimesToAdd = new ArrayList<StopTime>();
    List<Trip> tripsToRemove = new ArrayList<Trip>();
    List<StopTime> stopTimesToRemove = new ArrayList<StopTime>();
    Set<AgencyAndId> newShapeIds = new HashSet<AgencyAndId>();
    for (Trip trip : dao.getAllTrips()) {
        String routeId = trip.getRoute().getId().getId();
        List<SubsectionOperation> operations = _operationsByRouteId.get(routeId);
        if (operations == null) {
            continue;
        }
        List<StopTime> stopTimes = dao.getStopTimesForTrip(trip);
        Map<String, Integer> stopToIndex = getStopIndices(stopTimes);
        boolean removeOriginalTrip = false;
        for (SubsectionOperation operation : operations) {
            Integer fromIndex = stopToIndex.get(operation.getFromStopId());
            Integer toIndex = stopToIndex.get(operation.getToStopId());
            if (fromIndex == null && toIndex == null) {
                if (operation.removeUnmatchedTrips) {
                    removeOriginalTrip = true;
                }
                continue;
            }
            removeOriginalTrip = true;
            Trip newTrip = new Trip(trip);
            String id = newTrip.getId().getId();
            if (fromIndex != null) {
                id += "-" + operation.getFromStopId();
            }
            if (toIndex != null) {
                id += "-" + operation.getToStopId();
            }
            if (fromIndex == null) {
                fromIndex = 0;
            } else if (!operation.isIncludeFromStop()) {
                fromIndex++;
            }
            if (toIndex == null) {
                toIndex = stopTimes.size() - 1;
            } else if (!operation.isIncludeToStop()) {
                toIndex--;
            }
            newTrip.setId(new AgencyAndId("1", id));
            tripsToAdd.add(newTrip);
            List<StopTime> newStopTimes = new ArrayList<StopTime>();
            for (int i = fromIndex; i <= toIndex; ++i) {
                StopTime stopTime = new StopTime(stopTimes.get(i));
                stopTime.setId(0);
                stopTime.setTrip(newTrip);
                newStopTimes.add(stopTime);
            }
            updateShape(dao, newTrip, newStopTimes, newShapeIds);
            stopTimesToAdd.addAll(newStopTimes);
        }
        if (removeOriginalTrip) {
            tripsToRemove.add(trip);
            stopTimesToRemove.addAll(stopTimes);
        }
    }
    for (StopTime stopTime : stopTimesToRemove) {
        dao.removeEntity(stopTime);
    }
    for (Trip trip : tripsToRemove) {
        dao.removeEntity(trip);
    }
    for (Trip trip : tripsToAdd) {
        dao.saveEntity(trip);
    }
    for (StopTime stopTime : stopTimesToAdd) {
        dao.saveEntity(stopTime);
    }
    ((GtfsRelationalDaoImpl) dao).clearAllCaches();
    Set<AgencyAndId> shapeIds = new HashSet<AgencyAndId>(dao.getAllShapeIds());
    for (Trip trip : dao.getAllTrips()) {
        shapeIds.remove(trip.getShapeId());
    }
    for (AgencyAndId shapeId : shapeIds) {
        for (ShapePoint point : dao.getShapePointsForShapeId(shapeId)) {
            dao.removeEntity(point);
        }
    }
}
Also used : Trip(org.onebusaway.gtfs.model.Trip) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) ArrayList(java.util.ArrayList) ShapePoint(org.onebusaway.gtfs.model.ShapePoint) ShapePoint(org.onebusaway.gtfs.model.ShapePoint) GtfsRelationalDaoImpl(org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl) StopTime(org.onebusaway.gtfs.model.StopTime) HashSet(java.util.HashSet)

Example 7 with GtfsRelationalDaoImpl

use of org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl in project onebusaway-gtfs-modules by OneBusAway.

the class EntityRetentionGraphTest method setup.

@Before
public void setup() throws IOException, URISyntaxException {
    _dao = new GtfsRelationalDaoImpl();
    _graph = new EntityRetentionGraph(_dao);
    GtfsReader reader = new GtfsReader();
    File path = new File(getClass().getResource("/org/onebusaway/gtfs_transformer/testagency").toURI());
    reader.setInputLocation(path);
    reader.setEntityStore(_dao);
    reader.run();
}
Also used : EntityRetentionGraph(org.onebusaway.gtfs_transformer.factory.EntityRetentionGraph) GtfsReader(org.onebusaway.gtfs.serialization.GtfsReader) GtfsRelationalDaoImpl(org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl) File(java.io.File) Before(org.junit.Before)

Example 8 with GtfsRelationalDaoImpl

use of org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl in project onebusaway-gtfs-modules by OneBusAway.

the class AbstractTestSupport method transform.

protected GtfsRelationalDao transform() {
    try {
        _transformer.run();
        GtfsReader reader = new GtfsReader();
        reader.setInputLocation(_outputPath);
        GtfsRelationalDaoImpl dao = new GtfsRelationalDaoImpl();
        reader.setEntityStore(dao);
        reader.run();
        return dao;
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
}
Also used : GtfsReader(org.onebusaway.gtfs.serialization.GtfsReader) GtfsRelationalDaoImpl(org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl) IOException(java.io.IOException)

Example 9 with GtfsRelationalDaoImpl

use of org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl 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 10 with GtfsRelationalDaoImpl

use of org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl 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

GtfsRelationalDaoImpl (org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl)34 Test (org.junit.Test)19 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)13 Agency (org.onebusaway.gtfs.model.Agency)10 Trip (org.onebusaway.gtfs.model.Trip)9 GtfsReader (org.onebusaway.gtfs.serialization.GtfsReader)8 GtfsMutableRelationalDao (org.onebusaway.gtfs.services.GtfsMutableRelationalDao)7 CalendarServiceData (org.onebusaway.gtfs.model.calendar.CalendarServiceData)6 File (java.io.File)5 ServiceDate (org.onebusaway.gtfs.model.calendar.ServiceDate)5 GtfsTransformStrategy (org.onebusaway.gtfs_transformer.services.GtfsTransformStrategy)5 TransformContext (org.onebusaway.gtfs_transformer.services.TransformContext)5 ArrayList (java.util.ArrayList)4 Date (java.util.Date)4 Before (org.junit.Before)4 Route (org.onebusaway.gtfs.model.Route)3 ServiceCalendar (org.onebusaway.gtfs.model.ServiceCalendar)3 ServiceCalendarDate (org.onebusaway.gtfs.model.ServiceCalendarDate)3 Stop (org.onebusaway.gtfs.model.Stop)3 IOException (java.io.IOException)2