aGrUM  0.21.0
a C++ library for (probabilistic) graphical models
gum::prm::PRMFactory< GUM_SCALAR > Class Template Reference

Factory which builds a PRM<GUM_SCALAR>. More...

#include <PRMType.h>

+ Inheritance diagram for gum::prm::PRMFactory< GUM_SCALAR >:
+ Collaboration diagram for gum::prm::PRMFactory< GUM_SCALAR >:

Public Member Functions

Constructors & destructor
 PRMFactory ()
 Default constructor. More...
 
 PRMFactory (PRM< GUM_SCALAR > *prm)
 This constructor let you set on which PRM instance the factory works. More...
 
 PRMFactory (const PRMFactory &from)=delete
 Copy constructor. Don't use it. More...
 
PRMFactoryoperator= (const PRMFactory &from)=delete
 Copy operator. Don't use it. More...
 
virtual ~PRMFactory ()
 Destructor. More...
 
Getters & Setters
PRM< GUM_SCALAR > * prm () const
 Returns a pointer on the PRM<GUM_SCALAR> created by this factory. More...
 
virtual PRMObject::prm_type currentType () const override
 
virtual PRMObjectgetCurrent () override
 
virtual const PRMObjectgetCurrent () const override
 Returns a pointer on the PRM<GUM_SCALAR> created by this factory. More...
 
virtual PRMObjectcloseCurrent () override
 Close current object being built. More...
 
virtual std::string currentPackage () const override
 
PRMClass< GUM_SCALAR > & retrieveClass (const std::string &name)
 Returns a reference over a Class<GUM_SCALAR> given its name. More...
 
PRMTyperetrieveType (const std::string &name)
 Returns a reference over a PRMType given its name. More...
 
PRMTyperetrieveCommonType (const std::vector< PRMClassElement< GUM_SCALAR > * > &elts)
 Returns a pointer on the PRM<GUM_SCALAR> created by this factory. More...
 
Package construction methods.
virtual void pushPackage (const std::string &name) override
 Define the current package. More...
 
virtual std::string popPackage () override
 Pop the current package from the package stack. More...
 
virtual void addImport (const std::string &name) override
 Add an import for namespace lookup. More...
 
PRMType construction methods.
virtual void startDiscreteType (const std::string &name, std::string super="") override
 Start a discrete subtype declaration. More...
 
virtual void addLabel (const std::string &l, std::string ext="") override
 Add a label to the current discrete type. More...
 
virtual void endDiscreteType () override
 End the current discrete type declaration. More...
 
virtual void startDiscretizedType (const std::string &name) override
 Start a discretized type declaration. More...
 
virtual void addTick (double tick) override
 Add a tick to the current discretized type. More...
 
virtual void endDiscretizedType () override
 End the current discretized type declaration. More...
 
virtual void addRangeType (const std::string &name, long minVal, long maxVal) override
 Add a range variable type declaration. More...
 
several checks for parsers
virtual bool isClassOrInterface (const std::string &type) const override
 
virtual bool isArrayInCurrentSystem (const std::string &name) const override
 
Class<GUM_SCALAR> construction models.
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. More...
 
virtual void continueClass (const std::string &c) override
 Continue the declaration of a class. More...
 
virtual void endClass (bool checkImplementations=true) override
 Tells the factory that we finished a class declaration. More...
 
PRMInterface<GUM_SCALAR> construction models.
virtual void startInterface (const std::string &i, const std::string &ext="", bool delayInheritance=false) override
 Tells the factory that we start an interface declaration. More...
 
virtual void continueInterface (const std::string &name) override
 Continue the declaration of an interface. More...
 
virtual void addAttribute (const std::string &type, const std::string &name) override
 Add an attribute to an interface. More...
 
virtual void endInterface () override
 Tells the factory that we finished an interface declaration. More...
 
Attributes construction methods.
virtual void addAttribute (PRMAttribute< GUM_SCALAR > *attr)
 Add an already created attribute to the current class. More...
 
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. More...
 
virtual void continueAttribute (const std::string &name) override
 Continues the declaration of an attribute. More...
 
virtual void addParent (const std::string &name) override
 Tells the factory that we add a parent to the current declared attribute. More...
 
void setRawCPFByLines (const std::vector< GUM_SCALAR > &array)
 Gives the factory the CPF in its raw form. More...
 
void setRawCPFByColumns (const std::vector< GUM_SCALAR > &array)
 Gives the factory the CPF in its raw form. More...
 
virtual void setCPFByRule (const std::vector< std::string > &labels, const std::vector< GUM_SCALAR > &values)
 Fills the CPF using a rule. More...
 
virtual void setCPFByRule (const std::vector< std::string > &labels, const std::vector< std::string > &values) override
 Fills the CPF using a rule and gum::Formula. More...
 
virtual void setRawCPFByFloatLines (const std::vector< float > &array) override
 Gives the factory the CPF in its raw form. More...
 
virtual void setRawCPFByFloatColumns (const std::vector< float > &array) override
 Gives the factory the CPF in its raw form. More...
 
virtual void setRawCPFByColumns (const std::vector< std::string > &array) override
 Gives the factory the CPF in its raw form use gum::Formula. More...
 
virtual void setRawCPFByLines (const std::vector< std::string > &array) override
 Gives the factory the CPF in its raw form using gum::Formula. More...
 
virtual void setCPFByFloatRule (const std::vector< std::string > &labels, const std::vector< float > &values) override
 Fills the CPF using a rule. More...
 
virtual void endAttribute () override
 Tells the factory that we finished declaring an attribute. More...
 
Parameters construction methods.
void addParameter (const std::string &type, const std::string &name, double value) override
 Add a parameter to the current class with a default value. More...
 
Aggregator and function construction methods.
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. More...
 
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. More...
 
void continueAggregator (const std::string &name)
 Conitnues an aggregator declaration. More...
 
void endAggregator ()
 Finishes an aggregate declaration. More...
 
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>. More...
 
PRMReferenceSlot<GUM_SCALAR> construction methods.
virtual void addReferenceSlot (const std::string &type, const std::string &name, bool isArray) override
 Tells the factory that we started declaring a slot. More...
 
Systems constructions methods.
virtual void startSystem (const std::string &name) override
 Tells the factory that we started declaring a model. More...
 
virtual void endSystem () override
 Tells the factory that we finished declaring a model. More...
 
virtual void addInstance (const std::string &type, const std::string &name) override
 Add an instance to the model. More...
 
void addInstance (const std::string &type, const std::string &name, const HashTable< std::string, double > &params) override
 Add an instance with params as values of type's parameters. More...
 
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. More...
 
virtual void incArray (const std::string &l_i, const std::string &r_i) override
 Add an instance to an array. More...
 
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. More...
 
virtual void setReferenceSlot (const std::string &l_i, const std::string &r_i) override
 Instantiate a reference in the current model. More...
 

Detailed Description

template<typename GUM_SCALAR>
class gum::prm::PRMFactory< GUM_SCALAR >

Factory which builds a PRM<GUM_SCALAR>.

The Factory always create one PRM<GUM_SCALAR> which is not deleted with the factory, so be very careful to delete the PRM<GUM_SCALAR> built by this factory.

The factory does not allow any sequence of calls, if you call a method when the factory is in an incoherent state it will raise a FactoryInvalidState exception.

Keep in mind that most methods could raise gum::FatalError if something unexpected happens, since the framework is meant to evolve it is possible.

See also
PRM<GUM_SCALAR> PRMObject

Definition at line 49 of file PRMType.h.

Constructor & Destructor Documentation

◆ PRMFactory() [1/3]

template<typename GUM_SCALAR >
INLINE gum::prm::PRMFactory< GUM_SCALAR >::PRMFactory ( )

Default constructor.

Definition at line 1292 of file PRMFactory_tpl.h.

1292  {
1293  GUM_CONSTRUCTOR(PRMFactory);
1294  _prm_ = new PRM< GUM_SCALAR >();
1295  }
PRMFactory()
Default constructor.
PRM< GUM_SCALAR > * _prm_
The pointer on the PRM<GUM_SCALAR> built by this factory.
Definition: PRMFactory.h:1026

◆ PRMFactory() [2/3]

template<typename GUM_SCALAR >
INLINE gum::prm::PRMFactory< GUM_SCALAR >::PRMFactory ( PRM< GUM_SCALAR > *  prm)

This constructor let you set on which PRM instance the factory works.

Definition at line 1298 of file PRMFactory_tpl.h.

1298  :
1299  IPRMFactory(), _prm_(prm) {
1300  GUM_CONSTRUCTOR(PRMFactory);
1301  }
PRMFactory()
Default constructor.
PRM< GUM_SCALAR > * _prm_
The pointer on the PRM<GUM_SCALAR> built by this factory.
Definition: PRMFactory.h:1026
PRM< GUM_SCALAR > * prm() const
Returns a pointer on the PRM<GUM_SCALAR> created by this factory.

◆ PRMFactory() [3/3]

template<typename GUM_SCALAR >
gum::prm::PRMFactory< GUM_SCALAR >::PRMFactory ( const PRMFactory< GUM_SCALAR > &  from)
delete

Copy constructor. Don't use it.

◆ ~PRMFactory()

template<typename GUM_SCALAR >
INLINE gum::prm::PRMFactory< GUM_SCALAR >::~PRMFactory ( )
virtual

Destructor.

Definition at line 1304 of file PRMFactory_tpl.h.

1304  {
1305  GUM_DESTRUCTOR(PRMFactory);
1306  while (!_namespaces_.empty()) {
1307  auto ns = _namespaces_.back();
1308  _namespaces_.pop_back();
1309  delete ns;
1310  }
1311  }
std::vector< List< std::string > *> _namespaces_
Set of all declared namespaces.
Definition: PRMFactory.h:1023
PRMFactory()
Default constructor.

Member Function Documentation

◆ _addInstance_()

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMFactory< GUM_SCALAR >::_addInstance_ ( PRMClass< GUM_SCALAR > *  type,
const std::string &  name 
)
private

Adds a instance to the current model.

Definition at line 1659 of file PRMFactory_tpl.h.

1660  {
1661  PRMInstance< GUM_SCALAR >* i = nullptr;
1662  try {
1663  auto s
1664  = static_cast< PRMSystem< GUM_SCALAR >* >(_checkStack_(1, PRMObject::prm_type::SYSTEM));
1665  i = new PRMInstance< GUM_SCALAR >(name, *type);
1666  s->add(i);
1667 
1668  } catch (OperationNotAllowed&) {
1669  if (i) { delete i; }
1670  throw;
1671  }
1672  }
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 gi...

◆ _addParent_() [1/2]

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMFactory< GUM_SCALAR >::_addParent_ ( PRMClassElementContainer< GUM_SCALAR > *  c,
PRMAttribute< GUM_SCALAR > *  agg,
const std::string &  name 
)
private

Add a parent to an attribute.

Definition at line 237 of file PRMFactory_tpl.h.

239  {
240  try {
241  PRMClassElement< GUM_SCALAR >& elt = c->get(name);
242 
243  switch (elt.elt_type()) {
245  GUM_ERROR(OperationNotAllowed,
246  "can not add a reference slot as a parent of an attribute")
247  break;
248  }
249 
250  case PRMClassElement< GUM_SCALAR >::prm_slotchain: {
251  if (static_cast< PRMSlotChain< GUM_SCALAR >& >(elt).isMultiple()) {
252  GUM_ERROR(OperationNotAllowed, "can not add a multiple slot chain to an attribute")
253  }
254 
255  c->addArc(name, a->name());
256 
257  break;
258  }
259 
262  c->addArc(name, a->name());
263  break;
264  }
265 
266  default: {
267  GUM_ERROR(FatalError, "unknown ClassElement<GUM_SCALAR>")
268  }
269  }
270  } catch (NotFound&) {
271  // Check if name is a slot chain
272  PRMSlotChain< GUM_SCALAR >* sc = _buildSlotChain_(c, name);
273 
274  if (sc == nullptr) {
275  std::string msg = "found no ClassElement<GUM_SCALAR> with the given name ";
276  GUM_ERROR(NotFound, msg + name)
277  } else if (!sc->isMultiple()) {
278  c->add(sc);
279  c->addArc(sc->name(), a->name());
280  } else {
281  delete sc;
282  GUM_ERROR(OperationNotAllowed,
283  "Impossible to add a multiple reference slot as"
284  " direct parent of an PRMAttribute<GUM_SCALAR>.");
285  }
286  }
287  }
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.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ _addParent_() [2/2]

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMFactory< GUM_SCALAR >::_addParent_ ( PRMClass< GUM_SCALAR > *  c,
PRMAggregate< GUM_SCALAR > *  agg,
const std::string &  name 
)
private

Add a parent to an aggregate.

Definition at line 593 of file PRMFactory_tpl.h.

595  {
596  auto chains = std::vector< std::string >{name};
597  auto inputs = std::vector< PRMClassElement< GUM_SCALAR >* >();
598  _retrieveInputs_(c, chains, inputs);
599 
600  switch (agg->agg_type()) {
603  if (inputs.front()->type() != *(_retrieveType_("boolean"))) {
604  GUM_ERROR(TypeError, "expected booleans")
605  }
606 
607  break;
608  }
609 
613  if (!agg->hasLabel()) {
614  auto param = agg->labelValue();
615  Idx label_idx = 0;
616 
617  while (label_idx < inputs.front()->type()->domainSize()) {
618  if (inputs.front()->type()->label(label_idx) == param) { break; }
619 
620  ++label_idx;
621  }
622 
623  if (label_idx == inputs.front()->type()->domainSize()) {
624  GUM_ERROR(NotFound, "could not find label")
625  }
626 
627  agg->setLabel(label_idx);
628  }
629 
630  break;
631  }
632 
638  break;
639  }
640 
641  default: {
642  GUM_ERROR(FatalError, "Unknown aggregator.")
643  }
644  }
645 
646  c->addArc(inputs.front()->safeName(), agg->safeName());
647  }
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...
bool _retrieveInputs_(PRMClass< GUM_SCALAR > *c, const std::vector< std::string > &chains, std::vector< PRMClassElement< GUM_SCALAR > * > &inputs)
Retrieve inputs for an PRMAggregate.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ _addPrefix_()

template<typename GUM_SCALAR >
INLINE std::string gum::prm::PRMFactory< GUM_SCALAR >::_addPrefix_ ( const std::string &  str) const
private

Adds prefix to str iff prefix != "".

Definition at line 1675 of file PRMFactory_tpl.h.

1675  {
1676  if (!_packages_.empty()) {
1677  std::string full_name = _packages_.back();
1678  full_name.append(".");
1679  full_name.append(str);
1680  return full_name;
1681  } else {
1682  return str;
1683  }
1684  }
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:1020

◆ _buildAggregateCPF_()

