aGrUM  0.18.1
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 45 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 60 of file IBayesNetFactory.h.

60  : char {
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 46 of file BayesNetFactory_tpl.h.

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

46  :
47  parents__(0),
48  impl__(0), bn__(bn) {
49  GUM_CONSTRUCTOR(BayesNetFactory);
50  states__.push_back(factory_state::NONE);
51 
52  for (auto node: bn->nodes()) {
53  if (varNameMap__.exists(bn->variable(node).name()))
54  GUM_ERROR(DuplicateElement,
55  "Name already used: " << bn->variable(node).name());
56 
57  varNameMap__.insert(bn->variable(node).name(), node);
58  }
59 
60  resetVerbose();
61  }
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:55
+ 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 66 of file BayesNetFactory_tpl.h.

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

67  :
68  parents__(0),
69  impl__(0), bn__(0) {
70  GUM_CONS_CPY(BayesNetFactory);
71 
72  if (source.state() != factory_state::NONE) {
73  GUM_ERROR(OperationNotAllowed, "Illegal state to proceed make a copy.");
74  } else {
75  states__ = source.states__;
76  bn__ = new BayesNet< GUM_SCALAR >(*(source.bn__));
77  }
78  }
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:55
+ 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 82 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::impl__, and gum::BayesNetFactory< GUM_SCALAR >::parents__.

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

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

References gum::BayesNetFactory< GUM_SCALAR >::illegalStateError__(), gum::BayesNetFactory< GUM_SCALAR >::state(), gum::BayesNetFactory< GUM_SCALAR >::stringBag__, gum::to_string(), and gum::IBayesNetFactory::VARIABLE.

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

275  {
276  if (state() != factory_state::VARIABLE) {
277  illegalStateError__("addMin");
278  } else {
279  stringBag__.push_back(std::to_string(max));
280  }
281  }
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:499
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:

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

References gum::BayesNetFactory< GUM_SCALAR >::illegalStateError__(), gum::BayesNetFactory< GUM_SCALAR >::state(), gum::BayesNetFactory< GUM_SCALAR >::stringBag__, gum::to_string(), and gum::IBayesNetFactory::VARIABLE.

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

263  {
264  if (state() != factory_state::VARIABLE) {
265  illegalStateError__("addMin");
266  } else {
267  stringBag__.push_back(std::to_string(min));
268  }
269  }
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:499
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:

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

References gum::BayesNetFactory< GUM_SCALAR >::checkModalityInBag__(), gum::BayesNetFactory< GUM_SCALAR >::illegalStateError__(), gum::BayesNetFactory< GUM_SCALAR >::state(), gum::BayesNetFactory< GUM_SCALAR >::stringBag__, and gum::IBayesNetFactory::VARIABLE.

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

250  {
251  if (state() != factory_state::VARIABLE) {
252  illegalStateError__("addModality");
253  } else {
254  checkModalityInBag__(name);
255  stringBag__.push_back(name);
256  }
257  }
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:
+ Here is the caller graph for this function:

◆ addNetworkProperty()

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

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

Implements gum::IBayesNetFactory.

Definition at line 161 of file BayesNetFactory_tpl.h.

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

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

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

◆ addParent()

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

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

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

Implements gum::IBayesNetFactory.

Definition at line 430 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::checkVariableName__(), gum::BayesNetFactory< GUM_SCALAR >::illegalStateError__(), gum::IBayesNetFactory::PARENTS, gum::BayesNetFactory< GUM_SCALAR >::state(), and gum::BayesNetFactory< GUM_SCALAR >::stringBag__.

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

430  {
431  if (state() != factory_state::PARENTS) {
432  illegalStateError__("addParent");
433  } else {
434  checkVariableName__(var);
435  stringBag__.push_back(var);
436  }
437  }
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:
+ Here is the caller 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 287 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::illegalStateError__(), gum::BayesNetFactory< GUM_SCALAR >::state(), gum::BayesNetFactory< GUM_SCALAR >::stringBag__, gum::to_string(), and gum::IBayesNetFactory::VARIABLE.

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

287  {
288  if (state() != factory_state::VARIABLE) {
289  illegalStateError__("addTick");
290  } else {
291  stringBag__.push_back(std::to_string(tick));
292  }
293  }
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:499
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:

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

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

101  {
102  return bn__;
103  }
BayesNet< GUM_SCALAR > * bn__
The constructed BayesNet.

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

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

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

951  {
952  for (size_t i = 3; i < stringBag__.size(); ++i) {
953  if (mod == stringBag__[i]) {
954  GUM_ERROR(DuplicateElement, "Label already used: " << mod);
955  }
956  }
957  }
std::vector< std::string > stringBag__
Just to keep track of strings between two start/end calls.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the caller graph for this function:

◆ checkVariableModality__()

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

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

Definition at line 936 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::bn__, gum::BayesNetFactory< GUM_SCALAR >::checkVariableName__(), gum::DiscreteVariable::domainSize(), GUM_ERROR, gum::DiscreteVariable::label(), and gum::BayesNetFactory< GUM_SCALAR >::varNameMap__.

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

937  {
938  checkVariableName__(name);
939  const DiscreteVariable& var = bn__->variable(varNameMap__[name]);
940 
941  for (Idx i = 0; i < var.domainSize(); ++i) {
942  if (mod == var.label(i)) { return i; }
943  }
944 
945  GUM_ERROR(NotFound, mod);
946  }
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:55
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ checkVariableName__()

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

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

Definition at line 929 of file BayesNetFactory_tpl.h.

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

Referenced by gum::BayesNetFactory< GUM_SCALAR >::addParent(), gum::BayesNetFactory< GUM_SCALAR >::checkVariableModality__(), gum::BayesNetFactory< GUM_SCALAR >::setCPTAndParents__(), 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().

929  {
930  if (!varNameMap__.exists(name)) { GUM_ERROR(NotFound, name); }
931  }
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:55
+ Here is the call graph for this function:
+ Here is the caller 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 143 of file BayesNetFactory_tpl.h.

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

143  {
144  return bn__->cpt(n).domainSize();
145  }
BayesNet< GUM_SCALAR > * bn__
The constructed BayesNet.

◆ endFactorizedEntry()

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

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

Implements gum::IBayesNetFactory.

Definition at line 654 of file BayesNetFactory_tpl.h.

References gum::IBayesNetFactory::FACT_ENTRY, gum::BayesNetFactory< GUM_SCALAR >::illegalStateError__(), gum::BayesNetFactory< GUM_SCALAR >::parents__, gum::BayesNetFactory< GUM_SCALAR >::state(), and gum::BayesNetFactory< GUM_SCALAR >::states__.

654  {
655  if (state() != factory_state::FACT_ENTRY) {
656  illegalStateError__("endFactorizedEntry");
657  } else {
658  delete parents__;
659  parents__ = 0;
660  states__.pop_back();
661  }
662  }
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 781 of file BayesNetFactory_tpl.h.

References gum::IBayesNetFactory::FACT_CPT, gum::BayesNetFactory< GUM_SCALAR >::illegalStateError__(), gum::BayesNetFactory< GUM_SCALAR >::resetParts__(), gum::BayesNetFactory< GUM_SCALAR >::state(), and gum::BayesNetFactory< GUM_SCALAR >::states__.

781  {
782  if (state() != factory_state::FACT_CPT) {
783  illegalStateError__("endFactorizedProbabilityDeclaration");
784  } else {
785  resetParts__();
786  states__.pop_back();
787  }
788  }
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::BayesNetFactory< GUM_SCALAR >::illegalStateError__(), gum::IBayesNetFactory::NETWORK, gum::BayesNetFactory< GUM_SCALAR >::state(), gum::BayesNetFactory< GUM_SCALAR >::states__, and VERBOSITY.

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

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:
+ Here is the caller graph for this function:

◆ endParentsDeclaration()

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

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

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

Implements gum::IBayesNetFactory.

Definition at line 444 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::bn__, gum::BayesNetFactory< GUM_SCALAR >::illegalStateError__(), gum::IBayesNetFactory::PARENTS, gum::BayesNetFactory< GUM_SCALAR >::resetParts__(), gum::BayesNetFactory< GUM_SCALAR >::state(), gum::BayesNetFactory< GUM_SCALAR >::states__, gum::BayesNetFactory< GUM_SCALAR >::stringBag__, gum::BayesNetFactory< GUM_SCALAR >::varNameMap__, and VERBOSITY.

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

444  {
445  if (state() != factory_state::PARENTS) {
446  illegalStateError__("endParentsDeclaration");
447  } else {
449 
450  // PLEASE NOTE THAT THE ORDER IS INVERSE
451 
452  for (size_t i = stringBag__.size() - 1; i > 0; --i) {
453  bn__->addArc(varNameMap__[stringBag__[i]], id);
454  VERBOSITY(" adding parent " << stringBag__[i] << " for "
455  << stringBag__[0]);
456  }
457 
458  resetParts__();
459 
460  states__.pop_back();
461  }
462 
463  VERBOSITY("end of parents for " << stringBag__[0]);
464  }
#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:98
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ endRawProbabilityDeclaration()

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

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

Implements gum::IBayesNetFactory.

Definition at line 614 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::illegalStateError__(), gum::IBayesNetFactory::RAW_CPT, gum::BayesNetFactory< GUM_SCALAR >::resetParts__(), gum::BayesNetFactory< GUM_SCALAR >::state(), gum::BayesNetFactory< GUM_SCALAR >::states__, gum::BayesNetFactory< GUM_SCALAR >::stringBag__, and VERBOSITY.

614  {
615  if (state() != factory_state::RAW_CPT) {
616  illegalStateError__("endRawProbabilityDeclaration");
617  } else {
618  resetParts__();
619  states__.pop_back();
620  }
621 
622  VERBOSITY(" cpt ending for " << stringBag__[0]);
623  }
#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 328 of file BayesNetFactory_tpl.h.

References gum::LabelizedVariable::addLabel(), gum::DiscretizedVariable< T_TICKS >::addTick(), gum::BayesNetFactory< GUM_SCALAR >::bar_flag__, gum::BayesNetFactory< GUM_SCALAR >::bn__, gum::BayesNetFactory< GUM_SCALAR >::foo_flag__, GUM_ERROR, gum::BayesNetFactory< GUM_SCALAR >::illegalStateError__(), gum::BayesNetFactory< GUM_SCALAR >::impl__, gum::HashTable< Key, Val, Alloc >::insert(), gum::Variable::name(), gum::BayesNetFactory< GUM_SCALAR >::resetParts__(), gum::BayesNetFactory< GUM_SCALAR >::state(), gum::BayesNetFactory< GUM_SCALAR >::states__, gum::BayesNetFactory< GUM_SCALAR >::stringBag__, gum::IBayesNetFactory::VARIABLE, gum::BayesNetFactory< GUM_SCALAR >::varNameMap__, and VERBOSITY.

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

328  {
329  if (state() != factory_state::VARIABLE) {
330  illegalStateError__("endVariableDeclaration");
331  } else if (foo_flag__ && (stringBag__.size() > 4)) {
332  DiscreteVariable* var = nullptr;
333 
334  // if the current variable is a LabelizedVariable
335  if (stringBag__[2] == "L") {
336  LabelizedVariable* l = new LabelizedVariable(
337  stringBag__[0], (bar_flag__) ? stringBag__[1] : "", 0);
338 
339  for (size_t i = 3; i < stringBag__.size(); ++i) {
340  l->addLabel(stringBag__[i]);
341  }
342 
343  var = l;
344  // if the current variable is a RangeVariable
345  } else if (stringBag__[2] == "R") {
346  RangeVariable* r = new RangeVariable(stringBag__[0],
347  (bar_flag__) ? stringBag__[1] : "",
348  std::stol(stringBag__[3]),
349  std::stol(stringBag__[4]));
350 
351  var = r;
352  // if the current variable is a DiscretizedVariable
353  } else if (stringBag__[2] == "D") {
354  DiscretizedVariable< GUM_SCALAR >* d =
355  new DiscretizedVariable< GUM_SCALAR >(
356  stringBag__[0], (bar_flag__) ? stringBag__[1] : "");
357 
358  for (size_t i = 3; i < stringBag__.size(); ++i) {
359  d->addTick(std::stof(stringBag__[i]));
360  }
361 
362  var = d;
363  }
364 
365  if (impl__ != 0) {
366  varNameMap__.insert(var->name(), bn__->add(*var, impl__));
367  impl__ = 0;
368  } else {
369  varNameMap__.insert(var->name(), bn__->add(*var));
370  }
371 
372  NodeId retVal = varNameMap__[var->name()];
373 
374  delete var;
375 
376  resetParts__();
377  states__.pop_back();
378 
379  VERBOSITY(" variable " << var->name() << " OK");
380  return retVal;
381  } else {
382  std::stringstream msg;
383  msg << "Not enough modalities (";
384 
385  if (stringBag__.size() > 3) {
386  msg << stringBag__.size() - 3;
387  } else {
388  msg << 0;
389  }
390 
391  msg << ") declared for variable ";
392 
393  if (foo_flag__) {
394  msg << stringBag__[0];
395  } else {
396  msg << "unknown";
397  }
398 
399  resetParts__();
400 
401  states__.pop_back();
402  GUM_ERROR(OperationNotAllowed, msg.str());
403  }
404 
405  // For noisy compilers
406  return 0;
407  }
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:98
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fillProbaWithValuesTable__() [1/2]

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

Fill a potential from a raw CPT.

Definition at line 506 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::bn__, gum::Instantiation::chgVal(), gum::BayesNetFactory< GUM_SCALAR >::increment__(), gum::List< Val, Alloc >::pushBack(), gum::MultiDimDecorator< GUM_SCALAR >::set(), gum::List< Val, Alloc >::size(), gum::BayesNetFactory< GUM_SCALAR >::stringBag__, and gum::BayesNetFactory< GUM_SCALAR >::varNameMap__.

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

508  {
509  const Potential< GUM_SCALAR >& table = bn__->cpt(varNameMap__[stringBag__[0]]);
510  Instantiation cptInst(table);
511 
512  List< const DiscreteVariable* > varList;
513 
514  for (size_t i = 0; i < variables.size(); ++i) {
515  varList.pushBack(&(bn__->variable(varNameMap__[variables[i]])));
516  }
517 
518  // varList.pushFront(&(bn__->variable(varNameMap__[stringBag__[0]])));
519 
520  Idx nbrVar = varList.size();
521 
522  std::vector< Idx > modCounter;
523 
524  // initializing the array
525  for (NodeId i = 0; i < nbrVar; i++) {
526  modCounter.push_back(Idx(0));
527  }
528 
529  Idx j = 0;
530 
531  do {
532  for (NodeId i = 0; i < nbrVar; i++) {
533  cptInst.chgVal(*(varList[i]), modCounter[i]);
534  }
535 
536  if (j < rawTable.size()) {
537  table.set(cptInst, (GUM_SCALAR)rawTable[j]);
538  } else {
539  table.set(cptInst, (GUM_SCALAR)0);
540  }
541 
542  j++;
543  } while (increment__(modCounter, varList));
544  }
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:53
Size NodeId
Type for node ids.
Definition: graphElements.h:98
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fillProbaWithValuesTable__() [2/2]

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

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

Definition at line 557 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::bn__, gum::Instantiation::end(), gum::Instantiation::incNotVar(), gum::Instantiation::incVar(), gum::MultiDimDecorator< GUM_SCALAR >::set(), gum::Instantiation::setFirstNotVar(), gum::Instantiation::setFirstVar(), gum::BayesNetFactory< GUM_SCALAR >::stringBag__, gum::Instantiation::unsetEnd(), gum::MultiDimDecorator< GUM_SCALAR >::variable(), and gum::BayesNetFactory< GUM_SCALAR >::varNameMap__.

558  {
559  const Potential< GUM_SCALAR >& table = bn__->cpt(varNameMap__[stringBag__[0]]);
560 
561  Instantiation cptInst(table);
562 
563  // the main loop is on the first variables. The others are in the right
564  // order.
565  const DiscreteVariable& first = table.variable(0);
566  Idx j = 0;
567 
568  for (cptInst.setFirstVar(first); !cptInst.end(); cptInst.incVar(first)) {
569  for (cptInst.setFirstNotVar(first); !cptInst.end(); cptInst.incNotVar(first))
570  table.set(cptInst,
571  (j < rawTable.size()) ? (GUM_SCALAR)rawTable[j++]
572  : (GUM_SCALAR)0);
573 
574  cptInst.unsetEnd();
575  }
576  }
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 876 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 >::addMax(), gum::BayesNetFactory< GUM_SCALAR >::addMin(), gum::BayesNetFactory< GUM_SCALAR >::addModality(), gum::BayesNetFactory< GUM_SCALAR >::addParent(), gum::BayesNetFactory< GUM_SCALAR >::addTick(), 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(), gum::BayesNetFactory< GUM_SCALAR >::variableName(), and gum::BayesNetFactory< GUM_SCALAR >::variableType().

876  {
877  std::string msg = "Illegal state call (";
878  msg += s;
879  msg += ") in state ";
880 
881  switch (state()) {
882  case factory_state::NONE: {
883  msg += "NONE";
884  break;
885  }
886 
887  case factory_state::NETWORK: {
888  msg += "NETWORK";
889  break;
890  }
891 
893  msg += "VARIABLE";
894  break;
895  }
896 
897  case factory_state::PARENTS: {
898  msg += "PARENTS";
899  break;
900  }
901 
902  case factory_state::RAW_CPT: {
903  msg += "RAW_CPT";
904  break;
905  }
906 
908  msg += "FACT_CPT";
909  break;
910  }
911 
913  msg += "FACT_ENTRY";
914  break;
915  }
916 
917  default: {
918  msg += "Unknown state";
919  }
920  }
921 
922  GUM_ERROR(OperationNotAllowed, msg);
923  }
factory_state state() const final
Returns the current state of the factory.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ increment__()

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

