aGrUM  0.14.1
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 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 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 43 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 57 of file IBayesNetFactory.h.

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

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 42 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::__states, gum::BayesNetFactory< GUM_SCALAR >::__varNameMap, gum::HashTable< Key, Val, Alloc >::exists(), GUM_ERROR, gum::HashTable< Key, Val, Alloc >::insert(), gum::DAGmodel::nodes(), gum::IBayesNetFactory::NONE, gum::IBayesNetFactory::resetVerbose(), and gum::BayesNet< GUM_SCALAR >::variable().

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

References gum::BayesNetFactory< GUM_SCALAR >::__bn, gum::BayesNetFactory< GUM_SCALAR >::__states, GUM_ERROR, gum::IBayesNetFactory::NONE, and gum::BayesNetFactory< GUM_SCALAR >::state().

63  :
64  __parents(0),
65  __impl(0), __bn(0) {
66  GUM_CONS_CPY(BayesNetFactory);
67 
68  if (source.state() != factory_state::NONE) {
69  GUM_ERROR(OperationNotAllowed, "Illegal state to proceed make a copy.");
70  } else {
71  __states = source.__states;
72  __bn = new BayesNet< GUM_SCALAR >(*(source.__bn));
73  }
74  }
BayesNetFactory(BayesNet< GUM_SCALAR > *bn)
Use this constructor if you want to use an already created BayesNet.
BayesNet< GUM_SCALAR > * __bn
The constructed BayesNet.
std::vector< factory_state > __states
State stack.
Instantiation * __parents
Used when a factorized CPT is built.
MultiDimImplementation< GUM_SCALAR > * __impl
Implementation of variable between two startVariableDeclaration/endVariableDeclaration calls...
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
+ 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 78 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::__impl, and gum::BayesNetFactory< GUM_SCALAR >::__parents.

78  {
79  GUM_DESTRUCTOR(BayesNetFactory);
80 
81  if (__parents != 0) delete __parents;
82 
83  if (__impl != 0) {
84  //@todo better than throwing an exception from inside a destructor but
85  // still ...
86  std::cerr
87  << "[BN factory] Implementation defined for a variable but not used. "
88  "You should call endVariableDeclaration() before "
89  "deleting me."
90  << std::endl;
91  exit(1000);
92  }
93  }
BayesNetFactory(BayesNet< GUM_SCALAR > *bn)
Use this constructor if you want to use an already created BayesNet.
Instantiation * __parents
Used when a factorized CPT is built.
MultiDimImplementation< GUM_SCALAR > * __impl
Implementation of variable between two startVariableDeclaration/endVariableDeclaration calls...

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 862 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::__stringBag, and GUM_ERROR.

Referenced by gum::BayesNetFactory< GUM_SCALAR >::addModality().

862  {
863  for (size_t i = 2; i < __stringBag.size(); ++i) {
864  if (mod == __stringBag[i]) {
865  GUM_ERROR(DuplicateElement, "Label already used: " << mod);
866  }
867  }
868  }
std::vector< std::string > __stringBag
Just to keep track of strings between two start/end calls.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
+ Here is the caller 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 847 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::__bn, gum::BayesNetFactory< GUM_SCALAR >::__checkVariableName(), gum::BayesNetFactory< GUM_SCALAR >::__varNameMap, gum::DiscreteVariable::domainSize(), GUM_ERROR, and gum::DiscreteVariable::label().

Referenced by gum::BayesNetFactory< GUM_SCALAR >::setParentModality().

848  {
849  __checkVariableName(name);
850  const DiscreteVariable& var = __bn->variable(__varNameMap[name]);
851 
852  for (Idx i = 0; i < var.domainSize(); ++i) {
853  if (mod == var.label(i)) { return i; }
854  }
855 
856  GUM_ERROR(NotFound, mod);
857  }
HashTable< std::string, NodeId > __varNameMap
Mapping between a declared variable&#39;s name and it&#39;s node id.
BayesNet< GUM_SCALAR > * __bn
The constructed BayesNet.
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:52
+ Here is the call graph for this function:
+ Here is the caller 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 840 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::__varNameMap, gum::HashTable< Key, Val, Alloc >::exists(), and GUM_ERROR.

Referenced by gum::BayesNetFactory< GUM_SCALAR >::__checkVariableModality(), gum::BayesNetFactory< GUM_SCALAR >::__setCPTAndParents(), gum::BayesNetFactory< GUM_SCALAR >::addParent(), gum::BayesNetFactory< GUM_SCALAR >::setParentModality(), gum::BayesNetFactory< GUM_SCALAR >::setVariable(), gum::BayesNetFactory< GUM_SCALAR >::setVariableCPT(), gum::BayesNetFactory< GUM_SCALAR >::startFactorizedProbabilityDeclaration(), gum::BayesNetFactory< GUM_SCALAR >::startParentsDeclaration(), and gum::BayesNetFactory< GUM_SCALAR >::startRawProbabilityDeclaration().

840  {
841  if (!__varNameMap.exists(name)) { GUM_ERROR(NotFound, name); }
842  }
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
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:52
+ Here is the call graph for this function:
+ Here is the caller 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 419 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::__bn, gum::BayesNetFactory< GUM_SCALAR >::__increment(), gum::BayesNetFactory< GUM_SCALAR >::__stringBag, gum::BayesNetFactory< GUM_SCALAR >::__varNameMap, gum::Instantiation::chgVal(), gum::List< Val, Alloc >::pushBack(), gum::MultiDimDecorator< GUM_SCALAR >::set(), and gum::List< Val, Alloc >::size().

Referenced by gum::BayesNetFactory< GUM_SCALAR >::rawConditionalTable().

