aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
UAIMNReader_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 Pierre-Henri WUILLEMIN(@LIP6) et Christophe GONZALES(@AMU)
4  * (@AMU) info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library 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 Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 #ifndef DOXYGEN_SHOULD_SKIP_THIS
23 
24 namespace gum {
25 
26  template < typename GUM_SCALAR >
27  UAIMNReader< GUM_SCALAR >::UAIMNReader(MarkovNet< GUM_SCALAR >* MN,
28  const std::string& filename) :
31  mn__ = MN;
32  mn__->clear();
33 
35  parseDone__ = false;
36 
37  ioerror__ = false;
38 
39  try {
42  } catch (IOError&) { ioerror__ = true; }
43  }
44 
45  template < typename GUM_SCALAR >
48 
49  if (!ioerror__) {
50  // this could lead to memory leak !!
51  if (parser__) delete (parser__);
52 
53  if (scanner__) delete (scanner__);
54  }
55  }
56 
57  template < typename GUM_SCALAR >
59  if (ioerror__) { GUM_ERROR(gum::IOError, "No such file " + streamName()); }
60 
61  return *scanner__;
62  }
63 
64  template < typename GUM_SCALAR >
65  INLINE const std::string& UAIMNReader< GUM_SCALAR >::streamName() const {
66  return streamName__;
67  }
68 
69  template < typename GUM_SCALAR >
70  INLINE bool UAIMNReader< GUM_SCALAR >::trace() const {
71  return traceScanning__;
72  }
73 
74  template < typename GUM_SCALAR >
75  INLINE void UAIMNReader< GUM_SCALAR >::trace(bool b) {
77  scanner().setTrace(b);
78  }
79 
80  template < typename GUM_SCALAR >
82  if (ioerror__) { GUM_ERROR(gum::IOError, "No such file " + streamName()); }
83 
84  if (!parseDone__) {
85  try {
86  parser__->Parse();
87  parseDone__ = true;
89  } catch (gum::Exception& e) {
91  return 1 + parser__->errors().error_count;
92  }
93  }
94 
95  return (parser__->errors().error_count);
96  }
97 
98  template < typename GUM_SCALAR >
100  std::vector< std::tuple< float, int, int, int > > quartets) {
101  Idx current;
102  Size max = quartets.size();
103  if (max == 0) {
104  addWarning__(1, 1, "Empty MarkovNet");
105  return;
106  }
107 
108  auto isInt = [&]() -> bool {
109  return (std::get< 0 >(quartets[current]) == -1);
110  };
111  auto lig = [&]() -> int {
112  return std::get< 2 >(quartets[current]);
113  };
114  auto col = [&]() -> int {
115  return std::get< 3 >(quartets[current]);
116  };
117 
118  auto getInt = [&]() -> int {
119  if (!isInt()) this->addFatalError__(lig(), col(), "int expected");
120  return std::get< 1 >(quartets[current]);
121  };
122  auto getVal = [&]() -> GUM_SCALAR {
123  return (isInt()) ? (std::get< 1 >(quartets[current]))
124  : (std::get< 0 >(quartets[current]));
125  };
126  auto incCurrent = [&]() {
127  current += 1;
128  if (current >= max)
129  this->addFatalError__(lig(), col(), "Not enough data in UAI file");
130  };
131 
132  current = 0;
133  Size nbrNode = (Size)getInt();
134 
135  for (NodeId i = 0; i < nbrNode; i++) {
136  incCurrent();
137  int mod = getInt();
138  if (mod < 2)
139  addError__(lig(), col(), "Number of modalities should be greater than 2.");
141  }
142 
143  incCurrent();
144  Size nbrFactors = (Size)getInt();
145 
146  std::vector< NodeSet > clicks;
147  for (NodeId i = 0; i < nbrFactors; i++) {
148  incCurrent();
149  Size nbrVar = (Size)getInt();
150  if (nbrVar == 0) addError__(lig(), col(), "0 is not possible here");
151 
152  NodeSet vars;
153  for (NodeId j = 0; j < nbrVar; j++) {
154  incCurrent();
155  NodeId nod = (NodeId)getInt();
156  if (nod >= nbrNode)
157  addError__(lig(), col(), "Not enough variables in the MarkovNet");
158  vars.insert(nod);
159  }
160  mn__->addFactor(vars);
162  }
163 
164  for (NodeId i = 0; i < nbrFactors; i++) {
165  incCurrent();
166  Size nbrParam = (Size)getInt();
167  if (nbrParam != mn__->factor(clicks[i]).domainSize()) {
169  col(),
170  "Size does not fit between clique and parameters");
171  }
172  std::vector< GUM_SCALAR > v;
173  for (Idx j = 0; j < nbrParam; j++) {
174  incCurrent();
175  v.push_back(getVal());
176  }
178  v.clear();
179  }
180 
181  if (current != max - 1) addError__(lig(), col(), "Too many data in this file");
182  }
183 
184  // @{
185  // publishing Errors API
186  template < typename GUM_SCALAR >
188  if (parseDone__)
189  return parser__->errors().error(i).line;
190  else {
191  GUM_ERROR(OperationNotAllowed, "UAI file not parsed yet");
192  }
193  }
194 
195  template < typename GUM_SCALAR >
197  if (parseDone__)
198  return parser__->errors().error(i).column;
199  else {
200  GUM_ERROR(OperationNotAllowed, "UAI file not parsed yet");
201  }
202  }
203 
204  template < typename GUM_SCALAR >
206  if (parseDone__)
207  return parser__->errors().error(i).is_error;
208  else {
209  GUM_ERROR(OperationNotAllowed, "UAI file not parsed yet");
210  }
211  }
212 
213  template < typename GUM_SCALAR >
215  if (parseDone__)
216  return parser__->errors().error(i).msg;
217  else {
218  GUM_ERROR(OperationNotAllowed, "UAI file not parsed yet");
219  }
220  }
221 
222  template < typename GUM_SCALAR >
224  if (parseDone__)
226  else {
227  GUM_ERROR(OperationNotAllowed, "UAI file not parsed yet");
228  }
229  }
230 
231  template < typename GUM_SCALAR >
232  INLINE void
234  if (parseDone__)
236  else {
237  GUM_ERROR(OperationNotAllowed, "UAI file not parsed yet");
238  }
239  }
240 
241  template < typename GUM_SCALAR >
243  if (parseDone__)
245  else {
246  GUM_ERROR(OperationNotAllowed, "UAI file not parsed yet");
247  }
248  }
249 
250  template < typename GUM_SCALAR >
252  if (parseDone__)
254  else {
255  GUM_ERROR(OperationNotAllowed, "UAI file not parsed yet");
256  }
257  }
258 
259  template < typename GUM_SCALAR >
261  return (!parseDone__) ? (Size)0 : parser__->errors().error_count;
262  }
263 
264  template < typename GUM_SCALAR >
266  return (!parseDone__) ? (Size)0 : parser__->errors().warning_count;
267  }
268 
269  template < typename GUM_SCALAR >
271  Idx col,
272  const std::string& s) {
275  }
276 
277  template < typename GUM_SCALAR >
279  Idx col,
280  const std::string& s) {
282  }
283 
284  template < typename GUM_SCALAR >
286  Idx col,
287  const std::string& s) {
289  }
290 
291  // @}
292 } // namespace gum
293 
294 #endif // DOXYGEN_SHOULD_SKIP_THIS
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669