Increment a modality counter for the fillProbaWithValuesTable__ method.

Definition at line 579 of file BayesNetFactory_tpl.h.

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

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

581  {
582  bool last = true;
583 
584  for (NodeId j = 0; j < modCounter.size(); j++) {
585  last = (modCounter[j] == (varList[j]->domainSize() - 1)) && last;
586 
587  if (!last) break;
588  }
589 
590  if (last) { return false; }
591 
592  bool add = false;
593 
594  NodeId i = NodeId(varList.size() - 1);
595 
596  do {
597  if (modCounter[i] == (varList[i]->domainSize() - 1)) {
598  modCounter[i] = 0;
599  add = true;
600  } else {
601  modCounter[i] += 1;
602  add = false;
603  }
604 
605  i--;
606  } while (add);
607 
608  return true;
609  }
Size NodeId
Type for node ids.
Definition: graphElements.h:98
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isVerbose()

bool gum::IBayesNetFactory::isVerbose ( )
inlineinherited

Definition at line 82 of file IBayesNetFactory.h.

References gum::IBayesNetFactory::addMax(), gum::IBayesNetFactory::addMin(), 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(), gum::IBayesNetFactory::variableType(), gum::IBayesNetFactory::varInBN(), and 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 866 of file BayesNetFactory_tpl.h.

