Search in sources :

Example 1 with Max

use of org.onebusaway.collections.Max 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;
}
Also used : HashMap(java.util.HashMap) Max(org.onebusaway.collections.Max) BlockTripEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockTripEntry) ArrayList(java.util.ArrayList) StopSequence(org.onebusaway.transit_data_federation.model.StopSequence) Counter(org.onebusaway.collections.Counter) ArrayList(java.util.ArrayList) List(java.util.List) StopSequenceCollection(org.onebusaway.transit_data_federation.model.StopSequenceCollection) HashSet(java.util.HashSet) TripEntry(org.onebusaway.transit_data_federation.services.transit_graph.TripEntry) BlockTripEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockTripEntry) StopSequenceCollectionKey(org.onebusaway.transit_data_federation.model.StopSequenceCollectionKey) HashMap(java.util.HashMap) Map(java.util.Map) FactoryMap(org.onebusaway.collections.FactoryMap) TripNarrative(org.onebusaway.transit_data_federation.model.narrative.TripNarrative)

Example 2 with Max

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

the class AbstractIdentifiableSingleEntityMergeStrategy method getFuzzyDuplicate.

@SuppressWarnings("unchecked")
@Override
protected IdentityBean<?> getFuzzyDuplicate(GtfsMergeContext context, IdentityBean<?> entity) {
    GtfsMutableRelationalDao targetDao = context.getTarget();
    Collection<T> targets = (Collection<T>) targetDao.getAllEntitiesForType(_entityType);
    if (targets.isEmpty()) {
        return null;
    }
    Max<T> best = new Max<T>();
    for (T target : targets) {
        /**
         * If we just added the target entity as part of the current feed, do not
         * attempt a fuzzy match against it.
         */
        String targetRawId = getRawId(target.getId());
        if (context.isEntityJustAddedWithRawId(targetRawId)) {
            continue;
        }
        double score = _duplicateScoringStrategy.score(context, (T) entity, target);
        best.add(score, target);
    }
    if (best.getMaxValue() < _minElementsDuplicateScoreForAutoDetect) {
        return null;
    }
    return (IdentityBean<?>) best.getMaxElement();
}
Also used : GtfsMutableRelationalDao(org.onebusaway.gtfs.services.GtfsMutableRelationalDao) Max(org.onebusaway.collections.Max) Collection(java.util.Collection) IdentityBean(org.onebusaway.gtfs.model.IdentityBean)

Example 3 with Max

use of org.onebusaway.collections.Max in project onebusaway-application-modules by camsys.

the class EstimateCurrentVehicleAction method fillInQuery.

private void fillInQuery() {
    List<CurrentVehicleEstimateQueryBean.Record> records = new ArrayList<CurrentVehicleEstimateQueryBean.Record>();
    Max<Record> max = new Max<Record>();
    for (String record : _data.split("\\|")) {
        String[] tokens = record.split(",");
        if (tokens.length != 4) {
            addFieldError("data", Messages.INVALID_FIELD_VALUE);
            return;
        }
        try {
            long t = Long.parseLong(tokens[0]);
            double lat = Double.parseDouble(tokens[1]);
            double lon = Double.parseDouble(tokens[2]);
            double accuracy = Double.parseDouble(tokens[3]);
            Record r = new Record();
            r.setTimestamp(t);
            r.setLocation(new CoordinatePoint(lat, lon));
            r.setAccuracy(accuracy);
            records.add(r);
            max.add(t, r);
        } catch (NumberFormatException ex) {
            addFieldError("data", Messages.INVALID_FIELD_VALUE);
            return;
        }
    }
    _query.setRecords(records);
    if (records.isEmpty()) {
        addFieldError("data", Messages.INVALID_FIELD_VALUE);
        return;
    }
    _query.setMostRecentLocation(max.getMaxElement().getLocation());
}
Also used : CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint) Max(org.onebusaway.collections.Max) CurrentVehicleEstimateQueryBean(org.onebusaway.transit_data.model.realtime.CurrentVehicleEstimateQueryBean) ArrayList(java.util.ArrayList) Record(org.onebusaway.transit_data.model.realtime.CurrentVehicleEstimateQueryBean.Record)

Aggregations

Max (org.onebusaway.collections.Max)3 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Counter (org.onebusaway.collections.Counter)1 FactoryMap (org.onebusaway.collections.FactoryMap)1 CoordinatePoint (org.onebusaway.geospatial.model.CoordinatePoint)1 IdentityBean (org.onebusaway.gtfs.model.IdentityBean)1 GtfsMutableRelationalDao (org.onebusaway.gtfs.services.GtfsMutableRelationalDao)1 CurrentVehicleEstimateQueryBean (org.onebusaway.transit_data.model.realtime.CurrentVehicleEstimateQueryBean)1 Record (org.onebusaway.transit_data.model.realtime.CurrentVehicleEstimateQueryBean.Record)1 StopSequence (org.onebusaway.transit_data_federation.model.StopSequence)1 StopSequenceCollection (org.onebusaway.transit_data_federation.model.StopSequenceCollection)1 StopSequenceCollectionKey (org.onebusaway.transit_data_federation.model.StopSequenceCollectionKey)1 TripNarrative (org.onebusaway.transit_data_federation.model.narrative.TripNarrative)1 BlockTripEntry (org.onebusaway.transit_data_federation.services.transit_graph.BlockTripEntry)1 TripEntry (org.onebusaway.transit_data_federation.services.transit_graph.TripEntry)1