aGrUM  0.18.1
a C++ library for (probabilistic) graphical models
PRMFactory_tpl.h
Go to the documentation of this file.
1 
29 #include <agrum/PRM/PRMFactory.h>
30 
31 #include <iostream>
32 #include <sstream>
33 
35 
38 
41 
42 namespace gum {
43 
44  namespace prm {
45 
46  template < typename GUM_SCALAR >
47  INLINE void
48  PRMFactory< GUM_SCALAR >::startClass(const std::string& name,
49  const std::string& extends,
50  const Set< std::string >* implements,
51  bool delayInheritance) {
52  std::string real_name = addPrefix__(name);
53  if (prm__->classMap__.exists(real_name)
54  || prm__->interfaceMap__.exists(real_name)) {
55  GUM_ERROR(DuplicateElement, "'" << real_name << "' is already used.");
56  }
57  PRMClass< GUM_SCALAR >* c = nullptr;
58  PRMClass< GUM_SCALAR >* mother = nullptr;
60 
61  if (implements != 0) {
62  for (const auto& imp: *implements) {
63  impl.insert(retrieveInterface__(imp));
64  }
65  }
66 
67  if (extends != "") { mother = retrieveClass__(extends); }
68 
69  if ((extends == "") && impl.empty()) {
70  c = new PRMClass< GUM_SCALAR >(real_name);
71  } else if ((extends != "") && impl.empty()) {
72  c = new PRMClass< GUM_SCALAR >(real_name, *mother, delayInheritance);
73  } else if ((extends == "") && (!impl.empty())) {
74  c = new PRMClass< GUM_SCALAR >(real_name, impl, delayInheritance);
75  } else if ((extends != "") && (!impl.empty())) {
76  c = new PRMClass< GUM_SCALAR >(real_name, *mother, impl, delayInheritance);
77  }
78 
79  prm__->classMap__.insert(c->name(), c);
80  prm__->classes__.insert(c);
81  stack__.push_back(c);
82  }
83 
84  template < typename GUM_SCALAR >
85  INLINE void PRMFactory< GUM_SCALAR >::continueClass(const std::string& name) {
86  std::string real_name = addPrefix__(name);
87  if (!(prm__->classMap__.exists(real_name))) {
88  std::stringstream msg;
89  msg << "'" << real_name << "' not found";
90  GUM_ERROR(NotFound, msg.str());
91  }
92  stack__.push_back(&(prm__->getClass(real_name)));
93  }
94 
95  template < typename GUM_SCALAR >
96  INLINE void PRMFactory< GUM_SCALAR >::endClass(bool checkImplementations) {
98  checkStack__(1, PRMObject::prm_type::CLASS));
99 
100  if (checkImplementations) { checkInterfaceImplementation__(c); }
101 
102  stack__.pop_back();
103  }
104 
105  template < typename GUM_SCALAR >
108  try {
109  for (const auto& i: c->implements()) {
110  try {
111  for (const auto& node: i->containerDag().nodes()) {
112  std::string name = i->get(node).name();
113 
114  switch (i->get(node).elt_type()) {
117  if ((c->get(name).elt_type()
119  || (c->get(name).elt_type()
121  if (!c->get(name).type().isSubTypeOf(i->get(name).type())) {
122  std::stringstream msg;
123  msg << "class " << c->name()
124  << " does not respect interface ";
125  GUM_ERROR(TypeError, msg.str() + i->name());
126  }
127  } else {
128  std::stringstream msg;
129  msg << "class " << c->name() << " does not respect interface ";
130  GUM_ERROR(TypeError, msg.str() + i->name());
131  }
132 
133  break;
134  }
135 
137  if (c->get(name).elt_type()
139  const PRMReferenceSlot< GUM_SCALAR >& ref_i =
140  static_cast< const PRMReferenceSlot< GUM_SCALAR >& >(
141  i->get(name));
142  const PRMReferenceSlot< GUM_SCALAR >& ref_this =
143  static_cast< const PRMReferenceSlot< GUM_SCALAR >& >(
144  c->get(name));
145 
146  if (!ref_this.slotType().isSubTypeOf(ref_i.slotType())) {
147  std::stringstream msg;
148  msg << "class " << c->name()
149  << " does not respect interface ";
150  GUM_ERROR(TypeError, msg.str() + i->name());
151  }
152  } else {
153  std::stringstream msg;
154  msg << "class " << c->name() << " does not respect interface ";
155  GUM_ERROR(TypeError, msg.str() + i->name());
156  }
157 
158  break;
159  }
160 
162  // Nothing to check: they are automatically inherited
163  break;
164  }
165 
166  default: {
167  std::string msg =
168  "unexpected ClassElement<GUM_SCALAR> in interface ";
169  GUM_ERROR(FatalError, msg + i->name());
170  }
171  }
172  }
173  } catch (NotFound&) {
174  std::stringstream msg;
175  msg << "class " << c->name() << " does not respect interface ";
176  GUM_ERROR(TypeError, msg.str() + i->name());
177  }
178  }
179  } catch (NotFound&) {
180  // this Class<GUM_SCALAR> does not implement any
181  // PRMInterface<GUM_SCALAR>
182  }
183  }
184 
185  template < typename GUM_SCALAR >
186  INLINE void
188  const std::string& extends,
189  bool delayInheritance) {
190  std::string real_name = addPrefix__(name);
191  if (prm__->classMap__.exists(real_name)
192  || prm__->interfaceMap__.exists(real_name)) {
193  GUM_ERROR(DuplicateElement, "'" << real_name << "' is already used.");
194  }
195  PRMInterface< GUM_SCALAR >* i = nullptr;
196  PRMInterface< GUM_SCALAR >* super = nullptr;
197 
198  if (extends != "") { super = retrieveInterface__(extends); }
199 
200  if (super != nullptr) {
201  i = new PRMInterface< GUM_SCALAR >(real_name, *super, delayInheritance);
202  } else {
203  i = new PRMInterface< GUM_SCALAR >(real_name);
204  }
205 
206  prm__->interfaceMap__.insert(i->name(), i);
207  prm__->interfaces__.insert(i);
208  stack__.push_back(i);
209  }
210 
211  template < typename GUM_SCALAR >
212  INLINE void
214  std::string real_name = addPrefix__(name);
215  if (!prm__->interfaceMap__.exists(real_name)) {
216  GUM_ERROR(DuplicateElement, "'" << real_name << "' not found.");
217  }
218 
219  PRMInterface< GUM_SCALAR >* i = retrieveInterface__(real_name);
220  stack__.push_back(i);
221  }
222 
223  template < typename GUM_SCALAR >
224  INLINE void
226  PRMClass< GUM_SCALAR >* c = static_cast< PRMClass< GUM_SCALAR >* >(
227  checkStack__(1, PRMObject::prm_type::CLASS));
228  c->add(attr);
229  Size count = 0;
231  attr->cpf().variablesSequence();
232 
233  for (const auto& node: c->containerDag().nodes()) {
234  try {
235  if (vars.exists(&(c->get(node).type().variable()))) {
236  ++count;
237 
238  if (&(attr->type().variable()) != &(c->get(node).type().variable())) {
239  c->addArc(c->get(node).safeName(), attr->safeName());
240  }
241  }
242  } catch (OperationNotAllowed&) {}
243  }
244 
245  if (count != attr->cpf().variablesSequence().size()) {
246  GUM_ERROR(NotFound, "unable to found all parents of this attribute");
247  }
248  }
249 
250  template < typename GUM_SCALAR >
254  const std::string& name) {
255  try {
256  PRMClassElement< GUM_SCALAR >& elt = c->get(name);
257 
258  switch (elt.elt_type()) {
261  "can not add a reference slot as a parent of an attribute");
262  break;
263  }
264 
266  if (static_cast< PRMSlotChain< GUM_SCALAR >& >(elt).isMultiple()) {
268  "can not add a multiple slot chain to an attribute");
269  }
270 
271  c->addArc(name, a->name());
272 
273  break;
274  }
275 
278  c->addArc(name, a->name());
279  break;
280  }
281 
282  default: {
283  GUM_ERROR(FatalError, "unknown ClassElement<GUM_SCALAR>");
284  }
285  }
286  } catch (NotFound&) {
287  // Check if name is a slot chain
288  PRMSlotChain< GUM_SCALAR >* sc = buildSlotChain__(c, name);
289 
290  if (sc == nullptr) {
291  std::string msg =
292  "found no ClassElement<GUM_SCALAR> with the given name ";
293  GUM_ERROR(NotFound, msg + name);
294  } else if (!sc->isMultiple()) {
295  c->add(sc);
296  c->addArc(sc->name(), a->name());
297  } else {
298  delete sc;
300  "Impossible to add a multiple reference slot as"
301  " direct parent of an PRMAttribute<GUM_SCALAR>.");
302  }
303  }
304  }
305 
306 
307  template < typename GUM_SCALAR >
308  INLINE void PRMFactory< GUM_SCALAR >::addParent(const std::string& name) {
309  PRMClassElementContainer< GUM_SCALAR >* c = checkStackContainter__(2);
310  try {
311  // Retrieving pointers
314  addParent__(c, a, name);
315  } catch (FactoryInvalidState&) {
316  auto agg = static_cast< PRMAggregate< GUM_SCALAR >* >(
318  addParent__(static_cast< PRMClass< GUM_SCALAR >* >(c), agg, name);
319  }
320  }
321 
322  template < typename GUM_SCALAR >
324  const std::vector< float >& array) {
327  checkStack__(2, PRMObject::prm_type::CLASS);
328 
329  if (a->cpf().domainSize() != array.size())
330  GUM_ERROR(OperationNotAllowed, "illegal CPF size");
331 
332  std::vector< GUM_SCALAR > array2(array.begin(), array.end());
333  a->cpf().fillWith(array2);
334  }
335 
336  template < typename GUM_SCALAR >
338  const std::vector< GUM_SCALAR >& array) {
339  auto elt = checkStack__(1, PRMClassElement< GUM_SCALAR >::prm_attribute);
340  auto a = static_cast< PRMAttribute< GUM_SCALAR >* >(elt);
341  checkStack__(2, PRMObject::prm_type::CLASS);
342 
343  if (a->cpf().domainSize() != array.size()) {
344  GUM_ERROR(OperationNotAllowed, "illegal CPF size");
345  }
346 
347  a->cpf().fillWith(array);
348  }
349 
350  template < typename GUM_SCALAR >
352  const std::vector< float >& array) {
355 
356  if (a->cpf().domainSize() != array.size()) {
357  GUM_ERROR(OperationNotAllowed, "illegal CPF size");
358  }
359 
360  std::vector< GUM_SCALAR > array2(array.begin(), array.end());
361  setRawCPFByColumns(array2);
362  }
363 
364  template < typename GUM_SCALAR >
366  const std::vector< GUM_SCALAR >& array) {
369 
370  if (a->cpf().domainSize() != array.size()) {
371  GUM_ERROR(OperationNotAllowed, "illegal CPF size");
372  }
373 
374  if (a->cpf().nbrDim() == 1) {
375  setRawCPFByLines(array);
376 
377  } else {
378  Instantiation inst(a->cpf());
379  Instantiation jnst;
380  for (auto idx = inst.variablesSequence().rbegin();
381  idx != inst.variablesSequence().rend();
382  --idx) {
383  jnst.add(**idx);
384  }
385 
386  jnst.setFirst();
387  auto idx = (std::size_t)0;
388  while ((!jnst.end()) && idx < array.size()) {
389  inst.setVals(jnst);
390  a->cpf().set(inst, array[idx]);
391  jnst.inc();
392  ++idx;
393  }
394  }
395  }
396 
397  template < typename GUM_SCALAR >
399  const std::vector< std::string >& parents,
400  const std::vector< float >& values) {
401  auto a = static_cast< PRMAttribute< GUM_SCALAR >* >(
403 
404  if ((parents.size() + 1) != a->cpf().variablesSequence().size()) {
405  GUM_ERROR(OperationNotAllowed, "wrong number of parents");
406  }
407 
408  if (values.size() != a->type().variable().domainSize()) {
409  GUM_ERROR(OperationNotAllowed, "wrong number of values");
410  }
411 
412  std::vector< GUM_SCALAR > values2(values.begin(), values.end());
413  setCPFByRule(parents, values2);
414  }
415 
416  template < typename GUM_SCALAR >
418  const std::vector< std::string >& parents,
419  const std::vector< GUM_SCALAR >& values) {
420  auto a = static_cast< PRMAttribute< GUM_SCALAR >* >(
422 
423  if ((parents.size() + 1) != a->cpf().variablesSequence().size()) {
424  GUM_ERROR(OperationNotAllowed, "wrong number of parents");
425  }
426 
427  if (values.size() != a->type().variable().domainSize()) {
428  GUM_ERROR(OperationNotAllowed, "wrong number of values");
429  }
430 
431  if (dynamic_cast< PRMFormAttribute< GUM_SCALAR >* >(a)) {
432  auto form = static_cast< PRMFormAttribute< GUM_SCALAR >* >(a);
433  // jnst holds parents with a specific value (not "*")
434  // knst holds parents without a specific value ("*")
435  Instantiation jnst, knst;
436  const DiscreteVariable* var = 0;
437  // not_used Size pos = 0;
438  bool found = false;
439 
440  for (Idx i = 0; i < parents.size(); ++i) {
441  var = form->formulas().variablesSequence().atPos(1 + i);
442 
443  if (parents[i] == "*") {
444  knst.add(*var);
445  } else {
446  jnst.add(*var);
447  // not_used pos = 0;
448  found = false;
449 
450  for (Size j = 0; j < var->domainSize(); ++j) {
451  if (var->label(j) == parents[i]) {
452  jnst.chgVal(*var, j);
453  found = true;
454  break;
455  }
456  }
457 
458  if (!found) {
459  std::string msg = "could not find label ";
460  GUM_ERROR(NotFound, msg + parents[i]);
461  }
462  }
463  }
464 
465  Instantiation inst(form->formulas());
466  inst.setVals(jnst);
467 
468  for (Size i = 0; i < form->type()->domainSize(); ++i) {
469  inst.chgVal(form->type().variable(), i);
470 
471  for (inst.setFirstIn(knst); !inst.end(); inst.incIn(knst)) {
472  form->formulas().set(inst, std::to_string(values[i]));
473  }
474  }
475 
476  } else {
477  GUM_ERROR(OperationNotAllowed, "invalide attribute type");
478  }
479  }
480 
481  template < typename GUM_SCALAR >
483  const std::vector< std::string >& parents,
484  const std::vector< std::string >& values) {
485  auto a = static_cast< PRMAttribute< GUM_SCALAR >* >(
487 
488  if ((parents.size() + 1) != a->cpf().variablesSequence().size()) {
489  GUM_ERROR(OperationNotAllowed, "wrong number of parents");
490  }
491 
492  if (values.size() != a->type().variable().domainSize()) {
493  GUM_ERROR(OperationNotAllowed, "wrong number of values");
494  }
495 
496  if (dynamic_cast< PRMFormAttribute< GUM_SCALAR >* >(a)) {
497  auto form = static_cast< PRMFormAttribute< GUM_SCALAR >* >(a);
498  // jnst holds parents with a specific value (not "*")
499  // knst holds parents without a specific value ("*")
500  Instantiation jnst, knst;
501  const DiscreteVariable* var = 0;
502  // not_used Size pos = 0;
503  bool found = false;
504 
505  for (Idx i = 0; i < parents.size(); ++i) {
506  var = form->formulas().variablesSequence().atPos(1 + i);
507 
508  if (parents[i] == "*") {
509  knst.add(*var);
510  } else {
511  jnst.add(*var);
512  // not_used pos = 0;
513  found = false;
514 
515  for (Size j = 0; j < var->domainSize(); ++j) {
516  if (var->label(j) == parents[i]) {
517  jnst.chgVal(*var, j);
518  found = true;
519  break;
520  }
521  }
522 
523  if (!found) {
524  std::string msg = "could not find label ";
525  GUM_ERROR(NotFound, msg + parents[i]);
526  }
527  }
528  }
529 
530  Instantiation inst(form->formulas());
531  inst.setVals(jnst);
532 
533  for (Size i = 0; i < form->type()->domainSize(); ++i) {
534  inst.chgVal(form->type().variable(), i);
535 
536  for (inst.setFirstIn(knst); !inst.end(); inst.incIn(knst)) {
537  form->formulas().set(inst, values[i]);
538  }
539  }
540 
541  } else {
542  GUM_ERROR(OperationNotAllowed, "invalide attribute type");
543  }
544  }
545 
546  template < typename GUM_SCALAR >
547  INLINE void PRMFactory< GUM_SCALAR >::addParameter(const std::string& type,
548  const std::string& name,
549  double value) {
550  auto c = static_cast< PRMClass< GUM_SCALAR >* >(
551  checkStack__(1, PRMObject::prm_type::CLASS));
552 
553  PRMParameter< GUM_SCALAR >* p = nullptr;
554  if (type == "int") {
556  name,
558  (GUM_SCALAR)value);
559  } else if (type == "real") {
561  name,
563  (GUM_SCALAR)value);
564  }
565 
566  try {
567  c->add(p);
568  } catch (DuplicateElement&) { c->overload(p); }
569  }
570 
571  template < typename GUM_SCALAR >
573  const std::string& name,
574  const std::string& agg_type,
575  const std::string& rv_type,
576  const std::vector< std::string >& params) {
577  PRMClass< GUM_SCALAR >* c = static_cast< PRMClass< GUM_SCALAR >* >(
578  checkStack__(1, PRMObject::prm_type::CLASS));
579 
580  auto agg = new PRMAggregate< GUM_SCALAR >(
581  name,
583  *retrieveType__(rv_type));
584 
585  try {
586  c->add(agg);
587  } catch (DuplicateElement&) { c->overload(agg); }
588 
589  switch (agg->agg_type()) {
593  if (params.size() != 1) {
594  GUM_ERROR(OperationNotAllowed, "aggregate requires a parameter");
595  }
596  agg->setLabel(params.front());
597  break;
598  }
599  default: {
600  // Nothing to do
601  }
602  }
603  stack__.push_back(agg);
604  }
605 
606  template < typename GUM_SCALAR >
607  INLINE void
609  PRMClassElementContainer< GUM_SCALAR >* c = checkStackContainter__(1);
610  if (!c->exists(name)) { GUM_ERROR(NotFound, name << "not found"); }
611  auto& agg = c->get(name);
613  GUM_ERROR(OperationNotAllowed, name << " not an aggregate");
614  }
615  stack__.push_back(&agg);
616  }
617 
618  template < typename GUM_SCALAR >
619  INLINE void
622  const std::string& name) {
623  auto chains = std::vector< std::string >{name};
624  auto inputs = std::vector< PRMClassElement< GUM_SCALAR >* >();
625  retrieveInputs__(c, chains, inputs);
626 
627  switch (agg->agg_type()) {
630  if (inputs.front()->type() != *(retrieveType__("boolean"))) {
631  GUM_ERROR(WrongType, "expected booleans");
632  }
633 
634  break;
635  }
636 
640  if (!agg->hasLabel()) {
641  auto param = agg->labelValue();
642  Idx label_idx = 0;
643 
644  while (label_idx < inputs.front()->type()->domainSize()) {
645  if (inputs.front()->type()->label(label_idx) == param) { break; }
646 
647  ++label_idx;
648  }
649 
650  if (label_idx == inputs.front()->type()->domainSize()) {
651  GUM_ERROR(NotFound, "could not find label");
652  }
653 
654  agg->setLabel(label_idx);
655  }
656 
657  break;
658  }
659 
665  break;
666  }
667 
668  default: {
669  GUM_ERROR(FatalError, "Unknown aggregator.");
670  }
671  }
672 
673  c->addArc(inputs.front()->safeName(), agg->safeName());
674  }
675 
676  template < typename GUM_SCALAR >
679  stack__.pop_back();
680  }
681 
682  template < typename GUM_SCALAR >
684  const std::string& name,
685  const std::string& agg_type,
686  const std::vector< std::string >& chains,
687  const std::vector< std::string >& params,
688  std::string type) {
689  PRMClass< GUM_SCALAR >* c = static_cast< PRMClass< GUM_SCALAR >* >(
690  checkStack__(1, PRMObject::prm_type::CLASS));
691  // Checking call legality
692 
693  if (chains.size() == 0) {
695  "a PRMAggregate<GUM_SCALAR> requires at least one parent");
696  }
697 
698  // Retrieving the parents of the aggregate
699  std::vector< PRMClassElement< GUM_SCALAR >* > inputs;
700 
701  // This helps knowing if the aggregate has parents outside the current
702  // class
703  // (see below)
704  bool hasSC = retrieveInputs__(c, chains, inputs);
705 
706  // Checking that all inputs shares the same PRMType (trivial
707  // if
708  // inputs.size() == 1)
709  if (inputs.size() > 1) {
710  for (auto iter = inputs.begin() + 1; iter != inputs.end(); ++iter) {
711  if ((**(iter - 1)).type() != (**iter).type()) {
712  GUM_ERROR(WrongType, "found different types");
713  }
714  }
715  }
716 
717  // Different treatments for different types of aggregate.
718  PRMAggregate< GUM_SCALAR >* agg = nullptr;
719 
720  switch (PRMAggregate< GUM_SCALAR >::str2enum(agg_type)) {
723  if (inputs.front()->type() != *(retrieveType__("boolean"))) {
724  GUM_ERROR(WrongType, "expected booleans");
725  }
726  if (params.size() != 0) {
727  GUM_ERROR(OperationNotAllowed, "invalid number of paramaters");
728  }
729 
730  agg = new PRMAggregate< GUM_SCALAR >(
731  name,
733  inputs.front()->type());
734 
735  break;
736  }
737 
740  if (params.size() != 1) {
741  GUM_ERROR(OperationNotAllowed, "invalid number of parameters");
742  }
743 
744  Idx label_idx = 0;
745 
746  while (label_idx < inputs.front()->type()->domainSize()) {
747  if (inputs.front()->type()->label(label_idx) == params.front()) {
748  break;
749  }
750 
751  ++label_idx;
752  }
753 
754  if (label_idx == inputs.front()->type()->domainSize()) {
755  GUM_ERROR(NotFound, "could not find label");
756  }
757 
758  // Creating and adding the PRMAggregate<GUM_SCALAR>
759  agg = new PRMAggregate< GUM_SCALAR >(
760  name,
762  *(retrieveType__("boolean")),
763  label_idx);
764  agg->label();
765 
766  break;
767  }
768 
774  if (params.size() != 0) {
775  GUM_ERROR(OperationNotAllowed, "invalid number of parameters");
776  }
777 
778  auto output_type = retrieveType__(type);
779 
780  // Creating and adding the PRMAggregate<GUM_SCALAR>
781  agg = new PRMAggregate< GUM_SCALAR >(
782  name, PRMAggregate< GUM_SCALAR >::str2enum(agg_type), *output_type);
783 
784  break;
785  }
786 
788  if (params.size() != 1) {
789  GUM_ERROR(OperationNotAllowed, "invalid number of parameters");
790  }
791 
792  Idx label_idx = 0;
793 
794  while (label_idx < inputs.front()->type()->domainSize()) {
795  if (inputs.front()->type()->label(label_idx) == params.front()) {
796  break;
797  }
798 
799  ++label_idx;
800  }
801 
802  if (label_idx == inputs.front()->type()->domainSize()) {
803  GUM_ERROR(NotFound, "could not find label");
804  }
805 
806  auto output_type = retrieveType__(type);
807 
808  // Creating and adding the PRMAggregate<GUM_SCALAR>
809  agg = new PRMAggregate< GUM_SCALAR >(
810  name,
812  *output_type,
813  label_idx);
814 
815  break;
816  }
817 
818  default: {
819  GUM_ERROR(FatalError, "Unknown aggregator.");
820  }
821  }
822 
823  std::string safe_name = agg->safeName();
824 
825  try {
826  if (hasSC) {
827  try {
828  c->add(agg);
829  } catch (DuplicateElement&) { c->overload(agg); }
830  } else {
831  // Inner aggregators can be directly used as attributes
832  auto attr = new PRMScalarAttribute< GUM_SCALAR >(
833  agg->name(), agg->type(), agg->buildImpl());
834 
835  try {
836  c->add(attr);
837  } catch (DuplicateElement&) { c->overload(attr); }
838 
839  delete agg;
840  }
841  } catch (DuplicateElement&) {
842  delete agg;
843  throw;
844  }
845 
846  for (const auto& elt: inputs) {
847  c->addArc(elt->safeName(), safe_name);
848  }
849  }
850 
851  template < typename GUM_SCALAR >
852  INLINE void PRMFactory< GUM_SCALAR >::addReferenceSlot(const std::string& type,
853  const std::string& name,
854  bool isArray) {
855  PRMClassElementContainer< GUM_SCALAR >* owner = checkStackContainter__(1);
857 
858  try {
859  slotType = retrieveClass__(type);
860  } catch (NotFound&) {
861  try {
862  slotType = retrieveInterface__(type);
863  } catch (NotFound&) {
864  GUM_ERROR(NotFound, "unknown ReferenceSlot<GUM_SCALAR> slot type");
865  }
866  }
867 
869  new PRMReferenceSlot< GUM_SCALAR >(name, *slotType, isArray);
870 
871  try {
872  owner->add(ref);
873  } catch (DuplicateElement&) { owner->overload(ref); }
874  }
875 
876  template < typename GUM_SCALAR >
877  INLINE void PRMFactory< GUM_SCALAR >::addArray(const std::string& type,
878  const std::string& name,
879  Size size) {
880  PRMSystem< GUM_SCALAR >* model = static_cast< PRMSystem< GUM_SCALAR >* >(
881  checkStack__(1, PRMObject::prm_type::SYSTEM));
882  PRMClass< GUM_SCALAR >* c = retrieveClass__(type);
883  PRMInstance< GUM_SCALAR >* inst = 0;
884 
885  try {
886  model->addArray(name, *c);
887 
888  for (Size i = 0; i < size; ++i) {
889  std::stringstream elt_name;
890  elt_name << name << "[" << i << "]";
891  inst = new PRMInstance< GUM_SCALAR >(elt_name.str(), *c);
892  model->add(name, inst);
893  }
894  } catch (TypeError&) {
895  delete inst;
896  throw;
897  } catch (NotFound&) {
898  delete inst;
899  throw;
900  }
901  }
902 
903  template < typename GUM_SCALAR >
904  INLINE void PRMFactory< GUM_SCALAR >::incArray(const std::string& l_i,
905  const std::string& r_i) {
906  PRMSystem< GUM_SCALAR >* model = static_cast< PRMSystem< GUM_SCALAR >* >(
907  checkStack__(1, PRMObject::prm_type::SYSTEM));
908 
909  if (model->isArray(l_i)) {
910  if (model->isInstance(r_i)) {
911  model->add(l_i, model->get(r_i));
912  } else {
913  GUM_ERROR(NotFound, "right value is not an instance");
914  }
915  } else {
916  GUM_ERROR(NotFound, "left value is no an array");
917  }
918  }
919 
920  template < typename GUM_SCALAR >
922  const std::string& l_i, const std::string& l_ref, const std::string& r_i) {
923  auto model = static_cast< PRMSystem< GUM_SCALAR >* >(
924  checkStack__(1, PRMObject::prm_type::SYSTEM));
925  std::vector< PRMInstance< GUM_SCALAR >* > lefts;
926  std::vector< PRMInstance< GUM_SCALAR >* > rights;
927 
928  if (model->isInstance(l_i)) {
929  lefts.push_back(&(model->get(l_i)));
930  } else if (model->isArray(l_i)) {
931  for (const auto& elt: model->getArray(l_i))
932  lefts.push_back(elt);
933  } else {
934  GUM_ERROR(NotFound, "left value does not name an instance or an array");
935  }
936 
937  if (model->isInstance(r_i)) {
938  rights.push_back(&(model->get(r_i)));
939  } else if (model->isArray(r_i)) {
940  for (const auto& elt: model->getArray(r_i))
941  rights.push_back(elt);
942  } else {
943  GUM_ERROR(NotFound, "left value does not name an instance or an array");
944  }
945 
946  for (const auto l: lefts) {
947  for (const auto r: rights) {
948  auto& elt = l->type().get(l_ref);
950  l->add(elt.id(), *r);
951 
952  } else {
953  GUM_ERROR(NotFound, "unfound reference slot");
954  }
955  }
956  }
957  }
958 
959  template < typename GUM_SCALAR >
961  PRMClassElementContainer< GUM_SCALAR >* start, const std::string& name) {
962  std::vector< std::string > v;
963  decomposePath(name, v);
965  PRMReferenceSlot< GUM_SCALAR >* ref = nullptr;
967 
968  for (size_t i = 0; i < v.size(); ++i) {
969  try {
970  switch (current->get(v[i]).elt_type()) {
972  ref = &(static_cast< PRMReferenceSlot< GUM_SCALAR >& >(
973  current->get(v[i])));
974  elts.insert(ref);
975  current = &(/*const_cast<PRMClassElementContainer<GUM_SCALAR>&>*/ (
976  ref->slotType()));
977  break;
978 
981 
982  if (i == v.size() - 1) {
983  elts.insert(&(current->get(v[i])));
984  break;
985  } else {
986  return nullptr;
987  }
988 
989  default: {
990  return nullptr;
991  }
992  }
993  } catch (NotFound&) { return nullptr; }
994  }
995 
996  GUM_ASSERT(v.size() == elts.size());
997 
998  current->setOutputNode(*(elts.back()), true);
999 
1000  return new PRMSlotChain< GUM_SCALAR >(name, elts);
1001  }
1002 
1003  template < typename GUM_SCALAR >
1006  const std::vector< std::string >& chains,
1007  std::vector< PRMClassElement< GUM_SCALAR >* >& inputs) {
1008  bool retVal = false;
1009 
1010  for (size_t i = 0; i < chains.size(); ++i) {
1011  try {
1012  inputs.push_back(&(c->get(chains[i])));
1013  retVal = retVal
1014  || PRMClassElement< GUM_SCALAR >::isSlotChain(*(inputs.back()));
1015  } catch (NotFound&) {
1016  inputs.push_back(buildSlotChain__(c, chains[i]));
1017  retVal = true;
1018 
1019  if (inputs.back()) {
1020  c->add(inputs.back());
1021  } else {
1022  GUM_ERROR(NotFound, "unknown slot chain");
1023  }
1024  }
1025  }
1026 
1027  PRMType* t = retrieveCommonType__(inputs);
1028 
1029  std::vector< std::pair< PRMClassElement< GUM_SCALAR >*,
1031  toAdd;
1032 
1033  for (const auto& elt: inputs) {
1034  if ((*elt).type() != (*t)) {
1037  static_cast< PRMSlotChain< GUM_SCALAR >* >(elt);
1038  std::stringstream name;
1039 
1040  for (Size idx = 0; idx < sc->chain().size() - 1; ++idx) {
1041  name << sc->chain().atPos(idx)->name() << ".";
1042  }
1043 
1044  name << ".(" << t->name() << ")" << sc->lastElt().name();
1045 
1046  try {
1047  toAdd.push_back(std::make_pair(elt, &(c->get(name.str()))));
1048  } catch (NotFound&) {
1049  toAdd.push_back(
1050  std::make_pair(elt, buildSlotChain__(c, name.str())));
1051  }
1052  } else {
1053  std::stringstream name;
1054  name << "(" << t->name() << ")" << elt->name();
1055  toAdd.push_back(std::make_pair(elt, &(c->get(name.str()))));
1056  }
1057  }
1058  }
1059 
1060  return retVal;
1061  }
1062 
1063  template < typename GUM_SCALAR >
1065  const std::vector< PRMClassElement< GUM_SCALAR >* >& elts) {
1066  const PRMType* current = nullptr;
1068  // Finding all types and super types
1069 
1070  for (const auto& elt: elts) {
1071  try {
1072  current = &((*elt).type());
1073 
1074  while (current != 0) {
1075  // Filling counters
1076  if (counters.exists(current->name())) {
1077  ++(counters[current->name()]);
1078  } else {
1079  counters.insert(current->name(), 1);
1080  }
1081 
1082  // Loop guard
1083  if (current->isSubType()) {
1084  current = &(current->superType());
1085  } else {
1086  current = nullptr;
1087  }
1088  }
1089  } catch (OperationNotAllowed&) {
1091  "found a ClassElement<GUM_SCALAR> without a type");
1092  }
1093  }
1094 
1095  // We need to find the most specialized (i.e. max depth) common type
1096  current = nullptr;
1097 
1098  int max_depth = -1;
1099 
1100  int current_depth = 0;
1101 
1102  for (const auto& elt: counters) {
1103  if ((elt.second) == elts.size()) {
1104  current_depth = typeDepth__(retrieveType__(elt.first));
1105 
1106  if (current_depth > max_depth) {
1107  max_depth = current_depth;
1108  current = retrieveType__(elt.first);
1109  }
1110  }
1111  }
1112 
1113  if (current) { return const_cast< PRMType* >(current); }
1114 
1115  GUM_ERROR(NotFound, "could not find a common type");
1116  }
1117 
1118  template < typename GUM_SCALAR >
1120  const std::string& name,
1121  const std::vector< std::string >& chains,
1122  const std::vector< float >& numbers,
1123  float leak,
1124  const std::vector< std::string >& labels) {
1125  if (currentType() != PRMObject::prm_type::CLASS) {
1126  GUM_ERROR(gum::FactoryInvalidState, "invalid state to add a noisy-or");
1127  }
1128 
1130  dynamic_cast< gum::prm::PRMClass< GUM_SCALAR >* >(getCurrent());
1131 
1132  std::vector< PRMClassElement< GUM_SCALAR >* > parents;
1133 
1134  for (const auto& elt: chains)
1135  parents.push_back(&(c->get(elt)));
1136 
1137  PRMType* common_type = retrieveCommonType__(parents);
1138 
1139  for (size_t idx = 0; idx < parents.size(); ++idx) {
1140  if (parents[idx]->type() != (*common_type)) {
1141  PRMClassElement< GUM_SCALAR >* parent = parents[idx];
1142  // Either safe_name is an non existing slot chain or an existing cast
1143  // descendant
1144  std::string safe_name = parent->cast(*common_type);
1145 
1146  if (!c->exists(safe_name)) {
1148  parents[idx] = buildSlotChain__(c, safe_name);
1149  c->add(parents[idx]);
1150  } else {
1151  GUM_ERROR(NotFound, "unable to find parent");
1152  }
1153  } else {
1154  parents[idx] = &(c->get(safe_name));
1155  }
1156  }
1157  }
1158 
1159  if (numbers.size() == 1) {
1160  auto impl =
1161  new gum::MultiDimNoisyORCompound< GUM_SCALAR >(leak, numbers.front());
1162  auto attr = new PRMScalarAttribute< GUM_SCALAR >(
1163  name, retrieveType("boolean"), impl);
1164  addAttribute(attr);
1165  } else if (numbers.size() == parents.size()) {
1170  name, retrieveType("boolean"), noisy);
1171 
1172  for (size_t idx = 0; idx < numbers.size(); ++idx) {
1173  noisy->causalWeight(parents[idx]->type().variable(), numbers[idx]);
1174  }
1175 
1176  addAttribute(attr);
1177  } else {
1178  GUM_ERROR(OperationNotAllowed, "invalid parameters for a noisy or");
1179  }
1180 
1181  if (!labels.empty()) {
1183  "labels definitions not handle for noisy-or");
1184  }
1185  }
1186 
1187  template < typename GUM_SCALAR >
1188  INLINE PRMType*
1189  PRMFactory< GUM_SCALAR >::retrieveType__(const std::string& name) const {
1190  PRMType* type = nullptr;
1191  std::string full_name;
1192 
1193  // Looking for the type using its name
1194  if (prm__->typeMap__.exists(name)) {
1195  type = prm__->typeMap__[name];
1196  full_name = name;
1197  }
1198 
1199  // Looking for the type in current package
1200  std::string prefixed = addPrefix__(name);
1201  if (prm__->typeMap__.exists(prefixed)) {
1202  if (type == 0) {
1203  type = prm__->typeMap__[prefixed];
1204  full_name = prefixed;
1205  } else if (full_name != prefixed) {
1207  "Type name '" << name << "' is ambiguous: specify full name.");
1208  }
1209  }
1210 
1211  // Looking for the type relatively to current package
1212  std::string relatif_ns = currentPackage();
1213  size_t last_dot = relatif_ns.find_last_of('.');
1214  if (last_dot != std::string::npos) {
1215  relatif_ns = relatif_ns.substr(0, last_dot) + '.' + name;
1216  if (prm__->typeMap__.exists(relatif_ns)) {
1217  if (type == 0) {
1218  type = prm__->typeMap__[relatif_ns];
1219  full_name = relatif_ns;
1220  } else if (full_name != relatif_ns) {
1222  "Type name '" << name
1223  << "' is ambiguous: specify full name.");
1224  }
1225  }
1226  }
1227 
1228 
1229  // Looking for the type using all declared namespaces
1230  if (!namespaces__.empty()) {
1231  auto ns_list = namespaces__.back();
1232  for (gum::Size i = 0; i < ns_list->size(); ++i) {
1233  std::string ns = (*ns_list)[i];
1234  std::string ns_name = ns + "." + name;
1235  if (prm__->typeMap__.exists(ns_name)) {
1236  if (type == 0) {
1237  type = prm__->typeMap__[ns_name];
1238  full_name = ns_name;
1239  } else if (full_name != ns_name) {
1241  "Type name '" << name
1242  << "' is ambiguous: specify full name.");
1243  }
1244  }
1245  }
1246  }
1247 
1248  if (type == 0) {
1249  GUM_ERROR(NotFound, "Type '" << name << "' not found, check imports.");
1250  }
1251 
1252  return type;
1253  }
1254 
1255  template < typename GUM_SCALAR >
1257  PRMFactory< GUM_SCALAR >::retrieveClass__(const std::string& name) const {
1258  PRMClass< GUM_SCALAR >* a_class = nullptr;
1259  std::string full_name;
1260 
1261  // Looking for the type using its name
1262  if (prm__->classMap__.exists(name)) {
1263  a_class = prm__->classMap__[name];
1264  full_name = name;
1265  }
1266 
1267  // Looking for the type using current package
1268  std::string prefixed = addPrefix__(name);
1269  if (prm__->classMap__.exists(prefixed)) {
1270  if (a_class == nullptr) {
1271  a_class = prm__->classMap__[prefixed];
1272  full_name = prefixed;
1273  } else if (full_name != prefixed) {
1275  "Class name '" << name
1276  << "' is ambiguous: specify full name.");
1277  }
1278  }
1279 
1280  // Looking for the class using all declared namespaces
1281  if (!namespaces__.empty()) {
1282  auto ns_list = namespaces__.back();
1283  for (gum::Size i = 0; i < ns_list->size(); ++i) {
1284  std::string ns = (*ns_list)[i];
1285  std::string ns_name = ns + "." + name;
1286  if (prm__->classMap__.exists(ns_name)) {
1287  if (a_class == 0) {
1288  a_class = prm__->classMap__[ns_name];
1289  full_name = ns_name;
1290  } else if (full_name != ns_name) {
1292  "Class name '" << name
1293  << "' is ambiguous: specify full name.");
1294  }
1295  }
1296  }
1297  }
1298 
1299  if (a_class == 0) {
1300  GUM_ERROR(NotFound, "Class '" << name << "' not found, check imports.");
1301  }
1302 
1303  return a_class;
1304  }
1305 
1306  template < typename GUM_SCALAR >
1308  const std::string& name) const {
1309  PRMInterface< GUM_SCALAR >* interface = nullptr;
1310  std::string full_name;
1311 
1312  // Looking for the type using its name
1313  if (prm__->interfaceMap__.exists(name)) {
1314  interface = prm__->interfaceMap__[name];
1315  full_name = name;
1316  }
1317 
1318  // Looking for the type using current package
1319  std::string prefixed = addPrefix__(name);
1320  if (prm__->interfaceMap__.exists(prefixed)) {
1321  if (interface == nullptr) {
1322  interface = prm__->interfaceMap__[prefixed];
1323  full_name = prefixed;
1324  } else if (full_name != prefixed) {
1326  "Interface name '" << name
1327  << "' is ambiguous: specify full name.");
1328  }
1329  }
1330 
1331  // Looking for the interf using all declared namespaces
1332  if (!namespaces__.empty()) {
1333  auto ns_list = namespaces__.back();
1334  // for( const auto & ns : *(namespaces__.top()) ) {
1335  for (gum::Size i = 0; i < ns_list->size(); ++i) {
1336  std::string ns = (*ns_list)[i];
1337  std::string ns_name = ns + "." + name;
1338 
1339  if (prm__->interfaceMap__.exists(ns_name)) {
1340  if (interface == nullptr) {
1341  interface = prm__->interfaceMap__[ns_name];
1342  full_name = ns_name;
1343  } else if (full_name != ns_name) {
1345  "Interface name '"
1346  << name << "' is ambiguous: specify full name.");
1347  }
1348  }
1349  }
1350  }
1351 
1352  if (interface == nullptr) {
1354  "Interface '" << name << "' not found, check imports.");
1355  }
1356 
1357  return interface;
1358  }
1359 
1360  template < typename GUM_SCALAR >
1362  GUM_CONSTRUCTOR(PRMFactory);
1363  prm__ = new PRM< GUM_SCALAR >();
1364  }
1365 
1366  template < typename GUM_SCALAR >
1368  IPRMFactory(), prm__(prm) {
1369  GUM_CONSTRUCTOR(PRMFactory);
1370  }
1371 
1372  template < typename GUM_SCALAR >
1374  GUM_DESTRUCTOR(PRMFactory);
1375  while (!namespaces__.empty()) {
1376  auto ns = namespaces__.back();
1377  namespaces__.pop_back();
1378  delete ns;
1379  }
1380  }
1381 
1382  template < typename GUM_SCALAR >
1384  return prm__;
1385  }
1386 
1387  template < typename GUM_SCALAR >
1389  if (stack__.size() == 0) { GUM_ERROR(NotFound, "no object being built"); }
1390 
1391  return stack__.back()->obj_type();
1392  }
1393 
1394  template < typename GUM_SCALAR >
1396  if (stack__.size() == 0) { GUM_ERROR(NotFound, "no object being built"); }
1397 
1398  return stack__.back();
1399  }
1400 
1401  template < typename GUM_SCALAR >
1403  if (stack__.size() == 0) { GUM_ERROR(NotFound, "no object being built"); }
1404 
1405  return stack__.back();
1406  }
1407 
1408  template < typename GUM_SCALAR >
1410  if (stack__.size() > 0) {
1411  PRMObject* obj = stack__.back();
1412  stack__.pop_back();
1413  return obj;
1414  } else {
1415  return 0;
1416  }
1417  }
1418 
1419  template < typename GUM_SCALAR >
1420  INLINE std::string PRMFactory< GUM_SCALAR >::currentPackage() const {
1421  return (packages__.empty()) ? "" : packages__.back();
1422  }
1423 
1424  template < typename GUM_SCALAR >
1425  INLINE void
1427  std::string super) {
1428  std::string real_name = addPrefix__(name);
1429  if (prm__->typeMap__.exists(real_name)) {
1430  GUM_ERROR(DuplicateElement, "'" << real_name << "' is already used.");
1431  }
1432  if (super == "") {
1433  auto t = new PRMType(LabelizedVariable(real_name, "", 0));
1434  stack__.push_back(t);
1435  } else {
1436  auto t = new PRMType(LabelizedVariable(real_name, "", 0));
1437  t->superType__ = retrieveType__(super);
1438  t->label_map__ = new std::vector< Idx >();
1439  stack__.push_back(t);
1440  }
1441  }
1442 
1443  template < typename GUM_SCALAR >
1444  INLINE void PRMFactory< GUM_SCALAR >::addLabel(const std::string& l,
1445  std::string extends) {
1446  if (extends == "") {
1447  PRMType* t =
1448  static_cast< PRMType* >(checkStack__(1, PRMObject::prm_type::TYPE));
1449  LabelizedVariable* var = dynamic_cast< LabelizedVariable* >(t->var__);
1450 
1451  if (!var) {
1453  "the current type's variable is not a LabelizedVariable.");
1454  } else if (t->superType__) {
1455  GUM_ERROR(OperationNotAllowed, "current type is a subtype.");
1456  }
1457 
1458  try {
1459  var->addLabel(l);
1460  } catch (DuplicateElement&) {
1461  GUM_ERROR(DuplicateElement, "a label '" << l << "' already exists");
1462  }
1463  } else {
1464  PRMType* t =
1465  static_cast< PRMType* >(checkStack__(1, PRMObject::prm_type::TYPE));
1466  LabelizedVariable* var = dynamic_cast< LabelizedVariable* >(t->var__);
1467 
1468  if (!var) {
1470  "the current type's variable is not a LabelizedVariable.");
1471  } else if (!t->superType__) {
1472  GUM_ERROR(OperationNotAllowed, "current type is not a subtype.");
1473  }
1474 
1475  bool found = false;
1476 
1477  for (Idx i = 0; i < t->superType__->var__->domainSize(); ++i) {
1478  if (t->superType__->var__->label(i) == extends) {
1479  try {
1480  var->addLabel(l);
1481  } catch (DuplicateElement&) {
1482  GUM_ERROR(DuplicateElement, "a label '" << l << "' already exists");
1483  }
1484 
1485  t->label_map__->push_back(i);
1486 
1487  found = true;
1488  break;
1489  }
1490  }
1491 
1492  if (!found) { GUM_ERROR(NotFound, "inexistent label in super type."); }
1493  }
1494  }
1495 
1496  template < typename GUM_SCALAR >
1498  PRMType* t =
1499  static_cast< PRMType* >(checkStack__(1, PRMObject::prm_type::TYPE));
1500 
1501  if (!t->isValid__()) {
1502  GUM_ERROR(OperationNotAllowed, "current type is not a valid subtype");
1503  } else if (t->variable().domainSize() < 2) {
1505  "current type is not a valid discrete type");
1506  }
1507 
1508  prm__->typeMap__.insert(t->name(), t);
1509 
1510  prm__->types__.insert(t);
1511  stack__.pop_back();
1512  }
1513 
1514  template < typename GUM_SCALAR >
1515  INLINE void
1517  std::string real_name = addPrefix__(name);
1518  if (prm__->typeMap__.exists(real_name)) {
1519  GUM_ERROR(DuplicateElement, "'" << real_name << "' is already used.");
1520  }
1521  auto var = DiscretizedVariable< double >(real_name, "");
1522  auto t = new PRMType(var);
1523  stack__.push_back(t);
1524  }
1525 
1526  template < typename GUM_SCALAR >
1527  INLINE void PRMFactory< GUM_SCALAR >::addTick(double tick) {
1528  PRMType* t =
1529  static_cast< PRMType* >(checkStack__(1, PRMObject::prm_type::TYPE));
1531  dynamic_cast< DiscretizedVariable< double >* >(t->var__);
1532 
1533  if (!var) {
1535  "the current type's variable is not a LabelizedVariable.");
1536  }
1537 
1538  try {
1539  var->addTick(tick);
1540  } catch (DefaultInLabel&) {
1541  GUM_ERROR(OperationNotAllowed, "tick already in used for this variable");
1542  }
1543  }
1544 
1545  template < typename GUM_SCALAR >
1547  PRMType* t =
1548  static_cast< PRMType* >(checkStack__(1, PRMObject::prm_type::TYPE));
1549 
1550  if (t->variable().domainSize() < 2) {
1552  "current type is not a valid discrete type");
1553  }
1554 
1555  prm__->typeMap__.insert(t->name(), t);
1556 
1557  prm__->types__.insert(t);
1558  stack__.pop_back();
1559  }
1560 
1561  template < typename GUM_SCALAR >
1562  INLINE void PRMFactory< GUM_SCALAR >::addRangeType(const std::string& name,
1563  long minVal,
1564  long maxVal) {
1565  std::string real_name = addPrefix__(name);
1566  if (prm__->typeMap__.exists(real_name)) {
1567  std::stringstream msg;
1568  msg << "\"" << real_name << "' is already used.";
1569  GUM_ERROR(DuplicateElement, msg.str());
1570  }
1571 
1572  auto var = RangeVariable(real_name, "", minVal, maxVal);
1573  auto t = new PRMType(var);
1574 
1575  if (t->variable().domainSize() < 2) {
1577  "current type is not a valid discrete type");
1578  }
1579 
1580  prm__->typeMap__.insert(t->name(), t);
1581  prm__->types__.insert(t);
1582  }
1583 
1584  template < typename GUM_SCALAR >
1587  stack__.pop_back();
1588  }
1589 
1590  template < typename GUM_SCALAR >
1591  INLINE void PRMFactory< GUM_SCALAR >::addAttribute(const std::string& type,
1592  const std::string& name) {
1594  startAttribute(type, name);
1595  endAttribute();
1596  }
1597 
1598  template < typename GUM_SCALAR >
1599  INLINE void PRMFactory< GUM_SCALAR >::startAttribute(const std::string& type,
1600  const std::string& name,
1601  bool scalar_attr) {
1603  PRMAttribute< GUM_SCALAR >* a = nullptr;
1604 
1605  if (PRMObject::isClass(*c) && (!scalar_attr)) {
1607  static_cast< PRMClass< GUM_SCALAR >& >(*c),
1608  name,
1609  *retrieveType__(type));
1610 
1611  } else {
1612  a = new PRMScalarAttribute< GUM_SCALAR >(name, *retrieveType__(type));
1613  }
1614 
1615  std::string dot = ".";
1616 
1617  try {
1618  try {
1619  c->add(a);
1620  } catch (DuplicateElement&) { c->overload(a); }
1621  } catch (Exception&) {
1622  if (a != nullptr && (!c->exists(a->id()))) { delete a; }
1623  }
1624 
1625  stack__.push_back(a);
1626  }
1627 
1628  template < typename GUM_SCALAR >
1629  INLINE void
1632  if (!c->exists(name)) { GUM_ERROR(NotFound, name << "not found"); }
1633  auto& a = c->get(name);
1635  GUM_ERROR(OperationNotAllowed, name << " not an attribute");
1636  }
1637  stack__.push_back(&a);
1638  }
1639 
1640  template < typename GUM_SCALAR >
1643  stack__.pop_back();
1644  }
1645 
1646  template < typename GUM_SCALAR >
1647  INLINE void PRMFactory< GUM_SCALAR >::startSystem(const std::string& name) {
1648  if (prm__->systemMap__.exists(name)) {
1649  GUM_ERROR(DuplicateElement, "'" << name << "' is already used.");
1650  }
1651  PRMSystem< GUM_SCALAR >* model =
1653  stack__.push_back(model);
1654  prm__->systemMap__.insert(model->name(), model);
1655  prm__->systems__.insert(model);
1656  }
1657 
1658  template < typename GUM_SCALAR >
1660  try {
1661  PRMSystem< GUM_SCALAR >* model = static_cast< PRMSystem< GUM_SCALAR >* >(
1663  stack__.pop_back();
1664  model->instantiate();
1665  } catch (Exception&) { GUM_ERROR(FatalError, "could not create system"); }
1666  }
1667 
1668  template < typename GUM_SCALAR >
1669  INLINE void PRMFactory< GUM_SCALAR >::addInstance(const std::string& type,
1670  const std::string& name) {
1671  auto c = retrieveClass__(type);
1672 
1673  // If class contains parameters, calls the proper addIsntance method
1674  if (c->parameters().size() > 0) {
1676  addInstance(type, name, params);
1677 
1678  } else {
1679  addInstance__(c, name);
1680  }
1681  }
1682 
1683  template < typename GUM_SCALAR >
1685  const std::string& type,
1686  const std::string& name,
1687  const HashTable< std::string, double >& params) {
1688  auto c = retrieveClass__(type);
1689 
1690  if (c->parameters().empty()) {
1691  if (params.empty()) {
1692  addInstance__(c, name);
1693  } else {
1695  "Class " + type + " does not have parameters");
1696  }
1697 
1698  } else {
1699  auto my_params = params;
1700  // Adding all parameters to my_params
1701  for (const auto& p: c->parameters()) {
1702  if (!my_params.exists(p->name())) {
1703  my_params.insert(p->name(), p->value());
1704  }
1705  }
1706 
1707  // Building sub class name using my_params
1708  std::stringstream sBuff;
1709  sBuff << c->name() << "<";
1710 
1711  for (const auto& p: my_params) {
1712  sBuff << p.first << "=" << p.second << ",";
1713  }
1714 
1715  // Removing last , and adding closing >
1716  std::string sub_c = sBuff.str().substr(0, sBuff.str().size() - 1) + ">";
1717 
1718  // Adding class in current package
1719  try {
1720  auto pck_cpy = packages__;
1721  packages__.clear();
1722 
1723  startClass(sub_c, c->name());
1724 
1725  // Update inherited parameters
1726  for (auto p: my_params) {
1727  auto type = static_cast< PRMParameter< GUM_SCALAR >& >(c->get(p.first))
1728  .valueType();
1730  addParameter("int", p.first, p.second);
1731 
1732  } else {
1733  addParameter("real", p.first, p.second);
1734  }
1735  }
1736 
1737  endClass();
1738 
1739  packages__ = pck_cpy;
1740 
1741  } catch (DuplicateElement&) {
1742  // Sub Class already exists in this system
1743  }
1744  c = retrieveClass__(sub_c);
1745  addInstance__(c, name);
1746  }
1747  }
1748 
1749  template < typename GUM_SCALAR >
1750  INLINE void
1752  const std::string& name) {
1753  PRMInstance< GUM_SCALAR >* i = nullptr;
1754  try {
1755  auto s = static_cast< PRMSystem< GUM_SCALAR >* >(
1757  i = new PRMInstance< GUM_SCALAR >(name, *type);
1758  s->add(i);
1759 
1760  } catch (OperationNotAllowed&) {
1761  if (i) { delete i; }
1762  throw;
1763  }
1764  }
1765 
1766  template < typename GUM_SCALAR >
1767  INLINE std::string
1768  PRMFactory< GUM_SCALAR >::addPrefix__(const std::string& str) const {
1769  if (!packages__.empty()) {
1770  std::string full_name = packages__.back();
1771  full_name.append(".");
1772  full_name.append(str);
1773  return full_name;
1774  } else {
1775  return str;
1776  }
1777  }
1778 
1779  template < typename GUM_SCALAR >
1780  INLINE PRMObject*
1782  PRMObject::prm_type obj_type) {
1783  // Don't forget that Idx are unsigned int
1784  if (stack__.size() - i > stack__.size()) {
1785  GUM_ERROR(FactoryInvalidState, "illegal sequence of calls");
1786  }
1787 
1788  PRMObject* obj = stack__[stack__.size() - i];
1789 
1790  if (obj->obj_type() != obj_type) {
1791  GUM_ERROR(FactoryInvalidState, "illegal sequence of calls");
1792  }
1793 
1794  return obj;
1795  }
1796 
1797  template < typename GUM_SCALAR >
1800  // Don't forget that Idx are unsigned int
1801  if (stack__.size() - i > stack__.size()) {
1802  GUM_ERROR(FactoryInvalidState, "illegal sequence of calls");
1803  }
1804 
1805  PRMObject* obj = stack__[stack__.size() - i];
1806 
1807  if ((obj->obj_type() == PRMObject::prm_type::CLASS)
1809  return static_cast< PRMClassElementContainer< GUM_SCALAR >* >(obj);
1810  } else {
1811  GUM_ERROR(FactoryInvalidState, "illegal sequence of calls");
1812  }
1813  }
1814 
1815  template < typename GUM_SCALAR >
1818  // Don't forget that Idx are unsigned int
1819  if (stack__.size() - i > stack__.size()) {
1820  GUM_ERROR(FactoryInvalidState, "illegal sequence of calls");
1821  }
1822 
1824  dynamic_cast< PRMClassElement< GUM_SCALAR >* >(
1825  stack__[stack__.size() - i]);
1826 
1827  if (obj == 0) {
1828  GUM_ERROR(FactoryInvalidState, "illegal sequence of calls");
1829  }
1830 
1831  if (obj->elt_type() != elt_type) {
1832  GUM_ERROR(FactoryInvalidState, "illegal sequence of calls");
1833  }
1834 
1835  return obj;
1836  }
1837 
1838  template < typename GUM_SCALAR >
1840  int depth = 0;
1841  const PRMType* current = t;
1842 
1843  while (current->isSubType()) {
1844  ++depth;
1845  current = &(current->superType());
1846  }
1847 
1848  return depth;
1849  }
1850 
1851  template < typename GUM_SCALAR >
1852  INLINE void PRMFactory< GUM_SCALAR >::pushPackage(const std::string& name) {
1853  packages__.push_back(name);
1854  namespaces__.push_back(new List< std::string >());
1855  }
1856 
1857  template < typename GUM_SCALAR >
1859  std::string plop = currentPackage();
1860 
1861  if (!packages__.empty()) {
1862  std::string s = packages__.back();
1863  packages__.pop_back();
1864 
1865  if (namespaces__.size() > 0) {
1866  delete namespaces__.back();
1867  namespaces__.pop_back();
1868  }
1869  return s;
1870  }
1871 
1872  return plop;
1873  }
1874 
1875  template < typename GUM_SCALAR >
1876  INLINE void PRMFactory< GUM_SCALAR >::addImport(const std::string& name) {
1877  if (name.size() == 0) {
1878  GUM_ERROR(OperationNotAllowed, "illegal import name");
1879  }
1880  if (namespaces__.empty()) {
1881  namespaces__.push_back(new List< std::string >());
1882  }
1883  namespaces__.back()->push_back(name);
1884  }
1885 
1886  template < typename GUM_SCALAR >
1887  INLINE void
1889  const std::string& r_i) {
1890  size_t pos = l_i.find_last_of('.');
1891 
1892  if (pos != std::string::npos) {
1893  std::string l_ref = l_i.substr(pos + 1, std::string::npos);
1894  setReferenceSlot(l_i.substr(0, pos), l_ref, r_i);
1895  } else {
1896  GUM_ERROR(NotFound, "left value does not name an instance or an array");
1897  }
1898  }
1899 
1900  template < typename GUM_SCALAR >
1901  INLINE PRMClass< GUM_SCALAR >&
1902  PRMFactory< GUM_SCALAR >::retrieveClass(const std::string& name) {
1903  return *retrieveClass__(name);
1904  }
1905 
1906  template < typename GUM_SCALAR >
1907  INLINE PRMType&
1908  PRMFactory< GUM_SCALAR >::retrieveType(const std::string& name) {
1909  return *retrieveType__(name);
1910  }
1911 
1912  template < typename GUM_SCALAR >
1914  const std::vector< PRMClassElement< GUM_SCALAR >* >& elts) {
1915  return *(retrieveCommonType__(elts));
1916  }
1917 
1918 
1919  template < typename GUM_SCALAR >
1921  const std::string& type) const {
1922  try {
1923  retrieveClass__(type);
1924  return true;
1925 
1926  } catch (NotFound&) {
1927  } catch (DuplicateElement&) {}
1928 
1929  try {
1930  retrieveInterface__(type);
1931  return true;
1932 
1933  } catch (NotFound&) {
1934  } catch (DuplicateElement&) {}
1935 
1936  return false;
1937  }
1938 
1939  template < typename GUM_SCALAR >
1941  const std::string& name) const {
1942  const PRMSystem< GUM_SCALAR >* system =
1943  static_cast< const PRMSystem< GUM_SCALAR >* >(getCurrent());
1944  return (system && system->isArray(name));
1945  }
1946 
1947  template < typename GUM_SCALAR >
1949  const std::vector< std::string >& array) {
1951 
1952  auto a = static_cast< PRMFormAttribute< GUM_SCALAR >* >(
1954 
1955  if (a->formulas().domainSize() != array.size()) {
1956  GUM_ERROR(OperationNotAllowed, "illegal CPF size");
1957  }
1958 
1959  if (a->formulas().nbrDim() == 1) {
1960  setRawCPFByLines(array);
1961 
1962  } else {
1963  Instantiation inst(a->formulas());
1964  Instantiation jnst;
1965  for (auto idx = inst.variablesSequence().rbegin();
1966  idx != inst.variablesSequence().rend();
1967  --idx) {
1968  jnst.add(**idx);
1969  }
1970 
1971  jnst.setFirst();
1972  auto idx = (std::size_t)0;
1973  while ((!jnst.end()) && idx < array.size()) {
1974  inst.setVals(jnst);
1975  a->formulas().set(inst, array[idx]);
1976  jnst.inc();
1977  ++idx;
1978  }
1979 
1980  // Generate cpf by calling it
1981  a->cpf();
1982  }
1983  }
1984 
1985  template < typename GUM_SCALAR >
1987  const std::vector< std::string >& array) {
1989 
1990  auto a = static_cast< PRMFormAttribute< GUM_SCALAR >* >(
1992 
1993  if (a->formulas().domainSize() != array.size()) {
1994  GUM_ERROR(OperationNotAllowed, "illegal CPF size");
1995  }
1996 
1997  a->formulas().populate(array);
1998 
2000  a->cpf();
2001  }
2002 
2003  } /* namespace prm */
2004 } /* namespace gum */
PRMObject * checkStack__(Idx i, PRMObject::prm_type obj_type)
Return a pointer on a PRMObject at stack__.size() - i position after checking the type of the object ...
void addParent__(PRMClassElementContainer< GUM_SCALAR > *c, PRMAttribute< GUM_SCALAR > *agg, const std::string &name)
Add a parent to an attribute.
virtual NodeId add(PRMClassElement< GUM_SCALAR > *elt)
See gum::prm::add(PRMClassElement<GUM_SCALAR>*).
Definition: PRMClass_tpl.h:621
const Set< PRMInterface< GUM_SCALAR > *> & implements() const
Returns the Set of PRMInterface<GUM_SCALAR> implemented by this Class<GUM_SCALAR>.
PRMParameter is a member of a Class in a PRM.
Definition: PRMParameter.h:52
virtual void endInterface() override
Tells the factory that we finished an interface declaration.
PRMSlotChain< GUM_SCALAR > * buildSlotChain__(PRMClassElementContainer< GUM_SCALAR > *start, const std::string &name)
This methods build a PRMSlotChain<GUM_SCALAR> given a starting element and a string.
virtual void pushPackage(const std::string &name) override
Define the current package.
virtual void addAggregator(const std::string &name, const std::string &agg_type, const std::vector< std::string > &chains, const std::vector< std::string > &params, std::string type="") override
Add an aggregator in the current declared class.
virtual void addRangeType(const std::string &name, long minVal, long maxVal) override
Add a range variable type declaration.
bool empty() const noexcept
Indicates whether the set is the empty set.
Definition: set_tpl.h:707
static AggregateType str2enum(const std::string &str)
Static method which returns the AggregateType given its string representation.
Definition: PRMAggregate.h:101
static INLINE bool isClass(const PRMObject &obj)
Returns true if obj_ptr is of type Class.
Definition: PRMObject.h:102
DiscreteVariable & variable()
Return a reference on the DiscreteVariable contained in this.
Definition: PRMType_inl.h:45
MultiDimImplementation< GUM_SCALAR > * buildImpl() const
Returns a pointer over an empty gum::MultiDimImplementation of the good type for this PRMAggregate...
void add(NodeId id, PRMInstance< GUM_SCALAR > &instance)
Add an PRMInstance<GUM_SCALAR> to a given PRMReferenceSlot, PRMSlotChain<GUM_SCALAR> or output node...
class LabelizedVariable
void endAggregator()
Finishes an aggregate declaration.
virtual void setReferenceSlot(const std::string &left_instance, const std::string &left_reference, const std::string &right_instance) override
Instantiate a reference in the current model.
Copyright 2005-2020 Pierre-Henri WUILLEMIN() & Christophe GONZALES() info_at_agrum_dot_org.
virtual ~PRMFactory()
Destructor.
virtual void addNoisyOrCompound(const std::string &name, const std::vector< std::string > &chains, const std::vector< float > &numbers, float leak, const std::vector< std::string > &label) override
Add a compound noisy-or as an PRMAttribute<GUM_SCALAR> to the current Class<GUM_SCALAR>.
const std::string & name() const
Returns the name of this object.
Definition: PRMObject_inl.h:35
virtual std::string popPackage() override
Pop the current package from the package stack.
Size size() const noexcept
Returns the size of the sequence.
Definition: sequence_tpl.h:38
virtual void addInstance(const std::string &type, const std::string &name) override
Add an instance to the model.
virtual void setRawCPFByFloatColumns(const std::vector< float > &array) override
Gives the factory the CPF in its raw form.
virtual PRMType & type()=0
See gum::PRMClassElement::type().
virtual void addArray(const std::string &type, const std::string &name, Size size) override
Creates an array with the given number of instances of the given type.
An PRMInstance is a Bayesian Network fragment defined by a Class and used in a PRMSystem.
Definition: PRMInstance.h:63
virtual void setCPFByFloatRule(const std::vector< std::string > &labels, const std::vector< float > &values) override
Fills the CPF using a rule.
The generic class for storing (ordered) sequences of objects.
Definition: sequence.h:1022
Class for discretized random variable.
virtual prm_type obj_type() const =0
Returns the type of this object.
virtual void continueClass(const std::string &c) override
Continue the declaration of a class.
virtual void continueInterface(const std::string &name) override
Continue the declaration of an interface.
Abstract class representing an element of PRM class.
virtual void addImport(const std::string &name) override
Add an import for namespace lookup.
virtual void startDiscretizedType(const std::string &name) override
Start a discretized type declaration.
PRMClass< GUM_SCALAR > & retrieveClass(const std::string &name)
Returns a reference over a Class<GUM_SCALAR> given its name.
virtual void endAttribute() override
Tells the factory that we finished declaring an attribute.
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
std::vector< std::string > packages__
The prefix used for classes and types names. It is normally the namespace of the corresponding compil...
Definition: PRMFactory.h:1042
virtual void startInterface(const std::string &i, const std::string &ext="", bool delayInheritance=false) override
Tells the factory that we start an interface declaration.
virtual void addReferenceSlot(const std::string &type, const std::string &name, bool isArray) override
Tells the factory that we started declaring a slot.
Instantiation & chgVal(const DiscreteVariable &v, Idx newval)
Assign newval to variable v in the Instantiation.
virtual PRMClassElement< GUM_SCALAR > & get(const std::string &name)=0
Getter on a member of this PRMClassElementContainer.
PRMType & retrieveCommonType(const std::vector< PRMClassElement< GUM_SCALAR > * > &elts)
Returns a pointer on the PRM<GUM_SCALAR> created by this factory.
Copyright 2005-2020 Pierre-Henri WUILLEMIN() & Christophe GONZALES() info_at_agrum_dot_org.
PRMClass< GUM_SCALAR > * retrieveClass__(const std::string &name) const
Returns a pointer on a class given it&#39;s name. Used when building models, meaning that the class name ...
Base class for discrete random variable.
PRMType & retrieveType(const std::string &name)
Returns a reference over a PRMType given its name.
virtual bool exists(const std::string &name) const
Returns true if a member with the given name exists in this PRMClassElementContainer or in the PRMCla...
Copyright 2005-2020 Pierre-Henri WUILLEMIN() & Christophe GONZALES() info_at_agrum_dot_org.
Definition: agrum.h:25
virtual void addAttribute(const std::string &type, const std::string &name) override
Add an attribute to an interface.
Instantiation & setVals(const Instantiation &i)
Assign the values from i in the Instantiation.
void setLabel(Idx idx)
Set the aggregator&#39;s label.
A PRMReferenceSlot represent a relation between two PRMClassElementContainer.
Definition: PRMObject.h:223
virtual void startClass(const std::string &c, const std::string &ext="", const Set< std::string > *implements=nullptr, bool delayInheritance=false) override
Tells the factory that we start a class declaration.
PRMClassElementContainer< GUM_SCALAR > & slotType()
Returns the type of this slot, which is a PRMClassElementContainer (it is not the type of PRMObject)...
void continueAggregator(const std::string &name)
Conitnues an aggregator declaration.
virtual void incArray(const std::string &l_i, const std::string &r_i) override
Add an instance to an array.
virtual void startSystem(const std::string &name) override
Tells the factory that we started declaring a model.
virtual void setRawCPFByFloatLines(const std::vector< float > &array) override
Gives the factory the CPF in its raw form.
virtual void startAttribute(const std::string &type, const std::string &name, bool scalar_atttr=false) override
Tells the factory that we start an attribute declaration.
virtual std::string cast(const PRMType &t) const
Returns the name of the cast descendant with PRMType t of this PRMClassElement.
NodeId add(PRMInstance< GUM_SCALAR > *i)
Add an PRMInstance to this system.
Definition: PRMSystem_tpl.h:69
virtual std::string currentPackage() const override
const std::string & safeName() const
Returns the safe name of this PRMClassElement, if any.
virtual Size domainSize() const =0
void setRawCPFByColumns(const std::vector< GUM_SCALAR > &array)
Gives the factory the CPF in its raw form.
prm_type
Enumeration of the different types of objects handled by a PRM.
Definition: PRMObject.h:69
virtual PRMObject::prm_type currentType() const override
bool retrieveInputs__(PRMClass< GUM_SCALAR > *c, const std::vector< std::string > &chains, std::vector< PRMClassElement< GUM_SCALAR > * > &inputs)
Retrieve inputs for an PRMAggregate.
bool isInstance(const std::string &name) const
Returns true if an PRMInstance with the given name exists.
PRMInstance< GUM_SCALAR > & get(NodeId id)
Returns an PRMInstance given it&#39;s NodeId in the relational skeleton.
Factory which builds a PRM<GUM_SCALAR>.
Definition: PRMType.h:50
const std::string & labelValue() const
See gum::PRMClassElement::elt_type().
std::string to_string(const Formula &f)
Definition: formula_inl.h:499
bool isMultiple() const
Return true if this slot chain contains at least one multiple reference slot.
DiscretizedVariable & addTick(const T_TICKS &aTick)
add a tick.
PRMClassElementContainer< GUM_SCALAR > * checkStackContainter__(Idx i)
Adds prefix__ to str iff prefix__ != "".
virtual std::string label(Idx i) const =0
get the indice-th label. This method is pure virtual.
std::string addPrefix__(const std::string &str) const
Adds prefix__ to str iff prefix__ != "".
AggregateType agg_type() const
Returns the aggregate of *this.
virtual const Potential< GUM_SCALAR > & cpf() const =0
See gum::PRMClassElement::cpf().
virtual void setCPFByRule(const std::vector< std::string > &labels, const std::vector< GUM_SCALAR > &values)
Fills the CPF using a rule.
<agrum/PRM/elements/funcAttribute.h>
PRMClassElement< GUM_SCALAR > & get(NodeId id)
See gum::prm::PRMClassElementContainer<GUM_SCALAR>::get(NodeId).
virtual void addArc(const std::string &tail, const std::string &head)
See gum::prm::PRMClassElementContainer<GUM_SCALAR>::addArc().
Definition: PRMClass_tpl.h:483
A PRMSystem is a container of PRMInstance and describe a relational skeleton.
Definition: PRMObject.h:229
bool exists(const Key &k) const
Check the existence of k in the sequence.
Definition: sequence_tpl.h:402
Base class for all aGrUM&#39;s exceptions.
Definition: exceptions.h:106
void instantiate()
Instantiate all the PRMInstance in this PRMSystem.
virtual PRMObject * getCurrent() override
virtual NodeId overload(PRMClassElement< GUM_SCALAR > *elt)
See gum::prm::overload(PRMClassElement<GUM_SCALAR>*).
Definition: PRMClass_tpl.h:733
PRMFactory()
Default constructor.
PRM< GUM_SCALAR > * prm__
The pointer on the PRM<GUM_SCALAR> built by this factory.
Definition: PRMFactory.h:1048
This is a decoration of the DiscreteVariable class.
Definition: PRMType.h:63
An PRMInterface is implemented by a Class<GUM_SCALAR> and defines a set of PRMReferenceSlot<GUM_SCALA...
Definition: PRMClass.h:54
virtual ClassElementType elt_type() const =0
Return the type of class element this object is.
int typeDepth__(const PRMType *t)
Returns the inheritance depth of a PRMType.
virtual void endClass(bool checkImplementations=true) override
Tells the factory that we finished a class declaration.
virtual PRMObject * closeCurrent() override
Close current object being built.
Defines a discrete random variable over an integer interval.
Definition: rangeVariable.h:54
A PRMSlotChain represents a sequence of gum::prm::PRMClassElement<GUM_SCALAR> where the n-1 first gum...
Definition: PRMObject.h:221
Copyright 2005-2020 Pierre-Henri WUILLEMIN() & Christophe GONZALES() info_at_agrum_dot_org.
void setRawCPFByLines(const std::vector< GUM_SCALAR > &array)
Gives the factory the CPF in its raw form.
virtual NodeId add(PRMClassElement< GUM_SCALAR > *elt)=0
Add a PRMClassElement<GUM_SCALAR> to this PRMClassElementContainer.
Abstract base class for any element defined in a PRM.
Definition: PRMObject.h:56
virtual NodeId overload(PRMClassElement< GUM_SCALAR > *elt)=0
Add a PRMClassElement<GUM_SCALAR> which overload an inherited PRMClassElement<GUM_SCALAR>.
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:83
virtual PRMType & type()
See gum::PRMClassElement::type().
std::vector< List< std::string > *> namespaces__
Set of all declared namespaces.
Definition: PRMFactory.h:1045
virtual void setOutputNode(const PRMClassElement< GUM_SCALAR > &elt, bool b)
Set the output flag value of id at b.
bool hasLabel() const
Returns true if the label is defined.
This class represents a Probabilistic Relational PRMSystem<GUM_SCALAR>.
Definition: PRM.h:66
virtual void startDiscreteType(const std::string &name, std::string super="") override
Start a discrete subtype declaration.
virtual void endDiscretizedType() override
End the current discretized type declaration.
<agrum/PRM/classElementContainer.h>
NodeId id() const
Returns the NodeId of this element in it&#39;s class DAG.
A PRMClass is an object of a PRM representing a fragment of a Bayesian Network which can be instantia...
Definition: PRMClass.h:66
GUM_SCALAR causalWeight(const DiscreteVariable &v) const
Copy of a multiDimICIModel.
bool isArray(const std::string &name) const
Returns true if an array with the given name exists.
void decomposePath(const std::string &path, std::vector< std::string > &v)
Decompose a string in a vector of strings using "." as separators.
Definition: utils_prm.cpp:29
virtual void endDiscreteType() override
End the current discrete type declaration.
virtual void continueAttribute(const std::string &name) override
Continues the declaration of an attribute.
static INLINE bool isSlotChain(const PRMClassElement< GUM_SCALAR > &elt)
Return true if obj is of type PRMSlotChain.
Copyright 2005-2020 Pierre-Henri WUILLEMIN() & Christophe GONZALES() info_at_agrum_dot_org.
Size Idx
Type for indexes.
Definition: types.h:53
void addParameter(const std::string &type, const std::string &name, double value) override
Add a parameter to the current class with a default value.
PRMInterface< GUM_SCALAR > * retrieveInterface__(const std::string &name) const
Returns a pointer on an interface given it&#39;s name. Used when building models, meaning that the interf...
void add(const DiscreteVariable &v) final
Adds a new variable in the Instantiation.
virtual bool isClassOrInterface(const std::string &type) const override
virtual void addLabel(const std::string &l, std::string ext="") override
Add a label to the current discrete type.
PRMAttribute is a member of a Class in a PRM.
Definition: PRMAttribute.h:61
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
PRMClassElement< GUM_SCALAR > & lastElt()
Returns the last element of the slot chain, typically this is an gum::PRMAttribute or a gum::PRMAggre...
virtual void addArc(const std::string &tail, const std::string &head)=0
Add an arc between two PRMClassElement<GUM_SCALAR>.
void addInstance__(PRMClass< GUM_SCALAR > *type, const std::string &name)
Adds a instance to the current model.
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
const std::string & name() const
returns the name of the variable
virtual void endSystem() override
Tells the factory that we finished declaring a model.
LabelizedVariable & addLabel(const std::string &aLabel)
add a label with a new index (we assume that we will NEVER remove a label)
void checkInterfaceImplementation__(PRMClass< GUM_SCALAR > *c)
Check if c implements correctly all his interfaces.
bool empty() const noexcept
Indicates whether the hash table is empty.
void addArray(const std::string &array, PRMClassElementContainer< GUM_SCALAR > &type)
Add an array of instances in this system. If the array doesn&#39;t exists it is created.
virtual const DAG & containerDag() const
Returns the gum::DAG of this PRMClassElementContainer.
<agrum/PRM/elements/formAttribute.h>
<agrum/PRM/elements/scalarAttribute.h>
Idx label() const
Returns the label&#39;s index on which this aggregate applies.
PRM< GUM_SCALAR > * prm() const
Returns a pointer on the PRM<GUM_SCALAR> created by this factory.
virtual bool isArrayInCurrentSystem(const std::string &name) const override
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:613
PRMType * retrieveType__(const std::string &name) const
Returns a pointer on a PRMType given it&#39;s name. Since the type can be given either with it&#39;s local na...
const Key & back() const
Returns the last element of the sequence.
Definition: sequence_tpl.h:568
void startAggregator(const std::string &name, const std::string &agg_type, const std::string &rv_type, const std::vector< std::string > &params)
Start an aggregator declaration.
virtual void addParent(const std::string &name) override
Tells the factory that we add a parent to the current declared attribute.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
PRMType * retrieveCommonType__(const std::vector< PRMClassElement< GUM_SCALAR > * > &elts)
Retrieve the common PRMType of a vector of PRMClassElement<GUM_SCALAR>.
virtual void addTick(double tick) override
Add a tick to the current discretized type.
Sequence< PRMClassElement< GUM_SCALAR > *> & chain()
Return the sequence representing the chain of elements in this PRMSlotChain.
non-template interface-like parent for every PRM Factory
Definition: IPRMFactory.h:57
ClassElementType
Returns true if obj_ptr is of type PRMReferenceSlot.
std::vector< PRMObject *> stack__
A stack used to keep track of created PRMObject.
Definition: PRMFactory.h:1051
Copyright 2005-2020 Pierre-Henri WUILLEMIN() & Christophe GONZALES() info_at_agrum_dot_org.
void insert(const Key &k)
Insert an element at the end of the sequence.
Definition: sequence_tpl.h:408