References GUM_ERROR.

867  {
868  GUM_ERROR(OperationNotAllowed, "Illegal!");
869  // For noisy compilers
870  return *this;
871  }
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55

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

497  {
498  if (state() != factory_state::RAW_CPT) {
499  illegalStateError__("rawConditionalTable");
500  } else {
501  fillProbaWithValuesTable__(variables, rawTable);
502  }
503  }
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 547 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().

548  {
549  if (state() != factory_state::RAW_CPT) {
550  illegalStateError__("rawConditionalTable");
551  } else {
552  fillProbaWithValuesTable__(rawTable);
553  }
554  }
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 980 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().

980  {
981  foo_flag__ = false;
982  bar_flag__ = false;
983  stringBag__.clear();
984  }
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 caller graph for this function:

◆ resetVerbose()

void gum::IBayesNetFactory::resetVerbose ( )
inlineinherited

Definition at line 80 of file IBayesNetFactory.h.

References gum::IBayesNetFactory::verbose__.

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

80 { verbose__ = false; };
+ Here is the caller graph for this function:

◆ setCPTAndParents__()

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

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

Definition at line 962 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::bn__, gum::BayesNetFactory< GUM_SCALAR >::checkVariableName__(), gum::Variable::name(), gum::MultiDimDecorator< GUM_SCALAR >::variablesSequence(), and gum::BayesNetFactory< GUM_SCALAR >::varNameMap__.

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

