source: observatorio/simulacion/SimEscenariosEconomicos/graph_builder.C

simulacion
Last change on this file was 7941763, checked in by Alejandro <amujica@…>, 10 years ago

eliminacion de un filtro redundante

  • Property mode set to 100644
File size: 8.0 KB
Line 
1/*
2  Copyright (C) 2012
3  Alejandro Mujica (amujica@cenditel.gob.ve)
4  Erwin Paredes (eparedes@cenditel.gob.ve)
5  José Ruiz (jruiz@cenditel.gob.ve)
6  Rodolfo Rangel (rrangel@cenditel.gob.ve)
7  Julie Vera (jvera@cenditel.gob.ve)
8 
9  CENDITEL Fundación Centro Nacional de Desarrollo e Investigación en
10  Tecnologías Libres
11 
12  Este programa es software libre; Usted puede usarlo bajo los términos de la
13  licencia de software GPL versión 2.0 de la Free Software Foundation.
14 
15  Este programa se distribuye con la esperanza de que sea útil, pero SIN
16  NINGUNA GARANTÍA; tampoco las implícitas garantías de MERCANTILIDAD o
17  ADECUACIÓN A UN PROPÓSITO PARTICULAR.
18  Consulte la licencia GPL para más detalles. Usted debe recibir una copia
19  de la GPL junto con este programa; si no, escriba a la Free Software
20  Foundation Inc. 51 Franklin Street,5 Piso, Boston, MA 02110-1301, USA.
21*/
22
23# include <argp.h>
24
25# include <dbProperties.H>
26# include <ioManager.H>
27# include <process.H>
28
29
30class Fields
31{
32  Fields() { /* Empty */ }
33
34public:
35  static std::string    dbFileName;
36
37  static std::string    xmlInputFileName;
38
39  static std::string    xmlOutputFileName;
40
41  static bool           generateXml;
42
43  static std::string    svgOutputFileName;
44
45  static bool           generateSvg;
46
47  static std::string    gvOutputFileName;
48
49  static std::string    dotOutputFileName;
50
51  static bool           generateGv;
52
53  static bool           generateDot;
54
55  static bool           verbose;
56};
57
58std::string Fields::dbFileName = "";
59
60std::string Fields::xmlInputFileName = "";
61
62std::string Fields::xmlOutputFileName = "";
63
64bool Fields::generateXml = false;
65
66std::string Fields::svgOutputFileName = "";
67
68bool Fields::generateSvg = false;
69
70std::string Fields::gvOutputFileName = "";
71
72bool Fields::generateGv = false;
73
74std::string Fields::dotOutputFileName = "";
75
76bool Fields::generateDot = false;
77
78bool Fields::verbose = false;
79
80const char * argp_program_version =  "Graph Builder v1.1";
81
82const char * argp_program_bug_address = "amujica@cenditel.gob.ve";
83
84static char doc[] = "";
85
86static char argDoc[] = "";
87
88static struct argp_option options [] = {
89  {"database", 'b', "database-filename", 0,
90   "filename with database attributes", 0},
91  {"xml-input", 'i', "xml-input-filename", 0, "xml input filename", 0},
92  {"xml-output", 'o', "xml-output-filename", OPTION_ARG_OPTIONAL,
93   "xml output filename", 0},
94  {"svg-output", 's', "svg-output-filename", OPTION_ARG_OPTIONAL,
95   "svg output filename", 0},
96  {"gv-output", 'g', "gv-output-filename", OPTION_ARG_OPTIONAL,
97   "gv output filename", 0},
98  {"dot-output", 'd', "dot-output-filename", OPTION_ARG_OPTIONAL,
99   "dot output filename", 0},
100  {"keep-dot", 'k', 0, OPTION_ARG_OPTIONAL, "Don't remove temporal'", 0},
101  {"verbose", 'v', 0, OPTION_ARG_OPTIONAL, "Verbose mode", 0},
102  { 0, 0, 0, 0, 0, 0 }
103}; 
104
105static error_t parser_opt(int key, char * arg, struct argp_state *)
106{
107  switch (key)
108    {
109    case 'b':
110      if (arg == NULL)
111        {
112          std::cout << "Waiting for filename with database attributes\n";
113          exit(0);
114        }
115      Fields::dbFileName = arg;
116      break;
117
118    case 'i':
119      if (arg == NULL)
120        {
121          std::cout << "Waiting for xml input filename\n";
122          exit(0);
123        }
124      Fields::xmlInputFileName = arg;
125      break;
126
127    case 'o':
128      if (arg == NULL)
129        {
130          std::cout << "Waiting for xml output filename\n";
131          exit(0);
132        }
133      Fields::xmlOutputFileName = arg;
134      Fields::generateXml = true;
135      break;
136
137    case 's':
138      if (arg == NULL)
139        {
140          std::cout << "Waiting for svg output filename\n";
141          exit(0);
142        }
143      Fields::svgOutputFileName = arg;
144      Fields::generateSvg = true;
145      break;
146
147    case 'g':
148      if (arg == NULL)
149        {
150          std::cout << "Waiting for gv output filename\n";
151          exit(0);
152        }
153      Fields::gvOutputFileName = arg;
154      Fields::generateGv = true;
155      break;
156
157    case 'd':
158      if (arg == NULL)
159        {
160          std::cout << "Waiting for dot output filename\n";
161          exit(0);
162        }
163      Fields::dotOutputFileName = arg;
164      Fields::generateDot = true;
165      break;
166
167    case 'k': 
168      Fields::generateDot = true;
169      break;
170
171    case 'v':
172      Fields::verbose = true;
173      break;
174     
175    default: return ARGP_ERR_UNKNOWN;
176    }
177
178  return 0;
179}
180
181static struct argp arg_defs = { options, parser_opt, argDoc, doc, 0, 0, 0 };
182
183int main (int argc, char * argv[]) {
184
185    argp_parse(&arg_defs, argc, argv, ARGP_IN_ORDER, 0, NULL);
186
187    if (Fields::dbFileName == "" or Fields::xmlInputFileName == "") {
188
189        std::cout << "filename with database attributes and xml input filename "
190                     "are required\n";
191        return 1;
192
193    }
194
195    IOManager ioManager;
196
197    Graph graph;
198
199    try {
200
201        DBProperties::getInstance().readFile(Fields::dbFileName);
202
203        ioManager.buildGraph(Fields::xmlInputFileName, graph);
204
205        if (Fields::generateXml) {
206
207            if (Fields::verbose) {
208                std::cout << "Generating xml output...\n";
209            }
210
211            ioManager.addGraphToFile(graph, Fields::xmlInputFileName,
212                                     Fields::xmlOutputFileName,
213                                     IOManager::GRAPH);
214
215            if (Fields::verbose) {
216                std::cout << "Xml output generated.\n";
217            }
218        }
219
220    } catch(const std::exception & e) {
221
222        std::cout << "Exception catched: " << e.what() << std::endl;
223        return 2;
224    }
225
226
227    if (Fields::generateSvg or Fields::generateGv or Fields::generateDot) {
228
229        std::string dotFileName =
230            Fields::generateDot ? Fields::dotOutputFileName : "temp.dot";
231
232        if (Fields::verbose) {
233            std::cout << "Generating " << dotFileName << "...\n";
234        }
235
236        ioManager.generateDot<IOManager::NodeLabel, IOManager::ArcLabel>
237            (graph, dotFileName);
238
239        if (Fields::verbose) {
240            std::cerr << dotFileName << " generated.\n";
241        }
242
243        int returnValue = 0;
244
245        char const * path = "dot";
246
247        char * args[] = {"dot", "", (char *) dotFileName.c_str(), "-o", "", 0};
248
249            if (Fields::generateSvg) {
250
251                args[1] = "-Tsvg";
252                args[4] = (char *) Fields::svgOutputFileName.c_str();
253
254                if (Fields::verbose) {
255                    std::cout << "Generating " << Fields::svgOutputFileName
256                              << "...\n";
257                }
258
259                returnValue = Process::exec(path, args);
260
261                if (returnValue == 0) {
262                    if (Fields::verbose) {
263                        std::cerr << Fields::svgOutputFileName
264                                  << " generated.\n";
265                    }
266                } else {
267                    std::cout << "Error generating "
268                              << Fields::svgOutputFileName << "\n";
269                }
270            }
271
272            if (Fields::generateGv) {
273
274                args[1] = "-Txdot";
275                args[4] = (char *) Fields::gvOutputFileName.c_str();
276
277                if (Fields::verbose) {
278                    std::cout << "Generating " << Fields::gvOutputFileName
279                              << "...\n";
280                }
281
282                returnValue = Process::exec(path, args);
283
284                if (returnValue == 0) {
285                    if (Fields::verbose) {
286                        std::cerr << Fields::gvOutputFileName
287                                  << " generated.\n";
288                    }
289                } else {
290                    std::cout << "Error generating "
291                              << Fields::gvOutputFileName << "\n";
292                }
293            }
294
295            if (not Fields::generateDot) {
296
297                if (Fields::verbose) {
298                    std::cout << "Removing " << dotFileName << "...\n";
299                }
300
301                remove(dotFileName.c_str());
302
303                if (Fields::verbose) {
304                    std::cerr << dotFileName << " removed.\n";
305                }
306            }
307
308        }
309
310    return 0;
311}
312
Note: See TracBrowser for help on using the repository browser.