Search in sources :

Example 1 with TypedEntityMatch

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

the class RetainEntitiesTransformStrategy method run.

@SuppressWarnings("unchecked")
@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
    if (_retentionMatchesByType.isEmpty())
        return;
    EntityRetentionGraph graph = new EntityRetentionGraph(dao);
    graph.setRetainBlocks(_retainBlocks);
    for (Map.Entry<Class<?>, List<EntityRetention>> entry : _retentionMatchesByType.entrySet()) {
        Class<?> entityType = entry.getKey();
        List<EntityRetention> retentions = entry.getValue();
        if (IdKey.class.isAssignableFrom(entityType)) {
            for (EntityRetention retention : retentions) {
                TypedEntityMatch typedMatch = retention.getMatch();
                IdKeyMatch match = (IdKeyMatch) typedMatch.getPropertyMatches();
                graph.retain(match.getKey(), retention.isRetainUp());
            }
        } else {
            Collection<Object> entities = new ArrayList<Object>(dao.getAllEntitiesForType(entityType));
            for (Object object : entities) {
                for (EntityRetention retention : retentions) {
                    EntityMatch match = retention.getMatch();
                    if (match.isApplicableToObject(object))
                        graph.retain(object, retention.isRetainUp());
                }
            }
        }
    }
    for (Class<?> entityClass : GtfsEntitySchemaFactory.getEntityClasses()) {
        List<Object> objectsToRemove = new ArrayList<Object>();
        for (Object entity : dao.getAllEntitiesForType(entityClass)) {
            if (!graph.isRetained(entity))
                objectsToRemove.add(entity);
        }
        for (Object toRemove : objectsToRemove) dao.removeEntity((IdentityBean<Serializable>) toRemove);
    }
}
Also used : TypedEntityMatch(org.onebusaway.gtfs_transformer.match.TypedEntityMatch) ArrayList(java.util.ArrayList) EntityMatch(org.onebusaway.gtfs_transformer.match.EntityMatch) TypedEntityMatch(org.onebusaway.gtfs_transformer.match.TypedEntityMatch) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) IdKeyMatch(org.onebusaway.gtfs_transformer.collections.IdKeyMatch) IdentityBean(org.onebusaway.gtfs.model.IdentityBean)

Example 2 with TypedEntityMatch

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

the class TransformFactory method getMatch.

private TypedEntityMatch getMatch(String line, JSONObject json) throws JSONException, TransformSpecificationException {
    if (!json.has(ARG_MATCH)) {
        throw new TransformSpecificationMissingArgumentException(line, ARG_MATCH);
    }
    JSONObject match = json.getJSONObject(ARG_MATCH);
    if (match.has(ARG_COLLECTION)) {
        return getCollectionMatch(line, match.getString(ARG_COLLECTION), match);
    }
    Class<?> entityType = getEntityClassFromJsonSpec(line, match);
    if (entityType == null) {
        throw new TransformSpecificationMissingArgumentException(line, new String[] { ARG_FILE, ARG_CLASS }, ARG_MATCH);
    }
    Map<String, DeferredValueMatcher> propertyMatches = getPropertyValueMatchersFromJsonObject(match, _excludeForMatchSpec);
    List<EntityMatch> matches = new ArrayList<EntityMatch>();
    for (Map.Entry<String, DeferredValueMatcher> entry : propertyMatches.entrySet()) {
        String property = entry.getKey();
        Matcher m = _anyMatcher.matcher(property);
        if (m.matches()) {
            PropertyPathCollectionExpression expression = new PropertyPathCollectionExpression(m.group(1));
            expression.setPropertyMethodResolver(_propertyMethodResolver);
            matches.add(new PropertyAnyValueEntityMatch(expression, entry.getValue()));
        } else {
            PropertyPathExpression expression = new PropertyPathExpression(property);
            expression.setPropertyMethodResolver(_propertyMethodResolver);
            matches.add(new PropertyValueEntityMatch(expression, entry.getValue()));
        }
    }
    return new TypedEntityMatch(entityType, new EntityMatchCollection(matches));
}
Also used : TypedEntityMatch(org.onebusaway.gtfs_transformer.match.TypedEntityMatch) EntityMatchCollection(org.onebusaway.gtfs_transformer.match.EntityMatchCollection) Matcher(java.util.regex.Matcher) DeferredValueMatcher(org.onebusaway.gtfs_transformer.deferred.DeferredValueMatcher) ArrayList(java.util.ArrayList) TypedEntityMatch(org.onebusaway.gtfs_transformer.match.TypedEntityMatch) EntityMatch(org.onebusaway.gtfs_transformer.match.EntityMatch) PropertyValueEntityMatch(org.onebusaway.gtfs_transformer.match.PropertyValueEntityMatch) PropertyAnyValueEntityMatch(org.onebusaway.gtfs_transformer.match.PropertyAnyValueEntityMatch) PropertyValueEntityMatch(org.onebusaway.gtfs_transformer.match.PropertyValueEntityMatch) PropertyPathCollectionExpression(org.onebusaway.collections.beans.PropertyPathCollectionExpression) PropertyAnyValueEntityMatch(org.onebusaway.gtfs_transformer.match.PropertyAnyValueEntityMatch) JSONObject(org.json.JSONObject) DeferredValueMatcher(org.onebusaway.gtfs_transformer.deferred.DeferredValueMatcher) TransformSpecificationMissingArgumentException(org.onebusaway.gtfs_transformer.TransformSpecificationMissingArgumentException) PropertyPathExpression(org.onebusaway.collections.beans.PropertyPathExpression) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 with TypedEntityMatch

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

