Merge branch 'master' of git@git.tuebingen.mpg.de:libdai
[libdai.git] / utils / fg2dot.cpp
1 /* Copyright (C) 2006-2008 Joris Mooij [j dot mooij at science dot ru dot nl]
2 Radboud University Nijmegen, The Netherlands
3
4 This file is part of libDAI.
5
6 libDAI is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 libDAI is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with libDAI; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21
22 #include <iostream>
23 #include <cstdlib>
24 #include <string>
25 #include <dai/factorgraph.h>
26
27
28 using namespace dai;
29 using namespace std;
30
31
32 int main( int argc, char *argv[] ) {
33 if( argc != 3 ) {
34 cout << "Usage: " << argv[0] << " <in.fg> <out.dot>" << endl << endl;
35 cout << "Converts a .fg (FactorGraph) file to a .dot (GraphViz) file for visualization." << endl;
36 cout << "The .dot file can be converted to .ps (PostScript by 'neato -T ps out.dot > out.ps'" << endl;
37 return 1;
38 } else {
39 // Read factorgraph
40 FactorGraph fg;
41 char *infile = argv[1];
42
43 if( fg.ReadFromFile( infile ) ) {
44 cerr << "Error reading file " << infile << endl;
45 return 2;
46 } else {
47 if( string( argv[2] ) != "-" )
48 fg.WriteToDotFile( argv[2] );
49 else {
50 cout << "graph G {" << endl;
51 cout << "graph[size=\"9,9\"];" << endl;
52 cout << "node[shape=circle,width=0.4,fixedsize=true];" << endl;
53 for( size_t i = 0; i < fg.nrVars(); i++ )
54 cout << "\tx" << fg.var(i).label() << ";" << endl;
55 cout << "node[shape=box,style=filled,color=lightgrey,width=0.3,height=0.3,fixedsize=true];" << endl;
56 for( size_t I = 0; I < fg.nrFactors(); I++ )
57 cout << "\tp" << I << ";" << endl;
58 for( size_t i = 0; i < fg.nrVars(); i++ )
59 foreach( const FactorGraph::Neighbor &I, fg.nbV(i) )
60 cout << "\tx" << fg.var(i).label() << " -- p" << I << ";" << endl;
61 cout << "}" << endl;
62 }
63
64 return 0;
65 }
66 }
67 }
68