Search in sources :

Example 6 with TypedEntityMatch

use of org.onebusaway.gtfs_transformer.match.TypedEntityMatch in project onebusaway-gtfs-modules by OneBusAway.

the class TrimTripTransformStrategyTest method testShapeTrimming.

@Test
public void testShapeTrimming() throws IOException {
    _gtfs.putAgencies(1);
    _gtfs.putLines("stops.txt", "stop_id,stop_name,stop_lat,stop_lon", "s0,S0,47.668422,-122.290149", "s1,S1,47.670300,-122.290235", "s2,S2,47.672172,-122.290213", "s3,S3,47.673985,-122.290277", "s4,S4,47.675791,-122.290213", "s5,S5,47.677626,-122.290320");
    _gtfs.putRoutes(1);
    _gtfs.putTrips(1, "r0", "sid0", "shape_id=shape0");
    _gtfs.putStopTimes("t0", "s0,s1,s2,s3,s4,s5");
    _gtfs.putLines("shapes.txt", "shape_id,shape_pt_sequence,shape_pt_lat,shape_pt_lon", "shape0,0,47.668422,-122.290149", "shape0,1,47.670300,-122.290235", "shape0,2,47.672172,-122.290213", "shape0,3,47.673985,-122.290277", "shape0,4,47.675791,-122.290213", "shape0,5,47.677626,-122.290320");
    GtfsMutableRelationalDao dao = _gtfs.read();
    TrimOperation operation = new TrimOperation();
    operation.setMatch(new TypedEntityMatch(Trip.class, new AlwaysMatch()));
    operation.setToStopId("s0");
    operation.setFromStopId("s4");
    _strategy.addOperation(operation);
    _strategy.run(_context, dao);
    Trip trip = dao.getTripForId(new AgencyAndId("a0", "t0-s0-s4"));
    assertEquals(new AgencyAndId("a0", "shape0-s1-s3"), trip.getShapeId());
    List<ShapePoint> shapePoints = dao.getShapePointsForShapeId(trip.getShapeId());
    assertEquals(3, shapePoints.size());
    assertEquals(47.670300, shapePoints.get(0).getLat(), 1e-6);
    assertEquals(47.673985, shapePoints.get(2).getLat(), 1e-6);
}
Also used : GtfsMutableRelationalDao(org.onebusaway.gtfs.services.GtfsMutableRelationalDao) TypedEntityMatch(org.onebusaway.gtfs_transformer.match.TypedEntityMatch) Trip(org.onebusaway.gtfs.model.Trip) ShapePoint(org.onebusaway.gtfs.model.ShapePoint) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) TrimOperation(org.onebusaway.gtfs_transformer.updates.TrimTripTransformStrategy.TrimOperation) AlwaysMatch(org.onebusaway.gtfs_transformer.match.AlwaysMatch) Test(org.junit.Test)

Example 7 with TypedEntityMatch

use of org.onebusaway.gtfs_transformer.match.TypedEntityMatch in project onebusaway-gtfs-modules by OneBusAway.

the class TransformFactory method handleTrimOperation.

private void handleTrimOperation(String line, JSONObject json) throws JSONException, TransformSpecificationException {
    TypedEntityMatch match = getMatch(line, json);
    if (match.getType() != Trip.class) {
        throw new TransformSpecificationException("the trim_trip op only supports matching against trips", line);
    }
    TrimTripTransformStrategy strategy = getStrategy(TrimTripTransformStrategy.class);
    TrimOperation operation = new TrimTripTransformStrategy.TrimOperation();
    operation.setMatch(match);
    if (json.has("to_stop_id")) {
        operation.setToStopId(json.getString("to_stop_id"));
    }
    if (json.has("from_stop_id")) {
        operation.setFromStopId(json.getString("from_stop_id"));
    }
    if (operation.getToStopId() == null && operation.getFromStopId() == null) {
        throw new TransformSpecificationMissingArgumentException(line, new String[] { "to_stop_id", "from_stop_id" });
    }
    strategy.addOperation(operation);
}
Also used : TypedEntityMatch(org.onebusaway.gtfs_transformer.match.TypedEntityMatch) TrimTripTransformStrategy(org.onebusaway.gtfs_transformer.updates.TrimTripTransformStrategy) TransformSpecificationMissingArgumentException(org.onebusaway.gtfs_transformer.TransformSpecificationMissingArgumentException) TrimOperation(org.onebusaway.gtfs_transformer.updates.TrimTripTransformStrategy.TrimOperation) TransformSpecificationException(org.onebusaway.gtfs_transformer.TransformSpecificationException)

