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() + "\"");
}
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;
}
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);
}
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);
}
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);
}
Aggregations