/**
   * Compute the edge length of the given edge according to its two nodes positions.
   *
   * @param id The identifier of the edge.
   * @return The edge length or -1 if the nodes of the edge have no positions.
   * @throws RuntimeException If the edge cannot be found.
   */
  public static double edgeLength(Graph graph, String id) {
    Edge edge = graph.getEdge(id);

    if (edge != null) return edgeLength(edge);

    throw new RuntimeException("edge '" + id + "' cannot be found");
  }
示例#2
0
  /**
   * Clone a given graph with same node/edge structure and same attributes.
   *
   * @param g the graph to clone
   * @return a copy of g
   */
  public static Graph clone(Graph g) {
    Graph copy;

    try {
      Class<? extends Graph> cls = g.getClass();
      copy = cls.getConstructor(String.class).newInstance(g.getId());
    } catch (Exception e) {
      logger.warning(String.format("Cannot create a graph of %s.", g.getClass().getName()));
      copy = new AdjacencyListGraph(g.getId());
    }

    copyAttributes(g, copy);

    for (int i = 0; i < g.getNodeCount(); i++) {
      Node source = g.getNode(i);
      Node target = copy.addNode(source.getId());

      copyAttributes(source, target);
    }

    for (int i = 0; i < g.getEdgeCount(); i++) {
      Edge source = g.getEdge(i);
      Edge target =
          copy.addEdge(
              source.getId(),
              source.getSourceNode().getId(),
              source.getTargetNode().getId(),
              source.isDirected());

      copyAttributes(source, target);
    }

    return copy;
  }
  // For adding Edge To Graph
  public void AddEdgeToGraph(
      String First_IP,
      String Second_IP,
      String edge_details,
      boolean IsSwitch_1,
      boolean IsSwitch_2) {
    try {
      String temp_1 = "OpenVSwitch";
      String temp_2 = "OpenVSwitch";

      if (!IsSwitch_1) {
        temp_1 = "EndHost";
      }

      if (!IsSwitch_2) {
        temp_2 = "EndHost";
      }

      String node_name_1 = temp_1 + " [" + First_IP + "]";
      String node_name_2 = temp_2 + " [" + Second_IP + "]";
      network_graph.addEdge(edge_details, node_name_1, node_name_2);
      Edge edge = network_graph.getEdge(edge_details);
      edge.addAttribute("ui.label", edge.getId());
    } catch (Exception e) {

    }
  }
  private static void createGraph(Network network) throws IOException {
    graph = new SingleGraph("Tutorial 1");

    graph.display();

    for (int i = 0; i < network.getNNodes(); i++) {
      graph.addNode(i + "");
    }

    int[][] edge = network.edges;

    int[] edges = edge[0];
    for (int k = 0; k < edges.length; k++) {
      int n1 = edge[0][k];
      int n2 = edge[1][k];
      if (graph.getEdge(n1 + "+" + n2) == null && graph.getEdge(n2 + "+" + n1) == null) {
        graph.addEdge(n1 + "+" + n2, n1 + "", n2 + "");
      }
    }
  }
示例#5
0
  public int testAccessById() {
    int foo = 0;

    // access each node by id
    start = System.currentTimeMillis();
    for (String id : nodeIds) {
      Node n = g.getNode(id);
      if (n.hasAttribute("foo")) foo++;
    }
    end = System.currentTimeMillis();
    measureValues.put(Measures.NODE_BY_ID, end - start);

    // access each edge by id
    start = System.currentTimeMillis();
    for (String id : edgeIds) {
      Edge e = g.getEdge(id);
      if (e.hasAttribute("foo")) foo++;
    }
    end = System.currentTimeMillis();
    measureValues.put(Measures.EDGE_BY_ID, end - start);
    return foo;
  }