421  {
422  const Potential< GUM_SCALAR >& table = __bn->cpt(__varNameMap[__stringBag[0]]);
423  Instantiation cptInst(table);
424 
425  List< const DiscreteVariable* > varList;
426 
427  for (size_t i = 0; i < variables.size(); ++i) {
428  varList.pushBack(&(__bn->variable(__varNameMap[variables[i]])));
429  }
430 
431  // varList.pushFront(&(__bn->variable(__varNameMap[__stringBag[0]])));
432 
433  Idx nbrVar = varList.size();
434 
435  std::vector< Idx > modCounter;
436 
437  // initializing the array
438  for (NodeId i = 0; i < nbrVar; i++) {
439  modCounter.push_back(Idx(0));
440  }
441 
442  Idx j = 0;
443 
444  do {
445  for (NodeId i = 0; i < nbrVar; i++) {
446  cptInst.chgVal(*(varList[i]), modCounter[i]);
447  }
448 
449  if (j < rawTable.size()) {
450  table.set(cptInst, (GUM_SCALAR)rawTable[j]);
451  } else {
452  table.set(cptInst, (GUM_SCALAR)0);
453  }
454 
455  j++;
456  } while (__increment(modCounter, varList));
457  }
HashTable< std::string, NodeId > __varNameMap
Mapping between a declared variable&#39;s name and it&#39;s node id.
BayesNet< GUM_SCALAR > * __bn
The constructed BayesNet.
std::vector< std::string > __stringBag
Just to keep track of strings between two start/end calls.
Size Idx
Type for indexes.
Definition: types.h:50
bool __increment(std::vector< gum::Idx > &modCounter, List< const DiscreteVariable * > &varList)
Increment a modality counter for the __fillProbaWithValuesTable method.
Size NodeId
Type for node ids.
Definition: graphElements.h:97
+ Here is the call graph for this function:
+ Here is the caller 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 470 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::__bn, gum::BayesNetFactory< GUM_SCALAR >::__stringBag, gum::BayesNetFactory< GUM_SCALAR >::__varNameMap, gum::Instantiation::end(), gum::Instantiation::incNotVar(), gum::Instantiation::incVar(), gum::MultiDimDecorator< GUM_SCALAR >::set(), gum::Instantiation::setFirstNotVar(), gum::Instantiation::setFirstVar(), gum::Instantiation::unsetEnd(), and gum::MultiDimDecorator< GUM_SCALAR >::variable().

471  {
472  const Potential< GUM_SCALAR >& table = __bn->cpt(__varNameMap[__stringBag[0]]);
473 
474  Instantiation cptInst(table);
475 
476  // the main loop is on the first variables. The others are in the right
477  // order.
478  const DiscreteVariable& first = table.variable(0);
479  Idx j = 0;
480 
481  for (cptInst.setFirstVar(first); !cptInst.end(); cptInst.incVar(first)) {
482  for (cptInst.setFirstNotVar(first); !cptInst.end(); cptInst.incNotVar(first))
483  table.set(cptInst,
484  (j < rawTable.size()) ? (GUM_SCALAR)rawTable[j++]
485  : (GUM_SCALAR)0);
486 
487  cptInst.unsetEnd();
488  }
489  }
HashTable< std::string, NodeId > __varNameMap
Mapping between a declared variable&#39;s name and it&#39;s node id.
BayesNet< GUM_SCALAR > * __bn
The constructed BayesNet.
std::vector< std::string > __stringBag
Just to keep track of strings between two start/end calls.
+ 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 789 of file BayesNetFactory_tpl.h.

References gum::IBayesNetFactory::FACT_CPT, gum::IBayesNetFactory::FACT_ENTRY, GUM_ERROR, gum::IBayesNetFactory::NETWORK, gum::IBayesNetFactory::NONE, gum::IBayesNetFactory::PARENTS, gum::IBayesNetFactory::RAW_CPT, gum::BayesNetFactory< GUM_SCALAR >::state(), and gum::IBayesNetFactory::VARIABLE.

Referenced by gum::BayesNetFactory< GUM_SCALAR >::addModality(), gum::BayesNetFactory< GUM_SCALAR >::addParent(), gum::BayesNetFactory< GUM_SCALAR >::endFactorizedEntry(), gum::BayesNetFactory< GUM_SCALAR >::endFactorizedProbabilityDeclaration(), gum::BayesNetFactory< GUM_SCALAR >::endNetworkDeclaration(), gum::BayesNetFactory< GUM_SCALAR >::endParentsDeclaration(), gum::BayesNetFactory< GUM_SCALAR >::endRawProbabilityDeclaration(), gum::BayesNetFactory< GUM_SCALAR >::endVariableDeclaration(), gum::BayesNetFactory< GUM_SCALAR >::rawConditionalTable(), gum::BayesNetFactory< GUM_SCALAR >::setParentModality(), gum::BayesNetFactory< GUM_SCALAR >::setVariable(), gum::BayesNetFactory< GUM_SCALAR >::setVariableCPT(), gum::BayesNetFactory< GUM_SCALAR >::setVariableCPTImplementation(), gum::BayesNetFactory< GUM_SCALAR >::setVariableValues(), gum::BayesNetFactory< GUM_SCALAR >::setVariableValuesUnchecked(), gum::BayesNetFactory< GUM_SCALAR >::startFactorizedEntry(), gum::BayesNetFactory< GUM_SCALAR >::startFactorizedProbabilityDeclaration(), gum::BayesNetFactory< GUM_SCALAR >::startNetworkDeclaration(), gum::BayesNetFactory< GUM_SCALAR >::startParentsDeclaration(), gum::BayesNetFactory< GUM_SCALAR >::startRawProbabilityDeclaration(), gum::BayesNetFactory< GUM_SCALAR >::startVariableDeclaration(), gum::BayesNetFactory< GUM_SCALAR >::variableDescription(), and gum::BayesNetFactory< GUM_SCALAR >::variableName().

789  {
790  std::string msg = "Illegal state call (";
791  msg += s;
792  msg += ") in state ";
793 
794  switch (state()) {
795  case factory_state::NONE: {
796  msg += "NONE";
797  break;
798  }
799 
800  case factory_state::NETWORK: {
801  msg += "NETWORK";
802  break;
803  }
804 
806  msg += "VARIABLE";
807  break;
808  }
809 
810  case factory_state::PARENTS: {
811  msg += "PARENTS";
812  break;
813  }
814 
815  case factory_state::RAW_CPT: {
816  msg += "RAW_CPT";
817  break;
818  }
819 
821  msg += "FACT_CPT";
822  break;
823  }
824 
826  msg += "FACT_ENTRY";
827  break;
828  }
829 
830  default: { msg += "Unknown state"; }
831  }
832 
833  GUM_ERROR(OperationNotAllowed, msg);
834  }
factory_state state() const final
Returns the current state of the factory.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
+ Here is the call graph for this function:
+ Here is the caller 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 492 of file BayesNetFactory_tpl.h.

References gum::List< Val, Alloc >::size().

Referenced by gum::BayesNetFactory< GUM_SCALAR >::__fillProbaWithValuesTable().

