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

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

#include <agrum/BN/BayesNetFactory.h>

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

Public Member Functions

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

(NONE, NETWORK)

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

Public Types

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

Detailed Description

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

A factory class to ease BayesNet construction.

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

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

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

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

Definition at line 44 of file BayesNet.h.

Member Enumeration Documentation

◆ factory_state

enum gum::IBayesNetFactory::factory_state : char
stronginherited

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

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

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

Enumerator
NONE 
NETWORK 
VARIABLE 
PARENTS 
RAW_CPT 
FACT_CPT 
FACT_ENTRY 

Definition at line 59 of file IBayesNetFactory.h.

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

Constructor & Destructor Documentation

◆ BayesNetFactory() [1/2]

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

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

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

Definition at line 45 of file BayesNetFactory_tpl.h.

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

45  :
46  parents__(0),
47  impl__(0), bn__(bn) {
48  GUM_CONSTRUCTOR(BayesNetFactory);
49  states__.push_back(factory_state::NONE);
50 
51  for (auto node: bn->nodes()) {
52  if (varNameMap__.exists(bn->variable(node).name()))
53  GUM_ERROR(DuplicateElement,
54  "Name already used: " << bn->variable(node).name());
55 
56  varNameMap__.insert(bn->variable(node).name(), node);
57  }
58 
59  resetVerbose();
60  }
BayesNetFactory(BayesNet< GUM_SCALAR > *bn)
Use this constructor if you want to use an already created BayesNet.
MultiDimImplementation< GUM_SCALAR > * impl__
Implementation of variable between two startVariableDeclaration/endVariableDeclaration calls...
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
BayesNet< GUM_SCALAR > * bn__
The constructed BayesNet.
std::vector< factory_state > states__
State stack.
HashTable< std::string, NodeId > varNameMap__
Mapping between a declared variable&#39;s name and it&#39;s node id.
Instantiation * parents__
Used when a factorized CPT is built.
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ 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 65 of file BayesNetFactory_tpl.h.

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

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

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

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

Member Function Documentation

◆ addMax()

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

Adds the max value of the current range variable.

Implements gum::IBayesNetFactory.

Definition at line 281 of file BayesNetFactory_tpl.h.

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

281  {
282  if (state() != factory_state::VARIABLE) {
283  illegalStateError__("addMin");
284  } else {
285  stringBag__.push_back(std::to_string(max));
286  }
287  }
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::string to_string(const Formula &f)
Definition: formula_inl.h:498
std::vector< std::string > stringBag__
Just to keep track of strings between two start/end calls.
+ Here is the call graph for this function:

◆ addMin()

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

Adds the min value of the current range variable.

Implements gum::IBayesNetFactory.

Definition at line 269 of file BayesNetFactory_tpl.h.

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

269  {
270  if (state() != factory_state::VARIABLE) {
271  illegalStateError__("addMin");
272  } else {
273  stringBag__.push_back(std::to_string(min));
274  }
275  }
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::string to_string(const Formula &f)
Definition: formula_inl.h:498
std::vector< std::string > stringBag__
Just to keep track of strings between two start/end calls.
+ Here is the call graph for this function:

◆ addModality()

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

Adds a modality to the current labelized variable.

Implements gum::IBayesNetFactory.

Definition at line 256 of file BayesNetFactory_tpl.h.

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

