aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
O3SystemFactory_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * 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 /**
23  * @file
24  * @brief Implementation for the O3SystemFactory class.
25  *
26  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
27  * @author Lionel TORTI
28  */
29 
30 #include <agrum/PRM/o3prm/O3SystemFactory.h>
31 
32 namespace gum {
33  namespace prm {
34  namespace o3prm {
35 
36  template < typename GUM_SCALAR >
37  INLINE O3SystemFactory< GUM_SCALAR >::O3SystemFactory(PRM< GUM_SCALAR >& prm,
38  O3PRM& o3_prm,
41  _prm_(&prm),
44  }
45 
46  template < typename GUM_SCALAR >
47  INLINE
49  _prm_(src._prm_),
53  }
54 
55  template < typename GUM_SCALAR >
61  }
62 
63  template < typename GUM_SCALAR >
66  }
67 
68  template < typename GUM_SCALAR >
71  if (this == &src) { return *this; }
72  _prm_ = src._prm_;
76  return *this;
77  }
78 
79  template < typename GUM_SCALAR >
82  if (this == &src) { return *this; }
83  _prm_ = std::move(src._prm_);
87  return *this;
88  }
89 
90  template < typename GUM_SCALAR >
93 
94  for (auto& sys: _o3_prm_->systems()) {
95  // Reseting name map for each system
97 
98  if (_checkSystem_(*sys)) {
100 
104 
105  try {
106  factory.endSystem();
108  }
109  }
110  }
111 
112  template < typename GUM_SCALAR >
114  O3System& sys) {
115  for (auto& i: sys.instances()) {
116  if (i.parameters().size() > 0) {
117  auto params = HashTable< std::string, double >();
118  for (auto& p: i.parameters()) {
119  params.insert(p.name().label(), (double)p.value().value());
120  }
122 
123  } else {
124  if (i.size().value() > 1) {
125  factory.addArray(i.type().label(), i.name().label(), i.size().value());
126  } else {
128  }
129  }
130  }
131  }
132 
133  template < typename GUM_SCALAR >
135  O3System& sys) {
136  const auto& real_sys = _prm_->getSystem(sys.name().label());
137 
138  for (auto& ass: sys.assignments()) {
139  auto leftInstance = ass.leftInstance().label();
142 
143  if (ass.leftIndex().value() > -1 && real_sys.isArray(leftInstance)) {
145  sBuff << leftInstance << "[" << ass.leftIndex().value() << "]";
146  leftInstance = sBuff.str();
147  }
148 
149  if (ass.rightIndex().value() > -1 && real_sys.isArray(rightInstance)) {
151  sBuff << rightInstance << "[" << ass.rightIndex().value() << "]";
152  rightInstance = sBuff.str();
153  }
154 
156  }
157  }
158 
159  template < typename GUM_SCALAR >
161  O3System& sys) {
162  const auto& real_sys = _prm_->getSystem(sys.name().label());
163  for (auto& inc: sys.increments()) {
164  auto leftInstance = inc.leftInstance().label();
167 
168  if (inc.leftIndex().value() > -1 && real_sys.isArray(leftInstance)) {
170  sBuff << leftInstance << "[" << inc.leftIndex().value() << "]";
171  leftInstance = sBuff.str();
172  }
173 
174  if (inc.rightIndex().value() > -1 && real_sys.isArray(rightInstance)) {
176  sBuff << rightInstance << "[" << inc.rightIndex().value() << "]";
177  rightInstance = sBuff.str();
178  }
179 
181  }
182  }
183 
184  template < typename GUM_SCALAR >
187  return true;
188  }
189 
190  return false;
191  }
192 
193  template < typename GUM_SCALAR >
195  for (auto& i: sys.instances()) {
196  if (!_solver_->resolveClass(i.type())) { return false; }
197 
198  const auto& type = _prm_->getClass(i.type().label());
199  if (type.parameters().size() > 0) {
200  if (!_checkParameters_(type, i)) { return false; }
201  }
202 
203  if (_nameMap_.exists(i.name().label())) {
205  return false;
206  }
207 
208  _nameMap_.insert(i.name().label(), &i);
209  }
210 
211  return true;
212  }
213 
214  template < typename GUM_SCALAR >
215  INLINE bool
217  const O3Instance& inst) {
218  for (const auto& param: inst.parameters()) {
219  if (!type.exists(param.name().label())) {
221  return false;
222  }
223 
226  return false;
227  }
228 
229  const auto& type_param
230  = static_cast< const PRMParameter< GUM_SCALAR >& >(type.get(param.name().label()));
231 
232  switch (type_param.valueType()) {
234  if (!param.isInteger()) {
236  return false;
237  }
238  break;
239  }
240 
242  if (param.isInteger()) {
244  return false;
245  }
246  break;
247  }
248 
249  default: {
250  GUM_ERROR(FatalError, "unknown parameter type")
251  }
252  }
253  }
254  return true;
255  }
256 
257  template < typename GUM_SCALAR >
259  for (auto& ass: sys.assignments()) {
260  // if ( ass.leftInstance().label() == ass.leftReference().label() ) {
261  // O3PRM_SYSTEM_INVALID_LEFT_VALUE( ass.leftInstance(), * _errors_ );
262  // return false;
263  //}
264 
265  if (!_nameMap_.exists(ass.leftInstance().label())) {
267  return false;
268  }
269 
270  auto i = _nameMap_[ass.leftInstance().label()];
271  const auto& type = _prm_->getClass(i->type().label());
272  const auto& ref = ass.leftReference().label();
273 
274  if (!(type.exists(ass.leftReference().label())
277  return false;
278  }
279 
280  const auto& real_ref
281  = static_cast< const PRMReferenceSlot< GUM_SCALAR >& >(type.get(ref));
282 
283  if (!_nameMap_.exists(ass.rightInstance().label())) {
285  return false;
286  }
287 
288  if (real_ref.isArray() && _nameMap_[ass.rightInstance().label()]->size().value() == 0) {
290  return false;
291  }
292 
293  if ((!real_ref.isArray()) && _nameMap_[ass.rightInstance().label()]->size().value() > 0
294  && ass.rightIndex().value() == -1) {
296  return false;
297  }
298  }
299  return true;
300  }
301 
302  template < typename GUM_SCALAR >
304  for (auto& inc: sys.increments()) {
305  // if ( inc.leftInstance().label() == inc.leftReference().label() ) {
306  // O3PRM_SYSTEM_INVALID_LEFT_VALUE( inc.leftInstance(), * _errors_ );
307  // return false;
308  //}
309 
310  if (!_nameMap_.exists(inc.leftInstance().label())) {
312  return false;
313  }
314 
315  auto i = _nameMap_[inc.leftInstance().label()];
316  const auto& type = _prm_->getClass(i->type().label());
317  const auto& ref = inc.leftReference().label();
318 
319  if (!(type.exists(inc.leftReference().label())
322  return false;
323  }
324 
325  const auto& real_ref
326  = static_cast< const PRMReferenceSlot< GUM_SCALAR >& >(type.get(ref));
327 
328  if (!real_ref.isArray()) {
330  return false;
331  }
332  }
333 
334  return true;
335  }
336  } // namespace o3prm
337  } // namespace prm
338 } // namespace gum
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
ParamScopeData(const std::string &s, const PRMReferenceSlot< GUM_SCALAR > &ref, Idx d)