aGrUM  0.13.2
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

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

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, bn->variable(node).name());
51 
52  __varNameMap.insert(bn->variable(node).name(), node);
53  }
54 
55  resetVerbose();
56  }
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:66

+ Here is the call graph for this function:

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 61 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().

62  :
63  __parents(0),
64  __impl(0), __bn(0) {
65  GUM_CONS_CPY(BayesNetFactory);
66 
67  if (source.state() != factory_state::NONE) {
68  GUM_ERROR(OperationNotAllowed, "Illegal state to proceed make a copy.");
69  } else {
70  __states = source.__states;
71  __bn = new BayesNet< GUM_SCALAR >(*(source.__bn));
72  }
73  }
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:66

+ Here is the call graph for this function:

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

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

77  {
78  GUM_DESTRUCTOR(BayesNetFactory);
79 
80  if (__parents != 0) delete __parents;
81 
82  if (__impl != 0) {
83  //@todo better than throwing an exception from inside a destructor but
84  // still ...
85  std::cerr
86  << "[BN factory] Implementation defined for a variable but not used. "
87  "You should call endVariableDeclaration() before "
88  "deleting me."
89  << std::endl;
90  exit(1000);
91  }
92  }
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

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

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

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

863  {
864  for (size_t i = 2; i < __stringBag.size(); ++i) {
865  if (mod == __stringBag[i]) { GUM_ERROR(DuplicateElement, mod); }
866  }
867  }
std::vector< std::string > __stringBag
Just to keep track of strings between two start/end calls.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:66

+ Here is the caller graph for this function:

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 848 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().

849  {
850  __checkVariableName(name);
851  const DiscreteVariable& var = __bn->variable(__varNameMap[name]);
852 
853  for (Idx i = 0; i < var.domainSize(); ++i) {
854  if (mod == var.label(i)) { return i; }
855  }
856 
857  GUM_ERROR(NotFound, mod);
858  }
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.
unsigned long Idx
Type for indexes.
Definition: types.h:43
#define GUM_ERROR(type, msg)
Definition: exceptions.h:66

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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 841 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().

841  {
842  if (!__varNameMap.exists(name)) { GUM_ERROR(NotFound, name); }
843  }
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:66

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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(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  }
unsigned int NodeId
Type for node ids.
Definition: graphElements.h:97
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 __increment(std::vector< gum::Idx > &modCounter, List< const DiscreteVariable * > &varList)
Increment a modality counter for the __fillProbaWithValuesTable method.
unsigned long Idx
Type for indexes.
Definition: types.h:43

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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.
unsigned long Idx
Type for indexes.
Definition: types.h:43

+ Here is the call graph for this function:

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 790 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().

790  {
791  std::string msg = "Illegal state call (";
792  msg += s;
793  msg += ") in state ";
794 
795  switch (state()) {
796  case factory_state::NONE: {
797  msg += "NONE";
798  break;
799  }
800 
801  case factory_state::NETWORK: {
802  msg += "NETWORK";
803  break;
804  }
805 
807  msg += "VARIABLE";
808  break;
809  }
810 
811  case factory_state::PARENTS: {
812  msg += "PARENTS";
813  break;
814  }
815 
816  case factory_state::RAW_CPT: {
817  msg += "RAW_CPT";
818  break;
819  }
820 
822  msg += "FACT_CPT";
823  break;
824  }
825 
827  msg += "FACT_ENTRY";
828  break;
829  }
830 
831  default: { msg += "Unknown state"; }
832  }
833 
834  GUM_ERROR(OperationNotAllowed, msg);
835  }
factory_state state() const final
Returns the current state of the factory.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:66

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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  }
unsigned int 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:

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

Reset the different parts used to constructed the BayesNet.

Definition at line 890 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().

890  {
891  __foo_flag = false;
892  __bar_flag = false;
893  __stringBag.clear();
894  }
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:

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 872 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().

873  {
874  NodeId varId = __varNameMap[var.name()];
875  __bn->_dag.eraseParents(varId);
876 
877  for (auto v : table->variablesSequence()) {
878  if (v != (&var)) {
879  __checkVariableName(v->name());
880  __bn->_dag.addArc(__varNameMap[v->name()], varId);
881  }
882  }
883 
884  // CPT are created when a variable is added.
885  __bn->_unsafeChangePotential(varId, table);
886  }
unsigned int NodeId
Type for node ids.
Definition: graphElements.h:97
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.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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< GUM_SCALAR >::__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:

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

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

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