Example 8 with TypedEntityMatch

use of org.onebusaway.gtfs_transformer.match.TypedEntityMatch in project onebusaway-gtfs-modules by OneBusAway.

the class TransformFactory method handleUpdateOperation.

private void handleUpdateOperation(String line, JSONObject json) throws JSONException, TransformSpecificationException {
    EntitiesTransformStrategy strategy = getStrategy(EntitiesTransformStrategy.class);
    TypedEntityMatch match = getMatch(line, json);
    if (json.has("factory")) {
        String factoryType = json.getString("factory");
        try {
            Class<?> clazz = Class.forName(factoryType);
            Object factoryObj = clazz.newInstance();
            if (!(factoryObj instanceof EntityTransformStrategy)) {
                throw new TransformSpecificationException("factory object is not an instance of EntityTransformStrategy: " + clazz.getName(), line);
            }
            strategy.addModification(match, (EntityTransformStrategy) factoryObj);
        } catch (Throwable ex) {
            throw new TransformSpecificationException("error creating factory ModificationStrategy instance", ex, line);
        }
        return;
    }
    if (json.has(ARG_UPDATE)) {
        JSONObject update = json.getJSONObject(ARG_UPDATE);
        EntityTransformStrategy mod = getUpdateEntityTransformStrategy(line, match, update);
        strategy.addModification(match, mod);
    }
    if (json.has("strings")) {
        JSONObject strings = json.getJSONObject("strings");
        Map<String, Pair<String>> replacements = getEntityPropertiesAndStringReplacementsFromJsonObject(match.getType(), strings);
        StringModificationStrategy mod = new StringModificationStrategy(replacements);
        strategy.addModification(match, mod);
    }
}
Also used : TypedEntityMatch(org.onebusaway.gtfs_transformer.match.TypedEntityMatch) EntityTransformStrategy(org.onebusaway.gtfs_transformer.services.EntityTransformStrategy) GtfsEntityTransformStrategy(org.onebusaway.gtfs_transformer.services.GtfsEntityTransformStrategy) JSONObject(org.json.JSONObject) StringModificationStrategy(org.onebusaway.gtfs_transformer.impl.StringModificationStrategy) JSONObject(org.json.JSONObject) TransformSpecificationException(org.onebusaway.gtfs_transformer.TransformSpecificationException) Pair(org.onebusaway.collections.tuple.Pair)

Example 9 with TypedEntityMatch

use of org.onebusaway.gtfs_transformer.match.TypedEntityMatch in project onebusaway-gtfs-modules by OneBusAway.

the class TransformFactory method handleRetainOperation.

private void handleRetainOperation(String line, JSONObject json) throws JSONException, TransformSpecificationException {
    RetainEntitiesTransformStrategy strategy = getStrategy(RetainEntitiesTransformStrategy.class);
    TypedEntityMatch match = getMatch(line, json);
    boolean retainUp = true;
    if (json.has("retainUp"))
        retainUp = json.getBoolean("retainUp");
    strategy.addRetention(match, retainUp);
    if (json.has("retainBlocks")) {
        boolean retainBlocks = json.getBoolean("retainBlocks");
        strategy.setRetainBlocks(retainBlocks);
    }
}
Also used : TypedEntityMatch(org.onebusaway.gtfs_transformer.match.TypedEntityMatch)

