aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
PRMSlotChain_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 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)),
39  isMultiple__(false) {
40  GUM_CONSTRUCTOR(PRMSlotChain);
41 
42  if (chain__->size() < 2) {
43  GUM_ERROR(OperationNotAllowed,
44  "chain must contain at least two ClassElement");
45  }
46 
47  for (Size i = 0; i < chain__->size() - 1; ++i) {
48  if (chain__->atPos(i)->elt_type()
49  != PRMClassElement< GUM_SCALAR >::prm_refslot) {
50  GUM_ERROR(WrongClassElement, "illegal ClassElement in chain");
51  } else {
52  isMultiple__
53  = isMultiple__
54  || static_cast< PRMReferenceSlot< GUM_SCALAR >* >(chain__->atPos(i))
55  ->isArray();
56  }
57  }
58 
59  copyLastElt__();
60 
61  this->safeName_ = PRMObject::LEFT_CAST() + lastElt().type().name()
62  + PRMObject::RIGHT_CAST() + name;
63  this->lastElt().type().variable().setName(name);
64  }
65 
66  // Parameters are inverse to prevent unwanted constructors calls (it
67  // happened)
68  template < typename GUM_SCALAR >
71  const std::string& name) :
73  // No need to
74  // GUM_CONSTRUCTOR(PRMSlotChain);
75 
76  // if (chain__->size() < 2) {
77  // GUM_ERROR(OperationNotAllowed,
78  // "chain must containt at least two PRMClassElement");
79  //}
80 
81  // for (Size i = 0; i < chain__->size() - 1; ++i) {
82  // if (not(chain__->atPos(i)->elt_type() !=
83  // PRMClassElement<GUM_SCALAR>::prm_refslot)) {
84  // GUM_ERROR(WrongClassElement, "illegal PRMClassElement in chain");
85  // } else {
86  // isMultiple__ =
87  // isMultiple__ or
88  // static_cast<PRMReferenceSlot<GUM_SCALAR>
89  // *>(chain__->atPos(i))->isArray();
90  // }
91  //}
92 
93  //__copyLastElt();
94  // this->safeName_ = PRMObject::LEFT_CAST() + lastElt().type().name() +
95  // PRMObject::RIGHT_CAST() + name;
96  }
97 
98  template < typename GUM_SCALAR >
100  PRMClassElement< GUM_SCALAR >* new_elt = nullptr;
101 
102  switch (chain__->back()->elt_type()) {
104  auto old_attr
105  = static_cast< const PRMAttribute< GUM_SCALAR >* >(chain__->back());
106 
108  for (auto var: old_attr->cpf().variablesSequence()) {
109  if (var != &(old_attr->type().variable())) { bij.insert(var, var); }
110  }
111 
112  new_elt = old_attr->copy(bij);
113  break;
114  }
115 
117  const PRMAggregate< GUM_SCALAR >* c_agg
118  = static_cast< const PRMAggregate< GUM_SCALAR >* >(chain__->back());
120  = new PRMAggregate< GUM_SCALAR >(c_agg->name(),
121  c_agg->agg_type(),
122  c_agg->type(),
123  c_agg->id());
124  new_elt = agg;
125  break;
126  }
127 
128  default: {
129  GUM_ERROR(WrongClassElement, "last element of chain is not valid");
130  }
131  }
132 
133  new_elt->setId(chain__->back()->id());
134  chain__->setAtPos(chain__->size() - 1, new_elt);
135  }
136 
137  template < typename GUM_SCALAR >
140  delete chain__->back();
141  delete chain__;
142  }
143 
144  template < typename GUM_SCALAR >
146  const PRMSlotChain< GUM_SCALAR >& source) :
151  copyLastElt__();
152  }
153 
154  template < typename GUM_SCALAR >
156  const PRMSlotChain< GUM_SCALAR >& source) {
158  "Illegal call to gum::SlotChain<GUM_SCALAR> copy constructor.");
159  }
160 
161  template < typename GUM_SCALAR >
164  return this->prm_slotchain;
165  }
166 
167  template < typename GUM_SCALAR >
169  return chain__->back()->type();
170  }
171 
172  template < typename GUM_SCALAR >
174  return chain__->back()->type();
175  }
176 
177  template < typename GUM_SCALAR >
179  return chain__->back()->cpf();
180  }
181 
182  template < typename GUM_SCALAR >
185  return static_cast< PRMReferenceSlot< GUM_SCALAR >* >(
186  chain__->atPos(chain__->size() - 2))
187  ->slotType();
188  }
189 
190  template < typename GUM_SCALAR >
193  return static_cast< PRMReferenceSlot< GUM_SCALAR >* >(
194  chain__->atPos(chain__->size() - 2))
195  ->slotType();
196  }
197 
198  template < typename GUM_SCALAR >
200  return *(chain__->back());
201  }
202 
203  template < typename GUM_SCALAR >
206  return *(chain__->back());
207  }
208 
209  template < typename GUM_SCALAR >
212  return *chain__;
213  }
214 
215  template < typename GUM_SCALAR >
218  return *chain__;
219  }
220 
221  template < typename GUM_SCALAR >
223  const PRMClassElement< GUM_SCALAR >& elt) {}
224 
225  template < typename GUM_SCALAR >
227  const PRMClassElement< GUM_SCALAR >& elt) {}
228 
229  template < typename GUM_SCALAR >
231  return isMultiple__;
232  }
233 
234  template < typename GUM_SCALAR >
237  GUM_ERROR(OperationNotAllowed, "This is a slotchain");
238  }
239 
240  } /* namespace prm */
241 } /* namespace gum */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
ParamScopeData(const std::string &s, const PRMReferenceSlot< GUM_SCALAR > &ref, Idx d)