Пример #1
0
  public GraphIndex(Graph graph) {
    LOG.info("Indexing graph...");

    for (String feedId : graph.getFeedIds()) {
      for (Agency agency : graph.getAgencies(feedId)) {
        Map<String, Agency> agencyForId = agenciesForFeedId.getOrDefault(feedId, new HashMap<>());
        agencyForId.put(agency.getId(), agency);
        this.agenciesForFeedId.put(feedId, agencyForId);
      }
    }

    Collection<Edge> edges = graph.getEdges();
    /* We will keep a separate set of all vertices in case some have the same label.
     * Maybe we should just guarantee unique labels. */
    Set<Vertex> vertices = Sets.newHashSet();
    for (Edge edge : edges) {
      vertices.add(edge.getFromVertex());
      vertices.add(edge.getToVertex());
      if (edge instanceof TablePatternEdge) {
        TablePatternEdge patternEdge = (TablePatternEdge) edge;
        TripPattern pattern = patternEdge.getPattern();
        patternForId.put(pattern.code, pattern);
      }
    }
    for (Vertex vertex : vertices) {
      vertexForId.put(vertex.getLabel(), vertex);
      if (vertex instanceof TransitStop) {
        TransitStop transitStop = (TransitStop) vertex;
        Stop stop = transitStop.getStop();
        stopForId.put(stop.getId(), stop);
        stopVertexForStop.put(stop, transitStop);
        stopsForParentStation.put(stop.getParentStation(), stop);
      }
    }
    for (TransitStop stopVertex : stopVertexForStop.values()) {
      Envelope envelope = new Envelope(stopVertex.getCoordinate());
      stopSpatialIndex.insert(envelope, stopVertex);
    }
    for (TripPattern pattern : patternForId.values()) {
      patternsForFeedId.put(pattern.getFeedId(), pattern);
      patternsForRoute.put(pattern.route, pattern);

      for (Trip trip : pattern.getTrips()) {
        patternForTrip.put(trip, pattern);
        tripForId.put(trip.getId(), trip);
      }
      for (Stop stop : pattern.getStops()) {
        patternsForStop.put(stop, pattern);
      }
    }
    for (Route route : patternsForRoute.asMap().keySet()) {
      routeForId.put(route.getId(), route);
    }

    // Copy these two service indexes from the graph until we have better ones.
    calendarService = graph.getCalendarService();
    serviceCodes = graph.serviceCodes;
    this.graph = graph;
    LOG.info("Done indexing graph.");
  }
  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;
  }