963  {
964  NodeId varId = varNameMap__[var.name()];
965  bn__->dag_.eraseParents(varId);
966 
967  for (auto v: table->variablesSequence()) {
968  if (v != (&var)) {
969  checkVariableName__(v->name());
970  bn__->dag_.addArc(varNameMap__[v->name()], varId);
971  }
972  }
973 
974  // CPT are created when a variable is added.
975  bn__->unsafeChangePotential_(varId, table);
976  }
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:98
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setParentModality()

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

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

Implements gum::IBayesNetFactory.

Definition at line 667 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::bn__, gum::BayesNetFactory< GUM_SCALAR >::checkVariableModality__(), gum::BayesNetFactory< GUM_SCALAR >::checkVariableName__(), gum::Instantiation::chgVal(), gum::IBayesNetFactory::FACT_ENTRY, gum::BayesNetFactory< GUM_SCALAR >::illegalStateError__(), gum::BayesNetFactory< GUM_SCALAR >::parents__, gum::BayesNetFactory< GUM_SCALAR >::state(), and gum::BayesNetFactory< GUM_SCALAR >::varNameMap__.

668  {
669  if (state() != factory_state::FACT_ENTRY) {
670  illegalStateError__("string");
671  } else {
672  checkVariableName__(parent);
673  Idx id = checkVariableModality__(parent, modality);
674  (*parents__) << bn__->variable(varNameMap__[parent]);
675  parents__->chgVal(bn__->variable(varNameMap__[parent]), id);
676  }
677  }
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 803 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::bn__, gum::BayesNetFactory< GUM_SCALAR >::checkVariableName__(), GUM_ERROR, gum::BayesNetFactory< GUM_SCALAR >::illegalStateError__(), gum::HashTable< Key, Val, Alloc >::insert(), gum::Variable::name(), gum::IBayesNetFactory::NONE, gum::BayesNetFactory< GUM_SCALAR >::state(), and gum::BayesNetFactory< GUM_SCALAR >::varNameMap__.

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

