2 * Copyright (c) 2003-2005 The BISON Project
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 package peersim.reports;
21 import peersim.config.Configuration;
22 import peersim.graph.GraphIO;
23 import peersim.util.FileNameGenerator;
24 import java.io.PrintStream;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
29 * Prints the whole graph in a given format.
31 public class GraphPrinter extends GraphObserver {
34 // ===================== fields =======================================
35 // ====================================================================
38 * This is the prefix of the filename where the graph is saved.
39 * The extension is ".graph" and after the prefix the basename contains
40 * a numeric index that is incremented at each saving point.
41 * If not given, the graph is dumped on the standard output.
44 private static final String PAR_BASENAME = "outf";
47 * The name for the format of the output. Defaults to "neighborlist",
48 * which is a plain dump of neighbors. The class
49 * {@link peersim.dynamics.WireFromFile} can read this format.
50 * Other supported formats are "chaco" to be used with Yehuda Koren's
51 * Embedder, "netmeter" to be used with Sergi Valverde's netmeter and also
53 * "edgelist" that dumps one (directed) node pair in each line for each edge,
54 * "gml" that is a generic format of many graph tools, and "dot" that can
55 * be used with the graphviz package.
56 * @see GraphIO#writeEdgeList
57 * @see GraphIO#writeChaco
58 * @see GraphIO#writeNeighborList
59 * @see GraphIO#writeNetmeter
62 private static final String PAR_FORMAT = "format";
64 private final String baseName;
66 private final FileNameGenerator fng;
68 private final String format;
71 // ===================== initialization ================================
72 // =====================================================================
76 * Standard constructor that reads the configuration parameters.
77 * Invoked by the simulation engine.
78 * @param name the configuration prefix for this class
80 public GraphPrinter(String name) {
83 baseName = Configuration.getString(name+"."+PAR_BASENAME,null);
84 format = Configuration.getString(name+"."+PAR_FORMAT,"neighborlist");
85 if(baseName!=null) fng = new FileNameGenerator(baseName,".graph");
90 // ====================== methods ======================================
91 // =====================================================================
95 * Saves the graph according to the specifications in the configuration.
96 * @return always false
98 public boolean execute() {
102 System.out.print(name+": ");
104 // initialize output streams
105 FileOutputStream fos = null;
106 PrintStream pstr = System.out;
107 if( baseName != null )
109 String fname = fng.nextCounterName();
110 fos = new FileOutputStream(fname);
111 System.out.println("writing to file "+fname);
112 pstr = new PrintStream(fos);
114 else System.out.println();
116 if( format.equals("neighborlist") )
117 GraphIO.writeNeighborList(g, pstr);
118 else if( format.equals("edgelist") )
119 GraphIO.writeEdgeList(g, pstr);
120 else if( format.equals("chaco") )
121 GraphIO.writeChaco(g, pstr);
122 else if( format.equals("netmeter") )
123 GraphIO.writeNetmeter(g, pstr);
124 else if( format.equals("gml") )
125 GraphIO.writeGML(g, pstr);
126 else if( format.equals("dot") )
127 GraphIO.writeDOT(g, pstr);
129 System.err.println(name+": unsupported format "+format);
131 if( fos != null ) fos.close();
135 catch( IOException e )
137 throw new RuntimeException(e);