Exemplo n.º 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;
  }
Exemplo n.º 2
0
  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);
  }
Exemplo n.º 3
0
  public Graph tree2graph(SimpleNode node) {

    Graph graph = new DefaultGraph("Sample graph");
    graph.setStrict(false);
    graph.addAttribute("ui.stylesheet", style);
    graph.addNode(node.getId()).addAttribute("label", node.getName());
    graph.getNode(node.getId()).addAttribute("ui.class", "root");

    this.iterateGraph(node, graph);

    return graph;
  }
  // For adding Node To Graph
  public void AddNodeToGraph(String IP, boolean IsSwitch) {
    String temp;

    if (IsSwitch) {
      temp = "OpenVSwitch";
    } else {
      temp = "EndHost";
    }

    network_graph.addNode(temp + " [" + IP + "]");
    Node node = network_graph.getNode(temp + " [" + IP + "]");
    node.addAttribute("ui.class", temp);
    node.addAttribute("ui.label", node.getId());
  }
Exemplo n.º 5
0
  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 + "");
      }
    }
  }