803  {
804  if ((state() != factory_state::NONE)) {
805  illegalStateError__("setVariable");
806  } else {
807  try {
808  checkVariableName__(var.name());
809  GUM_ERROR(DuplicateElement, "Name already used: " << var.name());
810  } catch (NotFound&) {
811  // The var name is unused
812  varNameMap__.insert(var.name(), bn__->add(var));
813  }
814  }
815  }
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:55
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setVariableCPT()

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

Define a variable's CPT.

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

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

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

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

Implements gum::IBayesNetFactory.

Definition at line 835 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::bn__, gum::BayesNetFactory< GUM_SCALAR >::checkVariableName__(), GUM_ERROR, gum::BayesNetFactory< GUM_SCALAR >::illegalStateError__(), gum::IBayesNetFactory::NONE, gum::BayesNetFactory< GUM_SCALAR >::setCPTAndParents__(), gum::BayesNetFactory< GUM_SCALAR >::state(), and gum::BayesNetFactory< GUM_SCALAR >::varNameMap__.

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

836  {
837  auto pot = dynamic_cast< Potential< GUM_SCALAR >* >(table);
838 
839  if (state() != factory_state::NONE) {
840  illegalStateError__("setVariableCPT");
841  } else {
842  checkVariableName__(varName);
843  const DiscreteVariable& var = bn__->variable(varNameMap__[varName]);
844  NodeId varId = varNameMap__[varName];
845  // If we have to change the structure of the BayesNet, then we call a sub
846  // method.
847 
848  if (redefineParents) {
849  setCPTAndParents__(var, pot);
850  } else if (pot->contains(var)) {
851  for (auto node: bn__->parents(varId)) {
852  if (!pot->contains(bn__->variable(node))) {
853  GUM_ERROR(OperationNotAllowed,
854  "The CPT is not valid in the current BayesNet.");
855  }
856  }
857 
858  // CPT are created when a variable is added.
859  bn__->unsafeChangePotential_(varId, pot);
860  }
861  }
862  }
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:98
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setVariableCPTImplementation()

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

