aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
gum::BayesNetFactory< GUM_SCALAR > Class Template Reference

A factory class to ease BayesNet construction. More...

#include <agrum/BN/BayesNetFactory.h>

+ Inheritance diagram for gum::BayesNetFactory< GUM_SCALAR >:
+ Collaboration diagram for gum::BayesNetFactory< GUM_SCALAR >:

Public Member Functions

Constructor & destructor.
 BayesNetFactory (BayesNet< GUM_SCALAR > *bn)
 Use this constructor if you want to use an already created BayesNet. More...
 
 BayesNetFactory (const BayesNetFactory< GUM_SCALAR > &source)
 Copy constructor. More...
 
virtual ~BayesNetFactory ()
 Destructor. More...
 
Getter and setters.
BayesNet< GUM_SCALAR > * bayesNet ()
 Returns the BayesNet created by this factory. More...
 
const DiscreteVariablevarInBN (NodeId id) final
 short-cut accessor for a DiscreveVariable in the BN More...
 
factory_state state () const final
 Returns the current state of the factory. More...
 
NodeId variableId (const std::string &name) const final
 Returns the NodeId of a variable given it's name. More...
 
const DiscreteVariablevariable (const std::string &name) const
 Returns a constant reference on a variable given it's name. More...
 
Size cptDomainSize (NodeId n) const final
 Returns the domainSize of the cpt for the node n. More...
 
Network declaration methods (NONE -> NETWORK)
void startNetworkDeclaration () final
 Tells the factory that we're in a network declaration. More...
 
void variableType (const VarType &type)
 Tells the factory the current variable's type. More...
 
void addNetworkProperty (const std::string &propName, const std::string &propValue) final
 Tells the factory to add a property to the current network. More...
 
void endNetworkDeclaration () final
 Tells the factory that we're out of a network declaration. More...
 
Variable declaration methods (NONE -> VARIABLE)
void startVariableDeclaration () final
 Tells the factory that we're in a variable declaration. More...
 
void variableName (const std::string &name) final
 Tells the factory the current variable's name. More...
 
void variableDescription (const std::string &desc) final
 Tells the factory the current variable's description. More...
 
void addModality (const std::string &name) final
 Adds a modality to the current labelized variable. More...
 
void addMin (const long &min)
 Adds the min value of the current range variable. More...
 
void addMax (const long &max)
 Adds the max value of the current range variable. More...
 
void addTick (const GUM_SCALAR &tick)
 Adds a tick to the current Discretized variable. More...
 
void setVariableCPTImplementation (MultiDimAdressable *adressable) final
 Defines the implementation to use for var's Potential. More...
 
NodeId endVariableDeclaration () final
 Tells the factory that we're out of a variable declaration. More...
 
Parents declaration methods (NONE -> PARENTS)
void startParentsDeclaration (const std::string &var) final
 Tells the factory that we're declaring parents for some variable. More...
 
void addParent (const std::string &var) final
 Tells the factory for which variable we're declaring parents. More...
 
void endParentsDeclaration () final
 Tells the factory that we've finished declaring parents for some variable. More...
 
Raw Probability table declaration methods (NONE -> RAW_CPT)
void startRawProbabilityDeclaration (const std::string &var) final
 Tells the factory that we're declaring a conditional probability table for some variable. More...
 
void rawConditionalTable (const std::vector< std::string > &variables, const std::vector< float > &rawTable) final
 Fills the variable's table with the values in rawTable. More...
 
void rawConditionalTable (const std::vector< float > &rawTable) final
 Fills the variable's table with the values in rawTable. More...
 
void endRawProbabilityDeclaration () final
 Tells the factory that we finished declaring a conditional probability table. More...
 
Factorized probability table declaration methods
void startFactorizedProbabilityDeclaration (const std::string &var) final
 Tells the factory that we're starting a factorized declaration. More...
 
void startFactorizedEntry () final
 Tells the factory that we start an entry of a factorized conditional probability table. More...
 
void endFactorizedEntry () final
 Tells the factory that we end an entry of a factorized conditional probability table. More...
 
void setParentModality (const std::string &parent, const std::string &modality) final
 Tells the factory on which modality we want to instantiate one of variable's parent. More...
 
void setVariableValuesUnchecked (const std::vector< float > &values) final
 Gives the values of the variable with respect to precedent parents modality. More...
 
void setVariableValues (const std::vector< float > &values) final
 same than below with gum::OperationNotAllowed exception if value's size not OK. More...
 
void endFactorizedProbabilityDeclaration () final
 Tells the factory that we finished declaring a conditional probability table. More...
 
Delegated CPT definitions methods

(NONE, NETWORK)

void setVariable (const DiscreteVariable &var) final
 Define a variable. More...
 
void setVariableCPT (const std::string &varName, MultiDimAdressable *table, bool redefineParents) final
 Define a variable's CPT. More...
 
verbosity control
void setVerbose ()
 
void resetVerbose ()
 
bool isVerbose ()
 

Public Types

enum  factory_state : char {
  factory_state::NONE, factory_state::NETWORK, factory_state::VARIABLE, factory_state::PARENTS,
  factory_state::RAW_CPT, factory_state::FACT_CPT, factory_state::FACT_ENTRY
}
 The enumeration of states in which the factory can be in. More...
 

Detailed Description

template<typename GUM_SCALAR>
class gum::BayesNetFactory< GUM_SCALAR >

A factory class to ease BayesNet construction.

A BayesNetFactory will never create a BayesNet and works on only one BayesNet.

The only exception of this behaviour is when you create a copy of the factory, it will create a copy of it's BayesNet. This is useful if you want to create two BayesNet sharing a common base.

However be very careful because the copy will not delete it's BayesNet.

Each method will raise an OperationNotAllowed if you call it when the factory is not in a valid state for that call. The error message is "Illegal state.".

Definition at line 44 of file BayesNet.h.

Member Enumeration Documentation

◆ factory_state

enum gum::IBayesNetFactory::factory_state : char
stronginherited

The enumeration of states in which the factory can be in.

Every documentation section's name indicates from which state you can call it's methods, and in which state it places the factory.

There is an exception for the delegated CPT definition methods which do not change the state of the factory.

Enumerator
NONE 
NETWORK 
VARIABLE 
PARENTS 
RAW_CPT 
FACT_CPT 
FACT_ENTRY 

Definition at line 59 of file IBayesNetFactory.h.

59  : char
60  {
61  NONE,
62  NETWORK,
63  VARIABLE,
64  PARENTS,
65  RAW_CPT,
66  FACT_CPT,
67  FACT_ENTRY
68  };

Constructor & Destructor Documentation

◆ BayesNetFactory() [1/2]

template<typename GUM_SCALAR >
INLINE gum::BayesNetFactory< GUM_SCALAR >::BayesNetFactory ( BayesNet< GUM_SCALAR > *  bn)
explicit

Use this constructor if you want to use an already created BayesNet.

Parameters
bnA pointer over the BayesNet filled by this factory.
Exceptions
DuplicateElementRaised if two variables in bn share the same name.

Definition at line 39 of file BayesNetFactory_tpl.h.

References gum::Set< Key, Alloc >::emplace().

39  :
40  _parents_(0), _impl_(0), _bn_(bn) {
41  GUM_CONSTRUCTOR(BayesNetFactory);
42  _states_.push_back(factory_state::NONE);
43 
44  for (auto node: bn->nodes()) {
45  if (_varNameMap_.exists(bn->variable(node).name()))
46  GUM_ERROR(DuplicateElement, "Name already used: " << bn->variable(node).name())
47 
48  _varNameMap_.insert(bn->variable(node).name(), node);
49  }
50 
51  resetVerbose();
52  }
BayesNet< GUM_SCALAR > * _bn_
The constructed BayesNet.
BayesNetFactory(BayesNet< GUM_SCALAR > *bn)
Use this constructor if you want to use an already created BayesNet.
HashTable< std::string, NodeId > _varNameMap_
Mapping between a declared variable&#39;s name and it&#39;s node id.
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
MultiDimImplementation< GUM_SCALAR > * _impl_
Implementation of variable between two startVariableDeclaration/endVariableDeclaration calls...
std::vector< factory_state > _states_
State stack.
Instantiation * _parents_
Used when a factorized CPT is built.
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
+ Here is the call graph for this function:

◆ BayesNetFactory() [2/2]

