Some small documentation updates
[libdai.git] / src / alldai.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-2010 Joris Mooij [joris dot mooij at libdai dot org]
8 * Copyright (C) 2006-2007 Radboud University Nijmegen, The Netherlands
9 */
10
11
12 #include <string>
13 #include <fstream>
14 #include <dai/alldai.h>
15 #include <dai/properties.h>
16 #include <dai/exceptions.h>
17
18
19 namespace dai {
20
21
22 using namespace std;
23
24
25 InfAlg *newInfAlg( const std::string &name, const FactorGraph &fg, const PropertySet &opts ) {
26 if( name == ExactInf::Name )
27 return new ExactInf (fg, opts);
28 #ifdef DAI_WITH_BP
29 if( name == BP::Name )
30 return new BP (fg, opts);
31 #endif
32 #ifdef DAI_WITH_FBP
33 if( name == FBP::Name )
34 return new FBP (fg, opts);
35 #endif
36 #ifdef DAI_WITH_TRWBP
37 if( name == TRWBP::Name )
38 return new TRWBP (fg, opts);
39 #endif
40 #ifdef DAI_WITH_MF
41 if( name == MF::Name )
42 return new MF (fg, opts);
43 #endif
44 #ifdef DAI_WITH_HAK
45 if( name == HAK::Name )
46 return new HAK (fg, opts);
47 #endif
48 #ifdef DAI_WITH_LC
49 if( name == LC::Name )
50 return new LC (fg, opts);
51 #endif
52 #ifdef DAI_WITH_TREEEP
53 if( name == TreeEP::Name )
54 return new TreeEP (fg, opts);
55 #endif
56 #ifdef DAI_WITH_JTREE
57 if( name == JTree::Name )
58 return new JTree (fg, opts);
59 #endif
60 #ifdef DAI_WITH_MR
61 if( name == MR::Name )
62 return new MR (fg, opts);
63 #endif
64 #ifdef DAI_WITH_GIBBS
65 if( name == Gibbs::Name )
66 return new Gibbs (fg, opts);
67 #endif
68 #ifdef DAI_WITH_CBP
69 if( name == CBP::Name )
70 return new CBP (fg, opts);
71 #endif
72 #ifdef DAI_WITH_DECMAP
73 if( name == DecMAP::Name )
74 return new DecMAP (fg, opts);
75 #endif
76 DAI_THROWE(UNKNOWN_DAI_ALGORITHM,"Unknown libDAI algorithm: " + name);
77 }
78
79
80 InfAlg *newInfAlgFromString( const std::string &nameOpts, const FactorGraph &fg ) {
81 pair<string,PropertySet> no = parseNameProperties( nameOpts );
82 return newInfAlg( no.first, fg, no.second );
83 }
84
85
86 InfAlg *newInfAlgFromString( const std::string &nameOpts, const FactorGraph &fg, const std::map<std::string,std::string> &aliases ) {
87 pair<string,PropertySet> no = parseNameProperties( nameOpts, aliases );
88 return newInfAlg( no.first, fg, no.second );
89 }
90
91
92 std::pair<std::string, PropertySet> parseNameProperties( const std::string &s ) {
93 string::size_type pos = s.find_first_of('[');
94 string name;
95 PropertySet opts;
96 if( pos == string::npos ) {
97 name = s;
98 } else {
99 name = s.substr(0,pos);
100
101 stringstream ss;
102 ss << s.substr(pos,s.length());
103 ss >> opts;
104 }
105 return make_pair(name,opts);
106 }
107
108
109 std::pair<std::string, PropertySet> parseNameProperties( const std::string &s, const std::map<std::string,std::string> &aliases ) {
110 // break string into method[properties]
111 pair<string,PropertySet> ps = parseNameProperties(s);
112 bool looped = false;
113
114 // as long as 'method' is an alias, update:
115 while( aliases.find(ps.first) != aliases.end() && !looped ) {
116 string astr = aliases.find(ps.first)->second;
117 pair<string,PropertySet> aps = parseNameProperties(astr);
118 if( aps.first == ps.first )
119 looped = true;
120 // override aps properties by ps properties
121 aps.second.set( ps.second );
122 // replace ps by aps
123 ps = aps;
124 // repeat until method name == alias name ('looped'), or
125 // there is no longer an alias 'method'
126 }
127
128 return ps;
129 }
130
131
132 std::map<std::string,std::string> readAliasesFile( const std::string &filename ) {
133 // Read aliases
134 map<string,string> result;
135 ifstream infile;
136 infile.open( filename.c_str() );
137 if( infile.is_open() ) {
138 while( true ) {
139 string line;
140 getline( infile,line );
141 if( infile.fail() )
142 break;
143 if( (!line.empty()) && (line[0] != '#') ) {
144 string::size_type pos = line.find(':',0);
145 if( pos == string::npos )
146 DAI_THROWE(INVALID_ALIAS,"Invalid alias '" + line + "'");
147 else {
148 string::size_type posl = line.substr(0, pos).find_last_not_of(" \t");
149 string key = line.substr(0, posl + 1);
150 string::size_type posr = line.substr(pos + 1, line.length()).find_first_not_of(" \t");
151 string val = line.substr(pos + 1 + posr, line.length());
152 result[key] = val;
153 }
154 }
155 }
156 infile.close();
157 } else
158 DAI_THROWE(CANNOT_READ_FILE,"Error opening aliases file " + filename);
159 return result;
160 }
161
162
163 } // end of namespace dai