aGrUM  0.16.0
instantiation.cpp
Go to the documentation of this file.
1 
32 
33 #ifdef GUM_NO_INLINE
35 #endif /* GUM_NO_INLINE */
36 
37 namespace gum {
38 
39  // Default constructor
40  Instantiation::Instantiation() : __master(nullptr), __overflow(false) {
41  GUM_CONSTRUCTOR(Instantiation);
42  }
43 
44  // destructor
46  GUM_DESTRUCTOR(Instantiation);
47  // unregister the Instantiation from its __master
48 
49  if (__master) __master->unregisterSlave(*this);
50  }
51 
53  // for speed issues
54  GUM_ASSERT(master != nullptr);
55 
57  __vars.resize(v.size());
58  __vals.reserve(v.size());
59  // fill the instantiation
60 
61  for (const auto var : v)
62  __add(*var);
63 
64  actAsSlave(master->getMasterRef());
65  }
66 
67  // constructor for a Instantiation contained into a MultiDimInterface
69  __master(0), __overflow(false) {
70  // for debugging purposes
71  GUM_CONSTRUCTOR(Instantiation);
72  __init(&d);
73  }
74 
76  __master(0), __overflow(false) {
77  // for debugging purposes
78  GUM_CONSTRUCTOR(Instantiation);
79  __init(const_cast< MultiDimAdressable* >(&d));
80  }
81 
82  // constructor for a Instantiation contained into a MultiDimInterface
84  __master(0), __overflow(false) {
85  // for debugging purposes
86  GUM_CONSTRUCTOR(Instantiation);
87 
88  if (d) __init(d);
89  }
90 
91  // constructor for a Instantiation contained into a MultiDimInterface this
92  // constructor is needed in order to allow creation of Instantiation(this) in
93  // MultiDimAdressable and below
95  __master(0), __overflow(false) {
96  // for debugging purposes
97  GUM_CONSTRUCTOR(Instantiation);
98 
99  if (const_d) __init(const_cast< MultiDimAdressable* >(const_d));
100  }
101 
102  // copy constructor
103  Instantiation::Instantiation(const Instantiation& aI, const bool notifyMaster) :
104  MultiDimInterface(), __master(0), __overflow(false) {
105  // for debugging purposes
106  GUM_CONS_CPY(Instantiation);
107  // copy the content of aI
108  __vars = aI.__vars;
109  __vals = aI.__vals;
110  __overflow = aI.__overflow;
111 
112  if (aI.__master && notifyMaster) actAsSlave(*aI.__master);
113  }
114 
115  // operator=
117  if (__master) {
118  if (!aI.isMaster(__master)) { // aI as the same master.
119  if (nbrDim() != aI.nbrDim()) {
120  GUM_ERROR(OperationNotAllowed, "in slave Instantiation");
121  }
122 
123  for (Idx i = 0; i < nbrDim(); i++) {
124  if ((!contains(aI.variable(i))) || (!aI.contains(variable(i)))) {
125  GUM_ERROR(OperationNotAllowed, "in slave Instantiation");
126  }
127  }
128  }
129 
130  setVals(aI);
131  } else {
132  // copy the content of aI
133  __vars = aI.__vars;
134  __vals = aI.__vals;
135  __overflow = aI.__overflow;
136 
137  if (aI.__master) actAsSlave(*aI.__master);
138  }
139 
140  return *this;
141  }
142 
143  // Gives a string version of a Instantiation
144  std::string Instantiation::toString() const {
145  std::stringstream sstr;
146  // check if the value of the instantiation is correct
147 
148  if (__overflow) { sstr << "<invalid>"; }
149 
150  sstr << "<";
151 
152  bool first = true;
153 
154  for (const auto var : __vars) {
155  if (!first) sstr << "|";
156 
157  first = false;
158  sstr << var->name() << ":" << var->label(val(*var));
159  }
160 
161  sstr << ">";
162 
163  return sstr.str();
164  }
165 
166  // give a Id value for Hamming distance
168  Idx res = 0;
169 
170  for (const auto var : __vars)
171  res += val(*var);
172 
173  return res;
174  }
175 
178  const Instantiation& external) {
179  for (const auto& elt : map) {
180  const DiscreteVariable& var = *elt.second;
181 
182  try {
183  Idx val = external.val(*elt.first);
184 
185  try {
186  chgVal(var, val);
187  } catch (NotFound&) {
189  var.name() << " : missing variable in instantiation");
190  }
191  } catch (NotFound&) {
193  var.name() << " : missing variable in external instantiation");
194  }
195  }
196  }
197 
199  Idx newVal,
200  Idx oldVal) const {
201  if (__master)
202  __master->changeNotification(*this, __vars[varPos], oldVal, newVal);
203  }
204 
207  }
208 
210  if (__master) __master->setIncNotification(*this);
211  }
214  }
216  if (__master) __master->setDecNotification(*this);
217  }
218 
219  // deassociate the master MultiDimAdressable, if any
221  if (__master) {
222  __master->unregisterSlave(*this);
223  __master = nullptr;
224  }
225  return true;
226  }
227  // force the variables sequence order to be the same as the master one
229  if (m != __master) {
230  GUM_ERROR(OperationNotAllowed, "only master can do this");
231  }
232 
234  }
235  // erase new dim by master
237  const DiscreteVariable& v) {
238  if (m != __master) {
239  GUM_ERROR(OperationNotAllowed, "only master can do this");
240  }
241 
242  __erase(v);
243 
245  }
246 
247  // tries to register the Instantiation to a MultiDimAdressable
249  // if __master : not allowed
250  if (__master != nullptr) {
251  GUM_ERROR(OperationNotAllowed, "in slave Instantiation");
252  }
253 
254  __master = &aMD;
255 
256  // perform the registration
257  if (aMD.registerSlave(*this)) {
258  return true;
259  } else {
260  __master = nullptr;
261  return false;
262  }
263  }
264 
265  // an operator for user-friendly displaying the content of a Instantiation
266  std::ostream& operator<<(std::ostream& aStream, const Instantiation& i) {
267  aStream << i.toString();
268  return aStream;
269  }
270 
271 } /* namespace gum */
void __erase(const DiscreteVariable &v)
Removes a variable from the sequence of vars.
void __init(MultiDimAdressable *master)
Initialize this Instantiation.
bool __overflow
Indicates whether the current value of the tuple is valid when we loop sufficiently over values of th...
virtual void setFirstNotification(const Instantiation &i)=0
Listen to setFirst in a given Instantiation.
void __masterDecNotification() const
~Instantiation()
Destructor.
Idx nbrDim() const final
Returns the number of variables in the Instantiation.
Size size() const noexcept
Returns the size of the sequence.
Definition: sequence_tpl.h:38
The generic class for storing (ordered) sequences of objects.
Definition: sequence.h:1022
Sequence< const DiscreteVariable *> __vars
The tuple of variables to be instantiated.
bool forgetMaster()
Deassociate the master MultiDimAdressable, if any.
virtual void setChangeNotification(const Instantiation &i)=0
Listen to an assignment of a value in a Instantiation.
void __masterIncNotification() const
virtual bool registerSlave(Instantiation &i)=0
Register i as a slave of this MultiDimAdressable.
Instantiation & chgVal(const DiscreteVariable &v, Idx newval)
Assign newval to variable v in the Instantiation.
Interface for all classes addressing in a multiDim fashion.
Base class for discrete random variable.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
Instantiation & setVals(const Instantiation &i)
Assign the values from i in the Instantiation.
virtual void setIncNotification(const Instantiation &i)=0
Listen to increment in a given Instantiation.
The class for generic Hash Tables.
Definition: hashTable.h:679
Idx val(Idx i) const
Returns the current value of the variable at position i.
std::ostream & operator<<(std::ostream &output, const BayesNet< GUM_SCALAR > &bn)
Prints map&#39;s DAG in output using the Graphviz-dot format.
Definition: BayesNet_tpl.h:605
virtual bool unregisterSlave(Instantiation &i)=0
Unregister i as a slave of this MultiDimAdressable.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
std::string toString() const
Give a string version of instantiation.
void __reorder(const Sequence< const DiscreteVariable * > &v)
Reorder vars of this instantiation giving the order in v.
Idx hamming() const
Returns the hamming distance of this instantiation.
void __masterFirstNotification() const
virtual void setLastNotification(const Instantiation &i)=0
Listen to setLast in a given Instantiation.
void __masterLastNotification() const
MultiDimAdressable * __master
The master, if any, contains precisely the set of variables to be instantiated.
Abstract base class for all multi dimensionnal addressable.
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:83
bool contains(const DiscreteVariable &v) const final
Indicates whether a given variable belongs to the Instantiation.
void setValsFrom(const HashTable< const DiscreteVariable *, const DiscreteVariable * > &map, const Instantiation &external)
Assign the values of external in *this, using map as a bijection between external and this variables...
bool isMaster(const MultiDimAdressable *m) const
Indicates whether m is the master of this instantiation.
void synchronizeWithMaster(const MultiDimAdressable *m)
Force the variables sequence to be the same as the master one.
Instantiation & operator=(const Instantiation &aI)
Copy operator.
Instantiation()
Default constructor: creates an empty tuple.
virtual const Sequence< const DiscreteVariable *> & variablesSequence() const =0
Returns a const ref to the sequence of DiscreteVariable*.
Size Idx
Type for indexes.
Definition: types.h:53
virtual void setDecNotification(const Instantiation &i)=0
Listen to increment in each recorded Instantiation.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
virtual void changeNotification(const Instantiation &i, const DiscreteVariable *const var, Idx oldval, Idx newval)=0
Listen to changes in a given Instantiation.
bool actAsSlave(MultiDimAdressable &aMD)
Tries to register the Instantiation to a MultiDimAdressable.
const std::string & name() const
returns the name of the variable
const DiscreteVariable & variable(Idx i) const final
Returns the variable at position i in the tuple.
void __add(const DiscreteVariable &v)
Adds a new var to the sequence of vars.
virtual MultiDimAdressable & getMasterRef()=0
In order to insure the dereference for decorators, we need to virtualize the access to master pointer...
void eraseWithMaster(const MultiDimAdressable *m, const DiscreteVariable &v)
Call Instantiation::__erase(const DiscreteVariable&) by master.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
void __masterChangeNotification(Idx varPos, Idx newVal, Idx oldVal) const
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
std::vector< Idx > __vals
The current instantiation: the value of the tuple.