256  {
257  if (state() != factory_state::VARIABLE) {
258  illegalStateError__("addModality");
259  } else {
260  checkModalityInBag__(name);
261  stringBag__.push_back(name);
262  }
263  }
factory_state state() const final
Returns the current state of the factory.
void checkModalityInBag__(const std::string &mod)
Check if in stringBag__ there is no other modality with the same name.
void illegalStateError__(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
std::vector< std::string > stringBag__
Just to keep track of strings between two start/end calls.
+ Here is the call graph for this function:

◆ addNetworkProperty()

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

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

Implements gum::IBayesNetFactory.

Definition at line 160 of file BayesNetFactory_tpl.h.

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

162  {
163  bn__->setProperty(propName, propValue);
164  }
BayesNet< GUM_SCALAR > * bn__
The constructed BayesNet.
+ Here is the call graph for this function:

◆ addParent()

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

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

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

Implements gum::IBayesNetFactory.

Definition at line 439 of file BayesNetFactory_tpl.h.

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

439  {
440  if (state() != factory_state::PARENTS) {
441  illegalStateError__("addParent");
442  } else {
443  checkVariableName__(var);
444  stringBag__.push_back(var);
445  }
446  }
factory_state state() const final
Returns the current state of the factory.
void checkVariableName__(const std::string &name)
Check if a variable with the given name exists, if not raise an NotFound exception.
void illegalStateError__(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
std::vector< std::string > stringBag__
Just to keep track of strings between two start/end calls.
+ Here is the call graph for this function:

◆ addTick()

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

Adds a tick to the current Discretized variable.

Definition at line 293 of file BayesNetFactory_tpl.h.

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

293  {
294  if (state() != factory_state::VARIABLE) {
295  illegalStateError__("addTick");
296  } else {
297  stringBag__.push_back(std::to_string(tick));
298  }
299  }
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::string to_string(const Formula &f)
Definition: formula_inl.h:498
std::vector< std::string > stringBag__
Just to keep track of strings between two start/end calls.
+ Here is the call graph for this function:

◆ bayesNet()

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

Returns the BayesNet created by this factory.

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

Definition at line 100 of file BayesNetFactory_tpl.h.

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

100  {
101  return bn__;
102  }
BayesNet< GUM_SCALAR > * bn__
The constructed BayesNet.
+ Here is the call graph for this function:

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

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

964  {
965  for (size_t i = 3; i < stringBag__.size(); ++i) {
966  if (mod == stringBag__[i]) {
967  GUM_ERROR(DuplicateElement, "Label already used: " << mod);
968  }
969  }
970  }
std::vector< std::string > stringBag__
Just to keep track of strings between two start/end calls.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call graph for this function:

◆ checkVariableModality__()

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

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

Definition at line 948 of file BayesNetFactory_tpl.h.

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

950  {
951  checkVariableName__(name);
952  const DiscreteVariable& var = bn__->variable(varNameMap__[name]);
953 
954  for (Idx i = 0; i < var.domainSize(); ++i) {
955  if (mod == var.label(i)) { return i; }
956  }
957 
958  GUM_ERROR(NotFound, mod);
959  }
void checkVariableName__(const std::string &name)
Check if a variable with the given name exists, if not raise an NotFound exception.
BayesNet< GUM_SCALAR > * bn__
The constructed BayesNet.
HashTable< std::string, NodeId > varNameMap__
Mapping between a declared variable&#39;s name and it&#39;s node id.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call graph for this function:

◆ checkVariableName__()

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

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

Definition at line 941 of file BayesNetFactory_tpl.h.

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

941  {
942  if (!varNameMap__.exists(name)) { GUM_ERROR(NotFound, name); }
943  }
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:54
+ Here is the call graph for this function:

◆ cptDomainSize()

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

Returns the domainSize of the cpt for the node n.

Exceptions
NotFoundraised if no such NodeId exists.

Implements gum::IBayesNetFactory.

Definition at line 142 of file BayesNetFactory_tpl.h.

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

142  {
143  return bn__->cpt(n).domainSize();
144  }
BayesNet< GUM_SCALAR > * bn__
The constructed BayesNet.
+ Here is the call graph for this function:

◆ endFactorizedEntry()

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

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

Implements gum::IBayesNetFactory.

Definition at line 663 of file BayesNetFactory_tpl.h.

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

663  {
664  if (state() != factory_state::FACT_ENTRY) {
665  illegalStateError__("endFactorizedEntry");
666  } else {
667  delete parents__;
668  parents__ = 0;
669  states__.pop_back();
670  }
671  }
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 791 of file BayesNetFactory_tpl.h.

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

791  {
792  if (state() != factory_state::FACT_CPT) {
793  illegalStateError__("endFactorizedProbabilityDeclaration");
794  } else {
795  resetParts__();
796  states__.pop_back();
797  }
798  }
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 168 of file BayesNetFactory_tpl.h.

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

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

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

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

453  {
454  if (state() != factory_state::PARENTS) {
455  illegalStateError__("endParentsDeclaration");
456  } else {
458 
459  // PLEASE NOTE THAT THE ORDER IS INVERSE
460 
461  for (size_t i = stringBag__.size() - 1; i > 0; --i) {
462  bn__->addArc(varNameMap__[stringBag__[i]], id);
463  VERBOSITY(" adding parent " << stringBag__[i] << " for "
464  << stringBag__[0]);
465  }
466 
467  resetParts__();
468 
469  states__.pop_back();
470  }
471 
472  VERBOSITY("end of parents for " << stringBag__[0]);
473  }
#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.".
BayesNet< GUM_SCALAR > * bn__
The constructed BayesNet.
std::vector< std::string > stringBag__
Just to keep track of strings between two start/end calls.
std::vector< factory_state > states__
State stack.
HashTable< std::string, NodeId > varNameMap__
Mapping between a declared variable&#39;s name and it&#39;s node id.
void resetParts__()
Reset the different parts used to constructed the BayesNet.
Size NodeId
Type for node ids.
Definition: graphElements.h:97
+ Here is the call graph for this function:

◆ endRawProbabilityDeclaration()

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

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

Implements gum::IBayesNetFactory.

Definition at line 623 of file BayesNetFactory_tpl.h.

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

623  {
624  if (state() != factory_state::RAW_CPT) {
625  illegalStateError__("endRawProbabilityDeclaration");
626  } else {
627  resetParts__();
628  states__.pop_back();
629  }
630 
631  VERBOSITY(" cpt ending for " << stringBag__[0]);
632  }
#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< std::string > stringBag__
Just to keep track of strings between two start/end calls.
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:

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

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

334  {
335  if (state() != factory_state::VARIABLE) {
336  illegalStateError__("endVariableDeclaration");
337  } else if (foo_flag__ && (stringBag__.size() > 4)) {
338  DiscreteVariable* var = nullptr;
339 
340  // if the current variable is a LabelizedVariable
341  if (stringBag__[2] == "L") {
342  LabelizedVariable* l
343  = new LabelizedVariable(stringBag__[0],
344  (bar_flag__) ? stringBag__[1] : "",
345  0);
346 
347  for (size_t i = 3; i < stringBag__.size(); ++i) {
348  l->addLabel(stringBag__[i]);
349  }
350 
351  var = l;
352  // if the current variable is a RangeVariable
353  } else if (stringBag__[2] == "R") {
354  RangeVariable* r = new RangeVariable(stringBag__[0],
355  (bar_flag__) ? stringBag__[1] : "",
356  std::stol(stringBag__[3]),
357  std::stol(stringBag__[4]));
358 
359  var = r;
360  // if the current variable is a DiscretizedVariable
361  } else if (stringBag__[2] == "D") {
362  DiscretizedVariable< GUM_SCALAR >* d
363  = new DiscretizedVariable< GUM_SCALAR >(stringBag__[0],
364  (bar_flag__) ? stringBag__[1]
365  : "");
366 
367  for (size_t i = 3; i < stringBag__.size(); ++i) {
368  d->addTick(std::stof(stringBag__[i]));
369  }
370 
371  var = d;
372  }
373 
374  if (impl__ != 0) {
375  varNameMap__.insert(var->name(), bn__->add(*var, impl__));
376  impl__ = 0;
377  } else {
378  varNameMap__.insert(var->name(), bn__->add(*var));
379  }
380 
381  NodeId retVal = varNameMap__[var->name()];
382 
383  delete var;
384 
385  resetParts__();
386  states__.pop_back();
387 
388  VERBOSITY(" variable " << var->name() << " OK");
389  return retVal;
390  } else {
391  std::stringstream msg;
392  msg << "Not enough modalities (";
393 
394  if (stringBag__.size() > 3) {
395  msg << stringBag__.size() - 3;
396  } else {
397  msg << 0;
398  }
399 
400  msg << ") declared for variable ";
401 
402  if (foo_flag__) {
403  msg << stringBag__[0];
404  } else {
405  msg << "unknown";
406  }
407 
408  resetParts__();
409 
410  states__.pop_back();
411  GUM_ERROR(OperationNotAllowed, msg.str());
412  }
413 
414  // For noisy compilers
415  return 0;
416  }
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.
#define VERBOSITY(x)
factory_state state() const final
Returns the current state of the factory.
MultiDimImplementation< GUM_SCALAR > * impl__
Implementation of variable between two startVariableDeclaration/endVariableDeclaration calls...
void illegalStateError__(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
BayesNet< GUM_SCALAR > * bn__
The constructed BayesNet.
std::vector< std::string > stringBag__
Just to keep track of strings between two start/end calls.
std::vector< factory_state > states__
State stack.
HashTable< std::string, NodeId > varNameMap__
Mapping between a declared variable&#39;s name and it&#39;s node id.
void resetParts__()
Reset the different parts used to constructed the BayesNet.
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:54
+ Here is the call graph for this function:

◆ fillProbaWithValuesTable__() [1/2]

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

Fill a potential from a raw CPT.

Definition at line 515 of file BayesNetFactory_tpl.h.

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

517  {
518  const Potential< GUM_SCALAR >& table = bn__->cpt(varNameMap__[stringBag__[0]]);
519  Instantiation cptInst(table);
520 
521  List< const DiscreteVariable* > varList;
522 
523  for (size_t i = 0; i < variables.size(); ++i) {
524  varList.pushBack(&(bn__->variable(varNameMap__[variables[i]])));
525  }
526 
527  // varList.pushFront(&(bn__->variable(varNameMap__[stringBag__[0]])));
528 
529  Idx nbrVar = varList.size();
530 
531  std::vector< Idx > modCounter;
532 
533  // initializing the array
534  for (NodeId i = 0; i < nbrVar; i++) {
535  modCounter.push_back(Idx(0));
536  }
537 
538  Idx j = 0;
539 
540  do {
541  for (NodeId i = 0; i < nbrVar; i++) {
542  cptInst.chgVal(*(varList[i]), modCounter[i]);
543  }
544 
545  if (j < rawTable.size()) {
546  table.set(cptInst, (GUM_SCALAR)rawTable[j]);
547  } else {
548  table.set(cptInst, (GUM_SCALAR)0);
549  }
550 
551  j++;
552  } while (increment__(modCounter, varList));
553  }
BayesNet< GUM_SCALAR > * bn__
The constructed BayesNet.
std::vector< std::string > stringBag__
Just to keep track of strings between two start/end calls.
HashTable< std::string, NodeId > varNameMap__
Mapping between a declared variable&#39;s name and it&#39;s node id.
bool increment__(std::vector< gum::Idx > &modCounter, List< const DiscreteVariable * > &varList)
Increment a modality counter for the fillProbaWithValuesTable__ method.
Size Idx
Type for indexes.
Definition: types.h:52
Size NodeId
Type for node ids.
Definition: graphElements.h:97
+ Here is the call graph for this function:

◆ fillProbaWithValuesTable__() [2/2]

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

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

Definition at line 566 of file BayesNetFactory_tpl.h.

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

567  {
568  const Potential< GUM_SCALAR >& table = bn__->cpt(varNameMap__[stringBag__[0]]);
569 
570  Instantiation cptInst(table);
571 
572  // the main loop is on the first variables. The others are in the right
573  // order.
574  const DiscreteVariable& first = table.variable(0);
575  Idx j = 0;
576 
577  for (cptInst.setFirstVar(first); !cptInst.end(); cptInst.incVar(first)) {
578  for (cptInst.setFirstNotVar(first); !cptInst.end(); cptInst.incNotVar(first))
579  table.set(cptInst,
580  (j < rawTable.size()) ? (GUM_SCALAR)rawTable[j++]
581  : (GUM_SCALAR)0);
582 
583  cptInst.unsetEnd();
584  }
585  }
BayesNet< GUM_SCALAR > * bn__
The constructed BayesNet.
std::vector< std::string > stringBag__
Just to keep track of strings between two start/end calls.
HashTable< std::string, NodeId > varNameMap__
Mapping between a declared variable&#39;s name and it&#39;s node id.
+ Here is the call graph for this function:

◆ illegalStateError__()

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

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

Definition at line 888 of file BayesNetFactory_tpl.h.

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

888  {
889  std::string msg = "Illegal state call (";
890  msg += s;
891  msg += ") in state ";
892 
893  switch (state()) {
894  case factory_state::NONE: {
895  msg += "NONE";
896  break;
897  }
898 
899  case factory_state::NETWORK: {
900  msg += "NETWORK";
901  break;
902  }
903 
905  msg += "VARIABLE";
906  break;
907  }
908 
909  case factory_state::PARENTS: {
910  msg += "PARENTS";
911  break;
912  }
913 
914  case factory_state::RAW_CPT: {
915  msg += "RAW_CPT";
916  break;
917  }
918 
920  msg += "FACT_CPT";
921  break;
922  }
923 
925  msg += "FACT_ENTRY";
926  break;
927  }
928 
929  default: {
930  msg += "Unknown state";
931  }
932  }
933 
934  GUM_ERROR(OperationNotAllowed, msg);
935  }
factory_state state() const final
Returns the current state of the factory.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call graph for this function:

◆ increment__()

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

Increment a modality counter for the fillProbaWithValuesTable__ method.

Definition at line 588 of file BayesNetFactory_tpl.h.

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

590  {
591  bool last = true;
592 
593  for (NodeId j = 0; j < modCounter.size(); j++) {
594  last = (modCounter[j] == (varList[j]->domainSize() - 1)) && last;
595 
596  if (!last) break;
597  }
598 
599  if (last) { return false; }
600 
601  bool add = false;
602 
603  NodeId i = NodeId(varList.size() - 1);
604 
605  do {
606  if (modCounter[i] == (varList[i]->domainSize() - 1)) {
607  modCounter[i] = 0;
608  add = true;
609  } else {
610  modCounter[i] += 1;
611  add = false;
612  }
613 
614  i--;
615  } while (add);
616 
617  return true;
618  }
Size NodeId
Type for node ids.
Definition: graphElements.h:97
+ Here is the call graph for this function:

◆ isVerbose()

bool gum::IBayesNetFactory::isVerbose ( )
inlineinherited

Definition at line 82 of file IBayesNetFactory.h.

References gum::IBayesNetFactory::verbose__.

82 { return verbose__; };

◆ operator=()

template<typename GUM_SCALAR >
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 878 of file BayesNetFactory_tpl.h.

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

