Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #1 from mquinson/master
[simgrid.git] / contrib / psg / src / peersim / reports / GraphPrinter.java
1 /*
2  * Copyright (c) 2003-2005 The BISON Project
3  *
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.
7  *
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.
12  *
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.
16  *
17  */
18                 
19 package peersim.reports;
20
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;
27
28 /**
29 * Prints the whole graph in a given format.
30 */
31 public class GraphPrinter extends GraphObserver {
32
33
34 // ===================== fields =======================================
35 // ====================================================================
36
37 /**
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.
42 * @config
43 */
44 private static final String PAR_BASENAME = "outf";
45
46 /**
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
52 * with pajek,
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
60 * @config
61 */
62 private static final String PAR_FORMAT = "format";
63
64 private final String baseName;
65
66 private final FileNameGenerator fng;
67
68 private final String format;
69
70
71 // ===================== initialization ================================
72 // =====================================================================
73
74
75 /**
76  * Standard constructor that reads the configuration parameters.
77  * Invoked by the simulation engine.
78  * @param name the configuration prefix for this class
79  */
80 public GraphPrinter(String name) {
81
82         super(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");
86         else fng = null;
87 }
88
89
90 // ====================== methods ======================================
91 // =====================================================================
92
93
94 /**
95 * Saves the graph according to the specifications in the configuration.
96 * @return always false
97 */
98 public boolean execute() {
99 try {   
100         updateGraph();
101         
102         System.out.print(name+": ");
103         
104         // initialize output streams
105         FileOutputStream fos = null;
106         PrintStream pstr = System.out;
107         if( baseName != null )
108         {
109                 String fname = fng.nextCounterName();
110                 fos = new FileOutputStream(fname);
111                 System.out.println("writing to file "+fname);
112                 pstr = new PrintStream(fos);
113         }
114         else    System.out.println();
115         
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);
128         else
129                 System.err.println(name+": unsupported format "+format);
130         
131         if( fos != null ) fos.close();
132         
133         return false;
134 }
135 catch( IOException e )
136 {
137         throw new RuntimeException(e);
138 }
139 }
140
141 }
142