the class EntitiesTransformStrategy method run.

@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
    for (MatchAndTransform modification : _modifications) {
        TypedEntityMatch match = modification.getMatch();
        Class<?> entityType = match.getType();
        EntityTransformStrategy transform = modification.getTransform();
        if (IdKey.class.isAssignableFrom(entityType)) {
            IdKeyMatch keyMatch = (IdKeyMatch) match.getPropertyMatches();
            transform.run(context, dao, keyMatch.getKey());
        } else {
            Collection<Object> entities = new ArrayList<Object>(dao.getAllEntitiesForType(entityType));
            for (Object object : entities) {
                if (match.isApplicableToObject(object)) {
                    transform.run(context, dao, object);
                }
            }
        }
    }
}
Also used : TypedEntityMatch(org.onebusaway.gtfs_transformer.match.TypedEntityMatch) EntityTransformStrategy(org.onebusaway.gtfs_transformer.services.EntityTransformStrategy) ArrayList(java.util.ArrayList) IdKeyMatch(org.onebusaway.gtfs_transformer.collections.IdKeyMatch)

Example 4 with TypedEntityMatch

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

the class TransformFactory method handleRemoveOperation.

private void handleRemoveOperation(String line, JSONObject json) throws JSONException, TransformSpecificationException {
    TypedEntityMatch match = getMatch(line, json);
    EntitiesTransformStrategy strategy = getStrategy(EntitiesTransformStrategy.class);
    RemoveEntityUpdateStrategy mod = new RemoveEntityUpdateStrategy();
    strategy.addModification(match, mod);
}
Also used : TypedEntityMatch(org.onebusaway.gtfs_transformer.match.TypedEntityMatch) RemoveEntityUpdateStrategy(org.onebusaway.gtfs_transformer.impl.RemoveEntityUpdateStrategy)

Example 5 with TypedEntityMatch

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

the class TrimTripTransformStrategyTest method testStopTimeTrimming.

@Test
public void testStopTimeTrimming() throws IOException {
    _gtfs.putAgencies(1);
    _gtfs.putStops(6);
    _gtfs.putRoutes(1);
    _gtfs.putTrips(1, "r0", "sid0");
    _gtfs.putStopTimes("t0", "s0,s1,s2,s3,s4,s5");
    GtfsMutableRelationalDao dao = _gtfs.read();
    TrimOperation operation = new TrimOperation();
    operation.setMatch(new TypedEntityMatch(Trip.class, new AlwaysMatch()));
    operation.setToStopId("s1");
    operation.setFromStopId("s4");
    _strategy.addOperation(operation);
    _strategy.run(_context, dao);
    Collection<Trip> allTrips = dao.getAllTrips();
    assertEquals(1, allTrips.size());
    Trip trip = allTrips.iterator().next();
    assertEquals(new AgencyAndId("a0", "t0-s1-s4"), trip.getId());
    List<StopTime> stopTimes = dao.getStopTimesForTrip(trip);
    assertEquals(2, stopTimes.size());
    assertEquals("s2", stopTimes.get(0).getStop().getId().getId());
    assertEquals("s3", stopTimes.get(1).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) TrimOperation(org.onebusaway.gtfs_transformer.updates.TrimTripTransformStrategy.TrimOperation) AlwaysMatch(org.onebusaway.gtfs_transformer.match.AlwaysMatch) StopTime(org.onebusaway.gtfs.model.StopTime) 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