Search in sources :

Example 6 with AgencyAndId

use of org.onebusaway.gtfs.model.AgencyAndId in project onebusaway-gtfs-modules by OneBusAway.

the class DeferredValueConverter method convertValue.

/**
   * Converts the specified value as appropriate such that the resulting value
   * can be assigned to the specified property of the specified bean.
   */
public Object convertValue(BeanWrapper targetBean, String targetPropertyName, Object value) {
    if (value == null) {
        return null;
    }
    Class<?> expectedValueType = targetBean.getPropertyType(targetPropertyName);
    Class<?> actualValueType = value.getClass();
    /**
     * When we introspect the "id" property of an IdentityBean instance, the
     * return type is always Serializable, when the actual type is String or
     * AgencyAndId. This causes trouble with the "isAssignableFrom" check below,
     * so we do a first check here.
     */
    Object parentObject = targetBean.getWrappedInstance(Object.class);
    if (parentObject instanceof IdentityBean && targetPropertyName.equals("id")) {
        Class<?> idType = getIdentityBeanIdType(parentObject);
        if (idType == AgencyAndId.class && actualValueType == String.class) {
            return _support.resolveAgencyAndId(targetBean, targetPropertyName, (String) value);
        }
    }
    if (expectedValueType.isAssignableFrom(actualValueType)) {
        return value;
    }
    if (isPrimitiveAssignable(expectedValueType, actualValueType)) {
        return value;
    }
    if (actualValueType == String.class) {
        String stringValue = (String) value;
        if (AgencyAndId.class.isAssignableFrom(expectedValueType)) {
            return _support.resolveAgencyAndId(targetBean, targetPropertyName, stringValue);
        }
        if (IdentityBean.class.isAssignableFrom(expectedValueType)) {
            Serializable id = stringValue;
            if (getIdType(expectedValueType) == AgencyAndId.class) {
                GtfsReaderContext context = _support.getReader().getGtfsReaderContext();
                String agencyId = context.getAgencyForEntity(expectedValueType, stringValue);
                id = new AgencyAndId(agencyId, stringValue);
            }
            Object entity = _dao.getEntityForId(expectedValueType, id);
            if (entity == null) {
                throw new IllegalStateException("entity not found: type=" + expectedValueType.getName() + " id=" + id);
            }
            return entity;
        }
        Class<?> parentEntityType = parentObject.getClass();
        Converter converter = _support.resolveConverter(parentEntityType, targetPropertyName, expectedValueType);
        if (converter != null) {
            return converter.convert(expectedValueType, value);
        }
    }
    throw new IllegalStateException("no conversion possible from type \"" + actualValueType.getName() + "\" to type \"" + expectedValueType.getName() + "\"");
}
Also used : Serializable(java.io.Serializable) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) Converter(org.apache.commons.beanutils.Converter) GtfsReaderContext(org.onebusaway.gtfs.serialization.GtfsReaderContext) IdentityBean(org.onebusaway.gtfs.model.IdentityBean)

Example 7 with AgencyAndId

use of org.onebusaway.gtfs.model.AgencyAndId in project onebusaway-gtfs-modules by OneBusAway.

the class CalendarSimplicationStrategy method getServiceDatesForServiceIds.

private Set<ServiceDate> getServiceDatesForServiceIds(CalendarService calendarService, Set<AgencyAndId> serviceIds) {
    Set<ServiceDate> allServiceDates = new HashSet<ServiceDate>();
    for (AgencyAndId serviceId : serviceIds) {
        Set<ServiceDate> serviceDates = calendarService.getServiceDatesForServiceId(serviceId);
        allServiceDates.addAll(serviceDates);
    }
    return allServiceDates;
}
Also used : ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) HashSet(java.util.HashSet)

Example 8 with AgencyAndId

use of org.onebusaway.gtfs.model.AgencyAndId in project onebusaway-gtfs-modules by OneBusAway.

the class DeduplicateServiceIdsStrategy method run.

@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
    CalendarService service = CalendarServiceDataFactoryImpl.createService(dao);
    Map<Set<ServiceDate>, List<AgencyAndId>> serviceIdsByServiceDates = new FactoryMap<Set<ServiceDate>, List<AgencyAndId>>(new ArrayList<AgencyAndId>());
    for (AgencyAndId serviceId : dao.getAllServiceIds()) {
        Set<ServiceDate> serviceDates = service.getServiceDatesForServiceId(serviceId);
        serviceIdsByServiceDates.get(serviceDates).add(serviceId);
    }
    Map<AgencyAndId, AgencyAndId> serviceIdMapping = new HashMap<AgencyAndId, AgencyAndId>();
    for (List<AgencyAndId> serviceIds : serviceIdsByServiceDates.values()) {
        Collections.sort(serviceIds);
        if (serviceIds.size() == 1) {
            continue;
        }
        AgencyAndId target = serviceIds.get(0);
        for (int i = 1; i < serviceIds.size(); ++i) {
            AgencyAndId source = serviceIds.get(i);
            serviceIdMapping.put(source, target);
        }
    }
    for (Trip trip : dao.getAllTrips()) {
        AgencyAndId mappedServiceId = serviceIdMapping.get(trip.getServiceId());
        if (mappedServiceId != null) {
            trip.setServiceId(mappedServiceId);
        }
    }
    for (AgencyAndId serviceId : serviceIdMapping.keySet()) {
        ServiceCalendar serviceCalendar = dao.getCalendarForServiceId(serviceId);
        if (serviceCalendar != null) {
            dao.removeEntity(serviceCalendar);
        }
        for (ServiceCalendarDate date : dao.getCalendarDatesForServiceId(serviceId)) {
            dao.removeEntity(date);
        }
    }
    _log.info("removed {} duplicate service ids", serviceIdMapping.size());
    UpdateLibrary.clearDaoCache(dao);
}
Also used : FactoryMap(org.onebusaway.collections.FactoryMap) ServiceCalendarDate(org.onebusaway.gtfs.model.ServiceCalendarDate) Trip(org.onebusaway.gtfs.model.Trip) Set(java.util.Set) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) HashMap(java.util.HashMap) CalendarService(org.onebusaway.gtfs.services.calendar.CalendarService) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) ArrayList(java.util.ArrayList) List(java.util.List) ServiceCalendar(org.onebusaway.gtfs.model.ServiceCalendar)

