aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
PRMSlotChain_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 Inline implementation of gum::PRMSlotChain<GUM_SCALAR>
25  *
26  * @author Lionel TORTI and Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 #include <agrum/PRM/elements/PRMSlotChain.h>
29 
30 namespace gum {
31  namespace prm {
32 
33  template < typename GUM_SCALAR >
34  PRMSlotChain< GUM_SCALAR >::PRMSlotChain(
35  const std::string& name,
36  const Sequence< PRMClassElement< GUM_SCALAR >* >& chain) :
37  PRMClassElement< GUM_SCALAR >(name),
38  _chain_(new Sequence< PRMClassElement< GUM_SCALAR >* >(chain)), _isMultiple_(false) {
39  GUM_CONSTRUCTOR(PRMSlotChain);
40 
41  if (_chain_->size() < 2) {
42  GUM_ERROR(OperationNotAllowed, "chain must contain at least two ClassElement")
43  }
44 
45  for (Size i = 0; i < _chain_->size() - 1; ++i) {
46  if (_chain_->atPos(i)->elt_type() != PRMClassElement< GUM_SCALAR >::prm_refslot) {
47  GUM_ERROR(WrongClassElement, "illegal ClassElement in chain")
48  } else {
49  _isMultiple_
50  = _isMultiple_
51  || static_cast< PRMReferenceSlot< GUM_SCALAR >* >(_chain_->atPos(i))->isArray();
52  }
53  }
54 
55  _copyLastElt_();
56 
57  this->safeName_
58  = PRMObject::LEFT_CAST() + lastElt().type().name() + PRMObject::RIGHT_CAST() + name;
59  this->lastElt().type().variable().setName(name);
60  }
61 
62  // Parameters are inverse to prevent unwanted constructors calls (it
63  // happened)
64  template < typename GUM_SCALAR >
66  const std::string& name) :
68  // No need to
69  // GUM_CONSTRUCTOR(PRMSlotChain);
70 
71  // if ( _chain_->size() < 2) {
72  // GUM_ERROR(OperationNotAllowed,
73  // "chain must containt at least two PRMClassElement")
74  //}
75 
76  // for (Size i = 0; i < _chain_->size() - 1; ++i) {
77  // if (not( _chain_->atPos(i)->elt_type() !=
78  // PRMClassElement<GUM_SCALAR>::prm_refslot)) {
79  // GUM_ERROR(WrongClassElement, "illegal PRMClassElement in chain")
80  // } else {
81  // _isMultiple_ =
82  // _isMultiple_ or
83  // static_cast<PRMReferenceSlot<GUM_SCALAR>
84  // *>( _chain_->atPos(i))->isArray();
85  // }
86  //}
87 
88  // __copyLastElt();
89  // this->safeName_ = PRMObject::LEFT_CAST() + lastElt().type().name() +
90  // PRMObject::RIGHT_CAST() + name;
91  }
92 
93  template < typename GUM_SCALAR >
95  PRMClassElement< GUM_SCALAR >* new_elt = nullptr;
96 
97  switch (_chain_->back()->elt_type()) {
99  auto old_attr = static_cast< const PRMAttribute< GUM_SCALAR >* >(_chain_->back());
100 
102  for (auto var: old_attr->cpf().variablesSequence()) {
103  if (var != &(old_attr->type().variable())) { bij.insert(var, var); }
104  }
105 
106  new_elt = old_attr->copy(bij);
107  break;
108  }
109 
111  const PRMAggregate< GUM_SCALAR >* c_agg
112  = static_cast< const PRMAggregate< GUM_SCALAR >* >(_chain_->back());
114  c_agg->agg_type(),
115  c_agg->type(),
116  c_agg->id());
117  new_elt = agg;
118  break;
119  }
120 
121  default: {
122  GUM_ERROR(WrongClassElement, "last element of chain is not valid")
123  }
124  }
125 
126  new_elt->setId(_chain_->back()->id());
127  _chain_->setAtPos(_chain_->size() - 1, new_elt);
128  }
129 
130  template < typename GUM_SCALAR >
133  delete _chain_->back();
134  delete _chain_;
135  }
136 
137  template < typename GUM_SCALAR >
143  _copyLastElt_();
144  }
145 
146  template < typename GUM_SCALAR >
149  GUM_ERROR(FatalError, "Illegal call to gum::SlotChain<GUM_SCALAR> copy constructor.")
150  }
151 
152  template < typename GUM_SCALAR >
155  return this->prm_slotchain;
156  }
157 
158  template < typename GUM_SCALAR >
160  return _chain_->back()->type();
161  }
162 
163  template < typename GUM_SCALAR >
165  return _chain_->back()->type();
166  }
167 
168  template < typename GUM_SCALAR >
170  return _chain_->back()->cpf();
171  }
172 
173  template < typename GUM_SCALAR >
175  return static_cast< PRMReferenceSlot< GUM_SCALAR >* >(_chain_->atPos(_chain_->size() - 2))
176  ->slotType();
177  }
178 
179  template < typename GUM_SCALAR >
181  return static_cast< PRMReferenceSlot< GUM_SCALAR >* >(_chain_->atPos(_chain_->size() - 2))
182  ->slotType();
183  }
184 
185  template < typename GUM_SCALAR >
187  return *(_chain_->back());
188  }
189 
190  template < typename GUM_SCALAR >
192  return *(_chain_->back());
193  }
194 
195  template < typename GUM_SCALAR >
197  return *_chain_;
198  }
199 
200  template < typename GUM_SCALAR >
203  return *_chain_;
204  }
205 
206  template < typename GUM_SCALAR >
208 
209  template < typename GUM_SCALAR >
211 
212  template < typename GUM_SCALAR >
214  return _isMultiple_;
215  }
216 
217  template < typename GUM_SCALAR >
219  GUM_ERROR(OperationNotAllowed, "This is a slotchain")
220  }
221 
222  } /* namespace prm */
223 } /* namespace gum */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
ParamScopeData(const std::string &s, const PRMReferenceSlot< GUM_SCALAR > &ref, Idx d)