/** * This method adds a Vertex to the graph. If a Vertex with the same label as the parameter exists * in the Graph, the existing Vertex is overwritten only if overwriteExisting is true. If the * existing Vertex is overwritten, the Edges incident to it are all removed from the Graph. * * @param vertex * @param overwriteExisting * @return true iff vertex was added to the Graph */ public boolean addVertex(Vertex vertex, boolean overwriteExisting) { Vertex current = this.vertices.get(vertex.getLabel()); if (current != null) { if (!overwriteExisting) { return false; } while (current.getNeighborCount() > 0) { this.removeEdge(current.getNeighbor(0)); } } vertices.put(vertex.getLabel(), vertex); return true; }
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."); }
/** * This constructor accepts an ArrayList<Vertex> and populates this.vertices. If multiple Vertex * objects have the same label, then the last Vertex with the given label is used. * * @param vertices The initial Vertices to populate this Graph */ public Graph(ArrayList<Vertex> vertices) { this.vertices = new HashMap<String, Vertex>(); this.edges = new HashMap<Integer, Edge>(); for (Vertex v : vertices) { this.vertices.put(v.getLabel(), v); } }
/** * @param vertex The Vertex to look up * @return true iff this Graph contains vertex */ public boolean containsVertex(Vertex vertex) { return this.vertices.get(vertex.getLabel()) != null; }