template<typename GUM_SCALAR >
INLINE gum::BayesNetFactory< GUM_SCALAR >::BayesNetFactory ( const BayesNetFactory< GUM_SCALAR > &  source)

Copy constructor.

The copy will have an exact copy of the constructed BayesNet in source.

Warning
You can only copy a factory if its current state is NONE or NETWORK.
Exceptions
OperationNotAllowedRaised if the state of source is not NONE or NETWORK.

Definition at line 58 of file BayesNetFactory_tpl.h.

References gum::Set< Key, Alloc >::emplace().

58  :
59  _parents_(nullptr),
60  _impl_(nullptr), _bn_(nullptr) {
61  GUM_CONS_CPY(BayesNetFactory);
62 
63  if (source.state() != factory_state::NONE) {
64  GUM_ERROR(OperationNotAllowed, "Illegal state to proceed make a copy.")
65  } else {
66  _states_ = source._states_;
67  _bn_ = new BayesNet< GUM_SCALAR >(*(source._bn_));
68  }
69  }
BayesNet< GUM_SCALAR > * _bn_
The constructed BayesNet.
BayesNetFactory(BayesNet< GUM_SCALAR > *bn)
Use this constructor if you want to use an already created BayesNet.
MultiDimImplementation< GUM_SCALAR > * _impl_
Implementation of variable between two startVariableDeclaration/endVariableDeclaration calls...
std::vector< factory_state > _states_
State stack.
Instantiation * _parents_
Used when a factorized CPT is built.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ ~BayesNetFactory()

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

Destructor.

To prevent strange behaviour you should always destroy a BayesNetFactory when it's state equals NONE.

Exceptions
FatalErrorRaised if the state of the factory prevents it to die peacefully.

Definition at line 73 of file BayesNetFactory_tpl.h.

References gum::Set< Key, Alloc >::emplace().

73  {
74  GUM_DESTRUCTOR(BayesNetFactory);
75 
76  if (_parents_ != nullptr) delete _parents_;
77 
78  if (_impl_ != nullptr) {
79  //@todo better than throwing an exception from inside a destructor but
80  // still ...
81  std::cerr << "[BN factory] Implementation defined for a variable but not used. "
82  "You should call endVariableDeclaration() before "
83  "deleting me."
84  << std::endl;
85  exit(1);
86  }
87  }
BayesNetFactory(BayesNet< GUM_SCALAR > *bn)
Use this constructor if you want to use an already created BayesNet.
MultiDimImplementation< GUM_SCALAR > * _impl_
Implementation of variable between two startVariableDeclaration/endVariableDeclaration calls...
Instantiation * _parents_
Used when a factorized CPT is built.
+ Here is the call graph for this function:

Member Function Documentation

◆ _checkModalityInBag_()

template<typename GUM_SCALAR >
INLINE void gum::BayesNetFactory< GUM_SCALAR >::_checkModalityInBag_ ( const std::string &  mod)
private

Check if in stringBag there is no other modality with the same name.

Definition at line 897 of file BayesNetFactory_tpl.h.

References gum::Set< Key, Alloc >::emplace().

897  {
898  for (size_t i = 3; i < _stringBag_.size(); ++i) {
899  if (mod == _stringBag_[i]) { GUM_ERROR(DuplicateElement, "Label already used: " << mod) }
900  }
901  }
std::vector< std::string > _stringBag_
Just to keep track of strings between two start/end calls.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ _checkVariableModality_()

template<typename GUM_SCALAR >
INLINE Idx gum::BayesNetFactory< GUM_SCALAR >::_checkVariableModality_ ( const std::string &  name,
const std::string &  mod 
)
private

Check if var exists and if mod is one of it's modality, if not raise an NotFound exception.

Definition at line 883 of file BayesNetFactory_tpl.h.

References gum::Set< Key, Alloc >::emplace().

884  {
885  _checkVariableName_(name);
886  const DiscreteVariable& var = _bn_->variable(_varNameMap_[name]);
887 
888  for (Idx i = 0; i < var.domainSize(); ++i) {
889  if (mod == var.label(i)) { return i; }
890  }
891 
892  GUM_ERROR(NotFound, mod)
893  }
BayesNet< GUM_SCALAR > * _bn_
The constructed BayesNet.
HashTable< std::string, NodeId > _varNameMap_
Mapping between a declared variable&#39;s name and it&#39;s node id.
void _checkVariableName_(const std::string &name)
Check if a variable with the given name exists, if not raise an NotFound exception.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ _checkVariableName_()

template<typename GUM_SCALAR >
INLINE void gum::BayesNetFactory< GUM_SCALAR >::_checkVariableName_ ( const std::string &  name)
private

Check if a variable with the given name exists, if not raise an NotFound exception.

Definition at line 876 of file BayesNetFactory_tpl.h.

References gum::Set< Key, Alloc >::emplace().

876  {
877  if (!_varNameMap_.exists(name)) { GUM_ERROR(NotFound, name) }
878  }
HashTable< std::string, NodeId > _varNameMap_
Mapping between a declared variable&#39;s name and it&#39;s node id.
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ _fillProbaWithValuesTable_() [1/2]

template<typename GUM_SCALAR >
INLINE void gum::BayesNetFactory< GUM_SCALAR >::_fillProbaWithValuesTable_ ( const std::vector< std::string > &  variables,
const std::vector< float > &  rawTable 
)
private

Fill a potential from a raw CPT.

Definition at line 477 of file BayesNetFactory_tpl.h.

References gum::Set< Key, Alloc >::emplace().

479  {
480  const Potential< GUM_SCALAR >& table = _bn_->cpt(_varNameMap_[_stringBag_[0]]);
481  Instantiation cptInst(table);
482 
483  List< const DiscreteVariable* > varList;
484 
485  for (size_t i = 0; i < variables.size(); ++i) {
486  varList.pushBack(&(_bn_->variable(_varNameMap_[variables[i]])));
487  }
488 
489  // varList.pushFront(&( _bn_->variable( _varNameMap_[ _stringBag_[0]])));
490 
491  Idx nbrVar = varList.size();
492 
493  std::vector< Idx > modCounter;
494 
495  // initializing the array
496  for (NodeId i = 0; i < nbrVar; i++) {
497  modCounter.push_back(Idx(0));
498  }
499 
500  Idx j = 0;
501 
502  do {
503  for (NodeId i = 0; i < nbrVar; i++) {
504  cptInst.chgVal(*(varList[i]), modCounter[i]);
505  }
506 
507  if (j < rawTable.size()) {
508  table.set(cptInst, (GUM_SCALAR)rawTable[j]);
509  } else {
510  table.set(cptInst, (GUM_SCALAR)0);
511  }
512 
513  j++;
514  } while (_increment_(modCounter, varList));
515  }
BayesNet< GUM_SCALAR > * _bn_
The constructed BayesNet.
std::vector< std::string > _stringBag_
Just to keep track of strings between two start/end calls.
HashTable< std::string, NodeId > _varNameMap_
Mapping between a declared variable&#39;s name and it&#39;s node id.
Size Idx
Type for indexes.
Definition: types.h:52
Size NodeId
Type for node ids.
Definition: graphElements.h:97
bool _increment_(std::vector< gum::Idx > &modCounter, List< const DiscreteVariable * > &varList)
Increment a modality counter for the fillProbaWithValuesTable method.
+ Here is the call graph for this function:

◆ _fillProbaWithValuesTable_() [2/2]

template<typename GUM_SCALAR >
INLINE void gum::BayesNetFactory< GUM_SCALAR >::_fillProbaWithValuesTable_ ( const std::vector< float > &  rawTable)
private

Fill a potential from a raw CPT.(using the canonical order of vars)

Definition at line 528 of file BayesNetFactory_tpl.h.

References gum::Set< Key, Alloc >::emplace().

529  {
530  const Potential< GUM_SCALAR >& table = _bn_->cpt(_varNameMap_[_stringBag_[0]]);
531 
532  Instantiation cptInst(table);
533 
534  // the main loop is on the first variables. The others are in the right
535  // order.
536  const DiscreteVariable& first = table.variable(0);
537  Idx j = 0;
538 
539  for (cptInst.setFirstVar(first); !cptInst.end(); cptInst.incVar(first)) {
540  for (cptInst.setFirstNotVar(first); !cptInst.end(); cptInst.incNotVar(first))
541  table.set(cptInst, (j < rawTable.size()) ? (GUM_SCALAR)rawTable[j++] : (GUM_SCALAR)0);
542 
543  cptInst.unsetEnd();
544  }
545  }
BayesNet< GUM_SCALAR > * _bn_
The constructed BayesNet.
std::vector< std::string > _stringBag_
Just to keep track of strings between two start/end calls.
HashTable< std::string, NodeId > _varNameMap_
Mapping between a declared variable&#39;s name and it&#39;s node id.
+ Here is the call graph for this function:

◆ _illegalStateError_()

template<typename GUM_SCALAR >
INLINE void gum::BayesNetFactory< GUM_SCALAR >::_illegalStateError_ ( const std::string &  s)
private

Raise an OperationNotAllowed with the message "Illegal state.".

Definition at line 824 of file BayesNetFactory_tpl.h.

References gum::Set< Key, Alloc >::emplace().

824  {
825  std::string msg = "Illegal state call (";
826  msg += s;
827  msg += ") in state ";
828 
829  switch (state()) {
830  case factory_state::NONE: {
831  msg += "NONE";
832  break;
833  }
834 
835  case factory_state::NETWORK: {
836  msg += "NETWORK";
837  break;
838  }
839 
841  msg += "VARIABLE";
842  break;
843  }
844 
845  case factory_state::PARENTS: {
846  msg += "PARENTS";
847  break;
848  }
849 
850  case factory_state::RAW_CPT: {
851  msg += "RAW_CPT";
852  break;
853  }
854 
856  msg += "FACT_CPT";
857  break;
858  }
859 
861  msg += "FACT_ENTRY";
862  break;
863  }
864 
865  default: {
866  msg += "Unknown state";
867  }
868  }
869 
870  GUM_ERROR(OperationNotAllowed, msg)
871  }
factory_state state() const final
Returns the current state of the factory.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ _increment_()

template<typename GUM_SCALAR >
INLINE bool gum::BayesNetFactory< GUM_SCALAR >::_increment_ ( std::vector< gum::Idx > &  modCounter,
List< const DiscreteVariable * > &  varList 
)
private

Increment a modality counter for the fillProbaWithValuesTable method.

Definition at line 548 of file BayesNetFactory_tpl.h.

References gum::Set< Key, Alloc >::emplace().

549  {
550  bool last = true;
551 
552  for (NodeId j = 0; j < modCounter.size(); j++) {
553  last = (modCounter[j] == (varList[j]->domainSize() - 1)) && last;
554 
555  if (!last) break;
556  }
557 
558  if (last) { return false; }
559 
560  bool add = false;
561 
562  NodeId i = NodeId(varList.size() - 1);
563 
564  do {
565  if (modCounter[i] == (varList[i]->domainSize() - 1)) {
566  modCounter[i] = 0;
567  add = true;
568  } else {
569  modCounter[i] += 1;
570  add = false;
571  }
572 
573  i--;
574  } while (add);
575 
576  return true;
577  }
Size NodeId
Type for node ids.
Definition: graphElements.h:97
+ Here is the call graph for this function:

◆ _resetParts_()

template<typename GUM_SCALAR >
INLINE void gum::BayesNetFactory< GUM_SCALAR >::_resetParts_ ( )
private

Reset the different parts used to constructed the BayesNet.

Definition at line 924 of file BayesNetFactory_tpl.h.

References gum::Set< Key, Alloc >::emplace().

924  {
925  _foo_flag_ = false;
926  _bar_flag_ = false;
927  _stringBag_.clear();
928  }
std::vector< std::string > _stringBag_
Just to keep track of strings between two start/end calls.
bool _bar_flag_
Depending on the context this flag is used for some VERY important reasons.
bool _foo_flag_
Depending on the context this flag is used for some VERY important reasons.
+ Here is the call graph for this function:

◆ _setCPTAndParents_()

template<typename GUM_SCALAR >
INLINE void gum::BayesNetFactory< GUM_SCALAR >::_setCPTAndParents_ ( const DiscreteVariable var,
Potential< GUM_SCALAR > *  table 
)
private

Sub method of setVariableCPT() which redefine the BayesNet's DAG with respect to table.

Definition at line 906 of file BayesNetFactory_tpl.h.

References gum::Set< Key, Alloc >::emplace().

907  {
908  NodeId varId = _varNameMap_[var.name()];
909  _bn_->dag_.eraseParents(varId);
910 
911  for (auto v: table->variablesSequence()) {
912  if (v != (&var)) {
913  _checkVariableName_(v->name());
914  _bn_->dag_.addArc(_varNameMap_[v->name()], varId);
915  }
916  }
917 
918  // CPT are created when a variable is added.
919  _bn_->_unsafeChangePotential_(varId, table);
920  }
BayesNet< GUM_SCALAR > * _bn_
The constructed BayesNet.
HashTable< std::string, NodeId > _varNameMap_
Mapping between a declared variable&#39;s name and it&#39;s node id.
void _checkVariableName_(const std::string &name)
Check if a variable with the given name exists, if not raise an NotFound exception.
Size NodeId
Type for node ids.
Definition: graphElements.h:97
+ Here is the call graph for this function:

◆ addMax()

template<typename GUM_SCALAR >
INLINE void gum::BayesNetFactory< GUM_SCALAR >::addMax ( const long &  max)
virtual

Adds the max value of the current range variable.

Implements gum::IBayesNetFactory.

Definition at line 256 of file BayesNetFactory_tpl.h.

References gum::Set< Key, Alloc >::emplace().