template<typename GUM_SCALAR >
void gum::prm::PRMFactory< GUM_SCALAR >::_buildAggregateCPF_ ( PRMSystem< GUM_SCALAR > *  model)
private

Builds all Aggregates CPF in the given model. This must be called after all the slot chains have been generated.

◆ _buildSlotChain_()

template<typename GUM_SCALAR >
INLINE PRMSlotChain< GUM_SCALAR > * gum::prm::PRMFactory< GUM_SCALAR >::_buildSlotChain_ ( PRMClassElementContainer< GUM_SCALAR > *  start,
const std::string &  name 
)
private

This methods build a PRMSlotChain<GUM_SCALAR> given a starting element and a string.

Returns
Return a pointer over a PRMSlotChain<GUM_SCALAR> or 0 if no PRMSlotChain<GUM_SCALAR> could be built.

Definition at line 916 of file PRMFactory_tpl.h.

917  {
918  std::vector< std::string > v;
919  decomposePath(name, v);
920  PRMClassElementContainer< GUM_SCALAR >* current = start;
921  PRMReferenceSlot< GUM_SCALAR >* ref = nullptr;
922  Sequence< PRMClassElement< GUM_SCALAR >* > elts;
923 
924  for (size_t i = 0; i < v.size(); ++i) {
925  try {
926  switch (current->get(v[i]).elt_type()) {
928  ref = &(static_cast< PRMReferenceSlot< GUM_SCALAR >& >(current->get(v[i])));
929  elts.insert(ref);
930  current = &(/*const_cast<PRMClassElementContainer<GUM_SCALAR>&>*/ (ref->slotType()));
931  break;
932 
935 
936  if (i == v.size() - 1) {
937  elts.insert(&(current->get(v[i])));
938  break;
939  } else {
940  return nullptr;
941  }
942 
943  default: {
944  return nullptr;
945  }
946  }
947  } catch (NotFound&) { return nullptr; }
948  }
949 
950  GUM_ASSERT(v.size() == elts.size());
951 
952  current->setOutputNode(*(elts.back()), true);
953 
954  return new PRMSlotChain< GUM_SCALAR >(name, elts);
955  }
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:28

◆ _checkInterfaceImplementation_()

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMFactory< GUM_SCALAR >::_checkInterfaceImplementation_ ( PRMClass< GUM_SCALAR > *  c)
private

Check if c implements correctly all his interfaces.

Definition at line 105 of file PRMFactory_tpl.h.

105  {
106  try {
107  for (const auto& i: c->implements()) {
108  try {
109  for (const auto& node: i->containerDag().nodes()) {
110  std::string name = i->get(node).name();
111 
112  switch (i->get(node).elt_type()) {
115  if ((c->get(name).elt_type() == PRMClassElement< GUM_SCALAR >::prm_attribute)
116  || (c->get(name).elt_type()
118  if (!c->get(name).type().isSubTypeOf(i->get(name).type())) {
119  std::stringstream msg;
120  msg << "class " << c->name() << " does not respect interface ";
121  GUM_ERROR(PRMTypeError, msg.str() + i->name())
122  }
123  } else {
124  std::stringstream msg;
125  msg << "class " << c->name() << " does not respect interface ";
126  GUM_ERROR(PRMTypeError, msg.str() + i->name())
127  }
128 
129  break;
130  }
131 
133  if (c->get(name).elt_type() == PRMClassElement< GUM_SCALAR >::prm_refslot) {
134  const PRMReferenceSlot< GUM_SCALAR >& ref_i
135  = static_cast< const PRMReferenceSlot< GUM_SCALAR >& >(i->get(name));
136  const PRMReferenceSlot< GUM_SCALAR >& ref_this
137  = static_cast< const PRMReferenceSlot< GUM_SCALAR >& >(c->get(name));
138 
139  if (!ref_this.slotType().isSubTypeOf(ref_i.slotType())) {
140  std::stringstream msg;
141  msg << "class " << c->name() << " does not respect interface ";
142  GUM_ERROR(PRMTypeError, msg.str() + i->name())
143  }
144  } else {
145  std::stringstream msg;
146  msg << "class " << c->name() << " does not respect interface ";
147  GUM_ERROR(PRMTypeError, msg.str() + i->name())
148  }
149 
150  break;
151  }
152 
154  // Nothing to check: they are automatically inherited
155  break;
156  }
157 
158  default: {
159  std::string msg = "unexpected ClassElement<GUM_SCALAR> in interface ";
160  GUM_ERROR(FatalError, msg + i->name())
161  }
162  }
163  }
164  } catch (NotFound&) {
165  std::stringstream msg;
166  msg << "class " << c->name() << " does not respect interface ";
167  GUM_ERROR(PRMTypeError, msg.str() + i->name())
168  }
169  }
170  } catch (NotFound&) {
171  // this Class<GUM_SCALAR> does not implement any
172  // PRMInterface<GUM_SCALAR>
173  }
174  }
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ _checkStack_() [1/2]

template<typename GUM_SCALAR >
INLINE PRMObject * gum::prm::PRMFactory< GUM_SCALAR >::_checkStack_ ( Idx  i,
PRMObject::prm_type  obj_type 
)
private

Return a pointer on a PRMObject at stack.size() - i position after checking the type of the object given obj_type.

Exceptions
FactoryInvalidStateRaised if the stack isn't consistent with the

Definition at line 1687 of file PRMFactory_tpl.h.

1687  {
1688  // Don't forget that Idx are unsigned int
1689  if (_stack_.size() - i > _stack_.size()) {
1690  GUM_ERROR(FactoryInvalidState, "illegal sequence of calls")
1691  }
1692 
1693  PRMObject* obj = _stack_[_stack_.size() - i];
1694 
1695  if (obj->obj_type() != obj_type) {
1696  GUM_ERROR(FactoryInvalidState, "illegal sequence of calls")
1697  }
1698 
1699  return obj;
1700  }
std::vector< PRMObject *> _stack_
A stack used to keep track of created PRMObject.
Definition: PRMFactory.h:1029
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ _checkStack_() [2/2]

template<typename GUM_SCALAR >
INLINE PRMClassElement< GUM_SCALAR > * gum::prm::PRMFactory< GUM_SCALAR >::_checkStack_ ( Idx  i,
typename PRMClassElement< GUM_SCALAR >::ClassElementType  obj_type 
)
private

Adds prefix to str iff prefix != "".

Definition at line 1721 of file PRMFactory_tpl.h.

1723  {
1724  // Don't forget that Idx are unsigned int
1725  if (_stack_.size() - i > _stack_.size()) {
1726  GUM_ERROR(FactoryInvalidState, "illegal sequence of calls")
1727  }
1728 
1729  PRMClassElement< GUM_SCALAR >* obj
1730  = dynamic_cast< PRMClassElement< GUM_SCALAR >* >(_stack_[_stack_.size() - i]);
1731 
1732  if (obj == 0) { GUM_ERROR(FactoryInvalidState, "illegal sequence of calls") }
1733 
1734  if (obj->elt_type() != elt_type) {
1735  GUM_ERROR(FactoryInvalidState, "illegal sequence of calls")
1736  }
1737 
1738  return obj;
1739  }
std::vector< PRMObject *> _stack_
A stack used to keep track of created PRMObject.
Definition: PRMFactory.h:1029
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ _checkStackContainter_()

template<typename GUM_SCALAR >
INLINE PRMClassElementContainer< GUM_SCALAR > * gum::prm::PRMFactory< GUM_SCALAR >::_checkStackContainter_ ( Idx  i)
private

Adds prefix to str iff prefix != "".

Definition at line 1704 of file PRMFactory_tpl.h.

1704  {
1705  // Don't forget that Idx are unsigned int
1706  if (_stack_.size() - i > _stack_.size()) {
1707  GUM_ERROR(FactoryInvalidState, "illegal sequence of calls")
1708  }
1709 
1710  PRMObject* obj = _stack_[_stack_.size() - i];
1711 
1712  if ((obj->obj_type() == PRMObject::prm_type::CLASS)
1713  || (obj->obj_type() == PRMObject::prm_type::PRM_INTERFACE)) {
1714  return static_cast< PRMClassElementContainer< GUM_SCALAR >* >(obj);
1715  } else {
1716  GUM_ERROR(FactoryInvalidState, "illegal sequence of calls")
1717  }
1718  }
std::vector< PRMObject *> _stack_
A stack used to keep track of created PRMObject.
Definition: PRMFactory.h:1029
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ _instantiateSlotChain_()

template<typename GUM_SCALAR >
void gum::prm::PRMFactory< GUM_SCALAR >::_instantiateSlotChain_ ( PRMSystem< GUM_SCALAR > *  model,
PRMInstance< GUM_SCALAR > *  inst,
PRMReferenceSlot< GUM_SCALAR > *  ref,
PRMSlotChain< GUM_SCALAR > *  sc 
)
private

Instantiate a slot chain in the given instance.

◆ _retrieveClass_()

template<typename GUM_SCALAR >
PRMClass< GUM_SCALAR > * gum::prm::PRMFactory< GUM_SCALAR >::_retrieveClass_ ( const std::string &  name) const
private

Returns a pointer on a class given it's name. Used when building models, meaning that the class name can either be local (need to add the current prefix) or global (no prefix needed).

Exceptions
NotFoundIf no class matching the name is found.
See also
PRMFactory:: retrieveType

Definition at line 1195 of file PRMFactory_tpl.h.

1195  {
1196  PRMClass< GUM_SCALAR >* a_class = nullptr;
1197  std::string full_name;
1198 
1199  // Looking for the type using its name
1200  if (_prm_->_classMap_.exists(name)) {
1201  a_class = _prm_->_classMap_[name];
1202  full_name = name;
1203  }
1204 
1205  // Looking for the type using current package
1206  std::string prefixed = _addPrefix_(name);
1207  if (_prm_->_classMap_.exists(prefixed)) {
1208  if (a_class == nullptr) {
1209  a_class = _prm_->_classMap_[prefixed];
1210  full_name = prefixed;
1211  } else if (full_name != prefixed) {
1212  GUM_ERROR(DuplicateElement,
1213  "Class name '" << name << "' is ambiguous: specify full name.");
1214  }
1215  }
1216 
1217  // Looking for the class using all declared namespaces
1218  if (!_namespaces_.empty()) {
1219  auto ns_list = _namespaces_.back();
1220  for (gum::Size i = 0; i < ns_list->size(); ++i) {
1221  std::string ns = (*ns_list)[i];
1222  std::string ns_name = ns + "." + name;
1223  if (_prm_->_classMap_.exists(ns_name)) {
1224  if (a_class == 0) {
1225  a_class = _prm_->_classMap_[ns_name];
1226  full_name = ns_name;
1227  } else if (full_name != ns_name) {
1228  GUM_ERROR(DuplicateElement,
1229  "Class name '" << name << "' is ambiguous: specify full name.");
1230  }
1231  }
1232  }
1233  }
1234 
1235  if (a_class == 0) { GUM_ERROR(NotFound, "Class '" << name << "' not found, check imports.") }
1236 
1237  return a_class;
1238  }
std::string _addPrefix_(const std::string &str) const
Adds prefix to str iff prefix != "".
std::vector< List< std::string > *> _namespaces_
Set of all declared namespaces.
Definition: PRMFactory.h:1023
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
PRM< GUM_SCALAR > * _prm_
The pointer on the PRM<GUM_SCALAR> built by this factory.
Definition: PRMFactory.h:1026
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ _retrieveCommonType_()

template<typename GUM_SCALAR >
INLINE PRMType * gum::prm::PRMFactory< GUM_SCALAR >::_retrieveCommonType_ ( const std::vector< PRMClassElement< GUM_SCALAR > * > &  elts)
private

Retrieve the common PRMType of a vector of PRMClassElement<GUM_SCALAR>.

The vector elts must only contains PRMClassElement<GUM_SCALAR> with a PRMType, i.e. PRMAttribute<GUM_SCALAR>, PRMAggregate and PRMSlotChain<GUM_SCALAR>. If not a WrongClassElement is raised.

A common PRMType is PRMType t such as t.isSuperTypeOf(elts[i]) for 0 < i < elts.size(), where elts is a PRMType container.

Parameters
eltsA vector of PRMClassElement<GUM_SCALAR>.
Returns
Returns the common super PRMType of all PRMClassElement<GUM_SCALAR> un elts.
Exceptions
WrongClassElementRaised if elts contains a PRMClassElement<GUM_SCALAR> without a PRMType.
NotFoundRaised if there exists no common super type of all PRMClassElement<GUM_SCALAR> in elts.

Definition at line 1014 of file PRMFactory_tpl.h.

1015  {
1016  const PRMType* current = nullptr;
1018  // Finding all types and super types
1019 
1020  for (const auto& elt: elts) {
1021  try {
1022  current = &((*elt).type());
1023 
1024  while (current != 0) {
1025  // Filling counters
1026  if (counters.exists(current->name())) {
1027  ++(counters[current->name()]);
1028  } else {
1029  counters.insert(current->name(), 1);
1030  }
1031 
1032  // Loop guard
1033  if (current->isSubType()) {
1034  current = &(current->superType());
1035  } else {
1036  current = nullptr;
1037  }
1038  }
1039  } catch (OperationNotAllowed&) {
1040  GUM_ERROR(WrongClassElement, "found a ClassElement<GUM_SCALAR> without a type")
1041  }
1042  }
1043 
1044  // We need to find the most specialized (i.e. max depth) common type
1045  current = nullptr;
1046 
1047  int max_depth = -1;
1048 
1049  int current_depth = 0;
1050 
1051  for (const auto& elt: counters) {
1052  if ((elt.second) == elts.size()) {
1053  current_depth = _typeDepth_(_retrieveType_(elt.first));
1054 
1055  if (current_depth > max_depth) {
1056  max_depth = current_depth;
1057  current = _retrieveType_(elt.first);
1058  }
1059  }
1060  }
1061 
1062  if (current) { return const_cast< PRMType* >(current); }
1063 
1064  GUM_ERROR(NotFound, "could not find a common type")
1065  }
int _typeDepth_(const PRMType *t)
Returns the inheritance depth of a PRMType.
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...
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ _retrieveInputs_()

template<typename GUM_SCALAR >
INLINE bool gum::prm::PRMFactory< GUM_SCALAR >::_retrieveInputs_ ( PRMClass< GUM_SCALAR > *  c,
const std::vector< std::string > &  chains,
std::vector< PRMClassElement< GUM_SCALAR > * > &  inputs 
)
private

Retrieve inputs for an PRMAggregate.

The vector chains contains names of the PRMAggregate inputs. If a name does not match an existing PRMClassElement<GUM_SCALAR> in c, then a call to PRMFactory:: buildSlotChains() is made. Such created PRMSlotChain<GUM_SCALAR> are added to c.

