Search in sources :

Example 1 with EntityMatch

use of org.onebusaway.gtfs_transformer.match.EntityMatch 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 EntityMatch

use of org.onebusaway.gtfs_transformer.match.EntityMatch 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 EntityMatch

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

the class TransformFactoryTest method test.

@Test
public void test() throws IOException, TransformSpecificationException {
    _factory.addModificationsFromString("{'op':'remove', 'match':{'class':'Route', 'shortName':'10'}}");
    GtfsTransformStrategy transform = _transformer.getLastTransform();
    assertEquals(EntitiesTransformStrategy.class, transform.getClass());
    EntitiesTransformStrategy strategy = (EntitiesTransformStrategy) transform;
    List<MatchAndTransform> transforms = strategy.getModifications();
    assertEquals(1, transforms.size());
    MatchAndTransform pair = transforms.get(0);
    EntityMatch match = pair.getMatch();
    Route route = new Route();
    assertFalse(match.isApplicableToObject(route));
    route.setShortName("10");
    assertTrue(match.isApplicableToObject(route));
    EntityTransformStrategy entityTransform = pair.getTransform();
    assertEquals(RemoveEntityUpdateStrategy.class, entityTransform.getClass());
}
Also used : EntityTransformStrategy(org.onebusaway.gtfs_transformer.services.EntityTransformStrategy) MatchAndTransform(org.onebusaway.gtfs_transformer.factory.EntitiesTransformStrategy.MatchAndTransform) GtfsTransformStrategy(org.onebusaway.gtfs_transformer.services.GtfsTransformStrategy) EntityMatch(org.onebusaway.gtfs_transformer.match.EntityMatch) Route(org.onebusaway.gtfs.model.Route) Test(org.junit.Test)

Aggregations

EntityMatch (org.onebusaway.gtfs_transformer.match.EntityMatch)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 TypedEntityMatch (org.onebusaway.gtfs_transformer.match.TypedEntityMatch)2 List (java.util.List)1 Matcher (java.util.regex.Matcher)1 JSONObject (org.json.JSONObject)1 Test (org.junit.Test)1 PropertyPathCollectionExpression (org.onebusaway.collections.beans.PropertyPathCollectionExpression)1 PropertyPathExpression (org.onebusaway.collections.beans.PropertyPathExpression)1 IdentityBean (org.onebusaway.gtfs.model.IdentityBean)1 Route (org.onebusaway.gtfs.model.Route)1 TransformSpecificationMissingArgumentException (org.onebusaway.gtfs_transformer.TransformSpecificationMissingArgumentException)1 IdKeyMatch (org.onebusaway.gtfs_transformer.collections.IdKeyMatch)1 DeferredValueMatcher (org.onebusaway.gtfs_transformer.deferred.DeferredValueMatcher)1 MatchAndTransform (org.onebusaway.gtfs_transformer.factory.EntitiesTransformStrategy.MatchAndTransform)1 EntityMatchCollection (org.onebusaway.gtfs_transformer.match.EntityMatchCollection)1 PropertyAnyValueEntityMatch (org.onebusaway.gtfs_transformer.match.PropertyAnyValueEntityMatch)1 PropertyValueEntityMatch (org.onebusaway.gtfs_transformer.match.PropertyValueEntityMatch)1