494  {
495  bool last = true;
496 
497  for (NodeId j = 0; j < modCounter.size(); j++) {
498  last = (modCounter[j] == (varList[j]->domainSize() - 1)) && last;
499 
500  if (!last) break;
501  }
502 
503  if (last) { return false; }
504 
505  bool add = false;
506 
507  NodeId i = NodeId(varList.size() - 1);
508 
509  do {
510  if (modCounter[i] == (varList[i]->domainSize() - 1)) {
511  modCounter[i] = 0;
512  add = true;
513  } else {
514  modCounter[i] += 1;
515  add = false;
516  }
517 
518  i--;
519  } while (add);
520 
521  return true;
522  }
Size NodeId
Type for node ids.
Definition: graphElements.h:97
+ Here is the call graph for this function:
+ Here is the caller 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 891 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::__bar_flag, gum::BayesNetFactory< GUM_SCALAR >::__foo_flag, and gum::BayesNetFactory< GUM_SCALAR >::__stringBag.

Referenced by gum::BayesNetFactory< GUM_SCALAR >::endFactorizedProbabilityDeclaration(), gum::BayesNetFactory< GUM_SCALAR >::endParentsDeclaration(), gum::BayesNetFactory< GUM_SCALAR >::endRawProbabilityDeclaration(), and gum::BayesNetFactory< GUM_SCALAR >::endVariableDeclaration().

891  {
892  __foo_flag = false;
893  __bar_flag = false;
894  __stringBag.clear();
895  }
std::vector< std::string > __stringBag
Just to keep track of strings between two start/end calls.
bool __foo_flag
Depending on the context this flag is used for some VERY important reasons.
bool __bar_flag
Depending on the context this flag is used for some VERY important reasons.
+ Here is the caller 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 873 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::__bn, gum::BayesNetFactory< GUM_SCALAR >::__checkVariableName(), gum::BayesNetFactory< GUM_SCALAR >::__varNameMap, gum::Variable::name(), and gum::MultiDimDecorator< GUM_SCALAR >::variablesSequence().

Referenced by gum::BayesNetFactory< GUM_SCALAR >::setVariableCPT().

874  {
875  NodeId varId = __varNameMap[var.name()];
876  __bn->_dag.eraseParents(varId);
877 
878  for (auto v : table->variablesSequence()) {
879  if (v != (&var)) {
880  __checkVariableName(v->name());
881  __bn->_dag.addArc(__varNameMap[v->name()], varId);
882  }
883  }
884 
885  // CPT are created when a variable is added.
886  __bn->_unsafeChangePotential(varId, table);
887  }
HashTable< std::string, NodeId > __varNameMap
Mapping between a declared variable&#39;s name and it&#39;s node id.
BayesNet< GUM_SCALAR > * __bn
The constructed BayesNet.
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:
+ Here is the caller 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 variable.

Implements gum::IBayesNetFactory.

Definition at line 225 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::__checkModalityInBag(), gum::BayesNetFactory< GUM_SCALAR >::__illegalStateError(), gum::BayesNetFactory< GUM_SCALAR >::__stringBag, gum::BayesNetFactory< GUM_SCALAR >::state(), and gum::IBayesNetFactory::VARIABLE.

Referenced by gum::prm::PRMSystem< double >::__groundAgg().