Parameters
cThe class in which the PRMAggregate is defined.
chainsVector of the PRMAggregate inputs names.
inputsVector filled with the PRMClassElement<GUM_SCALAR> matching the names in chains.
Returns
true if there was at least one slotchain in chains.
Exceptions
NotFoundRaised if a name in chains does not match a legal PRMSlotChain<GUM_SCALAR> or an existing PRMClassElement<GUM_SCALAR> in c.

Definition at line 958 of file PRMFactory_tpl.h.

961  {
962  bool retVal = false;
963 
964  for (size_t i = 0; i < chains.size(); ++i) {
965  try {
966  inputs.push_back(&(c->get(chains[i])));
967  retVal = retVal || PRMClassElement< GUM_SCALAR >::isSlotChain(*(inputs.back()));
968  } catch (NotFound&) {
969  inputs.push_back(_buildSlotChain_(c, chains[i]));
970  retVal = true;
971 
972  if (inputs.back()) {
973  c->add(inputs.back());
974  } else {
975  GUM_ERROR(NotFound, "unknown slot chain")
976  }
977  }
978  }
979 
980  PRMType* t = _retrieveCommonType_(inputs);
981 
982  std::vector< std::pair< PRMClassElement< GUM_SCALAR >*, PRMClassElement< GUM_SCALAR >* > >
983  toAdd;
984 
985  for (const auto& elt: inputs) {
986  if ((*elt).type() != (*t)) {
988  PRMSlotChain< GUM_SCALAR >* sc = static_cast< PRMSlotChain< GUM_SCALAR >* >(elt);
989  std::stringstream name;
990 
991  for (Size idx = 0; idx < sc->chain().size() - 1; ++idx) {
992  name << sc->chain().atPos(idx)->name() << ".";
993  }
994 
995  name << ".(" << t->name() << ")" << sc->lastElt().name();
996 
997  try {
998  toAdd.push_back(std::make_pair(elt, &(c->get(name.str()))));
999  } catch (NotFound&) {
1000  toAdd.push_back(std::make_pair(elt, _buildSlotChain_(c, name.str())));
1001  }
1002  } else {
1003  std::stringstream name;
1004  name << "(" << t->name() << ")" << elt->name();
1005  toAdd.push_back(std::make_pair(elt, &(c->get(name.str()))));
1006  }
1007  }
1008  }
1009 
1010  return retVal;
1011  }
PRMType * _retrieveCommonType_(const std::vector< PRMClassElement< GUM_SCALAR > * > &elts)
Retrieve the common PRMType of a vector of PRMClassElement<GUM_SCALAR>.
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.
static INLINE bool isSlotChain(const PRMClassElement< GUM_SCALAR > &elt)
Return true if obj is of type PRMSlotChain.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ _retrieveInstanceSequence_()

template<typename GUM_SCALAR >
std::string gum::prm::PRMFactory< GUM_SCALAR >::_retrieveInstanceSequence_ ( PRMInstance< GUM_SCALAR > *  inst,
Sequence< PRMInstance< GUM_SCALAR > * > &  seq,
PRMSlotChain< GUM_SCALAR > *  sc 
)
private

Fill seq with the sequence of instance build using inst as the instantiation of sc-> class and seeking each instantiation of reference in sc.

Returns
Returns the name of the corresponding PRMSlotChain<GUM_SCALAR>.

◆ _retrieveInterface_()

template<typename GUM_SCALAR >
PRMInterface< GUM_SCALAR > * gum::prm::PRMFactory< GUM_SCALAR >::_retrieveInterface_ ( const std::string &  name) const
private

Returns a pointer on an interface given it's name. Used when building models, meaning that the interface name can either be local (need to add the current prefix) or global (no prefix needed).

Exceptions
NotFoundIf no class matching the name is found.
See also
PRMFactory:: retrieveType

Definition at line 1242 of file PRMFactory_tpl.h.

1242  {
1243  PRMInterface< GUM_SCALAR >* interface = nullptr;
1244  std::string full_name;
1245 
1246  // Looking for the type using its name
1247  if (_prm_->_interfaceMap_.exists(name)) {
1248  interface = _prm_->_interfaceMap_[name];
1249  full_name = name;
1250  }
1251 
1252  // Looking for the type using current package
1253  std::string prefixed = _addPrefix_(name);
1254  if (_prm_->_interfaceMap_.exists(prefixed)) {
1255  if (interface == nullptr) {
1256  interface = _prm_->_interfaceMap_[prefixed];
1257  full_name = prefixed;
1258  } else if (full_name != prefixed) {
1259  GUM_ERROR(DuplicateElement,
1260  "Interface name '" << name << "' is ambiguous: specify full name.");
1261  }
1262  }
1263 
1264  // Looking for the interf using all declared namespaces
1265  if (!_namespaces_.empty()) {
1266  auto ns_list = _namespaces_.back();
1267  // for( const auto & ns : *( _namespaces_.top()) ) {
1268  for (gum::Size i = 0; i < ns_list->size(); ++i) {
1269  std::string ns = (*ns_list)[i];
1270  std::string ns_name = ns + "." + name;
1271 
1272  if (_prm_->_interfaceMap_.exists(ns_name)) {
1273  if (interface == nullptr) {
1274  interface = _prm_->_interfaceMap_[ns_name];
1275  full_name = ns_name;
1276  } else if (full_name != ns_name) {
1277  GUM_ERROR(DuplicateElement,
1278  "Interface name '" << name << "' is ambiguous: specify full name.");
1279  }
1280  }
1281  }
1282  }
1283 
1284  if (interface == nullptr) {
1285  GUM_ERROR(NotFound, "Interface '" << name << "' not found, check imports.")
1286  }
1287 
1288  return interface;
1289  }
std::string _addPrefix_(const std::string &str) const
Adds prefix to str iff prefix != "".
std::vector< List< std::string > *> _namespaces_
Set of all declared namespaces.
Definition: PRMFactory.h:1023
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
PRM< GUM_SCALAR > * _prm_
The pointer on the PRM<GUM_SCALAR> built by this factory.
Definition: PRMFactory.h:1026
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ _retrieveType_()

template<typename GUM_SCALAR >
INLINE PRMType * gum::prm::PRMFactory< GUM_SCALAR >::_retrieveType_ ( const std::string &  name) const
private

Returns a pointer on a PRMType given it's name. Since the type can be given either with it's local name (without the prefix), full name (with the prefix) or can come from an import unit, or maybe one day with a using declaration we need some processing to find it.

The following precedences on finding the name is used:

  • name
  • prefix.name
  • import_1.name
  • import_2.name
  • ...
  • import_N.name In the case a local name is used multiple time, it's preferable to use it's full name.
    Exceptions
    OperationNotAllowedIf the type is undeclared.

Definition at line 1132 of file PRMFactory_tpl.h.

1132  {
1133  PRMType* type = nullptr;
1134  std::string full_name;
1135 
1136  // Looking for the type using its name
1137  if (_prm_->_typeMap_.exists(name)) {
1138  type = _prm_->_typeMap_[name];
1139  full_name = name;
1140  }
1141 
1142  // Looking for the type in current package
1143  std::string prefixed = _addPrefix_(name);
1144  if (_prm_->_typeMap_.exists(prefixed)) {
1145  if (type == 0) {
1146  type = _prm_->_typeMap_[prefixed];
1147  full_name = prefixed;
1148  } else if (full_name != prefixed) {
1149  GUM_ERROR(DuplicateElement, "Type name '" << name << "' is ambiguous: specify full name.")
1150  }
1151  }
1152 
1153  // Looking for the type relatively to current package
1154  std::string relatif_ns = currentPackage();
1155  size_t last_dot = relatif_ns.find_last_of('.');
1156  if (last_dot != std::string::npos) {
1157  relatif_ns = relatif_ns.substr(0, last_dot) + '.' + name;
1158  if (_prm_->_typeMap_.exists(relatif_ns)) {
1159  if (type == 0) {
1160  type = _prm_->_typeMap_[relatif_ns];
1161  full_name = relatif_ns;
1162  } else if (full_name != relatif_ns) {
1163  GUM_ERROR(DuplicateElement,
1164  "Type name '" << name << "' is ambiguous: specify full name.");
1165  }
1166  }
1167  }
1168 
1169 
1170  // Looking for the type using all declared namespaces
1171  if (!_namespaces_.empty()) {
1172  auto ns_list = _namespaces_.back();
1173  for (gum::Size i = 0; i < ns_list->size(); ++i) {
1174  std::string ns = (*ns_list)[i];
1175  std::string ns_name = ns + "." + name;
1176  if (_prm_->_typeMap_.exists(ns_name)) {
1177  if (type == 0) {
1178  type = _prm_->_typeMap_[ns_name];
1179  full_name = ns_name;
1180  } else if (full_name != ns_name) {
1181  GUM_ERROR(DuplicateElement,
1182  "Type name '" << name << "' is ambiguous: specify full name.");
1183  }
1184  }
1185  }
1186  }
1187 
1188  if (type == 0) { GUM_ERROR(NotFound, "Type '" << name << "' not found, check imports.") }
1189 
1190  return type;
1191  }
std::string _addPrefix_(const std::string &str) const
Adds prefix to str iff prefix != "".
std::vector< List< std::string > *> _namespaces_
Set of all declared namespaces.
Definition: PRMFactory.h:1023
virtual std::string currentPackage() const override
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
PRM< GUM_SCALAR > * _prm_
The pointer on the PRM<GUM_SCALAR> built by this factory.
Definition: PRMFactory.h:1026
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ _typeDepth_()

template<typename GUM_SCALAR >
INLINE int gum::prm::PRMFactory< GUM_SCALAR >::_typeDepth_ ( const PRMType t)
private

Returns the inheritance depth of a PRMType.

This used by PRMFactory:: retrieveCommonType. This returns 0 if t does not have a super type.

Parameters
tThe PRMType for which we compute its depth.
Returns
Returns the depth of t.

Definition at line 1742 of file PRMFactory_tpl.h.

1742  {
1743  int depth = 0;
1744  const PRMType* current = t;
1745 
1746  while (current->isSubType()) {
1747  ++depth;
1748  current = &(current->superType());
1749  }
1750 
1751  return depth;
1752  }

◆ addAggregator()

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMFactory< GUM_SCALAR >::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 = "" 
)
overridevirtual

Add an aggregator in the current declared class.

If at least one parent of an aggregator is a PRMSlotChain<GUM_SCALAR>, then all of it's parents must be PRMSlotChain<GUM_SCALAR>. When an aggregator parents are only composed of PRMAttribute<GUM_SCALAR> and PRMAggregate, then it is directly added as an PRMAttribute<GUM_SCALAR> to it's Class<GUM_SCALAR>.

Parameters
nameThe name of this aggregator.
agg_typeThe name of the aggregator type of this aggregator.
chainsThe set of chains on which this aggregate applies.
paramsThe list of parameters for this aggregator.
typeSome aggregators have a user defined type, use this parameter to define it.
Exceptions
OperationNotAllowedRaised if one or more parameters misses or are not correct.
TypeErrorRaised if the aggregator's type or one or more of the chains are not of the good type.

Implements gum::prm::IPRMFactory.

Definition at line 656 of file PRMFactory_tpl.h.

660  {
661  PRMClass< GUM_SCALAR >* c
662  = static_cast< PRMClass< GUM_SCALAR >* >(_checkStack_(1, PRMObject::prm_type::CLASS));
663  // Checking call legality
664 
665  if (chains.size() == 0) {
666  GUM_ERROR(OperationNotAllowed, "a PRMAggregate<GUM_SCALAR> requires at least one parent")
667  }
668 
669  // Retrieving the parents of the aggregate
670  std::vector< PRMClassElement< GUM_SCALAR >* > inputs;
671 
672  // This helps knowing if the aggregate has parents outside the current
673  // class
674  // (see below)
675  bool hasSC = _retrieveInputs_(c, chains, inputs);
676 
677  // Checking that all inputs shares the same PRMType (trivial
678  // if
679  // inputs.size() == 1)
680  if (inputs.size() > 1) {
681  for (auto iter = inputs.begin() + 1; iter != inputs.end(); ++iter) {
682  if ((**(iter - 1)).type() != (**iter).type()) {
683  GUM_ERROR(TypeError, "found different types")
684  }
685  }
686  }
687 
688  // Different treatments for different types of aggregate.
689  PRMAggregate< GUM_SCALAR >* agg = nullptr;
690 
691  switch (PRMAggregate< GUM_SCALAR >::str2enum(agg_type)) {
694  if (inputs.front()->type() != *(_retrieveType_("boolean"))) {
695  GUM_ERROR(TypeError, "expected booleans")
696  }
697  if (params.size() != 0) { GUM_ERROR(OperationNotAllowed, "invalid number of paramaters") }
698 
699  agg = new PRMAggregate< GUM_SCALAR >(name,
701  inputs.front()->type());
702 
703  break;
704  }
705 
708  if (params.size() != 1) { GUM_ERROR(OperationNotAllowed, "invalid number of parameters") }
709 
710  Idx label_idx = 0;
711 
712  while (label_idx < inputs.front()->type()->domainSize()) {
713  if (inputs.front()->type()->label(label_idx) == params.front()) { break; }
714 
715  ++label_idx;
716  }
717 
718  if (label_idx == inputs.front()->type()->domainSize()) {
719  GUM_ERROR(NotFound, "could not find label")
720  }
721 
722  // Creating and adding the PRMAggregate<GUM_SCALAR>
723  agg = new PRMAggregate< GUM_SCALAR >(name,
725  *(_retrieveType_("boolean")),
726  label_idx);
727  agg->label();
728 
729  break;
730  }
731 
737  if (params.size() != 0) { GUM_ERROR(OperationNotAllowed, "invalid number of parameters") }
738 
739  auto output_type = _retrieveType_(type);
740 
741  // Creating and adding the PRMAggregate<GUM_SCALAR>
742  agg = new PRMAggregate< GUM_SCALAR >(name,
744  *output_type);
745 
746  break;
747  }
748 
750  if (params.size() != 1) { GUM_ERROR(OperationNotAllowed, "invalid number of parameters") }
751 
752  Idx label_idx = 0;
753 
754  while (label_idx < inputs.front()->type()->domainSize()) {
755  if (inputs.front()->type()->label(label_idx) == params.front()) { break; }
756 
757  ++label_idx;
758  }
759 
760  if (label_idx == inputs.front()->type()->domainSize()) {
761  GUM_ERROR(NotFound, "could not find label")
762  }
763 
764  auto output_type = _retrieveType_(type);
765 
766  // Creating and adding the PRMAggregate<GUM_SCALAR>
767  agg = new PRMAggregate< GUM_SCALAR >(name,
769  *output_type,
770  label_idx);
771 
772  break;
773  }
774 
775  default: {
776  GUM_ERROR(FatalError, "Unknown aggregator.")
777  }
778  }
779 
780  std::string safe_name = agg->safeName();
781 
782  try {
783  if (hasSC) {
784  try {
785  c->add(agg);
786  } catch (DuplicateElement&) { c->overload(agg); }
787  } else {
788  // Inner aggregators can be directly used as attributes
789  auto attr
790  = new PRMScalarAttribute< GUM_SCALAR >(agg->name(), agg->type(), agg->buildImpl());
791 
792  try {
793  c->add(attr);
794  } catch (DuplicateElement&) { c->overload(attr); }
795 
796  delete agg;
797  }
798  } catch (DuplicateElement&) {
799  delete agg;
800  throw;
801  }
802 
803  for (const auto& elt: inputs) {
804  c->addArc(elt->safeName(), safe_name);
805  }
806  }
static AggregateType str2enum(const std::string &str)
Static method which returns the AggregateType given its string representation.
Definition: PRMAggregate.h:101
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...
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 gi...
bool _retrieveInputs_(PRMClass< GUM_SCALAR > *c, const std::vector< std::string > &chains, std::vector< PRMClassElement< GUM_SCALAR > * > &inputs)
Retrieve inputs for an PRMAggregate.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ addArray()

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMFactory< GUM_SCALAR >::addArray ( const std::string &  type,
const std::string &  name,
Size  size 
)
overridevirtual