Defines the implementation to use for var's Potential.

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

Implements gum::IBayesNetFactory.

Definition at line 306 of file BayesNetFactory_tpl.h.

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

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

307  {
308  MultiDimImplementation< GUM_SCALAR >* impl =
309  dynamic_cast< MultiDimImplementation< GUM_SCALAR >* >(adressable);
310 
311  if (state() != factory_state::VARIABLE) {
312  illegalStateError__("setVariableCPTImplementation");
313  } else {
314  if (impl == 0) {
315  GUM_ERROR(OperationNotAllowed,
316  "An implementation for this variable is already "
317  "defined.");
318  } else if (impl->nbrDim() > 0) {
319  GUM_ERROR(OperationNotAllowed, "This implementation is not empty.");
320  }
321 
322  impl__ = impl;
323  }
324  }
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:55
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setVariableValues()

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

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

Implements gum::IBayesNetFactory.

Definition at line 758 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::bn__, gum::DiscreteVariable::domainSize(), gum::IBayesNetFactory::FACT_ENTRY, GUM_ERROR, gum::BayesNetFactory< GUM_SCALAR >::illegalStateError__(), gum::Variable::name(), gum::BayesNetFactory< GUM_SCALAR >::setVariableValuesUnchecked(), gum::BayesNetFactory< GUM_SCALAR >::state(), gum::BayesNetFactory< GUM_SCALAR >::stringBag__, and gum::BayesNetFactory< GUM_SCALAR >::varNameMap__.