225  {
226  if (state() != factory_state::VARIABLE) {
227  __illegalStateError("addModality");
228  } else {
229  __checkModalityInBag(name);
230  __stringBag.push_back(name);
231  }
232  }
factory_state state() const final
Returns the current state of the factory.
void __illegalStateError(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
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.
+ Here is the call graph for this function:
+ Here is the caller 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 160 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::__bn.

Referenced by gum::prm::PRMSystem< double >::groundedBN().

161  {
162  __bn->setProperty(propName, propValue);
163  }
BayesNet< GUM_SCALAR > * __bn
The constructed BayesNet.
+ Here is the caller 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 343 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::__checkVariableName(), gum::BayesNetFactory< GUM_SCALAR >::__illegalStateError(), gum::BayesNetFactory< GUM_SCALAR >::__stringBag, gum::IBayesNetFactory::PARENTS, and gum::BayesNetFactory< GUM_SCALAR >::state().

Referenced by gum::prm::PRMSystem< double >::__groundRef().

343  {
344  if (state() != factory_state::PARENTS) {
345  __illegalStateError("addParent");
346  } else {
347  __checkVariableName(var);
348  __stringBag.push_back(var);
349  }
350  }
factory_state state() const final
Returns the current state of the factory.
void __illegalStateError(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
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.
+ Here is the call graph for this function:
+ Here is the caller 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 97 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::__bn.

97  {
98  return __bn;
99  }
BayesNet< GUM_SCALAR > * __bn
The constructed BayesNet.

◆ 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 139 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::__bn.

139  {
140  // (from PH) nowhere in the code, I see a check type if (__bn) __bn->. I
141  // assume
142  // __bn is forced not to be nullptr ...
143  return __bn->cpt(n).domainSize();
144  }
BayesNet< GUM_SCALAR > * __bn
The constructed BayesNet.

◆ 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 567 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::__illegalStateError(), gum::BayesNetFactory< GUM_SCALAR >::__parents, gum::BayesNetFactory< GUM_SCALAR >::__states, gum::IBayesNetFactory::FACT_ENTRY, and gum::BayesNetFactory< GUM_SCALAR >::state().

567  {
568  if (state() != factory_state::FACT_ENTRY) {
569  __illegalStateError("endFactorizedEntry");
570  } else {
571  delete __parents;
572  __parents = 0;
573  __states.pop_back();
574  }
575  }
factory_state state() const final
Returns the current state of the factory.
void __illegalStateError(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
std::vector< factory_state > __states
State stack.
Instantiation * __parents
Used when a factorized CPT is built.
+ 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 694 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::__illegalStateError(), gum::BayesNetFactory< GUM_SCALAR >::__resetParts(), gum::BayesNetFactory< GUM_SCALAR >::__states, gum::IBayesNetFactory::FACT_CPT, and gum::BayesNetFactory< GUM_SCALAR >::state().

694  {
695  if (state() != factory_state::FACT_CPT) {
696  __illegalStateError("endFactorizedProbabilityDeclaration");
697  } else {
698  __resetParts();
699  __states.pop_back();
700  }
701  }
factory_state state() const final
Returns the current state of the factory.
void __illegalStateError(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
std::vector< factory_state > __states
State stack.
void __resetParts()
Reset the different parts used to constructed the BayesNet.
+ 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 167 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::__illegalStateError(), gum::BayesNetFactory< GUM_SCALAR >::__states, gum::IBayesNetFactory::NETWORK, gum::BayesNetFactory< GUM_SCALAR >::state(), and VERBOSITY.

Referenced by gum::prm::PRMSystem< double >::groundedBN().

167  {
168  if (state() != factory_state::NETWORK) {
169  __illegalStateError("endNetworkDeclaration");
170  } else {
171  __states.pop_back();
172  }
173 
174  VERBOSITY("network OK");
175  }
#define VERBOSITY(x)
factory_state state() const final
Returns the current state of the factory.
void __illegalStateError(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
std::vector< factory_state > __states
State stack.
+ Here is the call graph for this function:
+ Here is the caller 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 357 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::__bn, gum::BayesNetFactory< GUM_SCALAR >::__illegalStateError(), gum::BayesNetFactory< GUM_SCALAR >::__resetParts(), gum::BayesNetFactory< GUM_SCALAR >::__states, gum::BayesNetFactory< GUM_SCALAR >::__stringBag, gum::BayesNetFactory< GUM_SCALAR >::__varNameMap, gum::IBayesNetFactory::PARENTS, gum::BayesNetFactory< GUM_SCALAR >::state(), and VERBOSITY.

Referenced by gum::prm::PRMSystem< double >::__groundRef().

357  {
358  if (state() != factory_state::PARENTS) {
359  __illegalStateError("endParentsDeclaration");
360  } else {
362 
363  // PLEASE NOTE THAT THE ORDER IS INVERSE
364 
365  for (size_t i = __stringBag.size() - 1; i > 0; --i) {
366  __bn->addArc(__varNameMap[__stringBag[i]], id);
367  VERBOSITY(" adding parent " << __stringBag[i] << " for "
368  << __stringBag[0]);
369  }
370 
371  __resetParts();
372 
373  __states.pop_back();
374  }
375 
376  VERBOSITY("end of parents for " << __stringBag[0]);
377  }
#define VERBOSITY(x)
factory_state state() const final
Returns the current state of the factory.
void __illegalStateError(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
HashTable< std::string, NodeId > __varNameMap
Mapping between a declared variable&#39;s name and it&#39;s node id.
BayesNet< GUM_SCALAR > * __bn
The constructed BayesNet.
std::vector< factory_state > __states
State stack.
std::vector< std::string > __stringBag
Just to keep track of strings between two start/end calls.
Size NodeId
Type for node ids.
Definition: graphElements.h:97
void __resetParts()
Reset the different parts used to constructed the BayesNet.
+ Here is the call graph for this function:
+ Here is the caller 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 527 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::__illegalStateError(), gum::BayesNetFactory< GUM_SCALAR >::__resetParts(), gum::BayesNetFactory< GUM_SCALAR >::__states, gum::BayesNetFactory< GUM_SCALAR >::__stringBag, gum::IBayesNetFactory::RAW_CPT, gum::BayesNetFactory< GUM_SCALAR >::state(), and VERBOSITY.

527  {
528  if (state() != factory_state::RAW_CPT) {
529  __illegalStateError("endRawProbabilityDeclaration");
530  } else {
531  __resetParts();
532  __states.pop_back();
533  }
534 
535  VERBOSITY(" cpt ending for " << __stringBag[0]);
536  }
#define VERBOSITY(x)
factory_state state() const final
Returns the current state of the factory.
void __illegalStateError(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
std::vector< factory_state > __states
State stack.
std::vector< std::string > __stringBag
Just to keep track of strings between two start/end calls.
void __resetParts()
Reset the different parts used to constructed the BayesNet.
+ 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 267 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::__bar_flag, gum::BayesNetFactory< GUM_SCALAR >::__bn, gum::BayesNetFactory< GUM_SCALAR >::__foo_flag, gum::BayesNetFactory< GUM_SCALAR >::__illegalStateError(), gum::BayesNetFactory< GUM_SCALAR >::__impl, gum::BayesNetFactory< GUM_SCALAR >::__resetParts(), gum::BayesNetFactory< GUM_SCALAR >::__states, gum::BayesNetFactory< GUM_SCALAR >::__stringBag, gum::BayesNetFactory< GUM_SCALAR >::__varNameMap, gum::LabelizedVariable::addLabel(), GUM_ERROR, gum::HashTable< Key, Val, Alloc >::insert(), gum::Variable::name(), gum::BayesNetFactory< GUM_SCALAR >::state(), gum::IBayesNetFactory::VARIABLE, and VERBOSITY.

Referenced by gum::prm::PRMSystem< double >::__groundAgg().

267  {
268  if (state() != factory_state::VARIABLE) {
269  __illegalStateError("endVariableDeclaration");
270  } else if (__foo_flag && (__stringBag.size() > 3)) {
271  LabelizedVariable* var = new LabelizedVariable(
272  __stringBag[0], (__bar_flag) ? __stringBag[1] : "", 0);
273 
274  for (size_t i = 2; i < __stringBag.size(); ++i) {
275  var->addLabel(__stringBag[i]);
276  }
277 
278  if (__impl != 0) {
279  __varNameMap.insert(var->name(), __bn->add(*var, __impl));
280  __impl = 0;
281  } else {
282  __varNameMap.insert(var->name(), __bn->add(*var));
283  }
284 
285  NodeId retVal = __varNameMap[var->name()];
286 
287  delete var;
288 
289  __resetParts();
290  __states.pop_back();
291 
292  VERBOSITY(" variable " << var->name() << " OK");
293  return retVal;
294  } else {
295  std::stringstream msg;
296  msg << "Not enough modalities (";
297 
298  if (__stringBag.size() > 2) {
299  msg << __stringBag.size() - 2;
300  } else {
301  msg << 0;
302  }
303 
304  msg << ") declared for variable ";
305 
306  if (__foo_flag) {
307  msg << __stringBag[0];
308  } else {
309  msg << "unknown";
310  }
311 
312  __resetParts();
313 
314  __states.pop_back();
315  GUM_ERROR(OperationNotAllowed, msg.str());
316  }
317 
318  // For noisy compilers
319  return 0;
320  }
#define VERBOSITY(x)
factory_state state() const final
Returns the current state of the factory.
void __illegalStateError(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
HashTable< std::string, NodeId > __varNameMap
Mapping between a declared variable&#39;s name and it&#39;s node id.
BayesNet< GUM_SCALAR > * __bn
The constructed BayesNet.
std::vector< factory_state > __states
State stack.
std::vector< std::string > __stringBag
Just to keep track of strings between two start/end calls.
bool __foo_flag
Depending on the context this flag is used for some VERY important reasons.
bool __bar_flag
Depending on the context this flag is used for some VERY important reasons.
MultiDimImplementation< GUM_SCALAR > * __impl
Implementation of variable between two startVariableDeclaration/endVariableDeclaration calls...
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
Size NodeId
Type for node ids.
Definition: graphElements.h:97
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
void __resetParts()
Reset the different parts used to constructed the BayesNet.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isVerbose()

bool gum::IBayesNetFactory::isVerbose ( )
inlineinherited

Definition at line 79 of file IBayesNetFactory.h.

References gum::IBayesNetFactory::__verbose, gum::IBayesNetFactory::addModality(), gum::IBayesNetFactory::addNetworkProperty(), gum::IBayesNetFactory::addParent(), gum::IBayesNetFactory::cptDomainSize(), gum::IBayesNetFactory::endFactorizedEntry(), gum::IBayesNetFactory::endFactorizedProbabilityDeclaration(), gum::IBayesNetFactory::endNetworkDeclaration(), gum::IBayesNetFactory::endParentsDeclaration(), gum::IBayesNetFactory::endRawProbabilityDeclaration(), gum::IBayesNetFactory::endVariableDeclaration(), gum::IBayesNetFactory::rawConditionalTable(), gum::IBayesNetFactory::setParentModality(), gum::IBayesNetFactory::setVariable(), gum::IBayesNetFactory::setVariableCPT(), gum::IBayesNetFactory::setVariableCPTImplementation(), gum::IBayesNetFactory::setVariableValues(), gum::IBayesNetFactory::setVariableValuesUnchecked(), gum::IBayesNetFactory::startFactorizedEntry(), gum::IBayesNetFactory::startFactorizedProbabilityDeclaration(), gum::IBayesNetFactory::startNetworkDeclaration(), gum::IBayesNetFactory::startParentsDeclaration(), gum::IBayesNetFactory::startRawProbabilityDeclaration(), gum::IBayesNetFactory::startVariableDeclaration(), gum::IBayesNetFactory::state(), gum::IBayesNetFactory::variableDescription(), gum::IBayesNetFactory::variableId(), gum::IBayesNetFactory::variableName(), and gum::IBayesNetFactory::varInBN().

79 { return __verbose; };
+ Here is the call graph for this function:

◆ operator=()

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

Copy operator is illegal, use only copy constructor.

Definition at line 780 of file BayesNetFactory_tpl.h.

References GUM_ERROR.

Referenced by gum::BayesNetFactory< GUM_SCALAR >::setVariableCPT().

780  {
781  GUM_ERROR(OperationNotAllowed, "Illegal!");
782  // For noisy compilers
783  return *this;
784  }
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
+ Here is the caller graph for this function:

◆ 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 408 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::__fillProbaWithValuesTable(), gum::BayesNetFactory< GUM_SCALAR >::__illegalStateError(), gum::IBayesNetFactory::RAW_CPT, and gum::BayesNetFactory< GUM_SCALAR >::state().

410  {
411  if (state() != factory_state::RAW_CPT) {
412  __illegalStateError("rawConditionalTable");
413  } else {
414  __fillProbaWithValuesTable(variables, rawTable);
415  }
416  }
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 460 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::__fillProbaWithValuesTable(), gum::BayesNetFactory< GUM_SCALAR >::__illegalStateError(), gum::IBayesNetFactory::RAW_CPT, and gum::BayesNetFactory< GUM_SCALAR >::state().

461  {
462  if (state() != factory_state::RAW_CPT) {
463  __illegalStateError("rawConditionalTable");
464  } else {
465  __fillProbaWithValuesTable(rawTable);
466  }
467  }
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 77 of file IBayesNetFactory.h.

References gum::IBayesNetFactory::__verbose.

Referenced by gum::BayesNetFactory< GUM_SCALAR >::BayesNetFactory().

77 { __verbose = false; };
+ Here is the caller graph for this function:

◆ 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 580 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::__bn, gum::BayesNetFactory< GUM_SCALAR >::__checkVariableModality(), gum::BayesNetFactory< GUM_SCALAR >::__checkVariableName(), gum::BayesNetFactory< GUM_SCALAR >::__illegalStateError(), gum::BayesNetFactory< GUM_SCALAR >::__parents, gum::BayesNetFactory< GUM_SCALAR >::__varNameMap, gum::Instantiation::chgVal(), gum::IBayesNetFactory::FACT_ENTRY, and gum::BayesNetFactory< GUM_SCALAR >::state().

581  {
582  if (state() != factory_state::FACT_ENTRY) {
583  __illegalStateError("string");
584  } else {
585  __checkVariableName(parent);
586  Idx id = __checkVariableModality(parent, modality);
587  (*__parents) << __bn->variable(__varNameMap[parent]);
588  __parents->chgVal(__bn->variable(__varNameMap[parent]), id);
589  }
590  }
factory_state state() const final
Returns the current state of the factory.
Instantiation & chgVal(const DiscreteVariable &v, Idx newval)
Assign newval to variable v in the Instantiation.
void __illegalStateError(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
HashTable< std::string, NodeId > __varNameMap
Mapping between a declared variable&#39;s name and it&#39;s node id.
BayesNet< GUM_SCALAR > * __bn
The constructed BayesNet.
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.
Instantiation * __parents
Used when a factorized CPT is built.
void __checkVariableName(const std::string &name)
Check if a variable with the given name exists, if not raise an NotFound exception.
+ 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 716 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::__bn, gum::BayesNetFactory< GUM_SCALAR >::__checkVariableName(), gum::BayesNetFactory< GUM_SCALAR >::__illegalStateError(), gum::BayesNetFactory< GUM_SCALAR >::__varNameMap, GUM_ERROR, gum::HashTable< Key, Val, Alloc >::insert(), gum::Variable::name(), gum::IBayesNetFactory::NONE, and gum::BayesNetFactory< GUM_SCALAR >::state().

Referenced by gum::prm::PRMSystem< double >::__groundAttr().

716  {
717  if ((state() != factory_state::NONE)) {
718  __illegalStateError("setVariable");
719  } else {
720  try {
721  __checkVariableName(var.name());
722  GUM_ERROR(DuplicateElement, "Name already used: " << var.name());
723  } catch (NotFound&) {
724  // The var name is unused
725  __varNameMap.insert(var.name(), __bn->add(var));
726  }
727  }
728  }
factory_state state() const final
Returns the current state of the factory.
void __illegalStateError(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
HashTable< std::string, NodeId > __varNameMap
Mapping between a declared variable&#39;s name and it&#39;s node id.
BayesNet< GUM_SCALAR > * __bn
The constructed BayesNet.
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.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
+ Here is the call graph for this function:
+ Here is the caller 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 748 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::__bn, gum::BayesNetFactory< GUM_SCALAR >::__checkVariableName(), gum::BayesNetFactory< GUM_SCALAR >::__illegalStateError(), gum::BayesNetFactory< GUM_SCALAR >::__setCPTAndParents(), gum::BayesNetFactory< GUM_SCALAR >::__varNameMap, GUM_ERROR, gum::IBayesNetFactory::NONE, gum::BayesNetFactory< GUM_SCALAR >::operator=(), and gum::BayesNetFactory< GUM_SCALAR >::state().

Referenced by gum::prm::PRMSystem< double >::__groundPotential().

749  {
750  auto pot = dynamic_cast< Potential< GUM_SCALAR >* >(table);
751 
752  if (state() != factory_state::NONE) {
753  __illegalStateError("setVariableCPT");
754  } else {
755  __checkVariableName(varName);
756  const DiscreteVariable& var = __bn->variable(__varNameMap[varName]);
757  NodeId varId = __varNameMap[varName];
758  // If we have to change the structure of the BayesNet, then we call a sub
759  // method.
760 
761  if (redefineParents) {
762  __setCPTAndParents(var, pot);
763  } else if (pot->contains(var)) {
764  for (auto node : __bn->parents(varId)) {
765  if (!pot->contains(__bn->variable(node))) {
766  GUM_ERROR(OperationNotAllowed,
767  "The CPT is not valid in the current BayesNet.");
768  }
769  }
770 
771  // CPT are created when a variable is added.
772  __bn->_unsafeChangePotential(varId, pot);
773  }
774  }
775  }
factory_state state() const final
Returns the current state of the factory.
void __illegalStateError(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
HashTable< std::string, NodeId > __varNameMap
Mapping between a declared variable&#39;s name and it&#39;s node id.
BayesNet< GUM_SCALAR > * __bn
The constructed BayesNet.
void __checkVariableName(const std::string &name)
Check if a variable with the given name exists, if not raise an NotFound exception.
void __setCPTAndParents(const DiscreteVariable &var, Potential< GUM_SCALAR > *table)
Sub method of setVariableCPT() which redefine the BayesNet&#39;s DAG with respect to table.
Size NodeId
Type for node ids.
Definition: graphElements.h:97
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
+ Here is the call graph for this function:
+ Here is the caller 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 245 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::__illegalStateError(), gum::BayesNetFactory< GUM_SCALAR >::__impl, GUM_ERROR, gum::MultiDimImplementation< GUM_SCALAR >::nbrDim(), gum::BayesNetFactory< GUM_SCALAR >::state(), and gum::IBayesNetFactory::VARIABLE.

Referenced by gum::prm::PRMSystem< double >::__groundAgg().

246  {
247  MultiDimImplementation< GUM_SCALAR >* impl =
248  dynamic_cast< MultiDimImplementation< GUM_SCALAR >* >(adressable);
249 
250  if (state() != factory_state::VARIABLE) {
251  __illegalStateError("setVariableCPTImplementation");
252  } else {
253  if (impl == 0) {
254  GUM_ERROR(OperationNotAllowed,
255  "An implementation for this variable is already "
256  "defined.");
257  } else if (impl->nbrDim() > 0) {
258  GUM_ERROR(OperationNotAllowed, "This implementation is not empty.");
259  }
260 
261  __impl = impl;
262  }
263  }
factory_state state() const final
Returns the current state of the factory.
void __illegalStateError(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
MultiDimImplementation< GUM_SCALAR > * __impl
Implementation of variable between two startVariableDeclaration/endVariableDeclaration calls...
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
+ Here is the call graph for this function:
+ Here is the caller 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 671 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::__bn, gum::BayesNetFactory< GUM_SCALAR >::__illegalStateError(), gum::BayesNetFactory< GUM_SCALAR >::__stringBag, gum::BayesNetFactory< GUM_SCALAR >::__varNameMap, gum::DiscreteVariable::domainSize(), gum::IBayesNetFactory::FACT_ENTRY, GUM_ERROR, gum::Variable::name(), gum::BayesNetFactory< GUM_SCALAR >::setVariableValuesUnchecked(), and gum::BayesNetFactory< GUM_SCALAR >::state().

672  {
673  if (state() != factory_state::FACT_ENTRY) {
674  __illegalStateError("setVariableValues");
675  } else {
676  const DiscreteVariable& var = __bn->variable(__varNameMap[__stringBag[0]]);
677  // Checking consistency between values and var.
678 
679  if (values.size() != var.domainSize()) {
680  GUM_ERROR(OperationNotAllowed,
681  var.name()
682  << " : invalid number of modalities: found " << values.size()
683  << " while needed " << var.domainSize());
684  }
685 
687  }
688  }
factory_state state() const final
Returns the current state of the factory.
void __illegalStateError(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
HashTable< std::string, NodeId > __varNameMap
Mapping between a declared variable&#39;s name and it&#39;s node id.
BayesNet< GUM_SCALAR > * __bn
The constructed BayesNet.
std::vector< std::string > __stringBag
Just to keep track of strings between two start/end calls.
void setVariableValuesUnchecked(const std::vector< float > &values) final
Gives the values of the variable with respect to precedent parents modality.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
+ 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 620 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::__bn, gum::BayesNetFactory< GUM_SCALAR >::__illegalStateError(), gum::BayesNetFactory< GUM_SCALAR >::__parents, gum::BayesNetFactory< GUM_SCALAR >::__stringBag, gum::BayesNetFactory< GUM_SCALAR >::__varNameMap, gum::Instantiation::contains(), gum::Instantiation::domainSize(), gum::Instantiation::end(), gum::IBayesNetFactory::FACT_ENTRY, gum::Instantiation::incIn(), gum::Instantiation::incOut(), gum::Variable::name(), gum::Instantiation::setFirst(), gum::Instantiation::setFirstIn(), gum::Instantiation::setFirstOut(), gum::Instantiation::setVals(), gum::BayesNetFactory< GUM_SCALAR >::state(), and gum::Instantiation::val().

Referenced by gum::BayesNetFactory< GUM_SCALAR >::setVariableValues().

621  {
622  if (state() != factory_state::FACT_ENTRY) {
623  __illegalStateError("setVariableValues");
624  } else {
625  const DiscreteVariable& var = __bn->variable(__varNameMap[__stringBag[0]]);
626  NodeId varId = __varNameMap[__stringBag[0]];
627 
628  if (__parents->domainSize() > 0) {
629  Instantiation inst(__bn->cpt(__varNameMap[var.name()]));
630  inst.setVals(*__parents);
631  // Creating an instantiation containing all the variables not ins
632  // __parents.
633  Instantiation inst_default;
634  inst_default << var;
635 
636  for (auto node : __bn->parents(varId)) {
637  if (!__parents->contains(__bn->variable(node))) {
638  inst_default << __bn->variable(node);
639  }
640  }
641 
642  // Filling the variable's table.
643  for (inst.setFirstIn(inst_default); !inst.end();
644  inst.incIn(inst_default)) {
645  (__bn->cpt(varId))
646  .set(inst,
647  inst.val(var) < values.size() ? (GUM_SCALAR)values[inst.val(var)]
648  : (GUM_SCALAR)0);
649  }
650  } else {
651  Instantiation inst(__bn->cpt(__varNameMap[var.name()]));
652  Instantiation var_inst;
653  var_inst << var;
654 
655  for (var_inst.setFirst(); !var_inst.end(); ++var_inst) {
656  inst.setVals(var_inst);
657 
658  for (inst.setFirstOut(var_inst); !inst.end(); inst.incOut(var_inst)) {
659  (__bn->cpt(varId))
660  .set(inst,
661  inst.val(var) < values.size()
662  ? (GUM_SCALAR)values[inst.val(var)]
663  : (GUM_SCALAR)0);
664  }
665  }
666  }
667  }
668  }
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.
void __illegalStateError(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
HashTable< std::string, NodeId > __varNameMap
Mapping between a declared variable&#39;s name and it&#39;s node id.
BayesNet< GUM_SCALAR > * __bn
The constructed BayesNet.
std::vector< std::string > __stringBag
Just to keep track of strings between two start/end calls.
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.
Size NodeId
Type for node ids.
Definition: graphElements.h:97
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setVerbose()

void gum::IBayesNetFactory::setVerbose ( )
inlineinherited

Definition at line 75 of file IBayesNetFactory.h.

References gum::IBayesNetFactory::__verbose.

75 { __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 555 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::__illegalStateError(), gum::BayesNetFactory< GUM_SCALAR >::__parents, gum::BayesNetFactory< GUM_SCALAR >::__states, gum::IBayesNetFactory::FACT_CPT, gum::IBayesNetFactory::FACT_ENTRY, and gum::BayesNetFactory< GUM_SCALAR >::state().

555  {
556  if (state() != factory_state::FACT_CPT) {
557  __illegalStateError("startFactorizedEntry");
558  } else {
559  __parents = new Instantiation();
561  }
562  }
factory_state state() const final
Returns the current state of the factory.
void __illegalStateError(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
std::vector< factory_state > __states
State stack.
Instantiation * __parents
Used when a factorized CPT is built.
+ 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 540 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::__checkVariableName(), gum::BayesNetFactory< GUM_SCALAR >::__illegalStateError(), gum::BayesNetFactory< GUM_SCALAR >::__states, gum::BayesNetFactory< GUM_SCALAR >::__stringBag, gum::IBayesNetFactory::FACT_CPT, gum::IBayesNetFactory::NONE, and gum::BayesNetFactory< GUM_SCALAR >::state().

541  {
542  if (state() != factory_state::NONE) {
543  __illegalStateError("startFactorizedProbabilityDeclaration");
544  } else {
545  __checkVariableName(var);
546  std::vector< std::string >::iterator iter = __stringBag.begin();
547  __stringBag.insert(iter, var);
549  }
550  }
factory_state state() const final
Returns the current state of the factory.
void __illegalStateError(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
std::vector< factory_state > __states
State stack.
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.
+ 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 148 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::__illegalStateError(), gum::BayesNetFactory< GUM_SCALAR >::__states, gum::IBayesNetFactory::NETWORK, gum::IBayesNetFactory::NONE, gum::BayesNetFactory< GUM_SCALAR >::state(), and VERBOSITY.

Referenced by gum::prm::PRMSystem< double >::groundedBN().

148  {
149  if (state() != factory_state::NONE) {
150  __illegalStateError("startNetworkDeclaration");
151  } else {
152  __states.push_back(factory_state::NETWORK);
153  }
154 
155  VERBOSITY("starting network");
156  }
#define VERBOSITY(x)
factory_state state() const final
Returns the current state of the factory.
void __illegalStateError(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
std::vector< factory_state > __states
State stack.
+ Here is the call graph for this function:
+ Here is the caller 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 325 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::__checkVariableName(), gum::BayesNetFactory< GUM_SCALAR >::__illegalStateError(), gum::BayesNetFactory< GUM_SCALAR >::__states, gum::BayesNetFactory< GUM_SCALAR >::__stringBag, gum::IBayesNetFactory::NONE, gum::IBayesNetFactory::PARENTS, gum::BayesNetFactory< GUM_SCALAR >::state(), and VERBOSITY.

Referenced by gum::prm::PRMSystem< double >::__groundRef().

326  {
327  if (state() != factory_state::NONE) {
328  __illegalStateError("startParentsDeclaration");
329  } else {
330  __checkVariableName(var);
331  std::vector< std::string >::iterator iter = __stringBag.begin();
332  __stringBag.insert(iter, var);
333  __states.push_back(factory_state::PARENTS);
334  }
335 
336  VERBOSITY("starting parents for " << var);
337  }
#define VERBOSITY(x)
factory_state state() const final
Returns the current state of the factory.
void __illegalStateError(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
std::vector< factory_state > __states
State stack.
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.
+ Here is the call graph for this function:
+ Here is the caller 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 383 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::__checkVariableName(), gum::BayesNetFactory< GUM_SCALAR >::__illegalStateError(), gum::BayesNetFactory< GUM_SCALAR >::__states, gum::BayesNetFactory< GUM_SCALAR >::__stringBag, gum::IBayesNetFactory::NONE, gum::IBayesNetFactory::RAW_CPT, gum::BayesNetFactory< GUM_SCALAR >::state(), and VERBOSITY.

384  {
385  if (state() != factory_state::NONE) {
386  __illegalStateError("startRawProbabilityDeclaration");
387  } else {
388  __checkVariableName(var);
389  __stringBag.push_back(var);
390  __states.push_back(factory_state::RAW_CPT);
391  }
392 
393  VERBOSITY(" cpt starting for " << var);
394  }
#define VERBOSITY(x)
factory_state state() const final
Returns the current state of the factory.
void __illegalStateError(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
std::vector< factory_state > __states
State stack.
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.
+ 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 179 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::__illegalStateError(), gum::BayesNetFactory< GUM_SCALAR >::__states, gum::BayesNetFactory< GUM_SCALAR >::__stringBag, gum::IBayesNetFactory::NONE, gum::BayesNetFactory< GUM_SCALAR >::state(), gum::IBayesNetFactory::VARIABLE, and VERBOSITY.

Referenced by gum::prm::PRMSystem< double >::__groundAgg().

179  {
180  if (state() != factory_state::NONE) {
181  __illegalStateError("startVariableDeclaration");
182  } else {
184  __stringBag.push_back("name");
185  __stringBag.push_back("desc");
186  }
187 
188  VERBOSITY(" starting variable");
189  }
#define VERBOSITY(x)
factory_state state() const final
Returns the current state of the factory.
void __illegalStateError(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
std::vector< factory_state > __states
State stack.
std::vector< std::string > __stringBag
Just to keep track of strings between two start/end calls.
+ Here is the call graph for this function:
+ Here is the caller 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 110 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::__states.

Referenced by gum::BayesNetFactory< GUM_SCALAR >::__illegalStateError(), gum::BayesNetFactory< GUM_SCALAR >::addModality(), gum::BayesNetFactory< GUM_SCALAR >::addParent(), gum::BayesNetFactory< GUM_SCALAR >::BayesNetFactory(), gum::BayesNetFactory< GUM_SCALAR >::endFactorizedEntry(), gum::BayesNetFactory< GUM_SCALAR >::endFactorizedProbabilityDeclaration(), gum::BayesNetFactory< GUM_SCALAR >::endNetworkDeclaration(), gum::BayesNetFactory< GUM_SCALAR >::endParentsDeclaration(), gum::BayesNetFactory< GUM_SCALAR >::endRawProbabilityDeclaration(), gum::BayesNetFactory< GUM_SCALAR >::endVariableDeclaration(), gum::BayesNetFactory< GUM_SCALAR >::rawConditionalTable(), gum::BayesNetFactory< GUM_SCALAR >::setParentModality(), gum::BayesNetFactory< GUM_SCALAR >::setVariable(), gum::BayesNetFactory< GUM_SCALAR >::setVariableCPT(), gum::BayesNetFactory< GUM_SCALAR >::setVariableCPTImplementation(), gum::BayesNetFactory< GUM_SCALAR >::setVariableValues(), gum::BayesNetFactory< GUM_SCALAR >::setVariableValuesUnchecked(), gum::BayesNetFactory< GUM_SCALAR >::startFactorizedEntry(), gum::BayesNetFactory< GUM_SCALAR >::startFactorizedProbabilityDeclaration(), gum::BayesNetFactory< GUM_SCALAR >::startNetworkDeclaration(), gum::BayesNetFactory< GUM_SCALAR >::startParentsDeclaration(), gum::BayesNetFactory< GUM_SCALAR >::startRawProbabilityDeclaration(), gum::BayesNetFactory< GUM_SCALAR >::startVariableDeclaration(), gum::BayesNetFactory< GUM_SCALAR >::variableDescription(), and gum::BayesNetFactory< GUM_SCALAR >::variableName().

110  {
111  // This is ok because there is alway at least the state NONE in the stack.
112  return __states.back();
113  }
std::vector< factory_state > __states
State stack.
+ Here is the caller 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 129 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::__bn, GUM_ERROR, and gum::BayesNetFactory< GUM_SCALAR >::variableId().

Referenced by gum::prm::PRMSystem< double >::__groundPotential().

129  {
130  try {
131  return __bn->variable(variableId(name));
132  } catch (NotFound&) { GUM_ERROR(NotFound, name); }
133  }
NodeId variableId(const std::string &name) const final
Returns the NodeId of a variable given it&#39;s name.
BayesNet< GUM_SCALAR > * __bn
The constructed BayesNet.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
+ Here is the call graph for this function:
+ Here is the caller 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 212 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::__bar_flag, gum::BayesNetFactory< GUM_SCALAR >::__illegalStateError(), gum::BayesNetFactory< GUM_SCALAR >::__stringBag, gum::BayesNetFactory< GUM_SCALAR >::state(), and gum::IBayesNetFactory::VARIABLE.

212  {
213  if (state() != factory_state::VARIABLE) {
214  __illegalStateError("variableDescription");
215  } else {
216  __bar_flag = true;
217  __stringBag[1] = desc;
218  }
219  }
factory_state state() const final
Returns the current state of the factory.
void __illegalStateError(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
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.
+ 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 119 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::__varNameMap, and GUM_ERROR.

Referenced by gum::BayesNetFactory< GUM_SCALAR >::variable().

119  {
120  try {
121  return __varNameMap[name];
122  } catch (NotFound&) { GUM_ERROR(NotFound, name); }
123  }
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:52
+ Here is the caller 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 194 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::__foo_flag, gum::BayesNetFactory< GUM_SCALAR >::__illegalStateError(), gum::BayesNetFactory< GUM_SCALAR >::__stringBag, gum::BayesNetFactory< GUM_SCALAR >::__varNameMap, gum::HashTable< Key, Val, Alloc >::exists(), GUM_ERROR, gum::BayesNetFactory< GUM_SCALAR >::state(), gum::IBayesNetFactory::VARIABLE, and VERBOSITY.

Referenced by gum::prm::PRMSystem< double >::__groundAgg().

194  {
195  if (state() != factory_state::VARIABLE) {
196  __illegalStateError("variableName");
197  } else {
198  if (__varNameMap.exists(name)) {
199  GUM_ERROR(DuplicateElement, "Name already used: " << name);
200  }
201 
202  __foo_flag = true;
203 
204  __stringBag[0] = name;
205  VERBOSITY(" -- variable " << name);
206  }
207  }
#define VERBOSITY(x)
factory_state state() const final
Returns the current state of the factory.
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
void __illegalStateError(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
HashTable< std::string, NodeId > __varNameMap
Mapping between a declared variable&#39;s name and it&#39;s node id.
std::vector< std::string > __stringBag
Just to keep track of strings between two start/end calls.
bool __foo_flag
Depending on the context this flag is used for some VERY important reasons.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
+ Here is the call graph for this function:
+ Here is the caller 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 103 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::__bn.

103  {
104  return __bn->variable(id);
105  }
BayesNet< GUM_SCALAR > * __bn
The constructed BayesNet.

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 392 of file BayesNetFactory.h.

Referenced by gum::BayesNetFactory< GUM_SCALAR >::__resetParts(), gum::BayesNetFactory< GUM_SCALAR >::endVariableDeclaration(), and gum::BayesNetFactory< GUM_SCALAR >::variableDescription().

◆ __bn

◆ __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 388 of file BayesNetFactory.h.

Referenced by gum::BayesNetFactory< GUM_SCALAR >::__resetParts(), gum::BayesNetFactory< GUM_SCALAR >::endVariableDeclaration(), and gum::BayesNetFactory< GUM_SCALAR >::variableName().

◆ __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 402 of file BayesNetFactory.h.

Referenced by gum::BayesNetFactory< GUM_SCALAR >::endVariableDeclaration(), gum::BayesNetFactory< GUM_SCALAR >::setVariableCPTImplementation(), and gum::BayesNetFactory< GUM_SCALAR >::~BayesNetFactory().

◆ __parents

◆ __states

◆ __stringBag

◆ __varNameMap


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