Creates an array with the given number of instances of the given type.

PRMInstance<GUM_SCALAR> are name using "name" as prefix and adding the suffix "[i]", with "i" being the position of the instance in the array.

Implements gum::prm::IPRMFactory.

Definition at line 832 of file PRMFactory_tpl.h.

834  {
835  PRMSystem< GUM_SCALAR >* model
836  = static_cast< PRMSystem< GUM_SCALAR >* >(_checkStack_(1, PRMObject::prm_type::SYSTEM));
837  PRMClass< GUM_SCALAR >* c = _retrieveClass_(type);
838  PRMInstance< GUM_SCALAR >* inst = 0;
839 
840  try {
841  model->addArray(name, *c);
842 
843  for (Size i = 0; i < size; ++i) {
844  std::stringstream elt_name;
845  elt_name << name << "[" << i << "]";
846  inst = new PRMInstance< GUM_SCALAR >(elt_name.str(), *c);
847  model->add(name, inst);
848  }
849  } catch (PRMTypeError&) {
850  delete inst;
851  throw;
852  } catch (NotFound&) {
853  delete inst;
854  throw;
855  }
856  }
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 gi...
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 ...
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47

◆ addAttribute() [1/2]

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMFactory< GUM_SCALAR >::addAttribute ( const std::string &  type,
const std::string &  name 
)
overridevirtual

Add an attribute to an interface.

Implements gum::prm::IPRMFactory.

Definition at line 1504 of file PRMFactory_tpl.h.

1505  {
1507  startAttribute(type, name);
1508  endAttribute();
1509  }
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 gi...
virtual void endAttribute() override
Tells the factory that we finished declaring an attribute.
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.

◆ addAttribute() [2/2]

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMFactory< GUM_SCALAR >::addAttribute ( PRMAttribute< GUM_SCALAR > *  attr)
virtual

Add an already created attribute to the current class.

Use this method when you must add functions, such as Noisy-Or.

Use this method when you need to add functions, such as Noisy-Or. The attribute CPT is checked for parents and arcs will be added using the DiscreteVariable pointers, thus be careful to use those of the attributes, aggregates and slotchains of the current class. gum::prm::Class<GUM_SCALAR>::insertArc() will be called for each found parent of attr, so you should overload gum::prm::PRMAttribute<GUM_SCALAR>::addParent() to prevent duplication errors. Such class exists: gum::prm::PRMFuncAttribute .

The pointer is given to the class, so do not worry about deleting it.

Parameters
attrThe attribute added to the current class.
Exceptions
NotFoundRaised if one of the DiscreteVariable in attr CPT does not match any PRMClassElement<GUM_SCALAR> in this.

Definition at line 212 of file PRMFactory_tpl.h.

212  {
213  PRMClass< GUM_SCALAR >* c
214  = static_cast< PRMClass< GUM_SCALAR >* >(_checkStack_(1, PRMObject::prm_type::CLASS));
215  c->add(attr);
216  Size count = 0;
217  const Sequence< const DiscreteVariable* >& vars = attr->cpf().variablesSequence();
218 
219  for (const auto& node: c->containerDag().nodes()) {
220  try {
221  if (vars.exists(&(c->get(node).type().variable()))) {
222  ++count;
223 
224  if (&(attr->type().variable()) != &(c->get(node).type().variable())) {
225  c->addArc(c->get(node).safeName(), attr->safeName());
226  }
227  }
228  } catch (OperationNotAllowed&) {}
229  }
230 
231  if (count != attr->cpf().variablesSequence().size()) {
232  GUM_ERROR(NotFound, "unable to found all parents of this attribute")
233  }
234  }
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 gi...
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ addImport()

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMFactory< GUM_SCALAR >::addImport ( const std::string &  name)
overridevirtual

Add an import for namespace lookup.

When loading a module, you should push all import declarations using this method. The order in which you add imports will impact name resolution (first found, first used).

Imports are sync with packages: when you push a new package a new empty list of imports is added. When you pop a package the current list of imports is discarded and the previous one is restored.

Parameters
nameThe name of the package for all further objects.

Implements gum::prm::IPRMFactory.

Definition at line 1779 of file PRMFactory_tpl.h.

1779  {
1780  if (name.size() == 0) { GUM_ERROR(OperationNotAllowed, "illegal import name") }
1781  if (_namespaces_.empty()) { _namespaces_.push_back(new List< std::string >()); }
1782  _namespaces_.back()->push_back(name);
1783  }
std::vector< List< std::string > *> _namespaces_
Set of all declared namespaces.
Definition: PRMFactory.h:1023
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ addInstance() [1/2]

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMFactory< GUM_SCALAR >::addInstance ( const std::string &  type,
const std::string &  name 
)
overridevirtual

Add an instance to the model.

Implements gum::prm::IPRMFactory.

Definition at line 1582 of file PRMFactory_tpl.h.

1583  {
1584  auto c = _retrieveClass_(type);
1585 
1586  // If class contains parameters, calls the proper addIsntance method
1587  if (c->parameters().size() > 0) {
1589  addInstance(type, name, params);
1590 
1591  } else {
1592  _addInstance_(c, name);
1593  }
1594  }
virtual void addInstance(const std::string &type, const std::string &name) override
Add an instance to the model.
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 ...
void _addInstance_(PRMClass< GUM_SCALAR > *type, const std::string &name)
Adds a instance to the current model.

◆ addInstance() [2/2]

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMFactory< GUM_SCALAR >::addInstance ( const std::string &  type,
const std::string &  name,
const HashTable< std::string, double > &  params 
)
overridevirtual

Add an instance with params as values of type's parameters.

Implements gum::prm::IPRMFactory.

Definition at line 1598 of file PRMFactory_tpl.h.

1600  {
1601  auto c = _retrieveClass_(type);
1602 
1603  if (c->parameters().empty()) {
1604  if (params.empty()) {
1605  _addInstance_(c, name);
1606  } else {
1607  GUM_ERROR(OperationNotAllowed, "Class " + type + " does not have parameters")
1608  }
1609 
1610  } else {
1611  auto my_params = params;
1612  // Adding all parameters to my_params
1613  for (const auto& p: c->parameters()) {
1614  if (!my_params.exists(p->name())) { my_params.insert(p->name(), p->value()); }
1615  }
1616 
1617  // Building sub class name using my_params
1618  std::stringstream sBuff;
1619  sBuff << c->name() << "<";
1620 
1621  for (const auto& p: my_params) {
1622  sBuff << p.first << "=" << p.second << ",";
1623  }
1624 
1625  // Removing last , and adding closing >
1626  std::string sub_c = sBuff.str().substr(0, sBuff.str().size() - 1) + ">";
1627 
1628  // Adding class in current package
1629  try {
1630  auto pck_cpy = _packages_;
1631  _packages_.clear();
1632 
1633  startClass(sub_c, c->name());
1634 
1635  // Update inherited parameters
1636  for (auto p: my_params) {
1637  auto type = static_cast< PRMParameter< GUM_SCALAR >& >(c->get(p.first)).valueType();
1638  if (type == PRMParameter< GUM_SCALAR >::ParameterType::INT) {
1639  addParameter("int", p.first, p.second);
1640 
1641  } else {
1642  addParameter("real", p.first, p.second);
1643  }
1644  }
1645 
1646  endClass();
1647 
1648  _packages_ = pck_cpy;
1649 
1650  } catch (DuplicateElement&) {
1651  // Sub Class already exists in this system
1652  }
1653  c = _retrieveClass_(sub_c);
1654  _addInstance_(c, name);
1655  }
1656  }
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:1020
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 ...
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.
virtual void endClass(bool checkImplementations=true) override
Tells the factory that we finished a class declaration.
void _addInstance_(PRMClass< GUM_SCALAR > *type, const std::string &name)
Adds a instance to the current model.
void addParameter(const std::string &type, const std::string &name, double value) override
Add a parameter to the current class with a default value.
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
bool empty() const noexcept
Indicates whether the hash table is empty.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ addLabel()

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMFactory< GUM_SCALAR >::addLabel ( const std::string &  l,
std::string  ext = "" 
)
overridevirtual

Add a label to the current discrete type.

Parameters
lThe label value.
extThe super type label for which l is an extension.
Exceptions
OperationNotAllowedRaised if the current type does not have a super type, see PRMFactory::addLabel(const std::string&).
DuplicateElementRaised if a label with the same value already exists.
NotFoundRaised if ext does not match any label in the current type's super type.

Implements gum::prm::IPRMFactory.

Definition at line 1374 of file PRMFactory_tpl.h.

1374  {
1375  if (extends == "") {
1376  PRMType* t = static_cast< PRMType* >(_checkStack_(1, PRMObject::prm_type::TYPE));
1377  LabelizedVariable* var = dynamic_cast< LabelizedVariable* >(t->_var_);
1378 
1379  if (!var) {
1380  GUM_ERROR(FatalError, "the current type's variable is not a LabelizedVariable.")
1381  } else if (t->_superType_) {
1382  GUM_ERROR(OperationNotAllowed, "current type is a subtype.")
1383  }
1384 
1385  try {
1386  var->addLabel(l);
1387  } catch (DuplicateElement&) {
1388  GUM_ERROR(DuplicateElement, "a label '" << l << "' already exists")
1389  }
1390  } else {
1391  PRMType* t = static_cast< PRMType* >(_checkStack_(1, PRMObject::prm_type::TYPE));
1392  LabelizedVariable* var = dynamic_cast< LabelizedVariable* >(t->_var_);
1393 
1394  if (!var) {
1395  GUM_ERROR(FatalError, "the current type's variable is not a LabelizedVariable.")
1396  } else if (!t->_superType_) {
1397  GUM_ERROR(OperationNotAllowed, "current type is not a subtype.")
1398  }
1399 
1400  bool found = false;
1401 
1402  for (Idx i = 0; i < t->_superType_->_var_->domainSize(); ++i) {
1403  if (t->_superType_->_var_->label(i) == extends) {
1404  try {
1405  var->addLabel(l);
1406  } catch (DuplicateElement&) {
1407  GUM_ERROR(DuplicateElement, "a label '" << l << "' already exists")
1408  }
1409 
1410  t->_label_map_->push_back(i);
1411 
1412  found = true;
1413  break;
1414  }
1415  }
1416 
1417  if (!found) { GUM_ERROR(NotFound, "inexistent label in super type.") }
1418  }
1419  }
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 gi...
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ addNoisyOrCompound()

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMFactory< GUM_SCALAR >::addNoisyOrCompound ( const std::string &  name,
const std::vector< std::string > &  chains,
const std::vector< float > &  numbers,
float  leak,
const std::vector< std::string > &  label 
)
overridevirtual

Add a compound noisy-or as an PRMAttribute<GUM_SCALAR> to the current Class<GUM_SCALAR>.

The type of a noisy-or must be a boolean.

Parameters
namethe name of the PRMAttribute<GUM_SCALAR> added as a noisy-or.
chainsthe list of parents of the noisy-or.
numbersthe list of weights for each parent. Can consist of only one value which will be applied to all the parents.
leakthe leak
labelthe label on which the noisy-or applies, can be an empty string (the noisy-or will behave as if chains are all booleans).
Exceptions
NotFoundRaised if one of the chains or the label is not found.
FactoryInvalidStateRaised if a Class<GUM_SCALAR> is not the current declared PRMObject.
OperationNotAllowedRaised if for some reasons the parameters are invalid.

Implements gum::prm::IPRMFactory.

Definition at line 1069 of file PRMFactory_tpl.h.

1073  {
1075  GUM_ERROR(gum::FactoryInvalidState, "invalid state to add a noisy-or")
1076  }
1077 
1078  PRMClass< GUM_SCALAR >* c = dynamic_cast< gum::prm::PRMClass< GUM_SCALAR >* >(getCurrent());
1079 
1080  std::vector< PRMClassElement< GUM_SCALAR >* > parents;
1081 
1082  for (const auto& elt: chains)
1083  parents.push_back(&(c->get(elt)));
1084 
1085  PRMType* common_type = _retrieveCommonType_(parents);
1086 
1087  for (size_t idx = 0; idx < parents.size(); ++idx) {
1088  if (parents[idx]->type() != (*common_type)) {
1089  PRMClassElement< GUM_SCALAR >* parent = parents[idx];
1090  // Either safe_name is an non existing slot chain or an existing cast
1091  // descendant
1092  std::string safe_name = parent->cast(*common_type);
1093 
1094  if (!c->exists(safe_name)) {
1096  parents[idx] = _buildSlotChain_(c, safe_name);
1097  c->add(parents[idx]);
1098  } else {
1099  GUM_ERROR(NotFound, "unable to find parent")
1100  }
1101  } else {
1102  parents[idx] = &(c->get(safe_name));
1103  }
1104  }
1105  }
1106 
1107  if (numbers.size() == 1) {
1108  auto impl = new gum::MultiDimNoisyORCompound< GUM_SCALAR >(leak, numbers.front());
1109  auto attr = new PRMScalarAttribute< GUM_SCALAR >(name, retrieveType("boolean"), impl);
1110  addAttribute(attr);
1111  } else if (numbers.size() == parents.size()) {
1115  = new gum::prm::PRMFuncAttribute< GUM_SCALAR >(name, retrieveType("boolean"), noisy);
1116 
1117  for (size_t idx = 0; idx < numbers.size(); ++idx) {
1118  noisy->causalWeight(parents[idx]->type().variable(), numbers[idx]);
1119  }
1120 
1121  addAttribute(attr);
1122  } else {
1123  GUM_ERROR(OperationNotAllowed, "invalid parameters for a noisy or")
1124  }
1125 
1126  if (!labels.empty()) {
1127  GUM_ERROR(OperationNotAllowed, "labels definitions not handle for noisy-or")
1128  }
1129  }
PRMType * _retrieveCommonType_(const std::vector< PRMClassElement< GUM_SCALAR > * > &elts)
Retrieve the common PRMType of a vector of PRMClassElement<GUM_SCALAR>.
PRMType & retrieveType(const std::string &name)
Returns a reference over a PRMType given its name.
virtual void addAttribute(const std::string &type, const std::string &name) override
Add an attribute to an interface.
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 PRMObject::prm_type currentType() const override
<agrum/PRM/elements/funcAttribute.h>
virtual PRMObject * getCurrent() override
A PRMClass is an object of a PRM representing a fragment of a Bayesian network which can be instantia...
Definition: PRMClass.h:65
GUM_SCALAR causalWeight(const DiscreteVariable &v) const
Copy of a multiDimICIModel.
static INLINE bool isSlotChain(const PRMClassElement< GUM_SCALAR > &elt)
Return true if obj is of type PRMSlotChain.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ addParameter()

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMFactory< GUM_SCALAR >::addParameter ( const std::string &  type,
const std::string &  name,
double  value 
)
overridevirtual

