use of org.onebusaway.collections.Counter in project onebusaway-application-modules by camsys.
the class StopWithArrivalsAndDeparturesBeanServiceImpl method getArrivalsAndDeparturesForStopIds.
public StopsWithArrivalsAndDeparturesBean getArrivalsAndDeparturesForStopIds(Set<AgencyAndId> ids, ArrivalsAndDeparturesQueryBean query) throws NoSuchStopServiceException {
List<StopBean> stops = new ArrayList<StopBean>();
List<ArrivalAndDepartureBean> allArrivalsAndDepartures = new ArrayList<ArrivalAndDepartureBean>();
Set<AgencyAndId> allNearbyStopIds = new HashSet<AgencyAndId>();
Map<String, ServiceAlertBean> situationsById = new HashMap<String, ServiceAlertBean>();
Counter<TimeZone> timeZones = new Counter<TimeZone>();
for (AgencyAndId id : ids) {
StopBean stopBean = _stopBeanService.getStopForId(id);
stops.add(stopBean);
List<ArrivalAndDepartureBean> arrivalsAndDepartures = _arrivalsAndDeparturesBeanService.getArrivalsAndDeparturesByStopId(id, query);
allArrivalsAndDepartures.addAll(arrivalsAndDepartures);
List<AgencyAndId> nearbyStopIds = _nearbyStopsBeanService.getNearbyStops(stopBean, 100);
allNearbyStopIds.addAll(nearbyStopIds);
TimeZone timeZone = _agencyService.getTimeZoneForAgencyId(id.getAgencyId());
timeZones.increment(timeZone);
List<ServiceAlertBean> situations = _serviceAlertsBeanService.getServiceAlertsForStopId(query.getTime(), id);
for (ServiceAlertBean situation : situations) situationsById.put(situation.getId(), situation);
}
allNearbyStopIds.removeAll(ids);
List<StopBean> nearbyStops = new ArrayList<StopBean>();
for (AgencyAndId id : allNearbyStopIds) {
StopBean stop = _stopBeanService.getStopForId(id);
nearbyStops.add(stop);
}
TimeZone timeZone = timeZones.getMax();
if (timeZone == null)
timeZone = TimeZone.getDefault();
StopsWithArrivalsAndDeparturesBean result = new StopsWithArrivalsAndDeparturesBean();
result.setStops(stops);
result.setArrivalsAndDepartures(allArrivalsAndDepartures);
result.setNearbyStops(nearbyStops);
result.setSituations(new ArrayList<ServiceAlertBean>(situationsById.values()));
result.setTimeZone(timeZone.getID());
return result;
}
use of org.onebusaway.collections.Counter in project onebusaway-application-modules by camsys.
the class StopSequenceCollectionServiceImpl method constructCollections.
/**
* @param route
* @param sequenceStats
* @param sequencesByGroupId
* @return
*/
private List<StopSequenceCollection> constructCollections(Map<StopSequence, PatternStats> sequenceStats, Map<String, List<StopSequence>> sequencesByGroupId) {
computeContinuations(sequenceStats, sequencesByGroupId);
Set<String> allNames = new HashSet<String>();
Map<String, String> directionToName = new HashMap<String, String>();
Map<String, Segment> segments = new HashMap<String, Segment>();
for (Map.Entry<String, List<StopSequence>> entry : sequencesByGroupId.entrySet()) {
String direction = entry.getKey();
List<StopSequence> sequences = entry.getValue();
Max<StopSequence> maxTripCount = new Max<StopSequence>();
Counter<String> names = new Counter<String>();
for (StopSequence sequence : sequences) {
maxTripCount.add(sequence.getTripCount(), sequence);
for (BlockTripEntry blockTrip : sequence.getTrips()) {
TripEntry trip = blockTrip.getTrip();
TripNarrative tripNarrative = _narrativeService.getTripForId(trip.getId());
String headsign = tripNarrative.getTripHeadsign();
if (headsign != null && headsign.length() > 0)
names.increment(headsign);
}
}
String dName = names.getMax();
RecursiveStats rs = new RecursiveStats();
rs.maxTripCount = (long) maxTripCount.getMaxValue();
exploreStopSequences(rs, sequenceStats, sequences, "");
allNames.add(dName);
directionToName.put(direction, dName);
segments.put(direction, rs.longestSegment.getMaxElement());
}
if (allNames.size() < directionToName.size()) {
for (Map.Entry<String, String> entry : directionToName.entrySet()) {
String direction = entry.getKey();
String name = entry.getValue();
direction = direction.charAt(0) + direction.substring(1).toLowerCase();
entry.setValue(name + " - " + direction);
}
}
List<StopSequenceCollection> blocks = new ArrayList<StopSequenceCollection>();
for (Map.Entry<String, String> entry : directionToName.entrySet()) {
String direction = entry.getKey();
String name = entry.getValue();
List<StopSequence> patterns = sequencesByGroupId.get(direction);
Segment segment = segments.get(direction);
// System.out.println(" " + direction + " => " + name);
StopSequenceCollection block = new StopSequenceCollection();
if (segment.fromLat == 0.0)
throw new IllegalStateException("what?");
StopSequenceCollectionKey key = new StopSequenceCollectionKey(null, direction);
block.setId(key);
block.setPublicId(direction);
block.setDescription(name);
block.setStopSequences(patterns);
block.setStartLat(segment.fromLat);
block.setStartLon(segment.fromLon);
block.setEndLat(segment.toLat);
block.setEndLon(segment.toLon);
blocks.add(block);
}
return blocks;
}
use of org.onebusaway.collections.Counter in project onebusaway-application-modules by camsys.
the class GenerateNarrativesTask method generateRouteNarratives.
public void generateRouteNarratives(NarrativeProviderImpl provider) {
for (RouteCollectionEntry routeCollectionEntry : _transitGraphDao.getAllRouteCollections()) {
List<Route> routes = new ArrayList<Route>();
Counter<Route> tripCounts = new Counter<Route>();
for (RouteEntry routeEntry : routeCollectionEntry.getChildren()) {
Route route = _gtfsDao.getRouteForId(routeEntry.getId());
routes.add(route);
int tripCount = routeEntry.getTrips().size();
tripCounts.increment(route, tripCount);
}
RouteCollectionNarrative.Builder builder = RouteCollectionNarrative.builder();
setPropertiesOfRouteCollectionFromRoutes(routes, tripCounts, builder);
provider.setNarrativeForRouteCollectionId(routeCollectionEntry.getId(), builder.create());
}
}
use of org.onebusaway.collections.Counter in project onebusaway-application-modules by camsys.
the class GenerateNarrativesTask method setPropertiesOfRouteCollectionFromRoutes.
/**
**
* Private Methods
***
*/
private void setPropertiesOfRouteCollectionFromRoutes(List<Route> routes, Counter<Route> tripCounts, RouteCollectionNarrative.Builder target) {
Counter<String> shortNames = new Counter<String>();
Counter<String> longNames = new Counter<String>();
Counter<String> descriptions = new Counter<String>();
Counter<String> colors = new Counter<String>();
Counter<String> textColors = new Counter<String>();
Counter<String> urls = new Counter<String>();
Counter<Integer> types = new Counter<Integer>();
for (Route route : routes) {
int count = tripCounts.getCount(route);
addValueToCounterIfValid(route.getShortName(), shortNames, count);
addValueToCounterIfValid(route.getLongName(), longNames, count);
addValueToCounterIfValid(route.getDesc(), descriptions, count);
addValueToCounterIfValid(route.getColor(), colors, count);
addValueToCounterIfValid(route.getTextColor(), textColors, count);
addValueToCounterIfValid(route.getUrl(), urls, count);
types.increment(route.getType(), count);
}
if (shortNames.size() > 0)
target.setShortName(deduplicate(shortNames.getMax()));
if (longNames.size() > 0)
target.setLongName(deduplicate(longNames.getMax()));
if (descriptions.size() > 0)
target.setDescription(deduplicate(descriptions.getMax()));
if (colors.size() > 0)
target.setColor(deduplicate(colors.getMax()));
if (textColors.size() > 0)
target.setTextColor(deduplicate(textColors.getMax()));
if (urls.size() > 0)
target.setUrl(deduplicate(urls.getMax()));
target.setType(deduplicate(types.getMax()));
}
use of org.onebusaway.collections.Counter in project onebusaway-application-modules by camsys.
the class StopScheduleBeanServiceImpl method getCalendarForStop.
@Cacheable
public StopCalendarDaysBean getCalendarForStop(AgencyAndId stopId) {
TimeZone timeZone = _agencyService.getTimeZoneForAgencyId(stopId.getAgencyId());
StopEntry stopEntry = _graph.getStopEntryForId(stopId);
Set<ServiceIdActivation> serviceIds = new HashSet<ServiceIdActivation>();
for (BlockStopTimeIndex index : _blockIndexService.getStopTimeIndicesForStop(stopEntry)) serviceIds.add(index.getServiceIds());
for (FrequencyBlockStopTimeIndex index : _blockIndexService.getFrequencyStopTimeIndicesForStop(stopEntry)) serviceIds.add(index.getServiceIds());
SortedMap<ServiceDate, Set<ServiceIdActivation>> serviceIdsByDate = getServiceIdsByDate(serviceIds);
Counter<Set<ServiceIdActivation>> counts = new Counter<Set<ServiceIdActivation>>();
for (Set<ServiceIdActivation> ids : serviceIdsByDate.values()) counts.increment(ids);
int total = counts.size();
Map<Set<ServiceIdActivation>, Integer> idsToGroup = new HashMap<Set<ServiceIdActivation>, Integer>();
for (Set<ServiceIdActivation> ids : counts.getSortedKeys()) idsToGroup.put(ids, total--);
List<StopCalendarDayBean> beans = new ArrayList<StopCalendarDayBean>(serviceIdsByDate.size());
for (Map.Entry<ServiceDate, Set<ServiceIdActivation>> entry : serviceIdsByDate.entrySet()) {
StopCalendarDayBean bean = new StopCalendarDayBean();
ServiceDate serviceDate = entry.getKey();
Date date = serviceDate.getAsDate(timeZone);
bean.setDate(date);
Integer indexId = idsToGroup.get(entry.getValue());
bean.setGroup(indexId);
beans.add(bean);
}
return new StopCalendarDaysBean(timeZone.getID(), beans);
}
Aggregations