public List<DirectEdge> getOutgoingEdges(Vertex vertex) {
    Graph graph = _graphService.getGraph();
    GraphVertex gv = graph.getGraphVertex(vertex);
    if (gv == null) {
      return Collections.emptyList();
    }
    List<DirectEdge> result = new ArrayList<DirectEdge>();
    for (Edge out : gv.getOutgoing()) {

      if (!(out instanceof TurnEdge || out instanceof OutEdge || out instanceof PlainStreetEdge)) {
        continue;
      }
      result.add((StreetEdge) out);
    }
    return result;
  }
 @Override
 public void buildGraph(Graph graph) {
   WeightTable wt = new WeightTable(graph);
   if (maxWalkSpeed != null) {
     wt.setMaxWalkSpeed(maxWalkSpeed);
   }
   wt.buildTable();
   graph.putService(WeightTable.class, wt);
 }
  public void testNetworkLinker() {
    int numVerticesBefore = graph.getVertices().size();
    NetworkLinker nl = new NetworkLinker(graph);
    nl.createLinkage();
    int numVerticesAfter = graph.getVertices().size();
    assertEquals(4, numVerticesAfter - numVerticesBefore);
    Collection<Edge> outgoing = graph.getOutgoing(station1);
    assertTrue(outgoing.size() == 1);
    DirectEdge edge = (DirectEdge) outgoing.iterator().next();

    Vertex midpoint = edge.getToVertex();
    assertTrue(Math.abs(midpoint.getCoordinate().y - 40.01) < 0.00000001);

    outgoing = graph.getOutgoing(station2);
    assertTrue(outgoing.size() == 1);
    edge = (DirectEdge) outgoing.iterator().next();

    Vertex station2point = edge.getToVertex();
    assertTrue(Math.abs(station2point.getCoordinate().x - -74.002) < 0.00000001);
  }
 public boolean multipleOptionsBefore(Edge edge, State state) {
   Graph graph = _graphService.getGraph();
   boolean foundAlternatePaths = false;
   Vertex start = edge.getFromVertex();
   GraphVertex gv = graph.getGraphVertex(start);
   if (gv == null) {
     return false;
   }
   for (Edge out : gv.getOutgoing()) {
     if (out == edge) {
       continue;
     }
     if (!(out instanceof TurnEdge || out instanceof OutEdge || out instanceof PlainStreetEdge)) {
       continue;
     }
     if (state != null && out.traverse(state) == null) {
       continue;
     }
     // there were paths we didn't take.
     foundAlternatePaths = true;
     break;
   }
   return foundAlternatePaths;
 }
  public void setUp() {
    graph = new Graph();
    // a 0.1 degree x 0.1 degree square
    top =
        new StreetVertex(
            "top",
            GeometryUtils.makeLineString(-74.01, 40.01, -74.0, 40.01),
            "top",
            1500,
            false,
            null);
    bottom =
        new StreetVertex(
            "bottom",
            GeometryUtils.makeLineString(-74.01, 40.0, -74.0, 40.0),
            "bottom",
            1500,
            false,
            null);
    left =
        new StreetVertex(
            "left",
            GeometryUtils.makeLineString(-74.01, 40.0, -74.01, 40.01),
            "left",
            1500,
            false,
            null);
    right =
        new StreetVertex(
            "right",
            GeometryUtils.makeLineString(-74.0, 40.0, -74.0, 40.01),
            "right",
            1500,
            false,
            null);

    StreetVertex topBack =
        new StreetVertex(
            "topBack",
            GeometryUtils.makeLineString(-74.0, 40.01, -74.01, 40.01),
            "topBack",
            1500,
            true,
            null);
    StreetVertex bottomBack =
        new StreetVertex(
            "bottomBack",
            GeometryUtils.makeLineString(-74.0, 40.0, -74.01, 40.0),
            "bottomBack",
            1500,
            true,
            null);
    leftBack =
        new StreetVertex(
            "leftBack",
            GeometryUtils.makeLineString(-74.01, 40.01, -74.01, 40.0),
            "leftBack",
            1500,
            true,
            null);
    rightBack =
        new StreetVertex(
            "rightBack",
            GeometryUtils.makeLineString(-74.0, 40.01, -74.0, 40.0),
            "rightBack",
            1500,
            true,
            null);

    right.setPermission(StreetTraversalPermission.PEDESTRIAN);

    graph.addVertex(top);
    graph.addVertex(bottom);
    graph.addVertex(left);
    graph.addVertex(right);

    graph.addVertex(topBack);
    graph.addVertex(bottomBack);
    graph.addVertex(leftBack);
    graph.addVertex(rightBack);

    EndpointVertex tlIn =
        (EndpointVertex) graph.addVertex(new EndpointVertex("tl in", -74.01, 40.01));
    EndpointVertex trIn =
        (EndpointVertex) graph.addVertex(new EndpointVertex("tr in", -74.0, 40.01));
    EndpointVertex blIn =
        (EndpointVertex) graph.addVertex(new EndpointVertex("bl in", -74.0, 40.0));
    EndpointVertex brIn =
        (EndpointVertex) graph.addVertex(new EndpointVertex("br in", -74.01, 40.0));

    Vertex tlOut = graph.addVertex(new EndpointVertex("tl out", -74.01, 40.01));
    trOut = graph.addVertex(new EndpointVertex("tr out", -74.0, 40.01));
    Vertex blOut = graph.addVertex(new EndpointVertex("bl out", -74.0, 40.0));
    brOut = graph.addVertex(new EndpointVertex("br out", -74.01, 40.0));

    graph.addEdge(new FreeEdge(tlOut, top));
    graph.addEdge(new FreeEdge(tlOut, leftBack));

    graph.addEdge(new FreeEdge(trOut, topBack));
    graph.addEdge(new FreeEdge(trOut, rightBack));

    graph.addEdge(new FreeEdge(blOut, bottom));
    graph.addEdge(new FreeEdge(blOut, left));

    graph.addEdge(new FreeEdge(brOut, bottomBack));
    graph.addEdge(new FreeEdge(brOut, right));

    graph.addEdge(new OutEdge(topBack, tlIn));
    graph.addEdge(new OutEdge(left, tlIn));

    graph.addEdge(new OutEdge(top, trIn));
    graph.addEdge(new OutEdge(right, trIn));

    graph.addEdge(new OutEdge(bottomBack, blIn));
    graph.addEdge(new OutEdge(leftBack, blIn));

    graph.addEdge(new OutEdge(bottom, brIn));
    graph.addEdge(new OutEdge(rightBack, brIn));

    graph.addEdge(new TurnEdge(top, rightBack));
    graph.addEdge(new TurnEdge(rightBack, bottomBack));
    graph.addEdge(new TurnEdge(bottomBack, left));
    graph.addEdge(new TurnEdge(left, top));

    graph.addEdge(new TurnEdge(topBack, leftBack));
    graph.addEdge(new TurnEdge(leftBack, bottom));
    graph.addEdge(new TurnEdge(bottom, right));
    graph.addEdge(new TurnEdge(right, topBack));

    station1 =
        new TransitStop(
            "transitVertex 1",
            -74.005,
            40.0099999,
            "transitVertex 1",
            new AgencyAndId("A", "fleem station"),
            null);
    graph.addVertex(station1);
    station2 =
        new TransitStop(
            "transitVertex 2",
            -74.002,
            40.0099999,
            "transitVertex 2",
            new AgencyAndId("A", "morx station"),
            null);
    graph.addVertex(station2);
  }
  public void testHalfEdges() {
    // the shortest half-edge from the start vertex takes you down, but the shortest total path
    // is up and over

    TraverseOptions options = new TraverseOptions();

    HashSet<Edge> turns = new HashSet<Edge>(graph.getOutgoing(left));
    turns.addAll(graph.getOutgoing(leftBack));

    StreetLocation start =
        StreetLocation.createStreetLocation(
            "start",
            "start",
            cast(turns, StreetEdge.class),
            new LinearLocation(0, 0.4).getCoordinate(left.getGeometry()));

    HashSet<Edge> endTurns = new HashSet<Edge>(graph.getOutgoing(right));
    endTurns.addAll(graph.getOutgoing(rightBack));

    StreetLocation end =
        StreetLocation.createStreetLocation(
            "end",
            "end",
            cast(endTurns, StreetEdge.class),
            new LinearLocation(0, 0.8).getCoordinate(right.getGeometry()));

    assertTrue(start.getX() < end.getX());
    assertTrue(start.getY() < end.getY());

    List<DirectEdge> extra = end.getExtra();

    assertEquals(12, extra.size());

    GregorianCalendar startTime = new GregorianCalendar(2009, 11, 1, 12, 34, 25);

    ShortestPathTree spt1 =
        AStar.getShortestPathTree(graph, brOut, end, TestUtils.toSeconds(startTime), options);

    GraphPath pathBr = spt1.getPath(end, false);
    assertNotNull("There must be a path from br to end", pathBr);

    ShortestPathTree spt2 =
        AStar.getShortestPathTree(graph, trOut, end, TestUtils.toSeconds(startTime), options);

    GraphPath pathTr = spt2.getPath(end, false);
    assertNotNull("There must be a path from tr to end", pathTr);
    assertTrue(
        "path from bottom to end must be longer than path from top to end",
        pathBr.getWeight() > pathTr.getWeight());

    ShortestPathTree spt =
        AStar.getShortestPathTree(graph, start, end, TestUtils.toSeconds(startTime), options);

    GraphPath path = spt.getPath(end, false);
    assertNotNull("There must be a path from start to end", path);

    // the bottom is not part of the shortest path
    for (State s : path.states) {
      assertNotSame(s.getVertex(), graph.getVertex("bottom"));
      assertNotSame(s.getVertex(), graph.getVertex("bottomBack"));
    }

    startTime = new GregorianCalendar(2009, 11, 1, 12, 34, 25);

    options.setArriveBy(true);
    spt = AStar.getShortestPathTree(graph, start, end, TestUtils.toSeconds(startTime), options);

    path = spt.getPath(start, false);
    assertNotNull("There must be a path from start to end (looking back)", path);

    // the bottom edge is not part of the shortest path
    for (State s : path.states) {
      assertNotSame(s.getVertex(), graph.getVertex("bottom"));
      assertNotSame(s.getVertex(), graph.getVertex("bottomBack"));
    }

    /* Now, the right edge is not bikeable.  But the user can walk their bike.  So here are some tests
     * that prove (a) that walking bikes works, but that (b) it is not preferred to riding a tiny bit longer.
     */

    options = new TraverseOptions(new TraverseModeSet(TraverseMode.BICYCLE));
    start =
        StreetLocation.createStreetLocation(
            "start1",
            "start1",
            cast(turns, StreetEdge.class),
            new LinearLocation(0, 0.95).getCoordinate(top.getGeometry()));
    end =
        StreetLocation.createStreetLocation(
            "end1",
            "end1",
            cast(turns, StreetEdge.class),
            new LinearLocation(0, 0.95).getCoordinate(bottom.getGeometry()));
    spt = AStar.getShortestPathTree(graph, start, end, TestUtils.toSeconds(startTime), options);

    path = spt.getPath(start, false);
    assertNotNull("There must be a path from top to bottom along the right", path);

    // the left edge is not part of the shortest path (even though the bike must be walked along the
    // right)
    for (State s : path.states) {
      assertNotSame(s.getVertex(), graph.getVertex("left"));
      assertNotSame(s.getVertex(), graph.getVertex("leftBack"));
    }

    start =
        StreetLocation.createStreetLocation(
            "start2",
            "start2",
            cast(turns, StreetEdge.class),
            new LinearLocation(0, 0.55).getCoordinate(top.getGeometry()));
    end =
        StreetLocation.createStreetLocation(
            "end2",
            "end2",
            cast(turns, StreetEdge.class),
            new LinearLocation(0, 0.55).getCoordinate(bottom.getGeometry()));
    spt = AStar.getShortestPathTree(graph, start, end, TestUtils.toSeconds(startTime), options);

    path = spt.getPath(start, false);
    assertNotNull("There must be a path from top to bottom", path);

    // the right edge is not part of the shortest path, e
    for (State s : path.states) {
      assertNotSame(s.getVertex(), graph.getVertex("right"));
      assertNotSame(s.getVertex(), graph.getVertex("rightBack"));
    }
  }