Search in sources :

Example 1 with PropertyPathExpression

use of org.onebusaway.collections.beans.PropertyPathExpression 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 2 with PropertyPathExpression

use of org.onebusaway.collections.beans.PropertyPathExpression in project onebusaway-gtfs-modules by OneBusAway.

the class TransformFactory method createSetterForValue.

private ValueSetter createSetterForValue(Object value) {
    String stringValue = value.toString();
    Matcher pathMatcher = _pathMatcher.matcher(stringValue);
    if (pathMatcher.matches()) {
        PropertyPathExpression expression = new PropertyPathExpression(pathMatcher.group(1));
        return new PropertyPathExpressionValueSetter(_transformer.getReader(), _schemaCache, _transformer.getDao(), expression);
    }
    Matcher replaceMatcher = _replaceMatcher.matcher(stringValue);
    if (replaceMatcher.matches()) {
        return new ReplaceValueSetter(replaceMatcher.group(1), replaceMatcher.group(2));
    }
    return new DeferredValueSetter(_transformer.getReader(), _schemaCache, _transformer.getDao(), value);
}
Also used : PropertyPathExpressionValueSetter(org.onebusaway.gtfs_transformer.deferred.PropertyPathExpressionValueSetter) Matcher(java.util.regex.Matcher) DeferredValueMatcher(org.onebusaway.gtfs_transformer.deferred.DeferredValueMatcher) ReplaceValueSetter(org.onebusaway.gtfs_transformer.deferred.ReplaceValueSetter) DeferredValueSetter(org.onebusaway.gtfs_transformer.deferred.DeferredValueSetter) PropertyPathExpression(org.onebusaway.collections.beans.PropertyPathExpression)

Example 3 with PropertyPathExpression

use of org.onebusaway.collections.beans.PropertyPathExpression in project onebusaway-gtfs-modules by OneBusAway.

the class PropertyPathExpressionValueSetterTest method test.

@Test
public void test() {
    PropertyPathExpression expression = new PropertyPathExpression("route.shortName");
    PropertyPathExpressionValueSetter setter = new PropertyPathExpressionValueSetter(_reader, _schemaCache, _dao, expression);
    Route route = new Route();
    route.setShortName("10");
    Trip trip = new Trip();
    trip.setRoute(route);
    setter.setValue(BeanWrapperFactory.wrap(trip), "tripShortName");
    assertEquals("10", trip.getTripShortName());
}
Also used : Trip(org.onebusaway.gtfs.model.Trip) PropertyPathExpression(org.onebusaway.collections.beans.PropertyPathExpression) Route(org.onebusaway.gtfs.model.Route) Test(org.junit.Test)

Example 4 with PropertyPathExpression

use of org.onebusaway.collections.beans.PropertyPathExpression 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

PropertyPathExpression (org.onebusaway.collections.beans.PropertyPathExpression)4 Matcher (java.util.regex.Matcher)2 Test (org.junit.Test)2 Trip (org.onebusaway.gtfs.model.Trip)2 DeferredValueMatcher (org.onebusaway.gtfs_transformer.deferred.DeferredValueMatcher)2 PropertyValueEntityMatch (org.onebusaway.gtfs_transformer.match.PropertyValueEntityMatch)2 TypedEntityMatch (org.onebusaway.gtfs_transformer.match.TypedEntityMatch)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 JSONObject (org.json.JSONObject)1 PropertyPathCollectionExpression (org.onebusaway.collections.beans.PropertyPathCollectionExpression)1 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)1 Route (org.onebusaway.gtfs.model.Route)1 GtfsMutableRelationalDao (org.onebusaway.gtfs.services.GtfsMutableRelationalDao)1 TransformSpecificationMissingArgumentException (org.onebusaway.gtfs_transformer.TransformSpecificationMissingArgumentException)1 DeferredValueSetter (org.onebusaway.gtfs_transformer.deferred.DeferredValueSetter)1 PropertyPathExpressionValueSetter (org.onebusaway.gtfs_transformer.deferred.PropertyPathExpressionValueSetter)1 ReplaceValueSetter (org.onebusaway.gtfs_transformer.deferred.ReplaceValueSetter)1