759  {
760  if (state() != factory_state::FACT_ENTRY) {
761  illegalStateError__("setVariableValues");
762  } else {
763  const DiscreteVariable& var = bn__->variable(varNameMap__[stringBag__[0]]);
764  // Checking consistency between values and var.
765 
766  if (values.size() != var.domainSize()) {
767  GUM_ERROR(OperationNotAllowed,
768  var.name()
769  << " : invalid number of modalities: found " << values.size()
770  << " while needed " << var.domainSize());
771  }
772 
774  }
775  }
factory_state state() const final
Returns the current state of the factory.
void illegalStateError__(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
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:55
+ 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 707 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::bn__, gum::Instantiation::contains(), gum::Instantiation::domainSize(), gum::Instantiation::end(), gum::IBayesNetFactory::FACT_ENTRY, gum::BayesNetFactory< GUM_SCALAR >::illegalStateError__(), gum::Instantiation::incIn(), gum::Instantiation::incOut(), gum::Variable::name(), gum::BayesNetFactory< GUM_SCALAR >::parents__, gum::Instantiation::setFirst(), gum::Instantiation::setFirstIn(), gum::Instantiation::setFirstOut(), gum::Instantiation::setVals(), gum::BayesNetFactory< GUM_SCALAR >::state(), gum::BayesNetFactory< GUM_SCALAR >::stringBag__, gum::Instantiation::val(), and gum::BayesNetFactory< GUM_SCALAR >::varNameMap__.

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

708  {
709  if (state() != factory_state::FACT_ENTRY) {
710  illegalStateError__("setVariableValues");
711  } else {
712  const DiscreteVariable& var = bn__->variable(varNameMap__[stringBag__[0]]);
713  NodeId varId = varNameMap__[stringBag__[0]];
714 
715  if (parents__->domainSize() > 0) {
716  Instantiation inst(bn__->cpt(varNameMap__[var.name()]));
717  inst.setVals(*parents__);
718  // Creating an instantiation containing all the variables not ins
719  // parents__.
720  Instantiation inst_default;
721  inst_default << var;
722 
723  for (auto node: bn__->parents(varId)) {
724  if (!parents__->contains(bn__->variable(node))) {
725  inst_default << bn__->variable(node);
726  }
727  }
728 
729  // Filling the variable's table.
730  for (inst.setFirstIn(inst_default); !inst.end();
731  inst.incIn(inst_default)) {
732  (bn__->cpt(varId))
733  .set(inst,
734  inst.val(var) < values.size() ? (GUM_SCALAR)values[inst.val(var)]
735  : (GUM_SCALAR)0);
736  }
737  } else {
738  Instantiation inst(bn__->cpt(varNameMap__[var.name()]));
739  Instantiation var_inst;
740  var_inst << var;
741 
742  for (var_inst.setFirst(); !var_inst.end(); ++var_inst) {
743  inst.setVals(var_inst);
744 
745  for (inst.setFirstOut(var_inst); !inst.end(); inst.incOut(var_inst)) {
746  (bn__->cpt(varId))
747  .set(inst,
748  inst.val(var) < values.size()
749  ? (GUM_SCALAR)values[inst.val(var)]
750  : (GUM_SCALAR)0);
751  }
752  }
753  }
754  }
755  }
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:98
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setVerbose()

void gum::IBayesNetFactory::setVerbose ( )
inlineinherited

Definition at line 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 642 of file BayesNetFactory_tpl.h.

References gum::IBayesNetFactory::FACT_CPT, gum::IBayesNetFactory::FACT_ENTRY, gum::BayesNetFactory< GUM_SCALAR >::illegalStateError__(), gum::BayesNetFactory< GUM_SCALAR >::parents__, gum::BayesNetFactory< GUM_SCALAR >::state(), and gum::BayesNetFactory< GUM_SCALAR >::states__.

642  {
643  if (state() != factory_state::FACT_CPT) {
644  illegalStateError__("startFactorizedEntry");
645  } else {
646  parents__ = new Instantiation();
648  }
649  }
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 627 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::checkVariableName__(), gum::IBayesNetFactory::FACT_CPT, gum::BayesNetFactory< GUM_SCALAR >::illegalStateError__(), gum::IBayesNetFactory::NONE, gum::BayesNetFactory< GUM_SCALAR >::state(), gum::BayesNetFactory< GUM_SCALAR >::states__, and gum::BayesNetFactory< GUM_SCALAR >::stringBag__.

628  {
629  if (state() != factory_state::NONE) {
630  illegalStateError__("startFactorizedProbabilityDeclaration");
631  } else {
632  checkVariableName__(var);
633  std::vector< std::string >::iterator iter = stringBag__.begin();
634  stringBag__.insert(iter, var);
636  }
637  }
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 149 of file BayesNetFactory_tpl.h.

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

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

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

◆ startParentsDeclaration()

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

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

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

Implements gum::IBayesNetFactory.

Definition at line 412 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::checkVariableName__(), gum::BayesNetFactory< GUM_SCALAR >::illegalStateError__(), gum::IBayesNetFactory::NONE, gum::IBayesNetFactory::PARENTS, gum::BayesNetFactory< GUM_SCALAR >::state(), gum::BayesNetFactory< GUM_SCALAR >::states__, gum::BayesNetFactory< GUM_SCALAR >::stringBag__, and VERBOSITY.

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

413  {
414  if (state() != factory_state::NONE) {
415  illegalStateError__("startParentsDeclaration");
416  } else {
417  checkVariableName__(var);
418  std::vector< std::string >::iterator iter = stringBag__.begin();
419  stringBag__.insert(iter, var);
420  states__.push_back(factory_state::PARENTS);
421  }
422 
423  VERBOSITY("starting parents for " << var);
424  }
#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:
+ Here is the caller graph for this function:

◆ startRawProbabilityDeclaration()

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

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

Parameters
varThe concerned variable's name.

Implements gum::IBayesNetFactory.

Definition at line 470 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::checkVariableName__(), gum::BayesNetFactory< GUM_SCALAR >::illegalStateError__(), gum::IBayesNetFactory::NONE, gum::IBayesNetFactory::RAW_CPT, gum::BayesNetFactory< GUM_SCALAR >::state(), gum::BayesNetFactory< GUM_SCALAR >::states__, gum::BayesNetFactory< GUM_SCALAR >::stringBag__, and VERBOSITY.

471  {
472  if (state() != factory_state::NONE) {
473  illegalStateError__("startRawProbabilityDeclaration");
474  } else {
475  checkVariableName__(var);
476  stringBag__.push_back(var);
477  states__.push_back(factory_state::RAW_CPT);
478  }
479 
480  VERBOSITY(" cpt starting for " << var);
481  }
#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::BayesNetFactory< GUM_SCALAR >::illegalStateError__(), gum::IBayesNetFactory::NONE, gum::BayesNetFactory< GUM_SCALAR >::state(), gum::BayesNetFactory< GUM_SCALAR >::states__, gum::BayesNetFactory< GUM_SCALAR >::stringBag__, gum::IBayesNetFactory::VARIABLE, and VERBOSITY.

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

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:
+ Here is the caller graph for this function:

◆ state()

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

Returns the current state of the factory.

Implements gum::IBayesNetFactory.

Definition at line 114 of file BayesNetFactory_tpl.h.

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

Referenced by gum::BayesNetFactory< GUM_SCALAR >::addMax(), gum::BayesNetFactory< GUM_SCALAR >::addMin(), gum::BayesNetFactory< GUM_SCALAR >::addModality(), gum::BayesNetFactory< GUM_SCALAR >::addParent(), gum::BayesNetFactory< GUM_SCALAR >::addTick(), 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 >::illegalStateError__(), 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(), gum::BayesNetFactory< GUM_SCALAR >::variableName(), and gum::BayesNetFactory< GUM_SCALAR >::variableType().

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

◆ variable()

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

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

Exceptions
NotFoundRaised if no variable matches the name.

Definition at line 133 of file BayesNetFactory_tpl.h.

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

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

133  {
134  try {
135  return bn__->variable(variableId(name));
136  } catch (NotFound&) { GUM_ERROR(NotFound, name); }
137  }
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:55
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ variableDescription()

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

Tells the factory the current variable's description.

Implements gum::IBayesNetFactory.

Definition at line 214 of file BayesNetFactory_tpl.h.

References gum::BayesNetFactory< GUM_SCALAR >::bar_flag__, gum::BayesNetFactory< GUM_SCALAR >::illegalStateError__(), gum::BayesNetFactory< GUM_SCALAR >::state(), gum::BayesNetFactory< GUM_SCALAR >::stringBag__, and gum::IBayesNetFactory::VARIABLE.

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

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

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

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

◆ variableName()

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

Tells the factory the current variable's name.

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

Implements gum::IBayesNetFactory.

Definition at line 197 of file BayesNetFactory_tpl.h.

References gum::HashTable< Key, Val, Alloc >::exists(), gum::BayesNetFactory< GUM_SCALAR >::foo_flag__, GUM_ERROR, gum::BayesNetFactory< GUM_SCALAR >::illegalStateError__(), gum::BayesNetFactory< GUM_SCALAR >::state(), gum::BayesNetFactory< GUM_SCALAR >::stringBag__, gum::IBayesNetFactory::VARIABLE, gum::BayesNetFactory< GUM_SCALAR >::varNameMap__, and VERBOSITY.

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

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:55
+ Here is the call graph for this function:
+ Here is the caller 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::Continuous, gum::Discretized, GUM_ERROR, gum::BayesNetFactory< GUM_SCALAR >::illegalStateError__(), gum::Labelized, gum::Range, gum::BayesNetFactory< GUM_SCALAR >::state(), gum::BayesNetFactory< GUM_SCALAR >::stringBag__, and gum::IBayesNetFactory::VARIABLE.

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

230  {
231  if (state() != factory_state::VARIABLE) {
232  illegalStateError__("variableType");
233  } else {
234  switch (type) {
235  case VarType::Discretized: stringBag__[2] = "D"; break;
236  case VarType::Range: stringBag__[2] = "R"; break;
237  case VarType::Continuous:
238  GUM_ERROR(OperationNotAllowed,
239  "Continuous variable (" + stringBag__[0]
240  + ") are not supported in bayesian networks.");
241  case VarType::Labelized: stringBag__[2] = "L"; break;
242  }
243  }
244  }
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:55
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ varInBN()

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

short-cut accessor for a DiscreveVariable in the BN

Implements gum::IBayesNetFactory.

Definition at line 107 of file BayesNetFactory_tpl.h.

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

107  {
108  return bn__->variable(id);
109  }
BayesNet< GUM_SCALAR > * bn__
The constructed BayesNet.

Member Data Documentation

◆ bar_flag__

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

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

Definition at line 407 of file BayesNetFactory.h.

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

◆ bn__

◆ foo_flag__

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

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

Definition at line 403 of file BayesNetFactory.h.

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

◆ impl__

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

Implementation of variable between two startVariableDeclaration/endVariableDeclaration calls.

Definition at line 417 of file BayesNetFactory.h.

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

◆ parents__

◆ states__

◆ stringBag__

◆ varNameMap__


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