aefdf3d3e53acef81bb5e0bb158779861ac2c4d5
[libdai.git] / utils / fg2dot.cpp
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * libDAI is licensed under the terms of the GNU General Public License version
4 * 2, or (at your option) any later version. libDAI is distributed without any
5 * warranty. See the file COPYING for more details.
6 *
7 * Copyright (C) 2006-2009 Joris Mooij [joris dot mooij at libdai dot org]
8 * Copyright (C) 2006-2007 Radboud University Nijmegen, The Netherlands
9 */
10
11
12 #include <iostream>
13 #include <fstream>
14 #include <cstdlib>
15 #include <string>
16 #include <dai/factorgraph.h>
17
18
19 using namespace dai;
20 using namespace std;
21
22
23 int main( int argc, char *argv[] ) {
24 if( argc != 3 ) {
25 cout << "Usage: " << argv[0] << " <in.fg> <out.dot>" << endl << endl;
26 cout << "Converts a .fg (FactorGraph) file to a .dot (GraphViz) file for" << endl;
27 cout << "visualization. The .dot file can be converted to .ps (PostScript) by" << endl;
28 cout << "'neato -T ps out.dot > out.ps' or by 'dot -T ps out.dot > out.ps'" << endl << endl;
29 return 1;
30 } else {
31 // Read factorgraph
32 FactorGraph fg;
33 char *infile = argv[1];
34
35 fg.ReadFromFile( infile );
36
37 ostream *os = &cout;
38 ofstream outfile;
39 if( string( argv[2] ) != "-" ) {
40 outfile.open( argv[2] );
41 if( !outfile.is_open() ) {
42 cerr << "Cannot open " << argv[2] << " for writing" << endl;
43 return 1;
44 }
45 os = &outfile;
46 }
47
48 fg.printDot( *os );
49
50 return 0;
51 }
52 }