public Map<RouteDirectionStopKey, String> getStopMatches( Map<NBRoute, Route> routeMatches, Map<NBStop, List<Stop>> potentialStopMatches, GtfsRelationalDao dao) { Map<RouteDirectionStopKey, String> stopIdMappings = new HashMap<RouteDirectionStopKey, String>(); for (Map.Entry<NBRoute, Route> entry : routeMatches.entrySet()) { NBRoute nbRoute = entry.getKey(); Route gtfsRoute = entry.getValue(); Set<List<Stop>> stopSequences = getStopSequencesForRoute(dao, gtfsRoute); List<Map<Stop, Integer>> stopSequenceIndices = new ArrayList<Map<Stop, Integer>>(); for (List<Stop> stopSequence : stopSequences) { Map<Stop, Integer> index = new HashMap<Stop, Integer>(); for (int i = 0; i < stopSequence.size(); ++i) { index.put(stopSequence.get(i), i); } stopSequenceIndices.add(index); } for (NBDirection direction : nbRoute.getDirections()) { List<Match> matches = new ArrayList<Match>(); for (NBStop fromStop : direction.getStops()) { List<Stop> toStops = potentialStopMatches.get(fromStop); if (toStops.isEmpty()) { continue; } Match m = new Match(fromStop, toStops); matches.add(m); } Min<Assignment> m = new Min<Assignment>(); Assignment assignment = new Assignment(direction.getStops(), stopSequenceIndices); matches = applyDirectMatchesToAssignment(matches, assignment); recursivelyBuildAndScoreAssignment(matches, 0, assignment, m); Assignment bestAssignment = m.getMinElement(); if (bestAssignment == null) { throw new IllegalStateException(); } for (NBStop stop : direction.getStops()) { Stop gtfsStop = bestAssignment.getGtfsStopForNBStop(stop); if (gtfsStop == null) { continue; } String stopId = gtfsStop.getId().getId(); RouteDirectionStopKey key = new RouteDirectionStopKey(nbRoute.getTag(), direction.getTag(), stop.getTag()); stopIdMappings.put(key, stopId); } } } return stopIdMappings; }
private void recursivelyBuildAndScoreAssignment( List<Match> matches, int depth, Assignment assignment, Min<Assignment> m) { if (depth == matches.size()) { double score = scoreAssignment(assignment); if (score < m.getMinValue()) { m.add(score, new Assignment(assignment)); } } else { Match match = matches.get(depth); for (int i = 0; i < match.to.size(); ++i) { assignment.applyMatch(match, i); recursivelyBuildAndScoreAssignment(matches, depth + 1, assignment, m); } } }