256  {
257  if (state() != factory_state::VARIABLE) {
258  _illegalStateError_("addMin");
259  } else {
260  _stringBag_.push_back(std::to_string(max));
261  }
262  }
factory_state state() const final
Returns the current state of the factory.
std::vector< std::string > _stringBag_
Just to keep track of strings between two start/end calls.
std::string to_string(const Formula &f)
Definition: formula_inl.h:474
void _illegalStateError_(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
+ Here is the call graph for this function:

◆ addMin()

template<typename GUM_SCALAR >
INLINE void gum::BayesNetFactory< GUM_SCALAR >::addMin ( const long &  min)
virtual

Adds the min value of the current range variable.

Implements gum::IBayesNetFactory.

Definition at line 244 of file BayesNetFactory_tpl.h.

References gum::Set< Key, Alloc >::emplace().

244  {
245  if (state() != factory_state::VARIABLE) {
246  _illegalStateError_("addMin");
247  } else {
248  _stringBag_.push_back(std::to_string(min));
249  }
250  }
factory_state state() const final
Returns the current state of the factory.
std::vector< std::string > _stringBag_
Just to keep track of strings between two start/end calls.
std::string to_string(const Formula &f)
Definition: formula_inl.h:474
void _illegalStateError_(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
+ Here is the call graph for this function:

◆ addModality()

template<typename GUM_SCALAR >
INLINE void gum::BayesNetFactory< GUM_SCALAR >::addModality ( const std::string &  name)
finalvirtual

Adds a modality to the current labelized variable.

Implements gum::IBayesNetFactory.

Definition at line 231 of file BayesNetFactory_tpl.h.

References gum::Set< Key, Alloc >::emplace().

231  {
232  if (state() != factory_state::VARIABLE) {
233  _illegalStateError_("addModality");
234  } else {
235  _checkModalityInBag_(name);
236  _stringBag_.push_back(name);
237  }
238  }
factory_state state() const final
Returns the current state of the factory.
std::vector< std::string > _stringBag_
Just to keep track of strings between two start/end calls.
void _checkModalityInBag_(const std::string &mod)
Check if in stringBag there is no other modality with the same name.
void _illegalStateError_(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
+ Here is the call graph for this function:

◆ addNetworkProperty()

template<typename GUM_SCALAR >
INLINE void gum::BayesNetFactory< GUM_SCALAR >::addNetworkProperty ( const std::string &  propName,
const std::string &  propValue 
)
finalvirtual

Tells the factory to add a property to the current network.

Implements gum::IBayesNetFactory.

Definition at line 146 of file BayesNetFactory_tpl.h.

References gum::Set< Key, Alloc >::emplace().

147  {
148  _bn_->setProperty(propName, propValue);
149  }
BayesNet< GUM_SCALAR > * _bn_
The constructed BayesNet.
+ Here is the call graph for this function:

◆ addParent()

template<typename GUM_SCALAR >
INLINE void gum::BayesNetFactory< GUM_SCALAR >::addParent ( const std::string &  var)
finalvirtual

Tells the factory for which variable we're declaring parents.

Parameters
varThe parent's name.
Exceptions
NotFoundRaised if var does not exists.

Implements gum::IBayesNetFactory.

Definition at line 407 of file BayesNetFactory_tpl.h.

References gum::Set< Key, Alloc >::emplace().

407  {
408  if (state() != factory_state::PARENTS) {
409  _illegalStateError_("addParent");
410  } else {
411  _checkVariableName_(var);
412  _stringBag_.push_back(var);
413  }
414  }
factory_state state() const final
Returns the current state of the factory.
std::vector< std::string > _stringBag_
Just to keep track of strings between two start/end calls.
void _checkVariableName_(const std::string &name)
Check if a variable with the given name exists, if not raise an NotFound exception.
void _illegalStateError_(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
+ Here is the call graph for this function:

◆ addTick()

template<typename GUM_SCALAR >
INLINE void gum::BayesNetFactory< GUM_SCALAR >::addTick ( const GUM_SCALAR &  tick)

Adds a tick to the current Discretized variable.

Definition at line 268 of file BayesNetFactory_tpl.h.

References gum::Set< Key, Alloc >::emplace().

268  {
269  if (state() != factory_state::VARIABLE) {
270  _illegalStateError_("addTick");
271  } else {
272  _stringBag_.push_back(std::to_string(tick));
273  }
274  }
factory_state state() const final
Returns the current state of the factory.
std::vector< std::string > _stringBag_
Just to keep track of strings between two start/end calls.
std::string to_string(const Formula &f)
Definition: formula_inl.h:474
void _illegalStateError_(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
+ Here is the call graph for this function:

◆ bayesNet()

template<typename GUM_SCALAR >
INLINE BayesNet< GUM_SCALAR > * gum::BayesNetFactory< GUM_SCALAR >::bayesNet ( )

Returns the BayesNet created by this factory.

Exceptions
OperationNotAllowedRaise if the state of the factory is different than NONE.

Definition at line 91 of file BayesNetFactory_tpl.h.

References gum::Set< Key, Alloc >::emplace().

91  {
92  return _bn_;
93  }
BayesNet< GUM_SCALAR > * _bn_
The constructed BayesNet.
+ Here is the call graph for this function:

◆ cptDomainSize()

template<typename GUM_SCALAR >
INLINE Size gum::BayesNetFactory< GUM_SCALAR >::cptDomainSize ( NodeId  n) const
finalvirtual

Returns the domainSize of the cpt for the node n.

Exceptions
NotFoundraised if no such NodeId exists.

Implements gum::IBayesNetFactory.

Definition at line 130 of file BayesNetFactory_tpl.h.

References gum::Set< Key, Alloc >::emplace().

130  {
131  return _bn_->cpt(n).domainSize();
132  }
BayesNet< GUM_SCALAR > * _bn_
The constructed BayesNet.
+ Here is the call graph for this function:

◆ endFactorizedEntry()

template<typename GUM_SCALAR >
INLINE void gum::BayesNetFactory< GUM_SCALAR >::endFactorizedEntry ( )
finalvirtual

Tells the factory that we end an entry of a factorized conditional probability table.

Implements gum::IBayesNetFactory.

Definition at line 620 of file BayesNetFactory_tpl.h.

References gum::Set< Key, Alloc >::emplace().

620  {
621  if (state() != factory_state::FACT_ENTRY) {
622  _illegalStateError_("endFactorizedEntry");
623  } else {
624  delete _parents_;
625  _parents_ = 0;
626  _states_.pop_back();
627  }
628  }
factory_state state() const final
Returns the current state of the factory.
std::vector< factory_state > _states_
State stack.
Instantiation * _parents_
Used when a factorized CPT is built.
void _illegalStateError_(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
+ Here is the call graph for this function:

◆ endFactorizedProbabilityDeclaration()

template<typename GUM_SCALAR >
INLINE void gum::BayesNetFactory< GUM_SCALAR >::endFactorizedProbabilityDeclaration ( )
finalvirtual

Tells the factory that we finished declaring a conditional probability table.

Implements gum::IBayesNetFactory.

Definition at line 739 of file BayesNetFactory_tpl.h.

References gum::Set< Key, Alloc >::emplace().

739  {
740  if (state() != factory_state::FACT_CPT) {
741  _illegalStateError_("endFactorizedProbabilityDeclaration");
742  } else {
743  _resetParts_();
744  _states_.pop_back();
745  }
746  }
factory_state state() const final
Returns the current state of the factory.
void _resetParts_()
Reset the different parts used to constructed the BayesNet.
std::vector< factory_state > _states_
State stack.
void _illegalStateError_(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
+ Here is the call graph for this function:

◆ endNetworkDeclaration()

template<typename GUM_SCALAR >
INLINE void gum::BayesNetFactory< GUM_SCALAR >::endNetworkDeclaration ( )
finalvirtual

Tells the factory that we're out of a network declaration.

Implements gum::IBayesNetFactory.

Definition at line 153 of file BayesNetFactory_tpl.h.

References gum::Set< Key, Alloc >::emplace().

153  {
154  if (state() != factory_state::NETWORK) {
155  _illegalStateError_("endNetworkDeclaration");
156  } else {
157  _states_.pop_back();
158  }
159  }
factory_state state() const final
Returns the current state of the factory.
std::vector< factory_state > _states_
State stack.
void _illegalStateError_(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
+ Here is the call graph for this function:

◆ endParentsDeclaration()

template<typename GUM_SCALAR >
INLINE void gum::BayesNetFactory< GUM_SCALAR >::endParentsDeclaration ( )
finalvirtual

Tells the factory that we've finished declaring parents for some variable.

Warning
When parents exist, endParentsDeclaration creates some arcs. Due to the order used in BIF file for probability specification, these arcs are created in the inverse order of the order of the parent specifications.

Implements gum::IBayesNetFactory.

Definition at line 421 of file BayesNetFactory_tpl.h.

References gum::Set< Key, Alloc >::emplace().

421  {
422  if (state() != factory_state::PARENTS) {
423  _illegalStateError_("endParentsDeclaration");
424  } else {
426 
427  // PLEASE NOTE THAT THE ORDER IS INVERSE
428 
429  for (size_t i = _stringBag_.size() - 1; i > 0; --i) {
430  _bn_->addArc(_varNameMap_[_stringBag_[i]], id);
431  }
432 
433  _resetParts_();
434 
435  _states_.pop_back();
436  }
437  }
BayesNet< GUM_SCALAR > * _bn_
The constructed BayesNet.
factory_state state() const final
Returns the current state of the factory.
std::vector< std::string > _stringBag_
Just to keep track of strings between two start/end calls.
HashTable< std::string, NodeId > _varNameMap_
Mapping between a declared variable&#39;s name and it&#39;s node id.
void _resetParts_()
Reset the different parts used to constructed the BayesNet.
std::vector< factory_state > _states_
State stack.
void _illegalStateError_(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
Size NodeId
Type for node ids.
Definition: graphElements.h:97
+ Here is the call graph for this function:

◆ endRawProbabilityDeclaration()

template<typename GUM_SCALAR >
INLINE void gum::BayesNetFactory< GUM_SCALAR >::endRawProbabilityDeclaration ( )
finalvirtual

Tells the factory that we finished declaring a conditional probability table.

Implements gum::IBayesNetFactory.

Definition at line 582 of file BayesNetFactory_tpl.h.

References gum::Set< Key, Alloc >::emplace().

582  {
583  if (state() != factory_state::RAW_CPT) {
584  _illegalStateError_("endRawProbabilityDeclaration");
585  } else {
586  _resetParts_();
587  _states_.pop_back();
588  }
589  }
factory_state state() const final
Returns the current state of the factory.
void _resetParts_()
Reset the different parts used to constructed the BayesNet.
std::vector< factory_state > _states_
State stack.
void _illegalStateError_(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
+ Here is the call graph for this function:

◆ endVariableDeclaration()

template<typename GUM_SCALAR >
INLINE NodeId gum::BayesNetFactory< GUM_SCALAR >::endVariableDeclaration ( )
finalvirtual

Tells the factory that we're out of a variable declaration.

Returns
The Node id of the created variable.
Exceptions
gum::OperationNotAllowedRaised if the variable isn't defined (or / not enough defined).

Implements gum::IBayesNetFactory.

Definition at line 309 of file BayesNetFactory_tpl.h.

References gum::Set< Key, Alloc >::emplace().

309  {
310  if (state() != factory_state::VARIABLE) {
311  _illegalStateError_("endVariableDeclaration");
312  } else if (_foo_flag_ && (_stringBag_.size() > 4)) {
313  DiscreteVariable* var = nullptr;
314 
315  // if the current variable is a LabelizedVariable
316  if (_stringBag_[2] == "L") {
317  LabelizedVariable* l
318  = new LabelizedVariable(_stringBag_[0], (_bar_flag_) ? _stringBag_[1] : "", 0);
319 
320  for (size_t i = 3; i < _stringBag_.size(); ++i) {
321  l->addLabel(_stringBag_[i]);
322  }
323 
324  var = l;
325  // if the current variable is a RangeVariable
326  } else if (_stringBag_[2] == "R") {
327  RangeVariable* r = new RangeVariable(_stringBag_[0],
328  (_bar_flag_) ? _stringBag_[1] : "",
329  std::stol(_stringBag_[3]),
330  std::stol(_stringBag_[4]));
331 
332  var = r;
333  // if the current variable is a DiscretizedVariable
334  } else if (_stringBag_[2] == "D") {
335  DiscretizedVariable< GUM_SCALAR >* d
336  = new DiscretizedVariable< GUM_SCALAR >(_stringBag_[0],
337  (_bar_flag_) ? _stringBag_[1] : "");
338 
339  for (size_t i = 3; i < _stringBag_.size(); ++i) {
340  d->addTick(std::stof(_stringBag_[i]));
341  }
342 
343  var = d;
344  }
345 
346  if (_impl_ != 0) {
347  _varNameMap_.insert(var->name(), _bn_->add(*var, _impl_));
348  _impl_ = 0;
349  } else {
350  _varNameMap_.insert(var->name(), _bn_->add(*var));
351  }
352 
353  NodeId retVal = _varNameMap_[var->name()];
354 
355  delete var;
356 
357  _resetParts_();
358  _states_.pop_back();
359 
360  return retVal;
361  } else {
362  std::stringstream msg;
363  msg << "Not enough modalities (";
364 
365  if (_stringBag_.size() > 3) {
366  msg << _stringBag_.size() - 3;
367  } else {
368  msg << 0;
369  }
370 
371  msg << ") declared for variable ";
372 
373  if (_foo_flag_) {
374  msg << _stringBag_[0];
375  } else {
376  msg << "unknown";
377  }
378 
379  _resetParts_();
380 
381  _states_.pop_back();
382  GUM_ERROR(OperationNotAllowed, msg.str())
383  }
384 
385  // For noisy compilers
386  return 0;
387  }
BayesNet< GUM_SCALAR > * _bn_
The constructed BayesNet.
factory_state state() const final
Returns the current state of the factory.
std::vector< std::string > _stringBag_
Just to keep track of strings between two start/end calls.
HashTable< std::string, NodeId > _varNameMap_
Mapping between a declared variable&#39;s name and it&#39;s node id.
void _resetParts_()
Reset the different parts used to constructed the BayesNet.
MultiDimImplementation< GUM_SCALAR > * _impl_
Implementation of variable between two startVariableDeclaration/endVariableDeclaration calls...
std::vector< factory_state > _states_
State stack.
bool _bar_flag_
Depending on the context this flag is used for some VERY important reasons.
bool _foo_flag_
Depending on the context this flag is used for some VERY important reasons.
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
void _illegalStateError_(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
Size NodeId
Type for node ids.
Definition: graphElements.h:97
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ isVerbose()

bool gum::IBayesNetFactory::isVerbose ( )
inlineinherited

Definition at line 82 of file IBayesNetFactory.h.

References gum::IBayesNetFactory::_verbose_.

82 { return _verbose_; };

◆ operator=()

template<typename GUM_SCALAR >
BayesNetFactory< GUM_SCALAR >& gum::BayesNetFactory< GUM_SCALAR >::operator= ( const BayesNetFactory< GUM_SCALAR > &  source)
privatedelete

Copy operator is illegal, use only copy constructor.

◆ rawConditionalTable() [1/2]

template<typename GUM_SCALAR >
INLINE void gum::BayesNetFactory< GUM_SCALAR >::rawConditionalTable ( const std::vector< std::string > &  variables,
const std::vector< float > &  rawTable 
)
finalvirtual

Fills the variable's table with the values in rawTable.

Parse the parents in the same order in variables

Given a sequence [var, p_1, p_2, ...,p_n-1, p_n] of parents, modalities are parsed in the given order (if all p_i are binary):

* [0, 0, ..., 0, 0],
* [0, 0, ..., 0, 1],
* [0, 0, ..., 1, 0],
* [0, 0, ..., 1, 1],
* ...,
* [1, 1, ..., 1, 0],
* [1, 1, ..., 1, 1].
* 
Parameters
variablesthe vector giving the order of parents
rawTableThe raw table.

Implements gum::IBayesNetFactory.

Definition at line 467 of file BayesNetFactory_tpl.h.

References gum::Set< Key, Alloc >::emplace().

468  {
469  if (state() != factory_state::RAW_CPT) {
470  _illegalStateError_("rawConditionalTable");
471  } else {
472  _fillProbaWithValuesTable_(variables, rawTable);
473  }
474  }
factory_state state() const final
Returns the current state of the factory.
void _fillProbaWithValuesTable_(const std::vector< std::string > &variables, const std::vector< float > &rawTable)
Fill a potential from a raw CPT.
void _illegalStateError_(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
+ Here is the call graph for this function:

◆ rawConditionalTable() [2/2]

template<typename GUM_SCALAR >
INLINE void gum::BayesNetFactory< GUM_SCALAR >::rawConditionalTable ( const std::vector< float > &  rawTable)
finalvirtual

Fills the variable's table with the values in rawTable.

use the canonical ordering for the variables (e.g. see BIF format) ...

Parameters
rawTableThe raw table.

Implements gum::IBayesNetFactory.

Definition at line 519 of file BayesNetFactory_tpl.h.

References gum::Set< Key, Alloc >::emplace().

519  {
520  if (state() != factory_state::RAW_CPT) {
521  _illegalStateError_("rawConditionalTable");
522  } else {
523  _fillProbaWithValuesTable_(rawTable);
524  }
525  }
factory_state state() const final
Returns the current state of the factory.
void _fillProbaWithValuesTable_(const std::vector< std::string > &variables, const std::vector< float > &rawTable)
Fill a potential from a raw CPT.
void _illegalStateError_(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
+ Here is the call graph for this function:

◆ resetVerbose()

void gum::IBayesNetFactory::resetVerbose ( )
inlineinherited

Definition at line 80 of file IBayesNetFactory.h.

References gum::IBayesNetFactory::_verbose_.

80 { _verbose_ = false; };

◆ setParentModality()

template<typename GUM_SCALAR >
INLINE void gum::BayesNetFactory< GUM_SCALAR >::setParentModality ( const std::string &  parent,
const std::string &  modality 
)
finalvirtual

Tells the factory on which modality we want to instantiate one of variable's parent.

Implements gum::IBayesNetFactory.

Definition at line 633 of file BayesNetFactory_tpl.h.

References gum::Set< Key, Alloc >::emplace().

634  {
635  if (state() != factory_state::FACT_ENTRY) {
636  _illegalStateError_("string");
637  } else {
638  _checkVariableName_(parent);
639  Idx id = _checkVariableModality_(parent, modality);
640  (*_parents_) << _bn_->variable(_varNameMap_[parent]);
641  _parents_->chgVal(_bn_->variable(_varNameMap_[parent]), id);
642  }
643  }
BayesNet< GUM_SCALAR > * _bn_
The constructed BayesNet.
factory_state state() const final
Returns the current state of the factory.
HashTable< std::string, NodeId > _varNameMap_
Mapping between a declared variable&#39;s name and it&#39;s node id.
Instantiation & chgVal(const DiscreteVariable &v, Idx newval)
Assign newval to variable v in the Instantiation.
Idx _checkVariableModality_(const std::string &name, const std::string &mod)
Check if var exists and if mod is one of it&#39;s modality, if not raise an NotFound exception.
void _checkVariableName_(const std::string &name)
Check if a variable with the given name exists, if not raise an NotFound exception.
Instantiation * _parents_
Used when a factorized CPT is built.
void _illegalStateError_(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
+ Here is the call graph for this function:

◆ setVariable()

template<typename GUM_SCALAR >
INLINE void gum::BayesNetFactory< GUM_SCALAR >::setVariable ( const DiscreteVariable var)
finalvirtual

Define a variable.

You can only call this method is the factory is in the NONE or NETWORK state.

The variable is added by copy.

Parameters
varThe pointer over a DiscreteVariable used to define a new variable in the built BayesNet.
Exceptions
DuplicateElementRaised if a variable with the same name already exists.
OperationNotAllowedRaised if redefineParents == false and if table is not a valid CPT for var in the current state of the BayesNet.

Implements gum::IBayesNetFactory.

Definition at line 760 of file BayesNetFactory_tpl.h.

References gum::Set< Key, Alloc >::emplace().

760  {
761  if ((state() != factory_state::NONE)) {
762  _illegalStateError_("setVariable");
763  } else {
764  try {
765  _checkVariableName_(var.name());
766  GUM_ERROR(DuplicateElement, "Name already used: " << var.name())
767  } catch (NotFound&) {
768  // The var name is unused
769  _varNameMap_.insert(var.name(), _bn_->add(var));
770  }
771  }
772  }
BayesNet< GUM_SCALAR > * _bn_
The constructed BayesNet.
factory_state state() const final
Returns the current state of the factory.
HashTable< std::string, NodeId > _varNameMap_
Mapping between a declared variable&#39;s name and it&#39;s node id.
void _checkVariableName_(const std::string &name)
Check if a variable with the given name exists, if not raise an NotFound exception.
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
void _illegalStateError_(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ setVariableCPT()

template<typename GUM_SCALAR >
INLINE void gum::BayesNetFactory< GUM_SCALAR >::setVariableCPT ( const std::string &  varName,
MultiDimAdressable table,
bool  redefineParents 
)
finalvirtual

Define a variable's CPT.

You can only call this method if the factory is in the NONE or NETWORK state.

Be careful that table is given to the built BayesNet, so it will be deleted with it, and you should not directly access it after you call this method.

When the redefineParents flag is set to true the constructed BayesNet's DAG is changed to fit with table's definition.

Parameters
varNameThe name of the concerned variable.
tableA pointer over the CPT used for var.
redefineParentsIf true redefine var's parents to match table's variables set.
Exceptions
NotFoundRaised if no variable matches var.
OperationNotAllowedRaised if redefineParents == false and if table is not a valid CPT for var in the current state of the BayesNet.

Implements gum::IBayesNetFactory.

Definition at line 793 of file BayesNetFactory_tpl.h.

References gum::Set< Key, Alloc >::emplace().

795  {
796  auto pot = dynamic_cast< Potential< GUM_SCALAR >* >(table);
797 
798  if (state() != factory_state::NONE) {
799  _illegalStateError_("setVariableCPT");
800  } else {
801  _checkVariableName_(varName);
802  const DiscreteVariable& var = _bn_->variable(_varNameMap_[varName]);
803  NodeId varId = _varNameMap_[varName];
804  // If we have to change the structure of the BayesNet, then we call a sub
805  // method.
806 
807  if (redefineParents) {
808  _setCPTAndParents_(var, pot);
809  } else if (pot->contains(var)) {
810  for (auto node: _bn_->parents(varId)) {
811  if (!pot->contains(_bn_->variable(node))) {
812  GUM_ERROR(OperationNotAllowed, "The CPT is not valid in the current BayesNet.")
813  }
814  }
815 
816  // CPT are created when a variable is added.
817  _bn_->_unsafeChangePotential_(varId, pot);
818  }
819  }
820  }
BayesNet< GUM_SCALAR > * _bn_
The constructed BayesNet.
factory_state state() const final
Returns the current state of the factory.
HashTable< std::string, NodeId > _varNameMap_
Mapping between a declared variable&#39;s name and it&#39;s node id.
void _setCPTAndParents_(const DiscreteVariable &var, Potential< GUM_SCALAR > *table)
Sub method of setVariableCPT() which redefine the BayesNet&#39;s DAG with respect to table.
void _checkVariableName_(const std::string &name)
Check if a variable with the given name exists, if not raise an NotFound exception.
void _illegalStateError_(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
Size NodeId
Type for node ids.
Definition: graphElements.h:97
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ setVariableCPTImplementation()

template<typename GUM_SCALAR >
INLINE void gum::BayesNetFactory< GUM_SCALAR >::setVariableCPTImplementation ( MultiDimAdressable adressable)
finalvirtual

Defines the implementation to use for var's Potential.

Warning
The implementation must be empty.
The pointer is always delegated to var's Potential! No copy of it is made.
Todo:
When copy of a MultiDimImplementation is available use a copy behaviour for this method.
Exceptions
NotFoundRaised if no variable matches var.
OperationNotAllowedRaised if impl is not empty.
OperationNotAllowedIf an implementation is already defined for the current variable.

Implements gum::IBayesNetFactory.

Definition at line 288 of file BayesNetFactory_tpl.h.

References gum::Set< Key, Alloc >::emplace().

288  {
289  MultiDimImplementation< GUM_SCALAR >* impl
290  = dynamic_cast< MultiDimImplementation< GUM_SCALAR >* >(adressable);
291 
292  if (state() != factory_state::VARIABLE) {
293  _illegalStateError_("setVariableCPTImplementation");
294  } else {
295  if (impl == 0) {
296  GUM_ERROR(OperationNotAllowed,
297  "An implementation for this variable is already "
298  "defined.")
299  } else if (impl->nbrDim() > 0) {
300  GUM_ERROR(OperationNotAllowed, "This implementation is not empty.")
301  }
302 
303  _impl_ = impl;
304  }
305  }
factory_state state() const final
Returns the current state of the factory.
MultiDimImplementation< GUM_SCALAR > * _impl_
Implementation of variable between two startVariableDeclaration/endVariableDeclaration calls...
void _illegalStateError_(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ setVariableValues()

template<typename GUM_SCALAR >
INLINE void gum::BayesNetFactory< GUM_SCALAR >::setVariableValues ( const std::vector< float > &  values)
finalvirtual

same than below with gum::OperationNotAllowed exception if value's size not OK.

Implements gum::IBayesNetFactory.

Definition at line 719 of file BayesNetFactory_tpl.h.

References gum::Set< Key, Alloc >::emplace().

719  {
720  if (state() != factory_state::FACT_ENTRY) {
721  _illegalStateError_("setVariableValues");
722  } else {
723  const DiscreteVariable& var = _bn_->variable(_varNameMap_[_stringBag_[0]]);
724  // Checking consistency between values and var.
725 
726  if (values.size() != var.domainSize()) {
727  GUM_ERROR(OperationNotAllowed,
728  var.name() << " : invalid number of modalities: found " << values.size()
729  << " while needed " << var.domainSize())
730  }
731 
733  }
734  }
BayesNet< GUM_SCALAR > * _bn_
The constructed BayesNet.
factory_state state() const final
Returns the current state of the factory.
std::vector< std::string > _stringBag_
Just to keep track of strings between two start/end calls.
HashTable< std::string, NodeId > _varNameMap_
Mapping between a declared variable&#39;s name and it&#39;s node id.
void setVariableValuesUnchecked(const std::vector< float > &values) final
Gives the values of the variable with respect to precedent parents modality.
void _illegalStateError_(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ setVariableValuesUnchecked()

template<typename GUM_SCALAR >
INLINE void gum::BayesNetFactory< GUM_SCALAR >::setVariableValuesUnchecked ( const std::vector< float > &  values)
finalvirtual

Gives the values of the variable with respect to precedent parents modality.

If some parents have no modality set, then we apply values for all instantiations of that parent.

This means you can declare a default value for the table by doing

// Do stuff
factory.startVariableDeclaration();
factory.variableName("foo");
factory.endVariableDeclaration();
factory.startParentsDeclaration("foo");
// add parents
factory.endParentsDeclaration();
factory.startFactorizedProbabilityDeclaration("foo");
std::vector<double> seq;
seq.insert(0.4); // if foo true
seq.insert(O.6); // if foo false
factory.setVariableValues(seq); // fills the table with a default value
// finish your stuff
factory.endFactorizedProbabilityDeclaration();

as for rawProba, if value's size is different than the number of modalities of the current variable, we don't use the supplementary values and we fill by 0 the missing values.

Implements gum::IBayesNetFactory.

Definition at line 673 of file BayesNetFactory_tpl.h.

References gum::Set< Key, Alloc >::emplace().

673  {
674  if (state() != factory_state::FACT_ENTRY) {
675  _illegalStateError_("setVariableValues");
676  } else {
677  const DiscreteVariable& var = _bn_->variable(_varNameMap_[_stringBag_[0]]);
678  NodeId varId = _varNameMap_[_stringBag_[0]];
679 
680  if (_parents_->domainSize() > 0) {
681  Instantiation inst(_bn_->cpt(_varNameMap_[var.name()]));
682  inst.setVals(*_parents_);
683  // Creating an instantiation containing all the variables not ins
684  // _parents_.
685  Instantiation inst_default;
686  inst_default << var;
687 
688  for (auto node: _bn_->parents(varId)) {
689  if (!_parents_->contains(_bn_->variable(node))) { inst_default << _bn_->variable(node); }
690  }
691 
692  // Filling the variable's table.
693  for (inst.setFirstIn(inst_default); !inst.end(); inst.incIn(inst_default)) {
694  (_bn_->cpt(varId))
695  .set(inst,
696  inst.val(var) < values.size() ? (GUM_SCALAR)values[inst.val(var)]
697  : (GUM_SCALAR)0);
698  }
699  } else {
700  Instantiation inst(_bn_->cpt(_varNameMap_[var.name()]));
701  Instantiation var_inst;
702  var_inst << var;
703 
704  for (var_inst.setFirst(); !var_inst.end(); ++var_inst) {
705  inst.setVals(var_inst);
706 
707  for (inst.setFirstOut(var_inst); !inst.end(); inst.incOut(var_inst)) {
708  (_bn_->cpt(varId))
709  .set(inst,
710  inst.val(var) < values.size() ? (GUM_SCALAR)values[inst.val(var)]
711  : (GUM_SCALAR)0);
712  }
713  }
714  }
715  }
716  }
BayesNet< GUM_SCALAR > * _bn_
The constructed BayesNet.
Size domainSize() const final
Returns the product of the variable&#39;s domain size in the Instantiation.
factory_state state() const final
Returns the current state of the factory.
std::vector< std::string > _stringBag_
Just to keep track of strings between two start/end calls.
HashTable< std::string, NodeId > _varNameMap_
Mapping between a declared variable&#39;s name and it&#39;s node id.
bool contains(const DiscreteVariable &v) const final
Indicates whether a given variable belongs to the Instantiation.
Instantiation * _parents_
Used when a factorized CPT is built.
void _illegalStateError_(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
Size NodeId
Type for node ids.
Definition: graphElements.h:97
+ Here is the call graph for this function:

◆ setVerbose()

void gum::IBayesNetFactory::setVerbose ( )
inlineinherited

Definition at line 78 of file IBayesNetFactory.h.

References gum::IBayesNetFactory::_verbose_.

78 { _verbose_ = true; };

◆ startFactorizedEntry()

template<typename GUM_SCALAR >
INLINE void gum::BayesNetFactory< GUM_SCALAR >::startFactorizedEntry ( )
finalvirtual

Tells the factory that we start an entry of a factorized conditional probability table.

Implements gum::IBayesNetFactory.

Definition at line 608 of file BayesNetFactory_tpl.h.

References gum::Set< Key, Alloc >::emplace().

608  {
609  if (state() != factory_state::FACT_CPT) {
610  _illegalStateError_("startFactorizedEntry");
611  } else {
612  _parents_ = new Instantiation();
614  }
615  }
factory_state state() const final
Returns the current state of the factory.
std::vector< factory_state > _states_
State stack.
Instantiation * _parents_
Used when a factorized CPT is built.
void _illegalStateError_(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
+ Here is the call graph for this function:

◆ startFactorizedProbabilityDeclaration()

template<typename GUM_SCALAR >
INLINE void gum::BayesNetFactory< GUM_SCALAR >::startFactorizedProbabilityDeclaration ( const std::string &  var)
finalvirtual

Tells the factory that we're starting a factorized declaration.

Implements gum::IBayesNetFactory.

Definition at line 594 of file BayesNetFactory_tpl.h.

References gum::Set< Key, Alloc >::emplace().

594  {
595  if (state() != factory_state::NONE) {
596  _illegalStateError_("startFactorizedProbabilityDeclaration");
597  } else {
598  _checkVariableName_(var);
599  std::vector< std::string >::iterator iter = _stringBag_.begin();
600  _stringBag_.insert(iter, var);
602  }
603  }
factory_state state() const final
Returns the current state of the factory.
std::vector< std::string > _stringBag_
Just to keep track of strings between two start/end calls.
std::vector< factory_state > _states_
State stack.
void _checkVariableName_(const std::string &name)
Check if a variable with the given name exists, if not raise an NotFound exception.
void _illegalStateError_(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
+ Here is the call graph for this function:

◆ startNetworkDeclaration()

template<typename GUM_SCALAR >
INLINE void gum::BayesNetFactory< GUM_SCALAR >::startNetworkDeclaration ( )
finalvirtual

Tells the factory that we're in a network declaration.

Implements gum::IBayesNetFactory.

Definition at line 136 of file BayesNetFactory_tpl.h.

References gum::Set< Key, Alloc >::emplace().

136  {
137  if (state() != factory_state::NONE) {
138  _illegalStateError_("startNetworkDeclaration");
139  } else {
140  _states_.push_back(factory_state::NETWORK);
141  }
142  }
factory_state state() const final
Returns the current state of the factory.
std::vector< factory_state > _states_
State stack.
void _illegalStateError_(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
+ Here is the call graph for this function:

◆ startParentsDeclaration()

template<typename GUM_SCALAR >
INLINE void gum::BayesNetFactory< GUM_SCALAR >::startParentsDeclaration ( const std::string &  var)
finalvirtual

Tells the factory that we're declaring parents for some variable.

Parameters
varThe concerned variable's name.
Exceptions
NotFoundRaised if var does not exists.

Implements gum::IBayesNetFactory.

Definition at line 392 of file BayesNetFactory_tpl.h.

References gum::Set< Key, Alloc >::emplace().

392  {
393  if (state() != factory_state::NONE) {
394  _illegalStateError_("startParentsDeclaration");
395  } else {
396  _checkVariableName_(var);
397  std::vector< std::string >::iterator iter = _stringBag_.begin();
398  _stringBag_.insert(iter, var);
399  _states_.push_back(factory_state::PARENTS);
400  }
401  }
factory_state state() const final
Returns the current state of the factory.
std::vector< std::string > _stringBag_
Just to keep track of strings between two start/end calls.
std::vector< factory_state > _states_
State stack.
void _checkVariableName_(const std::string &name)
Check if a variable with the given name exists, if not raise an NotFound exception.
void _illegalStateError_(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
+ Here is the call graph for this function:

◆ startRawProbabilityDeclaration()

template<typename GUM_SCALAR >
INLINE void gum::BayesNetFactory< GUM_SCALAR >::startRawProbabilityDeclaration ( const std::string &  var)
finalvirtual

Tells the factory that we're declaring a conditional probability table for some variable.

Parameters
varThe concerned variable's name.

Implements gum::IBayesNetFactory.

Definition at line 444 of file BayesNetFactory_tpl.h.

References gum::Set< Key, Alloc >::emplace().

444  {
445  if (state() != factory_state::NONE) {
446  _illegalStateError_("startRawProbabilityDeclaration");
447  } else {
448  _checkVariableName_(var);
449  _stringBag_.push_back(var);
450  _states_.push_back(factory_state::RAW_CPT);
451  }
452  }
factory_state state() const final
Returns the current state of the factory.
std::vector< std::string > _stringBag_
Just to keep track of strings between two start/end calls.
std::vector< factory_state > _states_
State stack.
void _checkVariableName_(const std::string &name)
Check if a variable with the given name exists, if not raise an NotFound exception.
void _illegalStateError_(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
+ Here is the call graph for this function:

◆ startVariableDeclaration()

template<typename GUM_SCALAR >
INLINE void gum::BayesNetFactory< GUM_SCALAR >::startVariableDeclaration ( )
finalvirtual

Tells the factory that we're in a variable declaration.

Implements gum::IBayesNetFactory.

Definition at line 164 of file BayesNetFactory_tpl.h.

References gum::Set< Key, Alloc >::emplace().

164  {
165  if (state() != factory_state::NONE) {
166  _illegalStateError_("startVariableDeclaration");
167  } else {
169  _stringBag_.push_back("name");
170  _stringBag_.push_back("desc");
171  _stringBag_.push_back("L");
172  }
173  }
factory_state state() const final
Returns the current state of the factory.
std::vector< std::string > _stringBag_
Just to keep track of strings between two start/end calls.
std::vector< factory_state > _states_
State stack.
void _illegalStateError_(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
+ Here is the call graph for this function:

◆ state()

template<typename GUM_SCALAR >
INLINE IBayesNetFactory::factory_state gum::BayesNetFactory< GUM_SCALAR >::state ( ) const
finalvirtual

Returns the current state of the factory.

Implements gum::IBayesNetFactory.

Definition at line 102 of file BayesNetFactory_tpl.h.

References gum::Set< Key, Alloc >::emplace().

102  {
103  // This is ok because there is always at least the state NONE in the stack.
104  return _states_.back();
105  }
std::vector< factory_state > _states_
State stack.
+ Here is the call graph for this function:

◆ variable()

template<typename GUM_SCALAR >
INLINE const DiscreteVariable & gum::BayesNetFactory< GUM_SCALAR >::variable ( const std::string &  name) const

Returns a constant reference on a variable given it's name.

Exceptions
NotFoundRaised if no variable matches the name.

Definition at line 120 of file BayesNetFactory_tpl.h.

References gum::Set< Key, Alloc >::emplace().

120  {
121  try {
122  return _bn_->variable(variableId(name));
123  } catch (NotFound&) { GUM_ERROR(NotFound, name) }
124  }
BayesNet< GUM_SCALAR > * _bn_
The constructed BayesNet.
NodeId variableId(const std::string &name) const final
Returns the NodeId of a variable given it&#39;s name.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ variableDescription()

template<typename GUM_SCALAR >
INLINE void gum::BayesNetFactory< GUM_SCALAR >::variableDescription ( const std::string &  desc)
finalvirtual

Tells the factory the current variable's description.

Implements gum::IBayesNetFactory.

Definition at line 190 of file BayesNetFactory_tpl.h.

References gum::Set< Key, Alloc >::emplace().

190  {
191  if (state() != factory_state::VARIABLE) {
192  _illegalStateError_("variableDescription");
193  } else {
194  _bar_flag_ = true;
195  _stringBag_[1] = desc;
196  }
197  }
factory_state state() const final
Returns the current state of the factory.
std::vector< std::string > _stringBag_
Just to keep track of strings between two start/end calls.
bool _bar_flag_
Depending on the context this flag is used for some VERY important reasons.
void _illegalStateError_(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
+ Here is the call graph for this function:

◆ variableId()

template<typename GUM_SCALAR >
INLINE NodeId gum::BayesNetFactory< GUM_SCALAR >::variableId ( const std::string &  name) const
finalvirtual

Returns the NodeId of a variable given it's name.

Exceptions
NotFoundRaised if no variable matches the name.

Implements gum::IBayesNetFactory.

Definition at line 110 of file BayesNetFactory_tpl.h.

References gum::Set< Key, Alloc >::emplace().

110  {
111  try {
112  return _varNameMap_[name];
113  } catch (NotFound&) { GUM_ERROR(NotFound, name) }
114  }
HashTable< std::string, NodeId > _varNameMap_
Mapping between a declared variable&#39;s name and it&#39;s node id.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ variableName()

template<typename GUM_SCALAR >
INLINE void gum::BayesNetFactory< GUM_SCALAR >::variableName ( const std::string &  name)
finalvirtual

Tells the factory the current variable's name.

Exceptions
DuplicateElementRaised if a variable with the same name already exist.

Implements gum::IBayesNetFactory.

Definition at line 177 of file BayesNetFactory_tpl.h.

References gum::Set< Key, Alloc >::emplace().

177  {
178  if (state() != factory_state::VARIABLE) {
179  _illegalStateError_("variableName");
180  } else {
181  if (_varNameMap_.exists(name)) { GUM_ERROR(DuplicateElement, "Name already used: " << name) }
182 
183  _foo_flag_ = true;
184  _stringBag_[0] = name;
185  }
186  }
factory_state state() const final
Returns the current state of the factory.
std::vector< std::string > _stringBag_
Just to keep track of strings between two start/end calls.
HashTable< std::string, NodeId > _varNameMap_
Mapping between a declared variable&#39;s name and it&#39;s node id.
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
bool _foo_flag_
Depending on the context this flag is used for some VERY important reasons.
void _illegalStateError_(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ variableType()

template<typename GUM_SCALAR >
INLINE void gum::BayesNetFactory< GUM_SCALAR >::variableType ( const VarType type)
virtual

Tells the factory the current variable's type.

Implements gum::IBayesNetFactory.

Definition at line 205 of file BayesNetFactory_tpl.h.

References gum::Set< Key, Alloc >::emplace().

205  {
206  if (state() != factory_state::VARIABLE) {
207  _illegalStateError_("variableType");
208  } else {
209  switch (type) {
211  _stringBag_[2] = "D";
212  break;
213  case VarType::Range:
214  _stringBag_[2] = "R";
215  break;
216  case VarType::Continuous:
217  GUM_ERROR(OperationNotAllowed,
218  "Continuous variable (" + _stringBag_[0]
219  + ") are not supported in Bayesian networks.")
220  case VarType::Labelized:
221  _stringBag_[2] = "L";
222  break;
223  }
224  }
225  }
factory_state state() const final
Returns the current state of the factory.
std::vector< std::string > _stringBag_
Just to keep track of strings between two start/end calls.
VarType
Definition: variable.h:40
void _illegalStateError_(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ varInBN()

template<typename GUM_SCALAR >
INLINE const DiscreteVariable & gum::BayesNetFactory< GUM_SCALAR >::varInBN ( NodeId  id)
finalvirtual

short-cut accessor for a DiscreveVariable in the BN

Implements gum::IBayesNetFactory.

Definition at line 96 of file BayesNetFactory_tpl.h.

References gum::Set< Key, Alloc >::emplace().

96  {
97  return _bn_->variable(id);
98  }
BayesNet< GUM_SCALAR > * _bn_
The constructed BayesNet.
+ Here is the call graph for this function:

Member Data Documentation

◆ _bar_flag_

template<typename GUM_SCALAR >
bool gum::BayesNetFactory< GUM_SCALAR >::_bar_flag_
private

Depending on the context this flag is used for some VERY important reasons.

Definition at line 404 of file BayesNetFactory.h.

◆ _bn_

template<typename GUM_SCALAR >
BayesNet< GUM_SCALAR >* gum::BayesNetFactory< GUM_SCALAR >::_bn_
private

The constructed BayesNet.

Definition at line 422 of file BayesNetFactory.h.

◆ _foo_flag_

template<typename GUM_SCALAR >
bool gum::BayesNetFactory< GUM_SCALAR >::_foo_flag_
private

Depending on the context this flag is used for some VERY important reasons.

Definition at line 400 of file BayesNetFactory.h.

◆ _impl_

template<typename GUM_SCALAR >
MultiDimImplementation< GUM_SCALAR >* gum::BayesNetFactory< GUM_SCALAR >::_impl_
private

Implementation of variable between two startVariableDeclaration/endVariableDeclaration calls.

Definition at line 414 of file BayesNetFactory.h.

◆ _parents_

template<typename GUM_SCALAR >
Instantiation* gum::BayesNetFactory< GUM_SCALAR >::_parents_
private

Used when a factorized CPT is built.

Definition at line 410 of file BayesNetFactory.h.

◆ _states_

template<typename GUM_SCALAR >
std::vector< factory_state > gum::BayesNetFactory< GUM_SCALAR >::_states_
private

State stack.

Definition at line 419 of file BayesNetFactory.h.

◆ _stringBag_

template<typename GUM_SCALAR >
std::vector< std::string > gum::BayesNetFactory< GUM_SCALAR >::_stringBag_
private

Just to keep track of strings between two start/end calls.

Definition at line 407 of file BayesNetFactory.h.

◆ _varNameMap_

template<typename GUM_SCALAR >
HashTable< std::string, NodeId > gum::BayesNetFactory< GUM_SCALAR >::_varNameMap_
private

Mapping between a declared variable's name and it's node id.

Definition at line 425 of file BayesNetFactory.h.


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