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