Add a parameter to the current class with a default value.

A parameter with a default value is an PRMAttribute<GUM_SCALAR> (aka mutable PRMAttribute<GUM_SCALAR>) with no parents and with a CPF filled with 1.

Parameters
typeThe type of this parameter.
nameThe name of this parameter.
valueThe label used as default value for this parameter.

Implements gum::prm::IPRMFactory.

Definition at line 524 of file PRMFactory_tpl.h.

526  {
527  auto c = static_cast< PRMClass< GUM_SCALAR >* >(_checkStack_(1, PRMObject::prm_type::CLASS));
528 
529  PRMParameter< GUM_SCALAR >* p = nullptr;
530  if (type == "int") {
531  p = new PRMParameter< GUM_SCALAR >(name,
532  PRMParameter< GUM_SCALAR >::ParameterType::INT,
533  (GUM_SCALAR)value);
534  } else if (type == "real") {
535  p = new PRMParameter< GUM_SCALAR >(name,
536  PRMParameter< GUM_SCALAR >::ParameterType::REAL,
537  (GUM_SCALAR)value);
538  }
539 
540  try {
541  c->add(p);
542  } catch (DuplicateElement&) { c->overload(p); }
543  }
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 gi...

◆ addParent()

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMFactory< GUM_SCALAR >::addParent ( const std::string &  name)
overridevirtual

Tells the factory that we add a parent to the current declared attribute.

The name can refer to an already declared attribute or aggregate of the same class. It can also be a slot chain only if there is no multiple reference in it.

Exceptions
OperationNotAllowedRaised if the given operation is illegal.

Implements gum::prm::IPRMFactory.

Definition at line 291 of file PRMFactory_tpl.h.

