aGrUM  0.16.0
PRMScalarAttribute_tpl.h
Go to the documentation of this file.
1 
30 #include <sstream>
31 
32 #include <agrum/PRM/utils_prm.h>
33 
36 
37 namespace gum {
38  namespace prm {
39 
40  template < typename GUM_SCALAR >
42  const std::string& name,
43  const PRMType& type,
45  PRMAttribute< GUM_SCALAR >(name),
46  __type(new PRMType(type)), __cpf(new Potential< GUM_SCALAR >(impl)) {
47  GUM_CONSTRUCTOR(PRMScalarAttribute);
48  __cpf->add(__type->variable());
49 
50  this->_safeName =
52  }
53 
54  template < typename GUM_SCALAR >
56  const PRMScalarAttribute< GUM_SCALAR >& source) :
57  PRMAttribute< GUM_SCALAR >(source),
58  __type(0), __cpf(0) {
59  GUM_CONS_CPY(PRMScalarAttribute);
61  "Illegal call to the copy constructor of gum::ScalarAttribute");
62  }
63 
64  template < typename GUM_SCALAR >
66  GUM_DESTRUCTOR(PRMScalarAttribute);
67  delete __cpf;
68  delete __type;
69  }
70 
71  template < typename GUM_SCALAR >
73  const PRMClass< GUM_SCALAR >& c) const {
74  auto impl = static_cast< MultiDimImplementation< GUM_SCALAR >* >(
75  this->cpf().content()->newFactory());
77  this->name(), this->type(), impl);
78  }
79 
80  template < typename GUM_SCALAR >
83  auto copy = new PRMScalarAttribute< GUM_SCALAR >(this->name(), this->type());
84 
85  if (!bij.existsFirst(&(type().variable()))) {
86  bij.insert(&(type().variable()), &(copy->type().variable()));
87  }
88 
89  delete copy->__cpf;
90  copy->__cpf = copyPotential(bij, cpf());
91 
92  return copy;
93  }
94 
95  template < typename GUM_SCALAR >
98  const PRMAttribute< GUM_SCALAR >& source) {
99  delete __cpf;
101 
102  for (auto var : source.cpf().variablesSequence()) {
103  __cpf->add(*(bij.second(var)));
104  }
105 
106  Instantiation inst(*__cpf), jnst(source.cpf());
107 
108  for (inst.setFirst(), jnst.setFirst(); !(inst.end() || jnst.end());
109  inst.inc(), jnst.inc()) {
110  __cpf->set(inst, source.cpf().get(jnst));
111  }
112 
113  GUM_ASSERT(inst.end() && jnst.end());
114  GUM_ASSERT(__cpf->contains(__type->variable()));
115  GUM_ASSERT(!__cpf->contains(source.type().variable()));
116  }
117 
118  template < typename GUM_SCALAR >
122  "Illegal call to the copy operator of gum::ScalarAttribute");
123  }
124 
125  template < typename GUM_SCALAR >
128  return this->prm_attribute;
129  }
130 
131  template < typename GUM_SCALAR >
133  return *__type;
134  }
135 
136  template < typename GUM_SCALAR >
138  return *__type;
139  }
140 
141  template < typename GUM_SCALAR >
142  INLINE const Potential< GUM_SCALAR >&
144  return *__cpf;
145  }
146 
147  template < typename GUM_SCALAR >
149  const PRMClassElement< GUM_SCALAR >& elt) {
150  try {
151  __cpf->add(elt.type().variable());
152  } catch (DuplicateElement&) {
154  elt.name() << " as parent of " << this->name());
155  } catch (OperationNotAllowed&) {
157  elt.name() << " of wrong type as parent of " << this->name());
158  }
159  }
160 
161  // See gum::PRMClassElement<GUM_SCALAR>::_addChild().
162  template < typename GUM_SCALAR >
164  const PRMClassElement< GUM_SCALAR >& elt) {}
165 
166  template < typename GUM_SCALAR >
170 
171  try {
172  cast =
174  } catch (NotFound&) {
176  "this ScalarAttribute can not have cast descendant");
177  }
178 
179  cast->addParent(*this);
180  const DiscreteVariable& my_var = type().variable();
181  DiscreteVariable& cast_var = cast->type().variable();
182  Instantiation inst(cast->cpf());
183 
184  for (inst.setFirst(); !inst.end(); inst.inc()) {
185  if (type().label_map()[inst.val(my_var)] == inst.val(cast_var)) {
186  cast->cpf().set(inst, 1);
187  } else {
188  cast->cpf().set(inst, 0);
189  }
190  }
191 
192  return cast;
193  }
194 
195  template < typename GUM_SCALAR >
198  try {
199  type().setSuper(cast->type());
200  } catch (OperationNotAllowed&) {
202  "this ScalarAttribute can not have cast descendant");
203  } catch (WrongType&) {
204  std::stringstream msg;
205  msg << type().name() << " is not a subtype of " << cast->type().name();
206  GUM_ERROR(WrongType, msg.str());
207  }
208  cast->becomeCastDescendant(type());
209  }
210 
211  template < typename GUM_SCALAR >
213  delete __cpf;
215  __cpf->add(type().variable());
216  __cpf->add(subtype.variable());
217 
218  Instantiation inst(*__cpf);
219 
220  for (inst.setFirst(); !inst.end(); inst.inc()) {
221  auto my_pos = inst.pos(subtype.variable());
222  if (subtype.label_map()[my_pos] == inst.pos(type().variable())) {
223  __cpf->set(inst, 1);
224  } else {
225  __cpf->set(inst, 0);
226  }
227  }
228  }
229 
230  template < typename GUM_SCALAR >
232  const PRMType& new_type) {
233  if (&(old_type) == __type) {
234  GUM_ERROR(OperationNotAllowed, "Cannot replace attribute own type");
235  }
236  if (old_type->domainSize() != new_type->domainSize()) {
238  "Cannot replace types with difference domain size");
239  }
240  if (!__cpf->contains(old_type.variable())) {
241  GUM_ERROR(NotFound, "could not find variable " + old_type.name());
242  }
243 
244  auto old = __cpf;
245 
247 
248  for (auto var : old->variablesSequence()) {
249  if (var != &(old_type.variable())) {
250  __cpf->add(*var);
251  } else {
252  __cpf->add(new_type.variable());
253  }
254  }
255 
256  Instantiation inst(__cpf), jnst(old);
257 
258  for (inst.setFirst(), jnst.setFirst(); !(inst.end() || jnst.end());
259  inst.inc(), jnst.inc()) {
260  __cpf->set(inst, old->get(jnst));
261  }
262 
263  delete old;
264 
265  GUM_ASSERT(inst.end() && jnst.end());
266  GUM_ASSERT(__cpf->contains(__type->variable()));
267  GUM_ASSERT(__cpf->contains(new_type.variable()));
268  GUM_ASSERT(!__cpf->contains(old_type.variable()));
269  }
270 
271  template < typename GUM_SCALAR >
273  return __type;
274  }
275 
276  template < typename GUM_SCALAR >
278  if (__type->variable().domainSize() != t->variable().domainSize()) {
280  "Cannot replace types with difference domain size");
281  }
282  auto old = __cpf;
283 
285 
286  for (auto var : old->variablesSequence()) {
287  if (var != &(__type->variable())) {
288  __cpf->add(*var);
289  } else {
290  __cpf->add(t->variable());
291  }
292  }
293 
294  Instantiation inst(__cpf), jnst(old);
295 
296  for (inst.setFirst(), jnst.setFirst(); !(inst.end() || jnst.end());
297  inst.inc(), jnst.inc()) {
298  __cpf->set(inst, old->get(jnst));
299  }
300 
301  delete old;
302 
303  __type = t;
304 
305  GUM_ASSERT(__cpf->contains(__type->variable()));
306  GUM_ASSERT(inst.end() && jnst.end());
307  }
308 
309  } /* namespace prm */
310 } /* namespace gum */
virtual void addChild(const PRMClassElement< GUM_SCALAR > &elt)
See gum::PRMClassElement::_addChild().
void insert(const T1 &first, const T2 &second)
Inserts a new association in the gum::Bijection.
aGrUM&#39;s Potential is a multi-dimensional array with tensor operators.
Definition: potential.h:60
const T2 & second(const T1 &first) const
Returns the second value of a pair given its first value.
const std::string & name() const
Returns the name of this object.
Definition: PRMType_inl.h:68
virtual void becomeCastDescendant(PRMType &subtype)
See gum::PRMClassElement::elt_type().
PRMType & superType()
Returns the super type of this type.
Definition: PRMType_inl.h:36
void setSuper(PRMType &t)
Changes the PRMType of this PRMType super.
Definition: PRMType_inl.h:80
virtual ~PRMScalarAttribute()
Destructor.
DiscreteVariable & variable()
Return a reference on the DiscreteVariable contained in this.
Definition: PRMType_inl.h:45
virtual void swap(const PRMType &old_type, const PRMType &new_type)
Swap old_type with new_type in the PRMClassElement cpt.
Idx pos(const DiscreteVariable &v) const final
Returns the position of the variable v.
const std::string & name() const
Returns the name of this object.
Definition: PRMObject_inl.h:35
virtual PRMType & type()=0
See gum::PRMClassElement::type().
Abstract class representing an element of PRM class.
Base class for discrete random variable.
virtual PRMType & type()
See gum::PRMClassElement::type().
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
virtual PRMAttribute< GUM_SCALAR > * newFactory(const PRMClass< GUM_SCALAR > &c) const
See gum::PRMClassElement::elt_type().
virtual PRMClassElement< GUM_SCALAR >::ClassElementType elt_type() const
See gum::PRMClassElement::elt_type().
virtual void copyCpf(const Bijection< const DiscreteVariable *, const DiscreteVariable * > &bif, const PRMAttribute< GUM_SCALAR > &source)
See gum::PRMClassElement::elt_type().
virtual void becomeCastDescendant(PRMType &subtype)=0
Change this attribute to be a cast descendant of a an attribute with type subtype.
virtual std::string cast(const PRMType &t) const
Returns the name of the cast descendant with PRMType t of this PRMClassElement.
virtual Size domainSize() const =0
bool existsFirst(const T1 &first) const
Returns true if first is the first element in a pair in the gum::Bijection.
virtual const Potential< GUM_SCALAR > & cpf() const
See gum::PRMClassElement::cpf().
void inc()
Operator increment.
PRMType * __type
The random variable type of this attribute.
virtual const Potential< GUM_SCALAR > & cpf() const =0
See gum::PRMClassElement::cpf().
Set of pairs of elements with fast search for both elements.
Definition: bijection.h:1805
Potential< GUM_SCALAR > * copyPotential(const Bijection< const DiscreteVariable *, const DiscreteVariable * > &bij, const Potential< GUM_SCALAR > &source)
Returns a copy of a Potential after applying a bijection over the variables in source.
Definition: utils_prm_tpl.h:29
This is a decoration of the DiscreteVariable class.
Definition: PRMType.h:63
virtual PRMType & type()=0
Return a reference over the gum::PRMType of this class element.
virtual PRMAttribute< GUM_SCALAR > * copy(Bijection< const DiscreteVariable *, const DiscreteVariable * > bij) const
See gum::PRMClassElement::elt_type().
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:83
static std::string LEFT_CAST()
Enumeration of the different types of objects handled by a PRM.
Definition: PRMObject.h:79
A PRMClass is an object of a PRM representing a fragment of a Bayesian Network which can be instantia...
Definition: PRMClass.h:66
virtual void setAsCastDescendant(PRMAttribute< GUM_SCALAR > *attr)
See gum::PRMClassElement::elt_type().
Potential< GUM_SCALAR > * __cpf
A pointer on the Potential of this attribute.
void setFirst()
Assign the first values to the tuple of the Instantiation.
const std::vector< Idx > & label_map() const
Returns the vector in which the i-th element is the Idx of the super type&#39;s label for the i-th label ...
Definition: PRMType_inl.h:94
virtual PRMAttribute< GUM_SCALAR > * getCastDescendant() const
See gum::PRMClassElement::elt_type().
<agrum/multidim/multiDimImplementation.h>
PRMAttribute is a member of a Class in a PRM.
Definition: PRMAttribute.h:61
virtual void addParent(const PRMClassElement< GUM_SCALAR > &elt)
See gum::PRMClassElement::_addParent().
const std::string & name() const
returns the name of the variable
static std::string RIGHT_CAST()
Enumeration of the different types of objects handled by a PRM.
Definition: PRMObject.h:80
<agrum/PRM/elements/scalarAttribute.h>
PRMScalarAttribute(const std::string &name, const PRMType &type, MultiDimImplementation< GUM_SCALAR > *impl=new MultiDimArray< GUM_SCALAR >())
Constructor used by gum::Class.
std::string _safeName
The safe name of this PRMClassElement.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
PRMScalarAttribute & operator=(const PRMScalarAttribute &from)
Copy operator. Don&#39;t use it.
bool end() const
Returns true if the Instantiation reached the end.
ClassElementType
Returns true if obj_ptr is of type PRMReferenceSlot.