160  {
161  __bn->setProperty(propName, propValue);
162  }
BayesNet< GUM_SCALAR > * __bn
The constructed BayesNet.

+ Here is the caller graph for this function:

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< GUM_SCALAR >::__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:

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

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

96  {
97  return __bn;
98  }
BayesNet< GUM_SCALAR > * __bn
The constructed BayesNet.
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 138 of file BayesNetFactory_tpl.h.

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

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

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 695 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().

695  {
696  if (state() != factory_state::FACT_CPT) {
697  __illegalStateError("endFactorizedProbabilityDeclaration");
698  } else {
699  __resetParts();
700  __states.pop_back();
701  }
702  }
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:

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 166 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< GUM_SCALAR >::groundedBN().

166  {
167  if (state() != factory_state::NETWORK) {
168  __illegalStateError("endNetworkDeclaration");
169  } else {
170  __states.pop_back();
171  }
172 
173  VERBOSITY("network OK");
174  }
#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:

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< GUM_SCALAR >::__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  }
unsigned int NodeId
Type for node ids.
Definition: graphElements.h:97
#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.
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:

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:

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< GUM_SCALAR >::__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  }
unsigned int NodeId
Type for node ids.
Definition: graphElements.h:97
#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.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:66
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:

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:

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

References GUM_ERROR.

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

781  {
782  GUM_ERROR(OperationNotAllowed, "Illegal!");
783  // For noisy compilers
784  return *this;
785  }
#define GUM_ERROR(type, msg)
Definition: exceptions.h:66

+ Here is the caller graph for this function:

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:

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:

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:

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 581 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().

582  {
583  if (state() != factory_state::FACT_ENTRY) {
584  __illegalStateError("string");
585  } else {
586  __checkVariableName(parent);
587  Idx id = __checkVariableModality(parent, modality);
588  (*__parents) << __bn->variable(__varNameMap[parent]);
589  __parents->chgVal(__bn->variable(__varNameMap[parent]), id);
590  }
591  }
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.
unsigned long Idx
Type for indexes.
Definition: types.h:43

+ Here is the call graph for this function:

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 717 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< GUM_SCALAR >::__groundAttr().

717  {
718  if ((state() != factory_state::NONE)) {
719  __illegalStateError("setVariable");
720  } else {
721  try {
722  __checkVariableName(var.name());
723  GUM_ERROR(DuplicateElement, var.name());
724  } catch (NotFound&) {
725  // The var name is unused
726  __varNameMap.insert(var.name(), __bn->add(var));
727  }
728  }
729  }
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:66

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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 749 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< GUM_SCALAR >::__groundPotential().

750  {
751  auto pot = dynamic_cast< Potential< GUM_SCALAR >* >(table);
752 
753  if (state() != factory_state::NONE) {
754  __illegalStateError("setVariableCPT");
755  } else {
756  __checkVariableName(varName);
757  const DiscreteVariable& var = __bn->variable(__varNameMap[varName]);
758  NodeId varId = __varNameMap[varName];
759  // If we have to change the structure of the BayesNet, then we call a sub
760  // method.
761 
762  if (redefineParents) {
763  __setCPTAndParents(var, pot);
764  } else if (pot->contains(var)) {
765  for (auto node : __bn->parents(varId)) {
766  if (!pot->contains(__bn->variable(node))) {
767  GUM_ERROR(OperationNotAllowed,
768  "The CPT is not valid in the current BayesNet.");
769  }
770  }
771 
772  // CPT are created when a variable is added.
773  __bn->_unsafeChangePotential(varId, pot);
774  }
775  }
776  }
unsigned int NodeId
Type for node ids.
Definition: graphElements.h:97
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.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:66

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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< GUM_SCALAR >::__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:66

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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 672 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().

673  {
674  if (state() != factory_state::FACT_ENTRY) {
675  __illegalStateError("setVariableValues");
676  } else {
677  const DiscreteVariable& var = __bn->variable(__varNameMap[__stringBag[0]]);
678  // Checking consistency between values and var.
679 
680  if (values.size() != var.domainSize()) {
681  GUM_ERROR(OperationNotAllowed,
682  var.name()
683  << " : invalid number of modalities: found " << values.size()
684  << " while needed " << var.domainSize());
685  }
686 
688  }
689  }
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:66

