Exemple #1
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) {

    }
  }
  public void testAddRemove() {
    // add 10000 new nodes
    start = System.currentTimeMillis();
    for (int i = 0; i < 10000; i++) g.addNode("__newnode__" + i);
    end = System.currentTimeMillis();
    measureValues.put(Measures.ADD_NODE, end - start);

    // for each new node n, add 100 edges between n and old nodes
    start = System.currentTimeMillis();
    int current = 0;
    int edgeId = 0;
    for (int i = 0; i < 10000; i++) {
      String id = "__newnode__" + i;
      for (int j = 0; j < 100; j++) {
        g.addEdge("__newedge__" + edgeId, id, nodeIds.get(current));
        edgeId++;
        current++;
        if (current == nodeIds.size()) current = 0;
      }
    }
    end = System.currentTimeMillis();
    measureValues.put(Measures.ADD_EDGE, end - start);

    // remove all the new nodes (and new edges)
    start = System.currentTimeMillis();
    for (int i = 0; i < 10000; i++) g.removeNode("__newnode__" + i);
    end = System.currentTimeMillis();
    measureValues.put(Measures.REMOVE_NODE, end - start);

    // remove 10000 edges
    start = System.currentTimeMillis();
    for (int i = 0; i < 10000; i++) g.removeEdge(edgeIds.get(i));
    end = System.currentTimeMillis();
    measureValues.put(Measures.REMOVE_EDGE, end - start);
  }
  private void iterateGraph(SimpleNode node, Graph graph) {
    if (node.isLeaf()) {
      graph.getNode(node.getId()).addAttribute("ui.class", "leaf");
    } else {
      Iterator<Entry<String, SimpleNode>> childrenIt = node.getChildren().entrySet().iterator();

      while (childrenIt.hasNext()) {
        Entry<String, SimpleNode> next = childrenIt.next();
        graph.addNode(next.getValue().getId()).addAttribute("label", next.getValue().getName());
        graph
            .addEdge(
                node.getId() + next.getValue().getId(), node.getId(), next.getValue().getId(), true)
            .addAttribute("label", next.getKey());
        iterateGraph(next.getValue(), graph);
      }
    }
  }
  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 + "");
      }
    }
  }