291  {
292  PRMClassElementContainer< GUM_SCALAR >* c = _checkStackContainter_(2);
293  try {
294  // Retrieving pointers
295  PRMAttribute< GUM_SCALAR >* a = static_cast< PRMAttribute< GUM_SCALAR >* >(
297  _addParent_(c, a, name);
298  } catch (FactoryInvalidState&) {
299  auto agg = static_cast< PRMAggregate< GUM_SCALAR >* >(
301  _addParent_(static_cast< PRMClass< GUM_SCALAR >* >(c), agg, name);
302  }
303  }
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 gi...
PRMClassElementContainer< GUM_SCALAR > * _checkStackContainter_(Idx i)
Adds prefix to str iff prefix != "".
void _addParent_(PRMClassElementContainer< GUM_SCALAR > *c, PRMAttribute< GUM_SCALAR > *agg, const std::string &name)
Add a parent to an attribute.

◆ addRangeType()

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMFactory< GUM_SCALAR >::addRangeType ( const std::string &  name,
long  minVal,
long  maxVal 
)
overridevirtual

Add a range variable type declaration.

Parameters
nameThe variable's name
minValThe variable's lower bound.
maxValThe variable's upper bound.
Exceptions
DuplicateElementRaised if an object with the same name already exists.
OperationNotAllowedRaised if the range variable is not a valid discrete type.

Implements gum::prm::IPRMFactory.

Definition at line 1478 of file PRMFactory_tpl.h.

1478  {
1479  std::string real_name = _addPrefix_(name);
1480  if (_prm_->_typeMap_.exists(real_name)) {
1481  std::stringstream msg;
1482  msg << "\"" << real_name << "' is already used.";
1483  GUM_ERROR(DuplicateElement, msg.str())
1484  }
1485 
1486  auto var = RangeVariable(real_name, "", minVal, maxVal);
1487  auto t = new PRMType(var);
1488 
1489  if (t->variable().domainSize() < 2) {
1490  GUM_ERROR(OperationNotAllowed, "current type is not a valid discrete type")
1491  }
1492 
1493  _prm_->_typeMap_.insert(t->name(), t);
1494  _prm_->_types_.insert(t);
1495  }
std::string _addPrefix_(const std::string &str) const
Adds prefix to str iff prefix != "".
PRM< GUM_SCALAR > * _prm_
The pointer on the PRM<GUM_SCALAR> built by this factory.
Definition: PRMFactory.h:1026
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ addReferenceSlot()

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMFactory< GUM_SCALAR >::addReferenceSlot ( const std::string &  type,
const std::string &  name,
bool  isArray 
)
overridevirtual

Tells the factory that we started declaring a slot.

Exceptions
OperationNotAllowedRaised if the given operation is illegal.

Implements gum::prm::IPRMFactory.

Definition at line 809 of file PRMFactory_tpl.h.

811  {
812  PRMClassElementContainer< GUM_SCALAR >* owner = _checkStackContainter_(1);
813  PRMClassElementContainer< GUM_SCALAR >* slotType = 0;
814 
815  try {
816  slotType = _retrieveClass_(type);
817  } catch (NotFound&) {
818  try {
819  slotType = _retrieveInterface_(type);
820  } catch (NotFound&) { GUM_ERROR(NotFound, "unknown ReferenceSlot<GUM_SCALAR> slot type") }
821  }
822 
823  PRMReferenceSlot< GUM_SCALAR >* ref
824  = new PRMReferenceSlot< GUM_SCALAR >(name, *slotType, isArray);
825 
826  try {
827  owner->add(ref);
828  } catch (DuplicateElement&) { owner->overload(ref); }
829  }
PRMClassElementContainer< GUM_SCALAR > * _checkStackContainter_(Idx i)
Adds prefix to str iff prefix != "".
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 ...
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...
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ addTick()

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMFactory< GUM_SCALAR >::addTick ( double  tick)
overridevirtual

Add a tick to the current discretized type.

Parameters
tickThe tick value.

Implements gum::prm::IPRMFactory.

Definition at line 1449 of file PRMFactory_tpl.h.

1449  {
1450  PRMType* t = static_cast< PRMType* >(_checkStack_(1, PRMObject::prm_type::TYPE));
1451  DiscretizedVariable< double >* var = dynamic_cast< DiscretizedVariable< double >* >(t->_var_);
1452 
1453  if (!var) { GUM_ERROR(FatalError, "the current type's variable is not a LabelizedVariable.") }
1454 
1455  try {
1456  var->addTick(tick);
1457  } catch (DefaultInLabel&) {
1458  GUM_ERROR(OperationNotAllowed, "tick already in used for this variable")
1459  }
1460  }
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 gi...
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ closeCurrent()

template<typename GUM_SCALAR >
INLINE PRMObject * gum::prm::PRMFactory< GUM_SCALAR >::closeCurrent ( )
overridevirtual

Close current object being built.

Nothing is done in particular except removing the current object from the factory's stack. If the object was not added to a container it will not be deleted properly, so you might want to delete it yourself (at your own risks!).

Returns
Returns the top most object removed in this factory's stack before. If their is no such object, 0 is returned.

Implements gum::prm::IPRMFactory.

Definition at line 1340 of file PRMFactory_tpl.h.

1340  {
1341  if (_stack_.size() > 0) {
1342  PRMObject* obj = _stack_.back();
1343  _stack_.pop_back();
1344  return obj;
1345  } else {
1346  return 0;
1347  }
1348  }
std::vector< PRMObject *> _stack_
A stack used to keep track of created PRMObject.
Definition: PRMFactory.h:1029

◆ continueAggregator()

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMFactory< GUM_SCALAR >::continueAggregator ( const std::string &  name)

Conitnues an aggregator declaration.

Definition at line 580 of file PRMFactory_tpl.h.

580  {
581  PRMClassElementContainer< GUM_SCALAR >* c = _checkStackContainter_(1);
582 
583  if (!c->exists(name)) GUM_ERROR(NotFound, "Element " << name << "not found")
584 
585  auto& agg = c->get(name);
587  GUM_ERROR(OperationNotAllowed, "Element " << name << " not an aggregate")
588 
589  _stack_.push_back(&agg);
590  }
PRMClassElementContainer< GUM_SCALAR > * _checkStackContainter_(Idx i)
Adds prefix to str iff prefix != "".
std::vector< PRMObject *> _stack_
A stack used to keep track of created PRMObject.
Definition: PRMFactory.h:1029
static INLINE bool isAggregate(const PRMClassElement< GUM_SCALAR > &elt)
Return true if obj is of type PRMAggregate.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ continueAttribute()

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMFactory< GUM_SCALAR >::continueAttribute ( const std::string &  name)
overridevirtual

Continues the declaration of an attribute.

Implements gum::prm::IPRMFactory.

Definition at line 1541 of file PRMFactory_tpl.h.

1541  {
1542  PRMClassElementContainer< GUM_SCALAR >* c = _checkStackContainter_(1);
1543 
1544  if (!c->exists(name)) GUM_ERROR(NotFound, "Attribute " << name << "not found")
1545 
1546  auto& a = c->get(name);
1547 
1549  GUM_ERROR(OperationNotAllowed, "Element " << name << " not an attribute")
1550 
1551  _stack_.push_back(&a);
1552  }
PRMClassElementContainer< GUM_SCALAR > * _checkStackContainter_(Idx i)
Adds prefix to str iff prefix != "".
static INLINE bool isAttribute(const PRMClassElement< GUM_SCALAR > &elt)
Returns true if obj_ptr is of type PRMAttribute.
std::vector< PRMObject *> _stack_
A stack used to keep track of created PRMObject.
Definition: PRMFactory.h:1029
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ continueClass()

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMFactory< GUM_SCALAR >::continueClass ( const std::string &  c)
overridevirtual

Continue the declaration of a class.

Implements gum::prm::IPRMFactory.

Definition at line 83 of file PRMFactory_tpl.h.

83  {
84  std::string real_name = _addPrefix_(name);
85  if (!(_prm_->_classMap_.exists(real_name))) {
86  std::stringstream msg;
87  msg << "'" << real_name << "' not found";
88  GUM_ERROR(NotFound, msg.str())
89  }
90  _stack_.push_back(&(_prm_->getClass(real_name)));
91  }
std::string _addPrefix_(const std::string &str) const
Adds prefix to str iff prefix != "".
std::vector< PRMObject *> _stack_
A stack used to keep track of created PRMObject.
Definition: PRMFactory.h:1029
PRM< GUM_SCALAR > * _prm_
The pointer on the PRM<GUM_SCALAR> built by this factory.
Definition: PRMFactory.h:1026
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ continueInterface()

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMFactory< GUM_SCALAR >::continueInterface ( const std::string &  name)
overridevirtual

Continue the declaration of an interface.

Implements gum::prm::IPRMFactory.

Definition at line 201 of file PRMFactory_tpl.h.

201  {
202  std::string real_name = _addPrefix_(name);
203  if (!_prm_->_interfaceMap_.exists(real_name)) {
204  GUM_ERROR(DuplicateElement, "'" << real_name << "' not found.")
205  }
206 
207  PRMInterface< GUM_SCALAR >* i = _retrieveInterface_(real_name);
208  _stack_.push_back(i);
209  }
std::string _addPrefix_(const std::string &str) const
Adds prefix to str iff prefix != "".
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...
std::vector< PRMObject *> _stack_
A stack used to keep track of created PRMObject.
Definition: PRMFactory.h:1029
PRM< GUM_SCALAR > * _prm_
The pointer on the PRM<GUM_SCALAR> built by this factory.
Definition: PRMFactory.h:1026
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ currentPackage()

template<typename GUM_SCALAR >
INLINE std::string gum::prm::PRMFactory< GUM_SCALAR >::currentPackage ( ) const
overridevirtual
Returns
Returns the current package name.

Implements gum::prm::IPRMFactory.

Definition at line 1351 of file PRMFactory_tpl.h.

1351  {
1352  return (_packages_.empty()) ? "" : _packages_.back();
1353  }
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:1020

◆ currentType()

template<typename GUM_SCALAR >
INLINE PRMObject::prm_type gum::prm::PRMFactory< GUM_SCALAR >::currentType ( ) const
overridevirtual
Returns
Returns the PRMObject type of the object begin built.
Exceptions
NotFoundif no type is being built.

Implements gum::prm::IPRMFactory.

Definition at line 1319 of file PRMFactory_tpl.h.

1319  {
1320  if (_stack_.size() == 0) { GUM_ERROR(NotFound, "no object being built") }
1321 
1322  return _stack_.back()->obj_type();
1323  }
std::vector< PRMObject *> _stack_
A stack used to keep track of created PRMObject.
Definition: PRMFactory.h:1029
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ endAggregator()

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMFactory< GUM_SCALAR >::endAggregator ( )

Finishes an aggregate declaration.

Definition at line 650 of file PRMFactory_tpl.h.

650  {
652  _stack_.pop_back();
653  }
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 gi...
std::vector< PRMObject *> _stack_
A stack used to keep track of created PRMObject.
Definition: PRMFactory.h:1029

◆ endAttribute()

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMFactory< GUM_SCALAR >::endAttribute ( )
overridevirtual

Tells the factory that we finished declaring an attribute.

Exceptions
OperationNotAllowedRaised if the given operation is illegal.

Implements gum::prm::IPRMFactory.

Definition at line 1555 of file PRMFactory_tpl.h.

1555  {
1557  _stack_.pop_back();
1558  }
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 gi...
std::vector< PRMObject *> _stack_
A stack used to keep track of created PRMObject.
Definition: PRMFactory.h:1029

◆ endClass()

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMFactory< GUM_SCALAR >::endClass ( bool  checkImplementations = true)
overridevirtual

Tells the factory that we finished a class declaration.

Exceptions
PRMTypeErrorRaised if the current Class<GUM_SCALAR> does not respect one of it's PRMInterface<GUM_SCALAR>.

Implements gum::prm::IPRMFactory.

Definition at line 94 of file PRMFactory_tpl.h.

94  {
95  PRMClass< GUM_SCALAR >* c
96  = static_cast< PRMClass< GUM_SCALAR >* >(_checkStack_(1, PRMObject::prm_type::CLASS));
97 
98  if (checkImplementations) { _checkInterfaceImplementation_(c); }
99 
100  _stack_.pop_back();
101  }
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 gi...
void _checkInterfaceImplementation_(PRMClass< GUM_SCALAR > *c)
Check if c implements correctly all his interfaces.
std::vector< PRMObject *> _stack_
A stack used to keep track of created PRMObject.
Definition: PRMFactory.h:1029

◆ endDiscreteType()

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMFactory< GUM_SCALAR >::endDiscreteType ( )
overridevirtual

End the current discrete type declaration.

Exceptions
OperationNotAllowedRaised if the current type is not a valid discrete type.

Implements gum::prm::IPRMFactory.

Definition at line 1422 of file PRMFactory_tpl.h.

1422  {
1423  PRMType* t = static_cast< PRMType* >(_checkStack_(1, PRMObject::prm_type::TYPE));
1424 
1425  if (!t->_isValid_()) {
1426  GUM_ERROR(OperationNotAllowed, "current type is not a valid subtype")
1427  } else if (t->variable().domainSize() < 2) {
1428  GUM_ERROR(OperationNotAllowed, "current type is not a valid discrete type")
1429  }
1430 
1431  _prm_->_typeMap_.insert(t->name(), t);
1432 
1433  _prm_->_types_.insert(t);
1434  _stack_.pop_back();
1435  }
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 gi...
std::vector< PRMObject *> _stack_
A stack used to keep track of created PRMObject.
Definition: PRMFactory.h:1029
PRM< GUM_SCALAR > * _prm_
The pointer on the PRM<GUM_SCALAR> built by this factory.
Definition: PRMFactory.h:1026
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ endDiscretizedType()

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMFactory< GUM_SCALAR >::endDiscretizedType ( )
overridevirtual

End the current discretized type declaration.

Exceptions
OperationNotAllowedRaised if the current type is not a valid discrete type.

Implements gum::prm::IPRMFactory.

Definition at line 1463 of file PRMFactory_tpl.h.

1463  {
1464  PRMType* t = static_cast< PRMType* >(_checkStack_(1, PRMObject::prm_type::TYPE));
1465 
1466  if (t->variable().domainSize() < 2) {
1467  GUM_ERROR(OperationNotAllowed, "current type is not a valid discrete type")
1468  }
1469 
1470  _prm_->_typeMap_.insert(t->name(), t);
1471 
1472  _prm_->_types_.insert(t);
1473  _stack_.pop_back();
1474  }
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 gi...
std::vector< PRMObject *> _stack_
A stack used to keep track of created PRMObject.
Definition: PRMFactory.h:1029
PRM< GUM_SCALAR > * _prm_
The pointer on the PRM<GUM_SCALAR> built by this factory.
Definition: PRMFactory.h:1026
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ endInterface()

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMFactory< GUM_SCALAR >::endInterface ( )
overridevirtual

Tells the factory that we finished an interface declaration.

Implements gum::prm::IPRMFactory.

Definition at line 1498 of file PRMFactory_tpl.h.

1498  {
1500  _stack_.pop_back();
1501  }
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 gi...
std::vector< PRMObject *> _stack_
A stack used to keep track of created PRMObject.
Definition: PRMFactory.h:1029

◆ endSystem()

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMFactory< GUM_SCALAR >::endSystem ( )
overridevirtual

Tells the factory that we finished declaring a model.

Exceptions
OperationNotAllowedRaised if the given operation is illegal.

Implements gum::prm::IPRMFactory.

Definition at line 1572 of file PRMFactory_tpl.h.

1572  {
1573  try {
1574  PRMSystem< GUM_SCALAR >* model
1575  = static_cast< PRMSystem< GUM_SCALAR >* >(_checkStack_(1, PRMObject::prm_type::SYSTEM));
1576  _stack_.pop_back();
1577  model->instantiate();
1578  } catch (Exception&) { GUM_ERROR(FatalError, "could not create system") }
1579  }
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 gi...
std::vector< PRMObject *> _stack_
A stack used to keep track of created PRMObject.
Definition: PRMFactory.h:1029
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ getCurrent() [1/2]

template<typename GUM_SCALAR >
INLINE PRMObject * gum::prm::PRMFactory< GUM_SCALAR >::getCurrent ( )
overridevirtual
Returns
the current PRMObject being built by this factory.
Exceptions
NotFoundif nothing is being built.

Implements gum::prm::IPRMFactory.

Definition at line 1326 of file PRMFactory_tpl.h.

1326  {
1327  if (_stack_.size() == 0) { GUM_ERROR(NotFound, "no object being built") }
1328 
1329  return _stack_.back();
1330  }
std::vector< PRMObject *> _stack_
A stack used to keep track of created PRMObject.
Definition: PRMFactory.h:1029
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ getCurrent() [2/2]

template<typename GUM_SCALAR >
INLINE const PRMObject * gum::prm::PRMFactory< GUM_SCALAR >::getCurrent ( ) const
overridevirtual

Returns a pointer on the PRM<GUM_SCALAR> created by this factory.

This pointer will not be deleted when the factory is destroyed.

Returns
Returns the PRM<GUM_SCALAR> created by this factory.

Implements gum::prm::IPRMFactory.

Definition at line 1333 of file PRMFactory_tpl.h.

1333  {
1334  if (_stack_.size() == 0) { GUM_ERROR(NotFound, "no object being built") }
1335 
1336  return _stack_.back();
1337  }
std::vector< PRMObject *> _stack_
A stack used to keep track of created PRMObject.
Definition: PRMFactory.h:1029
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ incArray()

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMFactory< GUM_SCALAR >::incArray ( const std::string &  l_i,
const std::string &  r_i 
)
overridevirtual

Add an instance to an array.

Implements gum::prm::IPRMFactory.

Definition at line 859 of file PRMFactory_tpl.h.

859  {
860  PRMSystem< GUM_SCALAR >* model
861  = static_cast< PRMSystem< GUM_SCALAR >* >(_checkStack_(1, PRMObject::prm_type::SYSTEM));
862 
863  if (model->isArray(l_i)) {
864  if (model->isInstance(r_i)) {
865  model->add(l_i, model->get(r_i));
866  } else {
867  GUM_ERROR(NotFound, "right value is not an instance")
868  }
869  } else {
870  GUM_ERROR(NotFound, "left value is no an array")
871  }
872  }
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 gi...
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ isArrayInCurrentSystem()

template<typename GUM_SCALAR >
INLINE bool gum::prm::PRMFactory< GUM_SCALAR >::isArrayInCurrentSystem ( const std::string &  name) const
overridevirtual

Implements gum::prm::IPRMFactory.

Definition at line 1836 of file PRMFactory_tpl.h.

1836  {
1837  const PRMSystem< GUM_SCALAR >* system
1838  = static_cast< const PRMSystem< GUM_SCALAR >* >(getCurrent());
1839  return (system && system->isArray(name));
1840  }
virtual PRMObject * getCurrent() override

◆ isClassOrInterface()

template<typename GUM_SCALAR >
INLINE bool gum::prm::PRMFactory< GUM_SCALAR >::isClassOrInterface ( const std::string &  type) const
overridevirtual

Implements gum::prm::IPRMFactory.

Definition at line 1817 of file PRMFactory_tpl.h.

1817  {
1818  try {
1819  _retrieveClass_(type);
1820  return true;
1821 
1822  } catch (NotFound&) {
1823  } catch (DuplicateElement&) {}
1824 
1825  try {
1826  _retrieveInterface_(type);
1827  return true;
1828 
1829  } catch (NotFound&) {
1830  } catch (DuplicateElement&) {}
1831 
1832  return false;
1833  }
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 ...
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...

◆ operator=()

template<typename GUM_SCALAR >
PRMFactory& gum::prm::PRMFactory< GUM_SCALAR >::operator= ( const PRMFactory< GUM_SCALAR > &  from)
delete

Copy operator. Don't use it.

◆ popPackage()

template<typename GUM_SCALAR >
INLINE std::string gum::prm::PRMFactory< GUM_SCALAR >::popPackage ( )
overridevirtual

Pop the current package from the package stack.

Returns
the popped package or an empty string if there was nothing to pop.

Implements gum::prm::IPRMFactory.

Definition at line 1761 of file PRMFactory_tpl.h.

1761  {
1762  std::string plop = currentPackage();
1763 
1764  if (!_packages_.empty()) {
1765  std::string s = _packages_.back();
1766  _packages_.pop_back();
1767 
1768  if (_namespaces_.size() > 0) {
1769  delete _namespaces_.back();
1770  _namespaces_.pop_back();
1771  }
1772  return s;
1773  }
1774 
1775  return plop;
1776  }
std::vector< List< std::string > *> _namespaces_
Set of all declared namespaces.
Definition: PRMFactory.h:1023
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:1020
virtual std::string currentPackage() const override

◆ prm()

template<typename GUM_SCALAR >
INLINE PRM< GUM_SCALAR > * gum::prm::PRMFactory< GUM_SCALAR >::prm ( ) const

Returns a pointer on the PRM<GUM_SCALAR> created by this factory.

This pointer will not be deleted when the factory is destroyed.

Returns
Returns the PRM<GUM_SCALAR> created by this factory.

Definition at line 1314 of file PRMFactory_tpl.h.

1314  {
1315  return _prm_;
1316  }
PRM< GUM_SCALAR > * _prm_
The pointer on the PRM<GUM_SCALAR> built by this factory.
Definition: PRMFactory.h:1026

◆ pushPackage()

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMFactory< GUM_SCALAR >::pushPackage ( const std::string &  name)
overridevirtual

Define the current package.

A stack is used to keep track of packages given the different imports.

Parameters
nameThe name of the package for all further objects.

Implements gum::prm::IPRMFactory.

Definition at line 1755 of file PRMFactory_tpl.h.

1755  {
1756  _packages_.push_back(name);
1757  _namespaces_.push_back(new List< std::string >());
1758  }
std::vector< List< std::string > *> _namespaces_
Set of all declared namespaces.
Definition: PRMFactory.h:1023
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:1020

◆ retrieveClass()

template<typename GUM_SCALAR >
INLINE PRMClass< GUM_SCALAR > & gum::prm::PRMFactory< GUM_SCALAR >::retrieveClass ( const std::string &  name)

Returns a reference over a Class<GUM_SCALAR> given its name.

This methods adds if necessary the current package as a prefix to name.

Parameters
nameThe name of the Class<GUM_SCALAR>.
Returns
the Class<GUM_SCALAR> with the given name.
Exceptions
NotFoundif no Class<GUM_SCALAR> matches the given name.

Definition at line 1800 of file PRMFactory_tpl.h.

1800  {
1801  return *_retrieveClass_(name);
1802  }
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 ...

◆ retrieveCommonType()

template<typename GUM_SCALAR >
INLINE PRMType & gum::prm::PRMFactory< GUM_SCALAR >::retrieveCommonType ( const std::vector< PRMClassElement< GUM_SCALAR > * > &  elts)

Returns a pointer on the PRM<GUM_SCALAR> created by this factory.

This pointer will not be deleted when the factory is destroyed.

Returns
Returns the PRM<GUM_SCALAR> created by this factory.

Definition at line 1810 of file PRMFactory_tpl.h.

1811  {
1812  return *(_retrieveCommonType_(elts));
1813  }
PRMType * _retrieveCommonType_(const std::vector< PRMClassElement< GUM_SCALAR > * > &elts)
Retrieve the common PRMType of a vector of PRMClassElement<GUM_SCALAR>.

◆ retrieveType()

template<typename GUM_SCALAR >
INLINE PRMType & gum::prm::PRMFactory< GUM_SCALAR >::retrieveType ( const std::string &  name)

Returns a reference over a PRMType given its name.

This methods adds if necessary the current package as a prefix to name.

Parameters
nameThe name of the PRMType.
Returns
the PRMType with the given name.
Exceptions
NotFoundif no PRMType matches the given name.

Definition at line 1805 of file PRMFactory_tpl.h.

1805  {
1806  return *_retrieveType_(name);
1807  }
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...

◆ setCPFByFloatRule()

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMFactory< GUM_SCALAR >::setCPFByFloatRule ( const std::vector< std::string > &  labels,
const std::vector< float > &  values 
)
overridevirtual

Fills the CPF using a rule.

The labels vector is filled with one of each parent's labels or with a wildcard ("*"). If a wildcard is used then all values of the corresponding parents are used. The sequence of parents must be the declaration order used when adding the current attribute's parents.

Parameters
labelsThe value of each parents.
valuesThe probability values of the current attribute given the values in parenst.

Implements gum::prm::IPRMFactory.

Definition at line 378 of file PRMFactory_tpl.h.

379  {
380  auto a = static_cast< PRMAttribute< GUM_SCALAR >* >(
382 
383  if ((parents.size() + 1) != a->cpf().variablesSequence().size()) {
384  GUM_ERROR(OperationNotAllowed, "wrong number of parents")
385  }
386 
387  if (values.size() != a->type().variable().domainSize()) {
388  GUM_ERROR(OperationNotAllowed, "wrong number of values")
389  }
390 
391  std::vector< GUM_SCALAR > values2(values.begin(), values.end());
392  setCPFByRule(parents, values2);
393  }
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 gi...
virtual void setCPFByRule(const std::vector< std::string > &labels, const std::vector< GUM_SCALAR > &values)
Fills the CPF using a rule.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ setCPFByRule() [1/2]

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMFactory< GUM_SCALAR >::setCPFByRule ( const std::vector< std::string > &  labels,
const std::vector< GUM_SCALAR > &  values 
)
virtual

Fills the CPF using a rule.

The labels vector is filled with one of each parent's labels or with a wildcard ("*"). If a wildcard is used then all values of the corresponding parents are used. The sequence of parents must be the declaration order used when adding the current attribute's parents.

Parameters
labelsThe value of each parents.
valuesThe probability values of the current attribute given the values in parents.

Definition at line 396 of file PRMFactory_tpl.h.

397  {
398  auto a = static_cast< PRMAttribute< GUM_SCALAR >* >(
400 
401  if ((parents.size() + 1) != a->cpf().variablesSequence().size()) {
402  GUM_ERROR(OperationNotAllowed, "wrong number of parents")
403  }
404 
405  if (values.size() != a->type().variable().domainSize()) {
406  GUM_ERROR(OperationNotAllowed, "wrong number of values")
407  }
408 
409  if (dynamic_cast< PRMFormAttribute< GUM_SCALAR >* >(a)) {
410  auto form = static_cast< PRMFormAttribute< GUM_SCALAR >* >(a);
411  // jnst holds parents with a specific value (not "*")
412  // knst holds parents without a specific value ("*")
413  Instantiation jnst, knst;
414  const DiscreteVariable* var = 0;
415  // not_used Size pos = 0;
416  bool found = false;
417 
418  for (Idx i = 0; i < parents.size(); ++i) {
419  var = form->formulas().variablesSequence().atPos(1 + i);
420 
421  if (parents[i] == "*") {
422  knst.add(*var);
423  } else {
424  jnst.add(*var);
425  // not_used pos = 0;
426  found = false;
427 
428  for (Size j = 0; j < var->domainSize(); ++j) {
429  if (var->label(j) == parents[i]) {
430  jnst.chgVal(*var, j);
431  found = true;
432  break;
433  }
434  }
435 
436  if (!found) {
437  std::string msg = "could not find label ";
438  GUM_ERROR(NotFound, msg + parents[i])
439  }
440  }
441  }
442 
443  Instantiation inst(form->formulas());
444  inst.setVals(jnst);
445 
446  for (Size i = 0; i < form->type()->domainSize(); ++i) {
447  inst.chgVal(form->type().variable(), i);
448 
449  for (inst.setFirstIn(knst); !inst.end(); inst.incIn(knst)) {
450  form->formulas().set(inst, std::to_string(values[i]));
451  }
452  }
453 
454  } else {
455  GUM_ERROR(OperationNotAllowed, "invalide attribute type")
456  }
457  }
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 gi...
std::string to_string(const Formula &f)
Definition: formula_inl.h:474
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ setCPFByRule() [2/2]

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMFactory< GUM_SCALAR >::setCPFByRule ( const std::vector< std::string > &  labels,
const std::vector< std::string > &  values 
)
overridevirtual

Fills the CPF using a rule and gum::Formula.

The labels vector is filled with one of each parent's labels or with a wildcard ("*"). If a wildcard is used then all values of the corresponding parents are used. The sequence of parents must be the declaration order used when adding the current attribute's parents.

Parameters
labelsThe value of each parents.
valuesThe probability values of the current attribute given the values in parents.

Implements gum::prm::IPRMFactory.

Definition at line 460 of file PRMFactory_tpl.h.

461  {
462  auto a = static_cast< PRMAttribute< GUM_SCALAR >* >(
464 
465  if ((parents.size() + 1) != a->cpf().variablesSequence().size()) {
466  GUM_ERROR(OperationNotAllowed, "wrong number of parents")
467  }
468 
469  if (values.size() != a->type().variable().domainSize()) {
470  GUM_ERROR(OperationNotAllowed, "wrong number of values")
471  }
472 
473  if (dynamic_cast< PRMFormAttribute< GUM_SCALAR >* >(a)) {
474  auto form = static_cast< PRMFormAttribute< GUM_SCALAR >* >(a);
475  // jnst holds parents with a specific value (not "*")
476  // knst holds parents without a specific value ("*")
477  Instantiation jnst, knst;
478  const DiscreteVariable* var = 0;
479  // not_used Size pos = 0;
480  bool found = false;
481 
482  for (Idx i = 0; i < parents.size(); ++i) {
483  var = form->formulas().variablesSequence().atPos(1 + i);
484 
485  if (parents[i] == "*") {
486  knst.add(*var);
487  } else {
488  jnst.add(*var);
489  // not_used pos = 0;
490  found = false;
491 
492  for (Size j = 0; j < var->domainSize(); ++j) {
493  if (var->label(j) == parents[i]) {
494  jnst.chgVal(*var, j);
495  found = true;
496  break;
497  }
498  }
499 
500  if (!found) {
501  std::string msg = "could not find label ";
502  GUM_ERROR(NotFound, msg + parents[i])
503  }
504  }
505  }
506 
507  Instantiation inst(form->formulas());
508  inst.setVals(jnst);
509 
510  for (Size i = 0; i < form->type()->domainSize(); ++i) {
511  inst.chgVal(form->type().variable(), i);
512 
513  for (inst.setFirstIn(knst); !inst.end(); inst.incIn(knst)) {
514  form->formulas().set(inst, values[i]);
515  }
516  }
517 
518  } else {
519  GUM_ERROR(OperationNotAllowed, "invalide attribute type")
520  }
521  }
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 gi...
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ setRawCPFByColumns() [1/2]

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMFactory< GUM_SCALAR >::setRawCPFByColumns ( const std::vector< GUM_SCALAR > &  array)

Gives the factory the CPF in its raw form.

The creation of the CPF is left to the factory because we do not know what level of complexity for CPF implementation can be handled by the PRM<GUM_SCALAR> implementation.

How to fill a CPT? If you want to fill the CPT of P(A|B,C) with A, B and C boolean variables ( {f, t}, the order is important), then the following array is valid:

//P(A|f,f),P(A|f,t),P(A|t,f),P(A|t,t)
[ 0.2, 0.7, 0.5, 0.01,
0.8, 0.3, 0.5, 0.99]

See PRMFactory::setRawCPFByLines() for more details.

Exceptions
OperationNotAllowedRaised if the given operation is illegal.

Definition at line 346 of file PRMFactory_tpl.h.

346  {
347  PRMAttribute< GUM_SCALAR >* a = static_cast< PRMAttribute< GUM_SCALAR >* >(
349 
350  if (a->cpf().domainSize() != array.size()) {
351  GUM_ERROR(OperationNotAllowed, "illegal CPF size")
352  }
353 
354  if (a->cpf().nbrDim() == 1) {
355  setRawCPFByLines(array);
356 
357  } else {
358  Instantiation inst(a->cpf());
359  Instantiation jnst;
360  for (auto idx = inst.variablesSequence().rbegin(); idx != inst.variablesSequence().rend();
361  --idx) {
362  jnst.add(**idx);
363  }
364 
365  jnst.setFirst();
366  auto idx = (std::size_t)0;
367  while ((!jnst.end()) && idx < array.size()) {
368  inst.setVals(jnst);
369  a->cpf().set(inst, array[idx]);
370  jnst.inc();
371  ++idx;
372  }
373  }
374  }
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 gi...
void setRawCPFByLines(const std::vector< GUM_SCALAR > &array)
Gives the factory the CPF in its raw form.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ setRawCPFByColumns() [2/2]

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMFactory< GUM_SCALAR >::setRawCPFByColumns ( const std::vector< std::string > &  array)
overridevirtual

Gives the factory the CPF in its raw form use gum::Formula.

The creation of the CPF is left to the factory because we do not know what level of complexity for CPF implementation can be handled by the PRM<GUM_SCALAR> implementation.

How to fill a CPT? If you want to fill the CPT of P(A|B,C) with A, B and C boolean variables ( {f, t}, the order is important), then the following array is valid:

//P(A|f,f),P(A|f,t),P(A|t,f),P(A|t,t)
[ 0.2, 0.7, 0.5, 0.01,
0.8, 0.3, 0.5, 0.99]

See PRMFactory::setRawCPFByLines() for more details.

Exceptions
OperationNotAllowedRaised if the given operation is illegal.

Implements gum::prm::IPRMFactory.

Definition at line 1844 of file PRMFactory_tpl.h.

1844  {
1846 
1847  auto a = static_cast< PRMFormAttribute< GUM_SCALAR >* >(
1849 
1850  if (a->formulas().domainSize() != array.size()) {
1851  GUM_ERROR(OperationNotAllowed, "illegal CPF size")
1852  }
1853 
1854  if (a->formulas().nbrDim() == 1) {
1855  setRawCPFByLines(array);
1856 
1857  } else {
1858  Instantiation inst(a->formulas());
1859  Instantiation jnst;
1860  for (auto idx = inst.variablesSequence().rbegin(); idx != inst.variablesSequence().rend();
1861  --idx) {
1862  jnst.add(**idx);
1863  }
1864 
1865  jnst.setFirst();
1866  auto idx = (std::size_t)0;
1867  while ((!jnst.end()) && idx < array.size()) {
1868  inst.setVals(jnst);
1869  a->formulas().set(inst, array[idx]);
1870  jnst.inc();
1871  ++idx;
1872  }
1873 
1874  // Generate cpf by calling it
1875  a->cpf();
1876  }
1877  }
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 gi...
void setRawCPFByLines(const std::vector< GUM_SCALAR > &array)
Gives the factory the CPF in its raw form.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ setRawCPFByFloatColumns()

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMFactory< GUM_SCALAR >::setRawCPFByFloatColumns ( const std::vector< float > &  array)
overridevirtual

Gives the factory the CPF in its raw form.

The creation of the CPF is left to the factory because we do not know what level of complexity for CPF implementation can be handled by the PRM<GUM_SCALAR> implementation.

How to fill a CPT? If you want to fill the CPT of P(A|B,C) with A, B and C boolean variables ( {f, t}, the order is important), then the following array is valid:

//P(A|f,f),P(A|f,t),P(A|t,f),P(A|t,t)
[ 0.2, 0.7, 0.5, 0.01,
0.8, 0.3, 0.5, 0.99]

See PRMFactory::setRawCPFByLines() for more details.

Exceptions
OperationNotAllowedRaised if the given operation is illegal.

Implements gum::prm::IPRMFactory.

Definition at line 332 of file PRMFactory_tpl.h.

332  {
333  PRMAttribute< GUM_SCALAR >* a = static_cast< PRMAttribute< GUM_SCALAR >* >(
335 
336  if (a->cpf().domainSize() != array.size()) {
337  GUM_ERROR(OperationNotAllowed, "illegal CPF size")
338  }
339 
340  std::vector< GUM_SCALAR > array2(array.begin(), array.end());
341  setRawCPFByColumns(array2);
342  }
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 gi...
void setRawCPFByColumns(const std::vector< GUM_SCALAR > &array)
Gives the factory the CPF in its raw form.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ setRawCPFByFloatLines()

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMFactory< GUM_SCALAR >::setRawCPFByFloatLines ( const std::vector< float > &  array)
overridevirtual

Gives the factory the CPF in its raw form.

The creation of the CPF is left to the factory because we do not know what level of complexity for CPF implementation can be handled by the PRM<GUM_SCALAR> implementation.

How to fill a CPT? If you want to fill the CPT of P(A|B,C) with A, B and C boolean variables ( {f, t}, the order is important), then the following array is valid:

[0.20, 0.80, // P(f|f, f) = 0.20 and P(t|f, f) = 0.80
0.50, 0.50, // P(f|t, f) = 0.50 and P(t|t, f) = 0.50
0.70, 0.30, // P(f|f, t) = 0.70 and P(t|f, t) = 0.30
0.01, 0.99] // P(f|t, t) = 0.01 and P(t|t, t) = 0.99
Exceptions
OperationNotAllowedRaised if the given operation is illegal.

Implements gum::prm::IPRMFactory.

Definition at line 306 of file PRMFactory_tpl.h.

306  {
307  PRMAttribute< GUM_SCALAR >* a = static_cast< PRMAttribute< GUM_SCALAR >* >(
310 
311  if (a->cpf().domainSize() != array.size()) GUM_ERROR(OperationNotAllowed, "illegal CPF size")
312 
313  std::vector< GUM_SCALAR > array2(array.begin(), array.end());
314  a->cpf().fillWith(array2);
315  }
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 gi...
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ setRawCPFByLines() [1/2]

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMFactory< GUM_SCALAR >::setRawCPFByLines ( const std::vector< GUM_SCALAR > &  array)

Gives the factory the CPF in its raw form.

The creation of the CPF is left to the factory because we do not know what level of complexity for CPF implementation can be handled by the PRM<GUM_SCALAR> implementation.

How to fill a CPT? If you want to fill the CPT of P(A|B,C) with A, B and C boolean variables ( {f, t}, the order is important), then the following array is valid:

[0.20, 0.80, // P(f|f, f) = 0.20 and P(t|f, f) = 0.80
0.50, 0.50, // P(f|t, f) = 0.50 and P(t|t, f) = 0.50
0.70, 0.30, // P(f|f, t) = 0.70 and P(t|f, t) = 0.30
0.01, 0.99] // P(f|t, t) = 0.01 and P(t|t, t) = 0.99
Exceptions
OperationNotAllowedRaised if the given operation is illegal.

Definition at line 318 of file PRMFactory_tpl.h.

318  {
320  auto a = static_cast< PRMAttribute< GUM_SCALAR >* >(elt);
322 
323  if (a->cpf().domainSize() != array.size()) {
324  GUM_ERROR(OperationNotAllowed, "illegal CPF size")
325  }
326 
327  a->cpf().fillWith(array);
328  }
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 gi...
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ setRawCPFByLines() [2/2]

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMFactory< GUM_SCALAR >::setRawCPFByLines ( const std::vector< std::string > &  array)
overridevirtual

Gives the factory the CPF in its raw form using gum::Formula.

The creation of the CPF is left to the factory because we do not know what level of complexity for CPF implementation can be handled by the PRM<GUM_SCALAR> implementation.

How to fill a CPT? If you want to fill the CPT of P(A|B,C) with A, B and C boolean variables ( {f, t}, the order is important), then the following array is valid:

[0.20, 0.80, // P(f|f, f) = 0.20 and P(t|f, f) = 0.80
0.50, 0.50, // P(f|t, f) = 0.50 and P(t|t, f) = 0.50
0.70, 0.30, // P(f|f, t) = 0.70 and P(t|f, t) = 0.30
0.01, 0.99] // P(f|t, t) = 0.01 and P(t|t, t) = 0.99
Exceptions
OperationNotAllowedRaised if the given operation is illegal.

Implements gum::prm::IPRMFactory.

Definition at line 1881 of file PRMFactory_tpl.h.

1881  {
1883 
1884  auto a = static_cast< PRMFormAttribute< GUM_SCALAR >* >(
1886 
1887  if (a->formulas().domainSize() != array.size()) {
1888  GUM_ERROR(OperationNotAllowed, "illegal CPF size")
1889  }
1890 
1891  a->formulas().populate(array);
1892 
1894  a->cpf();
1895  }
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 gi...
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ setReferenceSlot() [1/2]

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMFactory< GUM_SCALAR >::setReferenceSlot ( const std::string &  left_instance,
const std::string &  left_reference,
const std::string &  right_instance 
)
overridevirtual

Instantiate a reference in the current model.

This is equivalent to the following SKOOL instruction: left_instance.left_reference = right_instance;

right_instance's type must be compatible with the slot type of left_reference.

Parameters
left_instanceThe name of an instance in the model.
left_referenceThe name of a reference of left_instance.
right_instanceThe name of an instance or an array of instances in the model.

Implements gum::prm::IPRMFactory.

Definition at line 875 of file PRMFactory_tpl.h.

877  {
878  auto model
879  = static_cast< PRMSystem< GUM_SCALAR >* >(_checkStack_(1, PRMObject::prm_type::SYSTEM));
880  std::vector< PRMInstance< GUM_SCALAR >* > lefts;
881  std::vector< PRMInstance< GUM_SCALAR >* > rights;
882 
883  if (model->isInstance(l_i)) {
884  lefts.push_back(&(model->get(l_i)));
885  } else if (model->isArray(l_i)) {
886  for (const auto& elt: model->getArray(l_i))
887  lefts.push_back(elt);
888  } else {
889  GUM_ERROR(NotFound, "left value does not name an instance or an array")
890  }
891 
892  if (model->isInstance(r_i)) {
893  rights.push_back(&(model->get(r_i)));
894  } else if (model->isArray(r_i)) {
895  for (const auto& elt: model->getArray(r_i))
896  rights.push_back(elt);
897  } else {
898  GUM_ERROR(NotFound, "left value does not name an instance or an array")
899  }
900 
901  for (const auto l: lefts) {
902  for (const auto r: rights) {
903  auto& elt = l->type().get(l_ref);
905  l->add(elt.id(), *r);
906 
907  } else {
908  GUM_ERROR(NotFound, "unfound reference slot")
909  }
910  }
911  }
912  }
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 gi...
static INLINE bool isReferenceSlot(const PRMClassElement< GUM_SCALAR > &elt)
Returns true if obj_ptr is of type PRMReferenceSlot.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ setReferenceSlot() [2/2]

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMFactory< GUM_SCALAR >::setReferenceSlot ( const std::string &  l_i,
const std::string &  r_i 
)
overridevirtual

Instantiate a reference in the current model.

This is equivalent to the following SKOOL instruction: left_instance.left_reference = right_instance;

right_instance's type must be compatible with the slot type of left_reference.

Parameters
l_iA slot chain starting from an instance and ending over a reference slot.
r_iThe name of an instance or an array of instances in the model.

Implements gum::prm::IPRMFactory.

Definition at line 1786 of file PRMFactory_tpl.h.

1787  {
1788  size_t pos = l_i.find_last_of('.');
1789 
1790  if (pos != std::string::npos) {
1791  std::string l_ref = l_i.substr(pos + 1, std::string::npos);
1792  setReferenceSlot(l_i.substr(0, pos), l_ref, r_i);
1793  } else {
1794  GUM_ERROR(NotFound, "left value does not name an instance or an array")
1795  }
1796  }
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.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ startAggregator()

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMFactory< GUM_SCALAR >::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.

Parameters
nameThe aggregator's name.
agg_typeThe aggregtor's type (
See also
Aggregate::agg_type()).
Parameters
rv_typeThe aggregator's random variable type (
See also
Aggregate::type()).
Parameters
paramsThe aggregator's parameters.

Definition at line 547 of file PRMFactory_tpl.h.

550  {
551  PRMClass< GUM_SCALAR >* c
552  = static_cast< PRMClass< GUM_SCALAR >* >(_checkStack_(1, PRMObject::prm_type::CLASS));
553 
554  auto agg = new PRMAggregate< GUM_SCALAR >(name,
556  *_retrieveType_(rv_type));
557 
558  try {
559  c->add(agg);
560  } catch (DuplicateElement&) { c->overload(agg); }
561 
562  switch (agg->agg_type()) {
566  if (params.size() != 1) {
567  GUM_ERROR(OperationNotAllowed, "aggregate requires a parameter")
568  }
569  agg->setLabel(params.front());
570  break;
571  }
572  default: {
573  // Nothing to do
574  }
575  }
576  _stack_.push_back(agg);
577  }
static AggregateType str2enum(const std::string &str)
Static method which returns the AggregateType given its string representation.
Definition: PRMAggregate.h:101
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...
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 gi...
std::vector< PRMObject *> _stack_
A stack used to keep track of created PRMObject.
Definition: PRMFactory.h:1029
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ startAttribute()

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMFactory< GUM_SCALAR >::startAttribute ( const std::string &  type,
const std::string &  name,
bool  scalar_atttr = false 
)
overridevirtual

Tells the factory that we start an attribute declaration.

Exceptions
FatalErrorRaised if the given operation is illegal.

Implements gum::prm::IPRMFactory.

Definition at line 1512 of file PRMFactory_tpl.h.

1514  {
1515  PRMClassElementContainer< GUM_SCALAR >* c = _checkStackContainter_(1);
1516  PRMAttribute< GUM_SCALAR >* a = nullptr;
1517 
1518  if (PRMObject::isClass(*c) && (!scalar_attr)) {
1519  a = new PRMFormAttribute< GUM_SCALAR >(static_cast< PRMClass< GUM_SCALAR >& >(*c),
1520  name,
1521  *_retrieveType_(type));
1522 
1523  } else {
1524  a = new PRMScalarAttribute< GUM_SCALAR >(name, *_retrieveType_(type));
1525  }
1526 
1527  std::string dot = ".";
1528 
1529  try {
1530  try {
1531  c->add(a);
1532  } catch (DuplicateElement&) { c->overload(a); }
1533  } catch (Exception&) {
1534  if (a != nullptr && (!c->exists(a->id()))) { delete a; }
1535  }
1536 
1537  _stack_.push_back(a);
1538  }
static INLINE bool isClass(const PRMObject &obj)
Returns true if obj_ptr is of type Class.
Definition: PRMObject.h:109
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...
PRMClassElementContainer< GUM_SCALAR > * _checkStackContainter_(Idx i)
Adds prefix to str iff prefix != "".
std::vector< PRMObject *> _stack_
A stack used to keep track of created PRMObject.
Definition: PRMFactory.h:1029

◆ startClass()

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMFactory< GUM_SCALAR >::startClass ( const std::string &  c,
const std::string &  ext = "",
const Set< std::string > *  implements = nullptr,
bool  delayInheritance = false 
)
overridevirtual

Tells the factory that we start a class declaration.

Use the default values if you do not want c to be a subclass or to implement interfaces.

Parameters
cThe class name.
extThe name of the super class of c.
implementsThe list of interface implemented by c.
delayInheritanceIf true, the created gum::prm::Class inheritance will be delayed.
Exceptions
OperationNotAllowedRaised if the given operation is illegal.

Implements gum::prm::IPRMFactory.

Definition at line 47 of file PRMFactory_tpl.h.

50  {
51  std::string real_name = _addPrefix_(name);
52  if (_prm_->_classMap_.exists(real_name) || _prm_->_interfaceMap_.exists(real_name)) {
53  GUM_ERROR(DuplicateElement, "'" << real_name << "' is already used.")
54  }
55  PRMClass< GUM_SCALAR >* c = nullptr;
56  PRMClass< GUM_SCALAR >* mother = nullptr;
57  Set< PRMInterface< GUM_SCALAR >* > impl;
58 
59  if (implements != 0) {
60  for (const auto& imp: *implements) {
61  impl.insert(_retrieveInterface_(imp));
62  }
63  }
64 
65  if (extends != "") { mother = _retrieveClass_(extends); }
66 
67  if ((extends == "") && impl.empty()) {
68  c = new PRMClass< GUM_SCALAR >(real_name);
69  } else if ((extends != "") && impl.empty()) {
70  c = new PRMClass< GUM_SCALAR >(real_name, *mother, delayInheritance);
71  } else if ((extends == "") && (!impl.empty())) {
72  c = new PRMClass< GUM_SCALAR >(real_name, impl, delayInheritance);
73  } else if ((extends != "") && (!impl.empty())) {
74  c = new PRMClass< GUM_SCALAR >(real_name, *mother, impl, delayInheritance);
75  }
76 
77  _prm_->_classMap_.insert(c->name(), c);
78  _prm_->_classes_.insert(c);
79  _stack_.push_back(c);
80  }
std::string _addPrefix_(const std::string &str) const
Adds prefix to str iff prefix != "".
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 ...
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...
std::vector< PRMObject *> _stack_
A stack used to keep track of created PRMObject.
Definition: PRMFactory.h:1029
PRM< GUM_SCALAR > * _prm_
The pointer on the PRM<GUM_SCALAR> built by this factory.
Definition: PRMFactory.h:1026
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ startDiscreteType()

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMFactory< GUM_SCALAR >::startDiscreteType ( const std::string &  name,
std::string  super = "" 
)
overridevirtual

Start a discrete subtype declaration.

Parameters
nameThe discrete type's name.
superThe super type of the declared discrete type.
Exceptions
DuplicateElementRaised if an object with the same name already exists.

Implements gum::prm::IPRMFactory.

Definition at line 1356 of file PRMFactory_tpl.h.

1357  {
1358  std::string real_name = _addPrefix_(name);
1359  if (_prm_->_typeMap_.exists(real_name)) {
1360  GUM_ERROR(DuplicateElement, "'" << real_name << "' is already used.")
1361  }
1362  if (super == "") {
1363  auto t = new PRMType(LabelizedVariable(real_name, "", 0));
1364  _stack_.push_back(t);
1365  } else {
1366  auto t = new PRMType(LabelizedVariable(real_name, "", 0));
1367  t->_superType_ = _retrieveType_(super);
1368  t->_label_map_ = new std::vector< Idx >();
1369  _stack_.push_back(t);
1370  }
1371  }
std::string _addPrefix_(const std::string &str) const
Adds prefix to str iff prefix != "".
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...
std::vector< PRMObject *> _stack_
A stack used to keep track of created PRMObject.
Definition: PRMFactory.h:1029
PRM< GUM_SCALAR > * _prm_
The pointer on the PRM<GUM_SCALAR> built by this factory.
Definition: PRMFactory.h:1026
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ startDiscretizedType()

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMFactory< GUM_SCALAR >::startDiscretizedType ( const std::string &  name)
overridevirtual

Start a discretized type declaration.

Parameters
nameThe discretized type's name.
Exceptions
DuplicateElementRaised if an object with the same name already exists.

Implements gum::prm::IPRMFactory.

Definition at line 1438 of file PRMFactory_tpl.h.

1438  {
1439  std::string real_name = _addPrefix_(name);
1440  if (_prm_->_typeMap_.exists(real_name)) {
1441  GUM_ERROR(DuplicateElement, "'" << real_name << "' is already used.")
1442  }
1443  auto var = DiscretizedVariable< double >(real_name, "");
1444  auto t = new PRMType(var);
1445  _stack_.push_back(t);
1446  }
std::string _addPrefix_(const std::string &str) const
Adds prefix to str iff prefix != "".
std::vector< PRMObject *> _stack_
A stack used to keep track of created PRMObject.
Definition: PRMFactory.h:1029
PRM< GUM_SCALAR > * _prm_
The pointer on the PRM<GUM_SCALAR> built by this factory.
Definition: PRMFactory.h:1026
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ startInterface()

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMFactory< GUM_SCALAR >::startInterface ( const std::string &  i,
const std::string &  ext = "",
bool  delayInheritance = false 
)
overridevirtual

Tells the factory that we start an interface declaration.

Use the default values if you do not want c to be a subclass or to implement interfaces.

Parameters
iThe interface name.
extThe name of the super interface of i.
delayInheritanceIf true, the created gum::prm::PRMInterface inheritance will be delayed.
Exceptions
NotFoundRaised if ext does not match any declared PRMInterface<GUM_SCALAR>.

Implements gum::prm::IPRMFactory.

Definition at line 177 of file PRMFactory_tpl.h.

179  {
180  std::string real_name = _addPrefix_(name);
181  if (_prm_->_classMap_.exists(real_name) || _prm_->_interfaceMap_.exists(real_name)) {
182  GUM_ERROR(DuplicateElement, "'" << real_name << "' is already used.")
183  }
184  PRMInterface< GUM_SCALAR >* i = nullptr;
185  PRMInterface< GUM_SCALAR >* super = nullptr;
186 
187  if (extends != "") { super = _retrieveInterface_(extends); }
188 
189  if (super != nullptr) {
190  i = new PRMInterface< GUM_SCALAR >(real_name, *super, delayInheritance);
191  } else {
192  i = new PRMInterface< GUM_SCALAR >(real_name);
193  }
194 
195  _prm_->_interfaceMap_.insert(i->name(), i);
196  _prm_->_interfaces_.insert(i);
197  _stack_.push_back(i);
198  }
std::string _addPrefix_(const std::string &str) const
Adds prefix to str iff prefix != "".
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...
std::vector< PRMObject *> _stack_
A stack used to keep track of created PRMObject.
Definition: PRMFactory.h:1029
PRM< GUM_SCALAR > * _prm_
The pointer on the PRM<GUM_SCALAR> built by this factory.
Definition: PRMFactory.h:1026
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ startSystem()

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMFactory< GUM_SCALAR >::startSystem ( const std::string &  name)
overridevirtual

Tells the factory that we started declaring a model.

Exceptions
OperationNotAllowedRaised if the given operation is illegal.

Implements gum::prm::IPRMFactory.

Definition at line 1561 of file PRMFactory_tpl.h.

1561  {
1562  if (_prm_->_systemMap_.exists(name)) {
1563  GUM_ERROR(DuplicateElement, "'" << name << "' is already used.")
1564  }
1565  PRMSystem< GUM_SCALAR >* model = new PRMSystem< GUM_SCALAR >(_addPrefix_(name));
1566  _stack_.push_back(model);
1567  _prm_->_systemMap_.insert(model->name(), model);
1568  _prm_->_systems_.insert(model);
1569  }
std::string _addPrefix_(const std::string &str) const
Adds prefix to str iff prefix != "".
std::vector< PRMObject *> _stack_
A stack used to keep track of created PRMObject.
Definition: PRMFactory.h:1029
PRM< GUM_SCALAR > * _prm_
The pointer on the PRM<GUM_SCALAR> built by this factory.
Definition: PRMFactory.h:1026
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

Member Data Documentation

◆ _agg_params_

template<typename GUM_SCALAR >
HashTable< PRMAggregate< GUM_SCALAR >*, std::vector< std::string > > gum::prm::PRMFactory< GUM_SCALAR >::_agg_params_
private

A mapping between aggregators and their parameters.

Definition at line 1032 of file PRMFactory.h.

◆ _namespaces_

template<typename GUM_SCALAR >
std::vector< List< std::string >* > gum::prm::PRMFactory< GUM_SCALAR >::_namespaces_
private

Set of all declared namespaces.

Definition at line 1023 of file PRMFactory.h.

◆ _packages_

template<typename GUM_SCALAR >
std::vector< std::string > gum::prm::PRMFactory< GUM_SCALAR >::_packages_
private

The prefix used for classes and types names. It is normally the namespace of the corresponding compilation unit.

Definition at line 1020 of file PRMFactory.h.

◆ _prm_

template<typename GUM_SCALAR >
PRM< GUM_SCALAR >* gum::prm::PRMFactory< GUM_SCALAR >::_prm_
private

The pointer on the PRM<GUM_SCALAR> built by this factory.

Definition at line 1026 of file PRMFactory.h.

◆ _stack_

template<typename GUM_SCALAR >
std::vector< PRMObject* > gum::prm::PRMFactory< GUM_SCALAR >::_stack_
private

A stack used to keep track of created PRMObject.

Definition at line 1029 of file PRMFactory.h.


The documentation for this class was generated from the following files: