/**
   * Saves a given graph to a dot file, it also creates the file, or overwrites the old one
   *
   * @param g : The jung graph to save
   * @param filename : A string that points to the destination of the save
   * @param labeler : A node object -> Node name converter object
   * @param graphName : The name of the graph to export (usually this is set the project's name)
   * @throws IOException On IO error
   */
  public void save(Graph<V, E> g, String filename, Transformer<V, String> labeler, String graphName)
      throws IOException {
    SortedSet<V> nodes = new TreeSet<V>();
    Map<V, SortedSet<V>> successors = new HashMap<V, SortedSet<V>>();
    for (V source : g.getVertices()) {
      Collection<V> actSuccessors = g.getSuccessors(source);
      SortedSet<V> successorTree = new TreeSet<V>();
      for (V destination : actSuccessors) {
        successorTree.add(destination);
      }

      nodes.add(source);
      successors.put(source, successorTree);
    }

    BufferedWriter writer =
        new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename), "UTF-8"));
    writer.write("digraph \"" + graphName + "\" {\n");
    for (V from : nodes) {
      Collection<V> actSuccessors = successors.get(from);
      for (V to : actSuccessors) {
        writer.write(
            "\t\"" + labeler.transform(from) + "\" -> \"" + labeler.transform(to) + "\";\n");
      }

      if (g.getPredecessorCount(from) == 0 && actSuccessors.isEmpty()) {
        writer.write("\t\"" + labeler.transform(from) + "\";\n");
      }
    }

    writer.write("}");
    writer.close();
  }
Exemplo n.º 2
0
  /**
   * Tests to see whether these two graphs are structurally equivalent, based on the connectivity of
   * the vertices with matching indices in each graph. Assumes a 0-based index.
   *
   * @param g1
   * @param g2
   */
  private void compareIndexedGraphs(Graph<Number, Number> g1, Graph<Number, Number> g2) {
    int n1 = g1.getVertexCount();
    int n2 = g2.getVertexCount();

    assertEquals(n1, n2);

    assertEquals(g1.getEdgeCount(), g2.getEdgeCount());

    List<Number> id1 = new ArrayList<Number>(g1.getVertices());
    List<Number> id2 = new ArrayList<Number>(g2.getVertices());

    for (int i = 0; i < n1; i++) {
      Number v1 = id1.get(i);
      Number v2 = id2.get(i);
      assertNotNull(v1);
      assertNotNull(v2);

      checkSets(g1.getPredecessors(v1), g2.getPredecessors(v2), id1, id2);
      checkSets(g1.getSuccessors(v1), g2.getSuccessors(v2), id1, id2);
    }
  }