879  {
880  GUM_ERROR(OperationNotAllowed, "Illegal!");
881  // For noisy compilers
882  return *this;
883  }
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call 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 504 of file BayesNetFactory_tpl.h.

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

506  {
507  if (state() != factory_state::RAW_CPT) {
508  illegalStateError__("rawConditionalTable");
509  } else {
510  fillProbaWithValuesTable__(variables, rawTable);
511  }
512  }
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.".
void fillProbaWithValuesTable__(const std::vector< std::string > &variables, const std::vector< float > &rawTable)
Fill a potential from a raw CPT.
+ 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 556 of file BayesNetFactory_tpl.h.

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

557  {
558  if (state() != factory_state::RAW_CPT) {
559  illegalStateError__("rawConditionalTable");
560  } else {
561  fillProbaWithValuesTable__(rawTable);
562  }
563  }
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.".
void fillProbaWithValuesTable__(const std::vector< std::string > &variables, const std::vector< float > &rawTable)
Fill a potential from a raw CPT.
+ Here is the call graph for this function:

◆ resetParts__()

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

Reset the different parts used to constructed the BayesNet.

Definition at line 994 of file BayesNetFactory_tpl.h.

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

994  {
995  foo_flag__ = false;
996  bar_flag__ = false;
997  stringBag__.clear();
998  }
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.
std::vector< std::string > stringBag__
Just to keep track of strings between two start/end calls.
+ Here is the call graph for this function:

◆ resetVerbose()

void gum::IBayesNetFactory::resetVerbose ( )
inlineinherited

Definition at line 80 of file IBayesNetFactory.h.

References gum::IBayesNetFactory::verbose__.

80 { verbose__ = false; };

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

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

977  {
978  NodeId varId = varNameMap__[var.name()];
979  bn__->dag_.eraseParents(varId);
980 
981  for (auto v: table->variablesSequence()) {
982  if (v != (&var)) {
983  checkVariableName__(v->name());
984  bn__->dag_.addArc(varNameMap__[v->name()], varId);
985  }
986  }
987 
988  // CPT are created when a variable is added.
989  bn__->unsafeChangePotential_(varId, table);
990  }
void checkVariableName__(const std::string &name)
Check if a variable with the given name exists, if not raise an NotFound exception.
BayesNet< GUM_SCALAR > * bn__
The constructed BayesNet.
HashTable< std::string, NodeId > varNameMap__
Mapping between a declared variable&#39;s name and it&#39;s node id.
Size NodeId
Type for node ids.
Definition: graphElements.h:97
+ Here is the call 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 676 of file BayesNetFactory_tpl.h.

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

678  {
679  if (state() != factory_state::FACT_ENTRY) {
680  illegalStateError__("string");
681  } else {
682  checkVariableName__(parent);
683  Idx id = checkVariableModality__(parent, modality);
684  (*parents__) << bn__->variable(varNameMap__[parent]);
685  parents__->chgVal(bn__->variable(varNameMap__[parent]), id);
686  }
687  }
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.
factory_state state() const final
Returns the current state of the factory.
void checkVariableName__(const std::string &name)
Check if a variable with the given name exists, if not raise an NotFound exception.
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.".
BayesNet< GUM_SCALAR > * bn__
The constructed BayesNet.
HashTable< std::string, NodeId > varNameMap__
Mapping between a declared variable&#39;s name and it&#39;s node id.
Instantiation * parents__
Used when a factorized CPT is built.
+ 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 813 of file BayesNetFactory_tpl.h.

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