+ Here is the call graph for this function:

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 621 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().

622  {
623  if (state() != factory_state::FACT_ENTRY) {
624  __illegalStateError("setVariableValues");
625  } else {
626  const DiscreteVariable& var = __bn->variable(__varNameMap[__stringBag[0]]);
627  NodeId varId = __varNameMap[__stringBag[0]];
628 
629  if (__parents->domainSize() > 0) {
630  Instantiation inst(__bn->cpt(__varNameMap[var.name()]));
631  inst.setVals(*__parents);
632  // Creating an instantiation containing all the variables not ins
633  // __parents.
634  Instantiation inst_default;
635  inst_default << var;
636 
637  for (auto node : __bn->parents(varId)) {
638  if (!__parents->contains(__bn->variable(node))) {
639  inst_default << __bn->variable(node);
640  }
641  }
642 
643  // Filling the variable's table.
644  for (inst.setFirstIn(inst_default); !inst.end();
645  inst.incIn(inst_default)) {
646  (__bn->cpt(varId))
647  .set(inst,
648  inst.val(var) < values.size() ? (GUM_SCALAR)values[inst.val(var)]
649  : (GUM_SCALAR)0);
650  }
651  } else {
652  Instantiation inst(__bn->cpt(__varNameMap[var.name()]));
653  Instantiation var_inst;
654  var_inst << var;
655 
656  for (var_inst.setFirst(); !var_inst.end(); ++var_inst) {
657  inst.setVals(var_inst);
658 
659  for (inst.setFirstOut(var_inst); !inst.end(); inst.incOut(var_inst)) {
660  (__bn->cpt(varId))
661  .set(inst,
662  inst.val(var) < values.size()
663  ? (GUM_SCALAR)values[inst.val(var)]
664  : (GUM_SCALAR)0);
665  }
666  }
667  }
668  }
669  }
unsigned int NodeId
Type for node ids.
Definition: graphElements.h:97
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.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void gum::IBayesNetFactory::setVerbose ( )
inlineinherited

Definition at line 75 of file IBayesNetFactory.h.

References gum::IBayesNetFactory::__verbose.

75 { __verbose = true; };
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:

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:

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 147 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< GUM_SCALAR >::groundedBN().

147  {
148  if (state() != factory_state::NONE) {
149  __illegalStateError("startNetworkDeclaration");
150  } else {
151  __states.push_back(factory_state::NETWORK);
152  }
153 
154  VERBOSITY("starting network");
155  }
#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:

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< GUM_SCALAR >::__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:

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:

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 178 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< GUM_SCALAR >::__groundAgg().

178  {
179  if (state() != factory_state::NONE) {
180  __illegalStateError("startVariableDeclaration");
181  } else {
183  __stringBag.push_back("name");
184  __stringBag.push_back("desc");
185  }
186 
187  VERBOSITY(" starting variable");
188  }
#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:

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 109 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().

109  {
110  // This is ok because there is alway at least the state NONE in the stack.
111  return __states.back();
112  }
std::vector< factory_state > __states
State stack.

+ Here is the caller graph for this function:

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

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

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

128  {
129  try {
130  return __bn->variable(variableId(name));
131  } catch (NotFound&) { GUM_ERROR(NotFound, name); }
132  }
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:66

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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:

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

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

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

118  {
119  try {
120  return __varNameMap[name];
121  } catch (NotFound&) { GUM_ERROR(NotFound, name); }
122  }
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:66

+ Here is the caller graph for this function:

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 193 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< GUM_SCALAR >::__groundAgg().

193  {
194  if (state() != factory_state::VARIABLE) {
195  __illegalStateError("variableName");
196  } else {
197  if (__varNameMap.exists(name)) {
198  std::string str = "Name already used: ";
199  GUM_ERROR(DuplicateElement, str + name);
200  }
201 
202  __foo_flag = true;
203 
204  __stringBag[0] = name;
205  VERBOSITY(" -- variable " << name);
206  }
207  }
#define VERBOSITY(x)
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
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.
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:66

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

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

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

Member Data Documentation

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().

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().

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().


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