aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
UAIMNReader_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by 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, const std::string& filename) :
30  _mn_ = MN;
31  _mn_->clear();
32 
34  _parseDone_ = false;
35 
36  _ioerror_ = false;
37 
38  try {
41  } catch (IOError&) { _ioerror_ = true; }
42  }
43 
44  template < typename GUM_SCALAR >
47 
48  if (!_ioerror_) {
49  // this could lead to memory leak !!
50  if (_parser_) delete (_parser_);
51 
52  if (_scanner_) delete (_scanner_);
53  }
54  }
55 
56  template < typename GUM_SCALAR >
58  if (_ioerror_) { GUM_ERROR(gum::IOError, "No such file " + streamName()) }
59 
60  return *_scanner_;
61  }
62 
63  template < typename GUM_SCALAR >
64  INLINE const std::string& UAIMNReader< GUM_SCALAR >::streamName() const {
65  return _streamName_;
66  }
67 
68  template < typename GUM_SCALAR >
69  INLINE bool UAIMNReader< GUM_SCALAR >::trace() const {
70  return _traceScanning_;
71  }
72 
73  template < typename GUM_SCALAR >
74  INLINE void UAIMNReader< GUM_SCALAR >::trace(bool b) {
76  scanner().setTrace(b);
77  }
78 
79  template < typename GUM_SCALAR >
81  if (_ioerror_) { GUM_ERROR(gum::IOError, "No such file " + streamName()) }
82 
83  if (!_parseDone_) {
84  try {
85  _parser_->Parse();
86  _parseDone_ = true;
88  } catch (gum::Exception& e) {
90  return 1 + _parser_->errors().error_count;
91  }
92  }
93 
94  return (_parser_->errors().error_count);
95  }
96 
97  template < typename GUM_SCALAR >
99  std::vector< std::tuple< float, int, int, int > > quartets) {
100  Idx current;
101  Size max = quartets.size();
102  if (max == 0) {
103  _addWarning_(1, 1, "Empty MarkovNet");
104  return;
105  }
106 
107  auto isInt = [&]() -> bool {
108  return (std::get< 0 >(quartets[current]) == -1);
109  };
110  auto lig = [&]() -> int {
111  return std::get< 2 >(quartets[current]);
112  };
113  auto col = [&]() -> int {
114  return std::get< 3 >(quartets[current]);
115  };
116 
117  auto getInt = [&]() -> int {
118  if (!isInt()) this->_addFatalError_(lig(), col(), "int expected");
119  return std::get< 1 >(quartets[current]);
120  };
121  auto getVal = [&]() -> GUM_SCALAR {
122  return (isInt()) ? (std::get< 1 >(quartets[current])) : (std::get< 0 >(quartets[current]));
123  };
124  auto incCurrent = [&]() {
125  current += 1;
126  if (current >= max) this->_addFatalError_(lig(), col(), "Not enough data in UAI file");
127  };
128 
129  current = 0;
130  Size nbrNode = (Size)getInt();
131 
132  for (NodeId i = 0; i < nbrNode; i++) {
133  incCurrent();
134  int mod = getInt();
135  if (mod < 2) _addError_(lig(), col(), "Number of modalities should be greater than 2.");
137  }
138 
139  incCurrent();
140  Size nbrFactors = (Size)getInt();
141 
142  std::vector< NodeSet > clicks;
143  for (NodeId i = 0; i < nbrFactors; i++) {
144  incCurrent();
145  Size nbrVar = (Size)getInt();
146  if (nbrVar == 0) _addError_(lig(), col(), "0 is not possible here");
147 
148  NodeSet vars;
149  for (NodeId j = 0; j < nbrVar; j++) {
150  incCurrent();
151  NodeId nod = (NodeId)getInt();
152  if (nod >= nbrNode) _addError_(lig(), col(), "Not enough variables in the MarkovNet");
153  vars.insert(nod);
154  }
155  _mn_->addFactor(vars);
157  }
158 
159  for (NodeId i = 0; i < nbrFactors; i++) {
160  incCurrent();
161  Size nbrParam = (Size)getInt();
162  if (nbrParam != _mn_->factor(clicks[i]).domainSize()) {
163  _addFatalError_(lig(), col(), "Size does not fit between clique and parameters");
164  }
165  std::vector< GUM_SCALAR > v;
166  for (Idx j = 0; j < nbrParam; j++) {
167  incCurrent();
168  v.push_back(getVal());
169  }
171  v.clear();
172  }
173 
174  if (current != max - 1) _addError_(lig(), col(), "Too many data in this file");
175  }
176 
177  // @{
178  // publishing Errors API
179  template < typename GUM_SCALAR >
181  if (_parseDone_)
182  return _parser_->errors().error(i).line;
183  else {
184  GUM_ERROR(OperationNotAllowed, "UAI file not parsed yet")
185  }
186  }
187 
188  template < typename GUM_SCALAR >
190  if (_parseDone_)
191  return _parser_->errors().error(i).column;
192  else {
193  GUM_ERROR(OperationNotAllowed, "UAI file not parsed yet")
194  }
195  }
196 
197  template < typename GUM_SCALAR >
199  if (_parseDone_)
200  return _parser_->errors().error(i).is_error;
201  else {
202  GUM_ERROR(OperationNotAllowed, "UAI file not parsed yet")
203  }
204  }
205 
206  template < typename GUM_SCALAR >
208  if (_parseDone_)
209  return _parser_->errors().error(i).msg;
210  else {
211  GUM_ERROR(OperationNotAllowed, "UAI file not parsed yet")
212  }
213  }
214 
215  template < typename GUM_SCALAR >
217  if (_parseDone_)
219  else {
220  GUM_ERROR(OperationNotAllowed, "UAI file not parsed yet")
221  }
222  }
223 
224  template < typename GUM_SCALAR >
226  if (_parseDone_)
228  else {
229  GUM_ERROR(OperationNotAllowed, "UAI file not parsed yet")
230  }
231  }
232 
233  template < typename GUM_SCALAR >
235  if (_parseDone_)
237  else {
238  GUM_ERROR(OperationNotAllowed, "UAI file not parsed yet")
239  }
240  }
241 
242  template < typename GUM_SCALAR >
244  if (_parseDone_)
246  else {
247  GUM_ERROR(OperationNotAllowed, "UAI file not parsed yet")
248  }
249  }
250 
251  template < typename GUM_SCALAR >
253  return (!_parseDone_) ? (Size)0 : _parser_->errors().error_count;
254  }
255 
256  template < typename GUM_SCALAR >
258  return (!_parseDone_) ? (Size)0 : _parser_->errors().warning_count;
259  }
260 
261  template < typename GUM_SCALAR >
265  }
266 
267  template < typename GUM_SCALAR >
270  }
271 
272  template < typename GUM_SCALAR >
275  }
276 
277  // @}
278 } // namespace gum
279 
280 #endif // DOXYGEN_SHOULD_SKIP_THIS
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643