Example 9 with AgencyAndId

use of org.onebusaway.gtfs.model.AgencyAndId in project onebusaway-gtfs-modules by OneBusAway.

the class DeduplicateTripsStrategy method run.

@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
    Map<String, List<Trip>> tripsByCommonId = new FactoryMap<String, List<Trip>>(new ArrayList<Trip>());
    int total = 0;
    int badIds = 0;
    for (Trip trip : dao.getAllTrips()) {
        AgencyAndId aid = trip.getId();
        String id = aid.getId();
        int index = id.indexOf('_');
        if (index != -1) {
            String commonId = id.substring(index + 1);
            tripsByCommonId.get(commonId).add(trip);
        } else {
            badIds++;
        }
    }
    _log.info("trips=" + total + " badIds=" + badIds);
    int weird = 0;
    int pairs = 0;
    int patternMismatch = 0;
    int propertyMismatch = 0;
    for (List<Trip> trips : tripsByCommonId.values()) {
        if (trips.size() == 1)
            continue;
        if (trips.size() != 2) {
            System.out.println("weird: " + trips);
            weird++;
            continue;
        }
        pairs++;
        Collections.sort(trips, _tripComparator);
        Trip tripA = trips.get(0);
        Trip tripB = trips.get(1);
        List<StopTime> stopTimesA = dao.getStopTimesForTrip(tripA);
        List<StopTime> stopTimesB = dao.getStopTimesForTrip(tripB);
        StopSequencePattern patternA = StopSequencePattern.getPatternForStopTimes(stopTimesA);
        StopSequencePattern patternB = StopSequencePattern.getPatternForStopTimes(stopTimesB);
        if (!patternA.equals(patternB)) {
            System.out.println("  pattern: " + tripA.getId() + " " + tripB.getId());
            patternMismatch++;
            continue;
        }
        String property = areTripsEquivalent(tripA, tripB);
        if (property != null) {
            System.out.println("  property: " + tripA.getId() + " " + tripB.getId() + " " + property);
            propertyMismatch++;
            continue;
        }
    }
    _log.info("weird=" + weird + " pairs=" + pairs + " patternMismatch=" + patternMismatch + " propertyMismatch=" + propertyMismatch);
}
Also used : FactoryMap(org.onebusaway.collections.FactoryMap) Trip(org.onebusaway.gtfs.model.Trip) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) ArrayList(java.util.ArrayList) List(java.util.List) StopTime(org.onebusaway.gtfs.model.StopTime)

Example 10 with AgencyAndId

use of org.onebusaway.gtfs.model.AgencyAndId in project onebusaway-gtfs-modules by OneBusAway.

the class RemoveDuplicateTripsStrategy method getPatternForTrip.

private Pattern getPatternForTrip(GtfsMutableRelationalDao dao, Trip trip) {
    List<StopTime> stopTimes = dao.getStopTimesForTrip(trip);
    int n = stopTimes.size();
    AgencyAndId[] stopIds = new AgencyAndId[n];
    int[] arrivalTimes = new int[n];
    int[] departureTimes = new int[n];
    for (int i = 0; i < n; i++) {
        StopTime stopTime = stopTimes.get(i);
        stopIds[i] = stopTime.getStop().getId();
        arrivalTimes[i] = stopTime.getArrivalTime();
        departureTimes[i] = stopTime.getDepartureTime();
    }
    return new Pattern(trip.getRoute().getId(), trip.getServiceId(), stopIds, arrivalTimes, departureTimes);
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) StopTime(org.onebusaway.gtfs.model.StopTime)

Aggregations

AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)102 Test (org.junit.Test)63 Trip (org.onebusaway.gtfs.model.Trip)37 ServiceDate (org.onebusaway.gtfs.model.calendar.ServiceDate)25 Stop (org.onebusaway.gtfs.model.Stop)24 ServiceCalendar (org.onebusaway.gtfs.model.ServiceCalendar)17 ServiceCalendarDate (org.onebusaway.gtfs.model.ServiceCalendarDate)16 ArrayList (java.util.ArrayList)15 Route (org.onebusaway.gtfs.model.Route)15 StopTime (org.onebusaway.gtfs.model.StopTime)15 GtfsMutableRelationalDao (org.onebusaway.gtfs.services.GtfsMutableRelationalDao)15 Agency (org.onebusaway.gtfs.model.Agency)13 ShapePoint (org.onebusaway.gtfs.model.ShapePoint)12 GtfsRelationalDao (org.onebusaway.gtfs.services.GtfsRelationalDao)11 HashSet (java.util.HashSet)10 GtfsRelationalDaoImpl (org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl)10 List (java.util.List)9 Frequency (org.onebusaway.gtfs.model.Frequency)9 CalendarService (org.onebusaway.gtfs.services.calendar.CalendarService)7 Set (java.util.Set)6