Fixed regression in SWIG interface (thanks to Benjamin Mumm for reporting this) and...
[libdai.git] / swig / dai.i
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
4 *
5 * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
6 */
7
8
9 %module dai
10
11 #include <fstream>
12
13 %include "std_string.i"
14 %include "std_vector.i"
15 %template(IntVector) std::vector<size_t>;
16 //%include "std_set.i" /* for python */
17 %include "../include/dai/dai_config.h"
18
19 %{
20 #include "../include/dai/alldai.h"
21
22 using namespace dai;
23 %}
24
25 // ************************************************************************************************
26 %include "../include/dai/util.h"
27
28 // ************************************************************************************************
29 %ignore dai::Var::label() const;
30 %ignore dai::Var::states() const;
31 %ignore operator<<(std::ostream&, const Var&);
32 %include "../include/dai/var.h"
33 %extend dai::Var {
34 inline const char* __str__() const { return (*self).toString().c_str(); } /* for python */
35 inline std::string __str() const { return (*self).toString(); } /* for octave */
36 };
37
38 // ************************************************************************************************
39 %ignore operator<<(std::ostream&, const SmallSet&);
40 %rename(__eq__) operator==(const SmallSet&, const SmallSet&); /* for python */
41 %rename(__ne__) operator!=(const SmallSet&, const SmallSet&); /* for python */
42 %rename(__lt__) operator<(const SmallSet&, const SmallSet&); /* for python */
43 %include "../include/dai/smallset.h"
44 %template(SmallSetVar) dai::SmallSet< dai::Var >;
45 %extend dai::SmallSet<dai::Var> {
46 inline const char* __str__() const { return (*self).toString().c_str(); } /* for python */
47 inline std::string __str() const { return (*self).toString(); } /* for octave */
48 }
49
50 // ************************************************************************************************
51 %ignore operator<<(std::ostream&, const VarSet&);
52 %include "../include/dai/varset.h"
53 %extend dai::VarSet {
54 inline void append(const dai::Var &v) { (*self) |= v; } /* for python, octave */
55 inline const char* __str__() const { return (*self).toString().c_str(); } /* for python */
56 inline std::string __str() const { return (*self).toString(); } /* for octave */
57 };
58
59 // ************************************************************************************************
60 %ignore dai::TProb::operator[];
61 %include "../include/dai/prob.h"
62 %template(Prob) dai::TProb<dai::Real>;
63 %extend dai::TProb<dai::Real> {
64 inline dai::Real __getitem__(int i) const {return (*self).get(i);} /* for python */
65 inline void __setitem__(int i,dai::Real d) {(*self).set(i,d);} /* for python */
66 inline dai::Real __paren__(int i) const {return (*self).get(i);} /* for octave */
67 inline void __paren_asgn__(int i,dai::Real d) {(*self).set(i,d);} /* for octave */
68 inline const char* __str__() const { return (*self).toString().c_str(); } /* for python */
69 inline std::string __str() const { return (*self).toString(); } /* for octave */
70 };
71
72 // ************************************************************************************************
73 %ignore dai::TFactor::operator[];
74 %include "../include/dai/factor.h"
75 %extend dai::TFactor<dai::Real> {
76 inline dai::Real __getitem__(int i) const {return (*self).get(i);} /* for python */
77 inline void __setitem__(int i,dai::Real d) {(*self).set(i,d);} /* for python */
78 inline dai::Real __paren__(int i) const {return (*self).get(i);} /* for octave */
79 inline void __paren_asgn__(int i,dai::Real d) {(*self).set(i,d);} /* for octave */
80 inline const char* __str__() const { return (*self).toString().c_str(); } /* for python */
81 inline std::string __str() const { return (*self).toString(); } /* for octave */
82 };
83 %template(Factor) dai::TFactor<dai::Real>;
84 %inline %{
85 typedef std::vector<dai::Factor> VecFactor;
86 typedef std::vector<VecFactor> VecVecFactor;
87 %}
88 %template(VecFactor) std::vector<dai::Factor>;
89 %template(VecVecFactor) std::vector<VecFactor>;
90
91 // ************************************************************************************************
92 %ignore operator<<(std::ostream&, const GraphAL&);
93 %rename(toInt) dai::Neighbor::operator size_t() const;
94 %include "../include/dai/graph.h"
95 %extend dai::GraphAL {
96 inline const char* __str__() const { return (*self).toString().c_str(); } /* for python */
97 inline std::string __str() const { return (*self).toString(); } /* for octave */
98 }
99
100 // ************************************************************************************************
101 %ignore operator<<(std::ostream&, const BipartiteGraph&);
102 %include "../include/dai/bipgraph.h"
103 %extend dai::BipartiteGraph {
104 inline const char* __str__() const { return (*self).toString().c_str(); } /* for python */
105 inline std::string __str() const { return (*self).toString(); } /* for octave */
106 }
107
108 // ************************************************************************************************
109 %ignore operator<<(std::ostream&, const FactorGraph&);
110 %ignore operator>>(std::istream&,FactorGraph&);
111 %include "../include/dai/factorgraph.h"
112 %extend dai::FactorGraph {
113 inline const char* __str__() const { return (*self).toString().c_str(); } /* for python */
114 inline std::string __str() const { return (*self).toString(); } /* for octave */
115 inline void printDot(const std::string& fname) const {
116 std::ofstream o(fname.c_str(), std::ofstream::out);
117 self->printDot(o);
118 o.close();
119 }
120 }
121
122 // ************************************************************************************************
123 %ignore operator<<(std::ostream&, const RegionGraph&);
124 %include "../include/dai/regiongraph.h"
125 %extend dai::RegionGraph {
126 inline const char* __str__() const { return (*self).toString().c_str(); } /* for python */
127 inline std::string __str() const { return (*self).toString(); } /* for octave */
128 }
129
130 // ************************************************************************************************
131 //%ignore operator<<(std::ostream&, const CobwebGraph&);
132 //%include "../include/dai/cobwebgraph.h"
133 //%extend dai::CobwebGraph {
134 // inline const char* __str__() const { return (*self).toString().c_str(); } /* for python */
135 // inline std::string __str() const { return (*self).toString(); } /* for octave */
136 //}
137 //TODO fix problems with CobwebGraph
138
139 // ************************************************************************************************
140 %ignore operator<<(std::ostream&, const PropertySet&);
141 %ignore operator>>(std::istream&,PropertySet&);
142 %include "../include/dai/properties.h"
143 %extend dai::PropertySet {
144 inline void __setitem__(char *name, char *val) {
145 self->set(std::string(name), std::string(val));
146 }
147 inline const char* __str__() const { return (*self).toString().c_str(); } /* for python */
148 inline std::string __str() const { return (*self).toString(); } /* for octave */
149 }
150
151 // ************************************************************************************************
152 %ignore dai::IndexFor::operator++;
153 %rename(toInt) dai::IndexFor::operator size_t() const;
154 %ignore dai::Permute::operator[];
155 %ignore dai::multifor::operator++;
156 %ignore dai::multifor::operator[];
157 %rename(toInt) dai::multifor::operator size_t() const;
158 %ignore dai::State::operator++;
159 %rename(toInt) dai::State::operator size_t() const;
160 %ignore dai::State::operator const std::map<Var,size_t>&;
161 %include "../include/dai/index.h"
162 %extend dai::IndexFor {
163 inline void next() { return (*self)++; }
164 };
165 %extend dai::Permute {
166 inline size_t __getitem__(int i) const {return (*self)[i];} /* for python */
167 inline size_t __paren__(int i) const {return (*self)[i];} /* for octave */
168 };
169 %extend dai::multifor {
170 inline void next() { return (*self)++; }
171 inline size_t __getitem__(int i) const {return (*self)[i];} /* for python */
172 inline size_t __paren__(int i) const {return (*self)[i];} /* for octave */
173 };
174 %extend dai::State {
175 inline void next() { return (*self)++; }
176 };
177
178 // ************************************************************************************************
179 %include "../include/dai/daialg.h"
180 //TODO: why do the following lines not work?
181 //%template(DAIAlgFG) dai::DAIAlg<dai::FactorGraph>;
182 //%template(DAIAlgRG) dai::DAIAlg<dai::RegionGraph>;
183 //%template(DAIAlgCG) dai::DAIAlg<dai::CobwebGraph>;
184
185 // ************************************************************************************************
186 %include "../include/dai/alldai.h"
187
188 // ************************************************************************************************
189 %ignore dai::BP::operator=;
190 %include "../include/dai/bp.h"
191
192 // ************************************************************************************************
193 %include "../include/dai/fbp.h"
194
195 // ************************************************************************************************
196 %include "../include/dai/trwbp.h"
197
198 // ************************************************************************************************
199 %include "../include/dai/mf.h"
200
201 // ************************************************************************************************
202 %include "../include/dai/hak.h"
203
204 // ************************************************************************************************
205 %include "../include/dai/lc.h"
206
207 // ************************************************************************************************
208 %include "../include/dai/jtree.h"
209
210 // ************************************************************************************************
211 %ignore dai::TreeEP::operator=;
212 %include "../include/dai/treeep.h"
213
214 // ************************************************************************************************
215 %include "../include/dai/mr.h"
216
217 // ************************************************************************************************
218 %include "../include/dai/gibbs.h"
219
220 // ************************************************************************************************
221 %include "../include/dai/cbp.h"
222
223 // ************************************************************************************************
224 %include "../include/dai/decmap.h"
225
226 // ************************************************************************************************
227 %include "../include/dai/glc.h"