813  {
814  if ((state() != factory_state::NONE)) {
815  illegalStateError__("setVariable");
816  } else {
817  try {
818  checkVariableName__(var.name());
819  GUM_ERROR(DuplicateElement, "Name already used: " << var.name());
820  } catch (NotFound&) {
821  // The var name is unused
822  varNameMap__.insert(var.name(), bn__->add(var));
823  }
824  }
825  }
factory_state state() const final
Returns the current state of the factory.
void checkVariableName__(const std::string &name)
Check if a variable with the given name exists, if not raise an NotFound exception.
void illegalStateError__(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
BayesNet< GUM_SCALAR > * bn__
The constructed BayesNet.
HashTable< std::string, NodeId > varNameMap__
Mapping between a declared variable&#39;s name and it&#39;s node id.
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:54
+ Here is the call graph for this function:

◆ setVariableCPT()

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

Define a variable's CPT.

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

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

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

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

Implements gum::IBayesNetFactory.

Definition at line 846 of file BayesNetFactory_tpl.h.

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

848  {
849  auto pot = dynamic_cast< Potential< GUM_SCALAR >* >(table);
850 
851  if (state() != factory_state::NONE) {
852  illegalStateError__("setVariableCPT");
853  } else {
854  checkVariableName__(varName);
855  const DiscreteVariable& var = bn__->variable(varNameMap__[varName]);
856  NodeId varId = varNameMap__[varName];
857  // If we have to change the structure of the BayesNet, then we call a sub
858  // method.
859 
860  if (redefineParents) {
861  setCPTAndParents__(var, pot);
862  } else if (pot->contains(var)) {
863  for (auto node: bn__->parents(varId)) {
864  if (!pot->contains(bn__->variable(node))) {
865  GUM_ERROR(OperationNotAllowed,
866  "The CPT is not valid in the current BayesNet.");
867  }
868  }
869 
870  // CPT are created when a variable is added.
871  bn__->unsafeChangePotential_(varId, pot);
872  }
873  }
874  }
factory_state state() const final
Returns the current state of the factory.
void checkVariableName__(const std::string &name)
Check if a variable with the given name exists, if not raise an NotFound exception.
void illegalStateError__(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
BayesNet< GUM_SCALAR > * bn__
The constructed BayesNet.
HashTable< std::string, NodeId > varNameMap__
Mapping between a declared variable&#39;s name and it&#39;s node id.
void 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:54
+ Here is the call graph for this function:

◆ setVariableCPTImplementation()

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

Defines the implementation to use for var's Potential.

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

Implements gum::IBayesNetFactory.

Definition at line 312 of file BayesNetFactory_tpl.h.

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

313  {
314  MultiDimImplementation< GUM_SCALAR >* impl
315  = dynamic_cast< MultiDimImplementation< GUM_SCALAR >* >(adressable);
316 
317  if (state() != factory_state::VARIABLE) {
318  illegalStateError__("setVariableCPTImplementation");
319  } else {
320  if (impl == 0) {
321  GUM_ERROR(OperationNotAllowed,
322  "An implementation for this variable is already "
323  "defined.");
324  } else if (impl->nbrDim() > 0) {
325  GUM_ERROR(OperationNotAllowed, "This implementation is not empty.");
326  }
327 
328  impl__ = impl;
329  }
330  }
factory_state state() const final
Returns the current state of the factory.
MultiDimImplementation< GUM_SCALAR > * impl__
Implementation of variable between two startVariableDeclaration/endVariableDeclaration calls...
void illegalStateError__(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call graph for this function:

◆ setVariableValues()

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

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

Implements gum::IBayesNetFactory.

Definition at line 768 of file BayesNetFactory_tpl.h.

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

769  {
770  if (state() != factory_state::FACT_ENTRY) {
771  illegalStateError__("setVariableValues");
772  } else {
773  const DiscreteVariable& var = bn__->variable(varNameMap__[stringBag__[0]]);
774  // Checking consistency between values and var.
775 
776  if (values.size() != var.domainSize()) {
777  GUM_ERROR(OperationNotAllowed,
778  var.name()
779  << " : invalid number of modalities: found " << values.size()
780  << " while needed " << var.domainSize());
781  }
782 
784  }
785  }
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.".
BayesNet< GUM_SCALAR > * bn__
The constructed BayesNet.
std::vector< std::string > stringBag__
Just to keep track of strings between two start/end calls.
HashTable< std::string, NodeId > varNameMap__
Mapping between a declared variable&#39;s name and it&#39;s node id.
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:54
+ 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 717 of file BayesNetFactory_tpl.h.

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

718  {
719  if (state() != factory_state::FACT_ENTRY) {
720  illegalStateError__("setVariableValues");
721  } else {
722  const DiscreteVariable& var = bn__->variable(varNameMap__[stringBag__[0]]);
723  NodeId varId = varNameMap__[stringBag__[0]];
724 
725  if (parents__->domainSize() > 0) {
726  Instantiation inst(bn__->cpt(varNameMap__[var.name()]));
727  inst.setVals(*parents__);
728  // Creating an instantiation containing all the variables not ins
729  // parents__.
730  Instantiation inst_default;
731  inst_default << var;
732 
733  for (auto node: bn__->parents(varId)) {
734  if (!parents__->contains(bn__->variable(node))) {
735  inst_default << bn__->variable(node);
736  }
737  }
738 
739  // Filling the variable's table.
740  for (inst.setFirstIn(inst_default); !inst.end();
741  inst.incIn(inst_default)) {
742  (bn__->cpt(varId))
743  .set(inst,
744  inst.val(var) < values.size() ? (GUM_SCALAR)values[inst.val(var)]
745  : (GUM_SCALAR)0);
746  }
747  } else {
748  Instantiation inst(bn__->cpt(varNameMap__[var.name()]));
749  Instantiation var_inst;
750  var_inst << var;
751 
752  for (var_inst.setFirst(); !var_inst.end(); ++var_inst) {
753  inst.setVals(var_inst);
754 
755  for (inst.setFirstOut(var_inst); !inst.end(); inst.incOut(var_inst)) {
756  (bn__->cpt(varId))
757  .set(inst,
758  inst.val(var) < values.size()
759  ? (GUM_SCALAR)values[inst.val(var)]
760  : (GUM_SCALAR)0);
761  }
762  }
763  }
764  }
765  }
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.".
BayesNet< GUM_SCALAR > * bn__
The constructed BayesNet.
std::vector< std::string > stringBag__
Just to keep track of strings between two start/end calls.
HashTable< std::string, NodeId > varNameMap__
Mapping between a declared variable&#39;s name and it&#39;s node id.
Instantiation * parents__
Used when a factorized CPT is built.
bool contains(const DiscreteVariable &v) const final
Indicates whether a given variable belongs to the Instantiation.
Size NodeId
Type for node ids.
Definition: graphElements.h:97
+ Here is the call graph for this function:

◆ setVerbose()

void gum::IBayesNetFactory::setVerbose ( )
inlineinherited

Definition at line 78 of file IBayesNetFactory.h.

References gum::IBayesNetFactory::verbose__.

78 { verbose__ = true; };

◆ startFactorizedEntry()

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

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

Implements gum::IBayesNetFactory.

Definition at line 651 of file BayesNetFactory_tpl.h.

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

651  {
652  if (state() != factory_state::FACT_CPT) {
653  illegalStateError__("startFactorizedEntry");
654  } else {
655  parents__ = new Instantiation();
657  }
658  }
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 636 of file BayesNetFactory_tpl.h.

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

637  {
638  if (state() != factory_state::NONE) {
639  illegalStateError__("startFactorizedProbabilityDeclaration");
640  } else {
641  checkVariableName__(var);
642  std::vector< std::string >::iterator iter = stringBag__.begin();
643  stringBag__.insert(iter, var);
645  }
646  }
factory_state state() const final
Returns the current state of the factory.
void checkVariableName__(const std::string &name)
Check if a variable with the given name exists, if not raise an NotFound exception.
void illegalStateError__(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
std::vector< std::string > stringBag__
Just to keep track of strings between two start/end calls.
std::vector< factory_state > states__
State stack.
+ 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::Set< Key, Alloc >::emplace().

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:

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

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

422  {
423  if (state() != factory_state::NONE) {
424  illegalStateError__("startParentsDeclaration");
425  } else {
426  checkVariableName__(var);
427  std::vector< std::string >::iterator iter = stringBag__.begin();
428  stringBag__.insert(iter, var);
429  states__.push_back(factory_state::PARENTS);
430  }
431 
432  VERBOSITY("starting parents for " << var);
433  }
#define VERBOSITY(x)
factory_state state() const final
Returns the current state of the factory.
void checkVariableName__(const std::string &name)
Check if a variable with the given name exists, if not raise an NotFound exception.
void illegalStateError__(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
std::vector< std::string > stringBag__
Just to keep track of strings between two start/end calls.
std::vector< factory_state > states__
State stack.
+ Here is the call graph for this function:

◆ startRawProbabilityDeclaration()

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

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

Parameters
varThe concerned variable's name.

Implements gum::IBayesNetFactory.

Definition at line 479 of file BayesNetFactory_tpl.h.

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

480  {
481  if (state() != factory_state::NONE) {
482  illegalStateError__("startRawProbabilityDeclaration");
483  } else {
484  checkVariableName__(var);
485  stringBag__.push_back(var);
486  states__.push_back(factory_state::RAW_CPT);
487  }
488 
489  VERBOSITY(" cpt starting for " << var);
490  }
#define VERBOSITY(x)
factory_state state() const final
Returns the current state of the factory.
void checkVariableName__(const std::string &name)
Check if a variable with the given name exists, if not raise an NotFound exception.
void illegalStateError__(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
std::vector< std::string > stringBag__
Just to keep track of strings between two start/end calls.
std::vector< factory_state > states__
State stack.
+ 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 181 of file BayesNetFactory_tpl.h.

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

181  {
182  if (state() != factory_state::NONE) {
183  illegalStateError__("startVariableDeclaration");
184  } else {
186  stringBag__.push_back("name");
187  stringBag__.push_back("desc");
188  stringBag__.push_back("L");
189  }
190 
191  VERBOSITY(" starting variable");
192  }
#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< std::string > stringBag__
Just to keep track of strings between two start/end calls.
std::vector< factory_state > states__
State stack.
+ Here is the call graph for this function:

◆ state()

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

Returns the current state of the factory.

Implements gum::IBayesNetFactory.

Definition at line 113 of file BayesNetFactory_tpl.h.

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

113  {
114  // This is ok because there is alway at least the state NONE in the stack.
115  return states__.back();
116  }
std::vector< factory_state > states__
State stack.
+ Here is the call graph for this function:

◆ variable()

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

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

Exceptions
NotFoundRaised if no variable matches the name.

Definition at line 132 of file BayesNetFactory_tpl.h.

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

132  {
133  try {
134  return bn__->variable(variableId(name));
135  } catch (NotFound&) { GUM_ERROR(NotFound, name); }
136  }
BayesNet< GUM_SCALAR > * bn__
The constructed BayesNet.
NodeId variableId(const std::string &name) const final
Returns the NodeId of a variable given it&#39;s name.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call graph for this function:

◆ variableDescription()

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

Tells the factory the current variable's description.

Implements gum::IBayesNetFactory.

Definition at line 214 of file BayesNetFactory_tpl.h.

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

214  {
215  if (state() != factory_state::VARIABLE) {
216  illegalStateError__("variableDescription");
217  } else {
218  bar_flag__ = true;
219  stringBag__[1] = desc;
220  }
221  }
bool bar_flag__
Depending on the context this flag is used for some VERY important reasons.
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.
+ 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 122 of file BayesNetFactory_tpl.h.

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

122  {
123  try {
124  return varNameMap__[name];
125  } catch (NotFound&) { GUM_ERROR(NotFound, name); }
126  }
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:54
+ Here is the call graph for this function:

◆ variableName()

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

Tells the factory the current variable's name.

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

Implements gum::IBayesNetFactory.

Definition at line 197 of file BayesNetFactory_tpl.h.

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

197  {
198  if (state() != factory_state::VARIABLE) {
199  illegalStateError__("variableName");
200  } else {
201  if (varNameMap__.exists(name)) {
202  GUM_ERROR(DuplicateElement, "Name already used: " << name);
203  }
204 
205  foo_flag__ = true;
206  stringBag__[0] = name;
207  VERBOSITY(" -- variable " << name);
208  }
209  }
bool foo_flag__
Depending on the context this flag is used for some VERY important reasons.
#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.".
std::vector< std::string > stringBag__
Just to keep track of strings between two start/end calls.
HashTable< std::string, NodeId > varNameMap__
Mapping between a declared variable&#39;s name and it&#39;s node id.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call graph for this function:

◆ variableType()

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

Tells the factory the current variable's type.

Implements gum::IBayesNetFactory.

Definition at line 230 of file BayesNetFactory_tpl.h.

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

230  {
231  if (state() != factory_state::VARIABLE) {
232  illegalStateError__("variableType");
233  } else {
234  switch (type) {
236  stringBag__[2] = "D";
237  break;
238  case VarType::Range:
239  stringBag__[2] = "R";
240  break;
241  case VarType::Continuous:
242  GUM_ERROR(OperationNotAllowed,
243  "Continuous variable (" + stringBag__[0]
244  + ") are not supported in Bayesian networks.");
245  case VarType::Labelized:
246  stringBag__[2] = "L";
247  break;
248  }
249  }
250  }
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.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call graph for this function:

◆ varInBN()

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

short-cut accessor for a DiscreveVariable in the BN

Implements gum::IBayesNetFactory.

Definition at line 106 of file BayesNetFactory_tpl.h.

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

106  {
107  return bn__->variable(id);
108  }
BayesNet< GUM_SCALAR > * bn__
The constructed BayesNet.
+ Here is the call graph for this function:

Member Data Documentation

◆ bar_flag__

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

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

Definition at line 406 of file BayesNetFactory.h.

◆ bn__

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

The constructed BayesNet.

Definition at line 424 of file BayesNetFactory.h.

◆ foo_flag__

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

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

Definition at line 402 of file BayesNetFactory.h.

◆ impl__

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

Implementation of variable between two startVariableDeclaration/endVariableDeclaration calls.

Definition at line 416 of file BayesNetFactory.h.

◆ parents__

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

Used when a factorized CPT is built.

Definition at line 412 of file BayesNetFactory.h.

◆ states__

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

State stack.

Definition at line 421 of file BayesNetFactory.h.

◆ stringBag__

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

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

Definition at line 409 of file BayesNetFactory.h.

◆ varNameMap__

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

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

Definition at line 427 of file BayesNetFactory.h.


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