Example 10 with TypedEntityMatch

use of org.onebusaway.gtfs_transformer.match.TypedEntityMatch in project onebusaway-gtfs-modules by OneBusAway.

the class TrimTripTransformStrategyTest method testMatching.

@Test
public void testMatching() throws IOException {
    _gtfs.putAgencies(1);
    _gtfs.putStops(6);
    _gtfs.putRoutes(2);
    _gtfs.putTrips(2, "r0,r1", "sid0");
    _gtfs.putStopTimes("t0,t1", "s0,s1,s2,s3,s4,s5");
    GtfsMutableRelationalDao dao = _gtfs.read();
    TrimOperation operation = new TrimOperation();
    operation.setMatch(new TypedEntityMatch(Trip.class, new PropertyValueEntityMatch(new PropertyPathExpression("route.id.id"), new SimpleValueMatcher("r1"))));
    operation.setFromStopId("s4");
    _strategy.addOperation(operation);
    _strategy.run(_context, dao);
    {
        Trip trip = dao.getTripForId(new AgencyAndId("a0", "t0"));
        List<StopTime> stopTimes = dao.getStopTimesForTrip(trip);
        assertEquals(6, stopTimes.size());
    }
    {
        Trip trip = dao.getTripForId(new AgencyAndId("a0", "t1-s4"));
        List<StopTime> stopTimes = dao.getStopTimesForTrip(trip);
        assertEquals(4, stopTimes.size());
        assertEquals("s0", stopTimes.get(0).getStop().getId().getId());
        assertEquals("s3", stopTimes.get(3).getStop().getId().getId());
    }
}
Also used : GtfsMutableRelationalDao(org.onebusaway.gtfs.services.GtfsMutableRelationalDao) TypedEntityMatch(org.onebusaway.gtfs_transformer.match.TypedEntityMatch) Trip(org.onebusaway.gtfs.model.Trip) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) SimpleValueMatcher(org.onebusaway.gtfs_transformer.match.SimpleValueMatcher) PropertyPathExpression(org.onebusaway.collections.beans.PropertyPathExpression) List(java.util.List) PropertyValueEntityMatch(org.onebusaway.gtfs_transformer.match.PropertyValueEntityMatch) TrimOperation(org.onebusaway.gtfs_transformer.updates.TrimTripTransformStrategy.TrimOperation) Test(org.junit.Test)

Aggregations

TypedEntityMatch (org.onebusaway.gtfs_transformer.match.TypedEntityMatch)10 TrimOperation (org.onebusaway.gtfs_transformer.updates.TrimTripTransformStrategy.TrimOperation)4 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)3 Trip (org.onebusaway.gtfs.model.Trip)3 GtfsMutableRelationalDao (org.onebusaway.gtfs.services.GtfsMutableRelationalDao)3 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 JSONObject (org.json.JSONObject)2 PropertyPathExpression (org.onebusaway.collections.beans.PropertyPathExpression)2 TransformSpecificationException (org.onebusaway.gtfs_transformer.TransformSpecificationException)2 TransformSpecificationMissingArgumentException (org.onebusaway.gtfs_transformer.TransformSpecificationMissingArgumentException)2 IdKeyMatch (org.onebusaway.gtfs_transformer.collections.IdKeyMatch)2 AlwaysMatch (org.onebusaway.gtfs_transformer.match.AlwaysMatch)2 EntityMatch (org.onebusaway.gtfs_transformer.match.EntityMatch)2 PropertyValueEntityMatch (org.onebusaway.gtfs_transformer.match.PropertyValueEntityMatch)2 EntityTransformStrategy (org.onebusaway.gtfs_transformer.services.EntityTransformStrategy)2 Matcher (java.util.regex.Matcher)1