aGrUM 3.1.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 Types

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

Public Member Functions

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

(NONE, NETWORK)

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

Private Member Functions

BayesNetFactory< GUM_SCALAR > & operator= (const BayesNetFactory< GUM_SCALAR > &source)=delete
 Copy operator is illegal, use only copy constructor.
void _illegalStateError_ (std::string_view s)
 Raise an OperationNotAllowed with the message "Illegal state.".
void _checkVariableName_ (std::string_view name) const
 Check if a variable with the given name exists, if not raise an NotFound exception.
Idx _checkVariableModality_ (std::string_view name, std::string_view mod)
 Check if var exists and if mod is one of it's modality, if not raise an NotFound exception.
void _checkModalityInBag_ (std::string_view mod)
 Check if in stringBag there is no other modality with the same name.
void _setCPTAndParents_ (const DiscreteVariable &var, Tensor< GUM_SCALAR > *table)
 Sub method of setVariableCPT() which redefine the BayesNet's DAG with respect to table.
void _resetParts_ ()
 Reset the different parts used to constructed the BayesNet.
void _fillProbaWithValuesTable_ (const std::vector< std::string > &variables, const std::vector< float > &rawTable)
 Fill a tensor from a raw CPT.
void _fillProbaWithValuesTable_ (const std::vector< float > &rawTable)
 Fill a tensor from a raw CPT.(using the canonical order of vars).
bool _increment_ (std::vector< gum::Idx > &modCounter, List< const DiscreteVariable * > &varList) const
 Increment a modality counter for the fillProbaWithValuesTable method.

Private Attributes

std::vector< factory_state_states_
 State stack.
BayesNet< GUM_SCALAR > * _bn_
 The constructed BayesNet.
HashTable< std::string, NodeId_varNameMap_
 Mapping between a declared variable's name and it's node id.
bool _verbose_
Current constructed BayesNet's parts.
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.
Instantiation_parents_
 Used when a factorized CPT is built.
MultiDimImplementation< GUM_SCALAR > * _impl_
 Implementation of variable between two startVariableDeclaration/endVariableDeclaration calls.

Detailed Description

template<GUM_Numeric 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 83 of file BayesNetFactory.h.

Member Enumeration Documentation

◆ factory_state

enum class 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 81 of file IBayesNetFactory.h.

81 : char {
82 NONE,
83 NETWORK,
85 PARENTS,
86 RAW_CPT,
87 FACT_CPT,
88 FACT_ENTRY
89 };

Constructor & Destructor Documentation

◆ BayesNetFactory() [1/2]

template<GUM_Numeric GUM_SCALAR>
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 61 of file BayesNetFactory_tpl.h.

61 :
62 _parents_(nullptr), _impl_(0), _bn_(bn) {
65
66 for (auto node: bn->nodes()) {
67 if (_varNameMap_.exists(bn->variable(node).name()))
68 GUM_ERROR(DuplicateElement, "Name already used: " << bn->variable(node).name())
69
71 }
72
74 }
A factory class to ease BayesNet construction.
std::vector< factory_state > _states_
State stack.
HashTable< std::string, NodeId > _varNameMap_
Mapping between a declared variable's name and it's node id.
BayesNet< GUM_SCALAR > * _bn_
The constructed BayesNet.
const DiscreteVariable & variable(std::string_view name) const
Returns a constant reference on a variable given it's name.
Instantiation * _parents_
Used when a factorized CPT is built.
MultiDimImplementation< GUM_SCALAR > * _impl_
Implementation of variable between two startVariableDeclaration/endVariableDeclaration calls.
BayesNetFactory(BayesNet< GUM_SCALAR > *bn)
Use this constructor if you want to use an already created BayesNet.
const std::string & name() const
returns the name of the variable
#define GUM_ERROR(type, msg)
Definition exceptions.h:76

References BayesNetFactory(), _bn_, _impl_, _parents_, _states_, _varNameMap_, GUM_ERROR, gum::IBayesNetFactory::NONE, and gum::IBayesNetFactory::resetVerbose().

Referenced by BayesNetFactory(), BayesNetFactory(), ~BayesNetFactory(), and operator=().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ BayesNetFactory() [2/2]

template<GUM_Numeric GUM_SCALAR>
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 79 of file BayesNetFactory_tpl.h.

79 :
80 _parents_(nullptr), _impl_(nullptr), _bn_(nullptr) {
82
84 GUM_ERROR(OperationNotAllowed, "Illegal state to proceed make a copy.")
85 } else {
88 }
89 }
factory_state state() const final
Returns the current state of the factory.

References BayesNetFactory(), _bn_, _impl_, _parents_, _states_, GUM_ERROR, gum::IBayesNetFactory::NONE, and state().

Here is the call graph for this function:

◆ ~BayesNetFactory()

template<GUM_Numeric GUM_SCALAR>
gum::BayesNetFactory< GUM_SCALAR >::~BayesNetFactory ( )
override

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

93 {
95
96 if (_parents_ != nullptr) delete _parents_;
97
98 if (_impl_ != nullptr) {
99 //@todo better than throwing an exception from inside a destructor but
100 // still ...
101 std::cerr << "[BN factory] Implementation defined for a variable but not used. "
102 "You should call endVariableDeclaration() before "
103 "deleting me."
104 << std::endl;
105 exit(1);
106 }
107 }

References BayesNetFactory(), _impl_, and _parents_.

Here is the call graph for this function:

Member Function Documentation

◆ _checkModalityInBag_()

template<GUM_Numeric GUM_SCALAR>
void gum::BayesNetFactory< GUM_SCALAR >::_checkModalityInBag_ ( std::string_view mod)
private

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

Definition at line 898 of file BayesNetFactory_tpl.h.

898 {
899 for (size_t i = 3; i < _stringBag_.size(); ++i) {
900 if (mod == _stringBag_[i]) { GUM_ERROR(DuplicateElement, "Label already used: " << mod) }
901 }
902 }
std::vector< std::string > _stringBag_
Just to keep track of strings between two start/end calls.

References _stringBag_, and GUM_ERROR.

Referenced by addModality().

Here is the caller graph for this function:

◆ _checkVariableModality_()

template<GUM_Numeric GUM_SCALAR>
Idx gum::BayesNetFactory< GUM_SCALAR >::_checkVariableModality_ ( std::string_view name,
std::string_view mod )
private

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

Definition at line 883 of file BayesNetFactory_tpl.h.

884 {
885 auto p = _varNameMap_.tryGet(name);
886 if (!p) { GUM_ERROR(NotFound, name) }
887 const DiscreteVariable& var = _bn_->variable(*p);
888
889 for (Idx i = 0; i < var.domainSize(); ++i) {
890 if (mod == var.label(i)) { return i; }
891 }
892
894 }

References _bn_, _varNameMap_, gum::DiscreteVariable::domainSize(), GUM_ERROR, and gum::DiscreteVariable::label().

Referenced by setParentModality().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _checkVariableName_()

template<GUM_Numeric GUM_SCALAR>
void gum::BayesNetFactory< GUM_SCALAR >::_checkVariableName_ ( std::string_view name) const
private

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

Definition at line 876 of file BayesNetFactory_tpl.h.

876 {
877 if (!_varNameMap_.exists(name)) { GUM_ERROR(NotFound, name) }
878 }

References _varNameMap_, and GUM_ERROR.

Referenced by _setCPTAndParents_(), addParent(), setParentModality(), setVariableCPT(), startFactorizedProbabilityDeclaration(), startParentsDeclaration(), and startRawProbabilityDeclaration().

Here is the caller graph for this function:

◆ _fillProbaWithValuesTable_() [1/2]

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

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

Definition at line 529 of file BayesNetFactory_tpl.h.

530 {
532
534
535 // the main loop is on the first variables. The others are in the right
536 // order.
538 Idx j = 0;
539
540 for (cptInst.setFirstVar(first); !cptInst.end(); cptInst.incVar(first)) {
541 for (cptInst.setFirstNotVar(first); !cptInst.end(); cptInst.incNotVar(first))
542 table.set(cptInst,
543 (j < rawTable.size()) ? static_cast< GUM_SCALAR >(rawTable[j++])
544 : static_cast< GUM_SCALAR >(0));
545
546 cptInst.unsetEnd();
547 }
548 }

References _bn_, _stringBag_, _varNameMap_, gum::Instantiation::end(), gum::Instantiation::incNotVar(), gum::Instantiation::incVar(), gum::Instantiation::setFirstNotVar(), gum::Instantiation::setFirstVar(), and gum::Instantiation::unsetEnd().

Here is the call graph for this function:

◆ _fillProbaWithValuesTable_() [2/2]

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

Fill a tensor from a raw CPT.

Definition at line 487 of file BayesNetFactory_tpl.h.

489 {
492
494 table.fillWith(GUM_SCALAR(0.0));
495
496 for (size_t i = 0; i < variables.size(); ++i) {
497 varList.pushBack(&(_bn_->variable(_varNameMap_[variables[i]])));
498 }
499
500 Idx nbrVar = varList.size();
501
503
504 // initializing the array
505 for (NodeId i = 0; i < nbrVar; i++) {
506 modCounter.push_back(Idx(0));
507 }
508
509 for (Idx j = 0; j < rawTable.size(); j++) {
510 for (NodeId i = 0; i < nbrVar; i++) {
511 cptInst.chgVal(*(varList[i]), modCounter[i]);
512 }
513
514 table.set(cptInst, static_cast< GUM_SCALAR >(rawTable[j]));
515 if (!_increment_(modCounter, varList)) { break; } // too many values (just not read)
516 }
517 }
bool _increment_(std::vector< gum::Idx > &modCounter, List< const DiscreteVariable * > &varList) const
Increment a modality counter for the fillProbaWithValuesTable method.

References _bn_, _increment_(), _stringBag_, _varNameMap_, gum::Instantiation::chgVal(), gum::List< Val >::pushBack(), and gum::List< Val >::size().

Referenced by rawConditionalTable(), and rawConditionalTable().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _illegalStateError_()

template<GUM_Numeric GUM_SCALAR>
void gum::BayesNetFactory< GUM_SCALAR >::_illegalStateError_ ( std::string_view s)
private

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

Definition at line 824 of file BayesNetFactory_tpl.h.

824 {
825 std::string msg = "Illegal state call (";
826 msg += s;
827 msg += ") in state ";
828
829 switch (state()) {
830 case factory_state::NONE : {
831 msg += "NONE";
832 break;
833 }
834
836 msg += "NETWORK";
837 break;
838 }
839
841 msg += "VARIABLE";
842 break;
843 }
844
846 msg += "PARENTS";
847 break;
848 }
849
851 msg += "RAW_CPT";
852 break;
853 }
854
856 msg += "FACT_CPT";
857 break;
858 }
859
861 msg += "FACT_ENTRY";
862 break;
863 }
864
865 default : {
866 msg += "Unknown state";
867 }
868 }
869
871 }

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

Referenced by addMax(), addMin(), addModality(), addParent(), addTick(), endFactorizedEntry(), endFactorizedProbabilityDeclaration(), endNetworkDeclaration(), endParentsDeclaration(), endRawProbabilityDeclaration(), endVariableDeclaration(), rawConditionalTable(), rawConditionalTable(), setParentModality(), setVariable(), setVariableCPT(), setVariableCPTImplementation(), setVariableValues(), setVariableValuesUnchecked(), startFactorizedEntry(), startFactorizedProbabilityDeclaration(), startNetworkDeclaration(), startParentsDeclaration(), startRawProbabilityDeclaration(), startVariableDeclaration(), variableDescription(), variableName(), and variableType().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _increment_()

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

Increment a modality counter for the fillProbaWithValuesTable method.

Definition at line 551 of file BayesNetFactory_tpl.h.

552 {
553 bool last = true;
554
555 for (NodeId j = 0; j < modCounter.size(); j++) {
556 last = (modCounter[j] == (varList[j]->domainSize() - 1)) && last;
557
558 if (!last) break;
559 }
560
561 if (last) { return false; }
562
563 bool add = false;
564
565 auto i = NodeId(varList.size() - 1);
566
567 do {
568 if (modCounter[i] == (varList[i]->domainSize() - 1)) {
569 modCounter[i] = 0;
570 add = true;
571 } else {
572 modCounter[i] += 1;
573 add = false;
574 }
575
576 i--;
577 } while (add);
578
579 return true;
580 }

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

Referenced by _fillProbaWithValuesTable_().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _resetParts_()

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

Reset the different parts used to constructed the BayesNet.

Definition at line 925 of file BayesNetFactory_tpl.h.

925 {
926 _foo_flag_ = false;
927 _bar_flag_ = false;
928 _stringBag_.clear();
929 }
bool _bar_flag_
Depending on the context this flag is used for some VERY important reasons.
bool _foo_flag_
Depending on the context this flag is used for some VERY important reasons.

References _bar_flag_, _foo_flag_, and _stringBag_.

Referenced by endFactorizedProbabilityDeclaration(), endParentsDeclaration(), endRawProbabilityDeclaration(), and endVariableDeclaration().

Here is the caller graph for this function:

◆ _setCPTAndParents_()

template<GUM_Numeric GUM_SCALAR>
void gum::BayesNetFactory< GUM_SCALAR >::_setCPTAndParents_ ( const DiscreteVariable & var,
Tensor< GUM_SCALAR > * table )
private

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

Definition at line 907 of file BayesNetFactory_tpl.h.

908 {
909 NodeId varId = _varNameMap_[var.name()];
910 _bn_->dag_.eraseParents(varId);
911
912 for (auto v: table->variablesSequence()) {
913 if (v != (&var)) {
914 _checkVariableName_(v->name());
915 _bn_->dag_.addArc(_varNameMap_[v->name()], varId);
916 }
917 }
918
919 // CPT are created when a variable is added.
920 _bn_->_unsafeChangeTensor_(varId, table);
921 }
void _checkVariableName_(std::string_view name) const
Check if a variable with the given name exists, if not raise an NotFound exception.

References _bn_, _checkVariableName_(), _varNameMap_, and gum::Variable::name().

Referenced by setVariableCPT().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ addMax()

template<GUM_Numeric GUM_SCALAR>
void gum::BayesNetFactory< GUM_SCALAR >::addMax ( const long & max)
overridevirtual

Adds the max value of the current range variable.

Implements gum::IBayesNetFactory.

Definition at line 271 of file BayesNetFactory_tpl.h.

271 {
273 _illegalStateError_("addMax");
274 } else {
275 _stringBag_.push_back(std::to_string(max));
276 }
277 }
void _illegalStateError_(std::string_view s)
Raise an OperationNotAllowed with the message "Illegal state.".

References _illegalStateError_(), _stringBag_, state(), and gum::IBayesNetFactory::VARIABLE.

Here is the call graph for this function:

◆ addMin()

template<GUM_Numeric GUM_SCALAR>
void gum::BayesNetFactory< GUM_SCALAR >::addMin ( const long & min)
overridevirtual

Adds the min value of the current range variable.

Implements gum::IBayesNetFactory.

Definition at line 259 of file BayesNetFactory_tpl.h.

259 {
261 _illegalStateError_("addMin");
262 } else {
263 _stringBag_.push_back(std::to_string(min));
264 }
265 }

References _illegalStateError_(), _stringBag_, state(), and gum::IBayesNetFactory::VARIABLE.

Here is the call graph for this function:

◆ addModality()

template<GUM_Numeric GUM_SCALAR>
void gum::BayesNetFactory< GUM_SCALAR >::addModality ( std::string_view name)
finalvirtual

Adds a modality to the current labelized variable.

Implements gum::IBayesNetFactory.

Definition at line 246 of file BayesNetFactory_tpl.h.

246 {
248 _illegalStateError_("addModality");
249 } else {
251 _stringBag_.emplace_back(name);
252 }
253 }
void _checkModalityInBag_(std::string_view mod)
Check if in stringBag there is no other modality with the same name.

References _checkModalityInBag_(), _illegalStateError_(), _stringBag_, state(), and gum::IBayesNetFactory::VARIABLE.

Referenced by gum::prm::PRMSystem< double >::add(), and gum::prm::PRMSystem< double >::getArrayType().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ addNetworkProperty()

template<GUM_Numeric GUM_SCALAR>
void gum::BayesNetFactory< GUM_SCALAR >::addNetworkProperty ( std::string_view propName,
std::string_view propValue )
finalvirtual

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

Implements gum::IBayesNetFactory.

Definition at line 165 of file BayesNetFactory_tpl.h.

166 {
167 _bn_->setProperty(propName, propValue);
168 }

References _bn_.

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

Here is the caller graph for this function:

◆ addParent()

template<GUM_Numeric GUM_SCALAR>
void gum::BayesNetFactory< GUM_SCALAR >::addParent ( std::string_view 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 418 of file BayesNetFactory_tpl.h.

418 {
419 if (state() != factory_state::PARENTS) {
420 _illegalStateError_("addParent");
421 } else {
423 _stringBag_.emplace_back(var);
424 }
425 }

References _checkVariableName_(), _illegalStateError_(), _stringBag_, gum::IBayesNetFactory::PARENTS, and state().

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

Here is the call graph for this function:
Here is the caller graph for this function:

◆ addTick()

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

Adds a tick to the current Discretized variable.

Definition at line 283 of file BayesNetFactory_tpl.h.

283 {
285 _illegalStateError_("addTick");
286 } else {
287 _stringBag_.push_back(std::to_string(tick));
288 }
289 }

References _illegalStateError_(), _stringBag_, state(), and gum::IBayesNetFactory::VARIABLE.

Here is the call graph for this function:

◆ bayesNet()

template<GUM_Numeric GUM_SCALAR>
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 111 of file BayesNetFactory_tpl.h.

111 {
112 return _bn_;
113 }

References _bn_.

◆ cptDomainSize()

template<GUM_Numeric GUM_SCALAR>
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 149 of file BayesNetFactory_tpl.h.

149 {
150 return _bn_->cpt(n).domainSize();
151 }

References _bn_.

◆ endFactorizedEntry()

template<GUM_Numeric GUM_SCALAR>
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 621 of file BayesNetFactory_tpl.h.

621 {
623 _illegalStateError_("endFactorizedEntry");
624 } else {
625 delete _parents_;
626 _parents_ = nullptr;
627 _states_.pop_back();
628 }
629 }

References _illegalStateError_(), _parents_, _states_, gum::IBayesNetFactory::FACT_ENTRY, and state().

Here is the call graph for this function:

◆ endFactorizedProbabilityDeclaration()

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

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

Implements gum::IBayesNetFactory.

Definition at line 741 of file BayesNetFactory_tpl.h.

741 {
743 _illegalStateError_("endFactorizedProbabilityDeclaration");
744 } else {
745 _resetParts_();
746 _states_.pop_back();
747 }
748 }
void _resetParts_()
Reset the different parts used to constructed the BayesNet.

References _illegalStateError_(), _resetParts_(), _states_, gum::IBayesNetFactory::FACT_CPT, and state().

Here is the call graph for this function:

◆ endNetworkDeclaration()

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

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

Implements gum::IBayesNetFactory.

Definition at line 172 of file BayesNetFactory_tpl.h.

172 {
173 if (state() != factory_state::NETWORK) {
174 _illegalStateError_("endNetworkDeclaration");
175 } else {
176 _states_.pop_back();
177 }
178 }

References _illegalStateError_(), _states_, gum::IBayesNetFactory::NETWORK, and state().

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

Here is the call graph for this function:
Here is the caller graph for this function:

◆ endParentsDeclaration()

template<GUM_Numeric GUM_SCALAR>
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 432 of file BayesNetFactory_tpl.h.

432 {
433 if (state() != factory_state::PARENTS) {
434 _illegalStateError_("endParentsDeclaration");
435 } else {
437
438 // PLEASE NOTE THAT THE ORDER IS INVERSE
439
440 for (size_t i = _stringBag_.size() - 1; i > 0; --i) {
441 _bn_->addArc(_varNameMap_[_stringBag_[i]], id);
442 }
443
444 _resetParts_();
445
446 _states_.pop_back();
447 }
448 }

References _bn_, _illegalStateError_(), _resetParts_(), _states_, _stringBag_, _varNameMap_, gum::IBayesNetFactory::PARENTS, and state().

Here is the call graph for this function:

◆ endRawProbabilityDeclaration()

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

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

Implements gum::IBayesNetFactory.

Definition at line 585 of file BayesNetFactory_tpl.h.

585 {
586 if (state() != factory_state::RAW_CPT) {
587 _illegalStateError_("endRawProbabilityDeclaration");
588 } else {
589 _resetParts_();
590 _states_.pop_back();
591 }
592 }

References _illegalStateError_(), _resetParts_(), _states_, gum::IBayesNetFactory::RAW_CPT, and state().

Here is the call graph for this function:

◆ endVariableDeclaration()

template<GUM_Numeric GUM_SCALAR>
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 322 of file BayesNetFactory_tpl.h.

322 {
324 _illegalStateError_("endVariableDeclaration");
325 } else if (_foo_flag_ && (_stringBag_.size() > 4)) {
326 DiscreteVariable* var = nullptr;
327
328 // if the current variable is a LabelizedVariable
329 if (_stringBag_[2] == "L") {
330 const auto l = new LabelizedVariable(_stringBag_[0], (_bar_flag_) ? _stringBag_[1] : "", 0);
331
332 for (size_t i = 3; i < _stringBag_.size(); ++i) {
333 l->addLabel(_stringBag_[i]);
334 }
335
336 var = l;
337 // if the current variable is a RangeVariable
338 } else if (_stringBag_[2] == "I") {
339 // try to create the domain of the variable
341 for (size_t i = 3; i < _stringBag_.size(); ++i) {
342 domain.push_back(std::stoi(_stringBag_[i]));
343 }
344
345 const auto v
347 var = v;
348 } else if (_stringBag_[2] == "R") {
349 const auto r = new RangeVariable(_stringBag_[0],
350 _bar_flag_ ? _stringBag_[1] : "",
353
354 var = r;
355 // if the current variable is a DiscretizedVariable
356 } else if (_stringBag_[2] == "D") {
358 _bar_flag_ ? _stringBag_[1] : "");
359
360 for (size_t i = 3; i < _stringBag_.size(); ++i) {
362 }
363
364 var = d;
365 }
366
367 if (var == nullptr) {
368 GUM_ERROR(OperationNotAllowed, "Unknown variable type for variable " + _stringBag_[0])
369 }
370
371 if (_impl_ != 0) {
372 _varNameMap_.insert(var->name(), _bn_->add(*var, _impl_));
373 _impl_ = 0;
374 } else {
375 _varNameMap_.insert(var->name(), _bn_->add(*var));
376 }
377
378 NodeId retVal = _varNameMap_[var->name()];
379
380 delete var;
381
382 _resetParts_();
383 _states_.pop_back();
384
385 return retVal;
386 } else {
387 const auto errMsg
388 = std::format("Not enough modalities ({}) declared for variable {}",
389 _stringBag_.size() > 3 ? _stringBag_.size() - 3 : std::size_t(0),
390 _foo_flag_ ? _stringBag_[0] : std::string("unknown"));
391 _resetParts_();
392
393 _states_.pop_back();
395 }
396
397 // For noisy compilers
398 return 0;
399 }
void addTick(const GUM_SCALAR &tick)
Adds a tick to the current Discretized variable.

References _bar_flag_, _bn_, _foo_flag_, _illegalStateError_(), _impl_, _resetParts_(), _states_, _stringBag_, _varNameMap_, GUM_ERROR, gum::Variable::name(), state(), and gum::IBayesNetFactory::VARIABLE.

Here is the call graph for this function:

◆ isVerbose()

INLINE bool gum::IBayesNetFactory::isVerbose ( )
inherited

Definition at line 54 of file IBayesNetFactory_inl.h.

54{ return _verbose_; }

References _verbose_.

◆ operator=()

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

Copy operator is illegal, use only copy constructor.

References BayesNetFactory().

Here is the call graph for this function:

◆ rawConditionalTable() [1/2]

template<GUM_Numeric GUM_SCALAR>
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 520 of file BayesNetFactory_tpl.h.

520 {
521 if (state() != factory_state::RAW_CPT) {
522 _illegalStateError_("rawConditionalTable");
523 } else {
525 }
526 }
void _fillProbaWithValuesTable_(const std::vector< std::string > &variables, const std::vector< float > &rawTable)
Fill a tensor from a raw CPT.

References _fillProbaWithValuesTable_(), _illegalStateError_(), gum::IBayesNetFactory::RAW_CPT, and state().

Here is the call graph for this function:

◆ rawConditionalTable() [2/2]

template<GUM_Numeric GUM_SCALAR>
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 476 of file BayesNetFactory_tpl.h.

478 {
479 if (state() != factory_state::RAW_CPT) {
480 _illegalStateError_("rawConditionalTable");
481 } else {
483 }
484 }

References _fillProbaWithValuesTable_(), _illegalStateError_(), gum::IBayesNetFactory::RAW_CPT, and state().

Here is the call graph for this function:

◆ resetVerbose()

INLINE void gum::IBayesNetFactory::resetVerbose ( )
inherited

Definition at line 52 of file IBayesNetFactory_inl.h.

52{ _verbose_ = false; }

References _verbose_.

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

Here is the caller graph for this function:

◆ setParentModality()

template<GUM_Numeric GUM_SCALAR>
void gum::BayesNetFactory< GUM_SCALAR >::setParentModality ( std::string_view parent,
std::string_view modality )
finalvirtual

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

Implements gum::IBayesNetFactory.

Definition at line 634 of file BayesNetFactory_tpl.h.

635 {
637 _illegalStateError_("string");
638 } else {
641 const DiscreteVariable& parentVar = _bn_->variable(*_varNameMap_.tryGet(parent));
642 (*_parents_) << parentVar;
643 _parents_->chgVal(parentVar, id);
644 }
645 }
Idx _checkVariableModality_(std::string_view name, std::string_view mod)
Check if var exists and if mod is one of it's modality, if not raise an NotFound exception.

References _bn_, _checkVariableModality_(), _checkVariableName_(), _illegalStateError_(), _parents_, _varNameMap_, gum::IBayesNetFactory::FACT_ENTRY, and state().

Here is the call graph for this function:

◆ setVariable()

template<GUM_Numeric GUM_SCALAR>
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 762 of file BayesNetFactory_tpl.h.

762 {
763 if (state() != factory_state::NONE) {
764 _illegalStateError_("setVariable");
765 } else {
766 if (_varNameMap_.exists(var.name())) {
767 GUM_ERROR(DuplicateElement, "Name already used: " << var.name())
768 }
769 // The var name is unused
770 _varNameMap_.insert(var.name(), _bn_->add(var));
771 }
772 }

References _bn_, _illegalStateError_(), _varNameMap_, GUM_ERROR, gum::Variable::name(), gum::IBayesNetFactory::NONE, and state().

Here is the call graph for this function:

◆ setVariableCPT()

template<GUM_Numeric GUM_SCALAR>
void gum::BayesNetFactory< GUM_SCALAR >::setVariableCPT ( std::string_view varName,
MultiDimAdressable * table,
bool redefineParents )
finalvirtual

Define a variable's CPT.

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

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

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

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

Implements gum::IBayesNetFactory.

Definition at line 793 of file BayesNetFactory_tpl.h.

795 {
796 auto pot = dynamic_cast< Tensor< GUM_SCALAR >* >(table);
797
798 if (state() != factory_state::NONE) {
799 _illegalStateError_("setVariableCPT");
800 } else {
802 NodeId varId = *_varNameMap_.tryGet(varName);
803 const DiscreteVariable& var = _bn_->variable(varId);
804 // If we have to change the structure of the BayesNet, then we call a sub
805 // method.
806
807 if (redefineParents) {
809 } else if (pot->contains(var)) {
810 for (auto node: _bn_->parents(varId)) {
811 if (!pot->contains(_bn_->variable(node))) {
812 GUM_ERROR(OperationNotAllowed, "The CPT is not valid in the current BayesNet.")
813 }
814 }
815
816 // CPT are created when a variable is added.
817 _bn_->_unsafeChangeTensor_(varId, pot);
818 }
819 }
820 }
void _setCPTAndParents_(const DiscreteVariable &var, Tensor< GUM_SCALAR > *table)
Sub method of setVariableCPT() which redefine the BayesNet's DAG with respect to table.

References _bn_, _checkVariableName_(), _illegalStateError_(), _setCPTAndParents_(), _varNameMap_, GUM_ERROR, gum::IBayesNetFactory::NONE, and state().

Here is the call graph for this function:

◆ setVariableCPTImplementation()

template<GUM_Numeric GUM_SCALAR>
void gum::BayesNetFactory< GUM_SCALAR >::setVariableCPTImplementation ( MultiDimAdressable * adressable)
finalvirtual

Defines the implementation to use for var's Tensor.

Warning
The implementation must be empty.
The pointer is always delegated to var's Tensor! 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 302 of file BayesNetFactory_tpl.h.

302 {
304
306 _illegalStateError_("setVariableCPTImplementation");
307 } else {
308 if (impl == nullptr) {
310 "An implementation for this variable is already "
311 "defined.")
312 } else if (impl->nbrDim() > 0) {
313 GUM_ERROR(OperationNotAllowed, "This implementation is not empty.")
314 }
315
316 _impl_ = impl;
317 }
318 }

References _illegalStateError_(), _impl_, GUM_ERROR, state(), and gum::IBayesNetFactory::VARIABLE.

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

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setVariableValues()

template<GUM_Numeric GUM_SCALAR>
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 722 of file BayesNetFactory_tpl.h.

722 {
724 _illegalStateError_("setVariableValues");
725 } else {
726 // Checking consistency between values and var.
727 if (const DiscreteVariable& var = _bn_->variable(_varNameMap_[_stringBag_[0]]);
728 values.size() != var.domainSize()) {
730 var.name() << " : invalid number of modalities: found " << values.size()
731 << " while needed " << var.domainSize())
732 }
733
735 }
736 }
void setVariableValuesUnchecked(const std::vector< float > &values) final
Gives the values of the variable with respect to precedent parents modality.

References _bn_, _illegalStateError_(), _stringBag_, _varNameMap_, gum::IBayesNetFactory::FACT_ENTRY, GUM_ERROR, setVariableValuesUnchecked(), and state().

Here is the call graph for this function:

◆ setVariableValuesUnchecked()

template<GUM_Numeric GUM_SCALAR>
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.variableName("foo");
factory.startParentsDeclaration("foo");
// add parents
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
void startFactorizedProbabilityDeclaration(std::string_view var) final
Tells the factory that we're starting a factorized declaration.
void endParentsDeclaration() final
Tells the factory that we've finished declaring parents for some variable.
void endFactorizedProbabilityDeclaration() final
Tells the factory that we finished declaring a conditional probability table.
void setVariableValues(const std::vector< float > &values) final
same than below with gum::OperationNotAllowed exception if value's size not OK.
void startParentsDeclaration(std::string_view var) final
Tells the factory that we're declaring parents for some variable.
void startVariableDeclaration() final
Tells the factory that we're in a variable declaration.
NodeId endVariableDeclaration() final
Tells the factory that we're out of a variable declaration.
void variableName(std::string_view name) final
Tells the factory the current variable's name.

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

675 {
677 _illegalStateError_("setVariableValues");
678 } else {
679 const DiscreteVariable& var = _bn_->variable(_varNameMap_[_stringBag_[0]]);
681
682 if (_parents_->domainSize() > 0) {
683 Instantiation inst(_bn_->cpt(_varNameMap_[var.name()]));
684 inst.setVals(*_parents_);
685 // Creating an instantiation containing all the variables not ins
686 // _parents_.
688 inst_default << var;
689
690 for (auto node: _bn_->parents(varId)) {
691 if (!_parents_->contains(_bn_->variable(node))) { inst_default << _bn_->variable(node); }
692 }
693
694 // Filling the variable's table.
695 for (inst.setFirstIn(inst_default); !inst.end(); inst.incIn(inst_default)) {
696 (_bn_->cpt(varId))
697 .set(inst,
698 inst.val(var) < values.size() ? static_cast< GUM_SCALAR >(values[inst.val(var)])
699 : static_cast< GUM_SCALAR >(0));
700 }
701 } else {
702 Instantiation inst(_bn_->cpt(_varNameMap_[var.name()]));
704 var_inst << var;
705
706 for (var_inst.setFirst(); !var_inst.end(); ++var_inst) {
707 inst.setVals(var_inst);
708
709 for (inst.setFirstOut(var_inst); !inst.end(); inst.incOut(var_inst)) {
710 (_bn_->cpt(varId))
711 .set(inst,
712 inst.val(var) < values.size()
713 ? static_cast< GUM_SCALAR >(values[inst.val(var)])
714 : static_cast< GUM_SCALAR >(0));
715 }
716 }
717 }
718 }
719 }

References _bn_, _illegalStateError_(), _parents_, _stringBag_, _varNameMap_, gum::Instantiation::end(), gum::IBayesNetFactory::FACT_ENTRY, gum::Instantiation::incIn(), gum::Instantiation::incOut(), gum::Variable::name(), gum::Instantiation::setFirst(), gum::Instantiation::setFirstIn(), gum::Instantiation::setFirstOut(), gum::Instantiation::setVals(), state(), and gum::Instantiation::val().

Referenced by setVariableValues().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setVerbose()

INLINE void gum::IBayesNetFactory::setVerbose ( )
inherited

Definition at line 50 of file IBayesNetFactory_inl.h.

50{ _verbose_ = true; }

References _verbose_.

◆ startFactorizedEntry()

template<GUM_Numeric GUM_SCALAR>
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 609 of file BayesNetFactory_tpl.h.

609 {
611 _illegalStateError_("startFactorizedEntry");
612 } else {
613 _parents_ = new Instantiation();
615 }
616 }

References _illegalStateError_(), _parents_, _states_, gum::IBayesNetFactory::FACT_CPT, gum::IBayesNetFactory::FACT_ENTRY, and state().

Here is the call graph for this function:

◆ startFactorizedProbabilityDeclaration()

template<GUM_Numeric GUM_SCALAR>
void gum::BayesNetFactory< GUM_SCALAR >::startFactorizedProbabilityDeclaration ( std::string_view var)
finalvirtual

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

Implements gum::IBayesNetFactory.

Definition at line 596 of file BayesNetFactory_tpl.h.

596 {
597 if (state() != factory_state::NONE) {
598 _illegalStateError_("startFactorizedProbabilityDeclaration");
599 } else {
601 _stringBag_.insert(_stringBag_.begin(), std::string(var));
603 }
604 }

References _checkVariableName_(), _illegalStateError_(), _states_, _stringBag_, gum::IBayesNetFactory::FACT_CPT, gum::IBayesNetFactory::NONE, and state().

Here is the call graph for this function:

◆ startNetworkDeclaration()

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

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

Implements gum::IBayesNetFactory.

Definition at line 155 of file BayesNetFactory_tpl.h.

155 {
156 if (state() != factory_state::NONE) {
157 _illegalStateError_("startNetworkDeclaration");
158 } else {
160 }
161 }

References _illegalStateError_(), _states_, gum::IBayesNetFactory::NETWORK, gum::IBayesNetFactory::NONE, and state().

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

Here is the call graph for this function:
Here is the caller graph for this function:

◆ startParentsDeclaration()

template<GUM_Numeric GUM_SCALAR>
void gum::BayesNetFactory< GUM_SCALAR >::startParentsDeclaration ( std::string_view 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 404 of file BayesNetFactory_tpl.h.

404 {
405 if (state() != factory_state::NONE) {
406 _illegalStateError_("startParentsDeclaration");
407 } else {
409 _stringBag_.insert(_stringBag_.begin(), std::string(var));
411 }
412 }

References _checkVariableName_(), _illegalStateError_(), _states_, _stringBag_, gum::IBayesNetFactory::NONE, gum::IBayesNetFactory::PARENTS, and state().

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

Here is the call graph for this function:
Here is the caller graph for this function:

◆ startRawProbabilityDeclaration()

template<GUM_Numeric GUM_SCALAR>
void gum::BayesNetFactory< GUM_SCALAR >::startRawProbabilityDeclaration ( std::string_view 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 454 of file BayesNetFactory_tpl.h.

454 {
455 if (state() != factory_state::NONE) {
456 _illegalStateError_("startRawProbabilityDeclaration");
457 } else {
459 _stringBag_.emplace_back(var);
461 }
462 }

References _checkVariableName_(), _illegalStateError_(), _states_, _stringBag_, gum::IBayesNetFactory::NONE, gum::IBayesNetFactory::RAW_CPT, and state().

Here is the call graph for this function:

◆ startVariableDeclaration()

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

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

Implements gum::IBayesNetFactory.

Definition at line 183 of file BayesNetFactory_tpl.h.

183 {
184 if (state() != factory_state::NONE) {
185 _illegalStateError_("startVariableDeclaration");
186 } else {
188 _stringBag_.emplace_back("name");
189 _stringBag_.emplace_back("desc");
190 _stringBag_.emplace_back("L");
191 }
192 }

References _illegalStateError_(), _states_, _stringBag_, gum::IBayesNetFactory::NONE, state(), and gum::IBayesNetFactory::VARIABLE.

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

Here is the call graph for this function:
Here is the caller graph for this function:

◆ state()

◆ variable()

template<GUM_Numeric GUM_SCALAR>
const DiscreteVariable & gum::BayesNetFactory< GUM_SCALAR >::variable ( std::string_view name) const

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

Exceptions
NotFoundRaised if no variable matches the name.

Definition at line 139 of file BayesNetFactory_tpl.h.

139 {
140 auto p = _varNameMap_.tryGet(name);
141 if (!p) { GUM_ERROR(NotFound, name) }
142 return _bn_->variable(*p);
143 }

References _bn_, _varNameMap_, and GUM_ERROR.

Referenced by gum::prm::PRMSystem< GUM_SCALAR >::_groundTensor_(), and gum::prm::PRMSystem< double >::_groundTensor_().

Here is the caller graph for this function:

◆ variableDescription()

template<GUM_Numeric GUM_SCALAR>
void gum::BayesNetFactory< GUM_SCALAR >::variableDescription ( std::string_view desc)
finalvirtual

Tells the factory the current variable's description.

Implements gum::IBayesNetFactory.

Definition at line 209 of file BayesNetFactory_tpl.h.

209 {
211 _illegalStateError_("variableDescription");
212 } else {
213 _bar_flag_ = true;
214 _stringBag_[1] = desc;
215 }
216 }

References _bar_flag_, _illegalStateError_(), _stringBag_, state(), and gum::IBayesNetFactory::VARIABLE.

Here is the call graph for this function:

◆ variableId()

template<GUM_Numeric GUM_SCALAR>
NodeId gum::BayesNetFactory< GUM_SCALAR >::variableId ( std::string_view 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 130 of file BayesNetFactory_tpl.h.

130 {
131 auto p = _varNameMap_.tryGet(name);
132 if (!p) { GUM_ERROR(NotFound, name) }
133 return *p;
134 }

References _varNameMap_, and GUM_ERROR.

◆ variableName()

template<GUM_Numeric GUM_SCALAR>
void gum::BayesNetFactory< GUM_SCALAR >::variableName ( std::string_view 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 196 of file BayesNetFactory_tpl.h.

196 {
198 _illegalStateError_("variableName");
199 } else {
200 if (_varNameMap_.exists(name)) { GUM_ERROR(DuplicateElement, "Name already used: " << name) }
201
202 _foo_flag_ = true;
203 _stringBag_[0] = name;
204 }
205 }

References _foo_flag_, _illegalStateError_(), _stringBag_, _varNameMap_, GUM_ERROR, state(), and gum::IBayesNetFactory::VARIABLE.

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

Here is the call graph for this function:
Here is the caller graph for this function:

◆ variableType()

template<GUM_Numeric GUM_SCALAR>
void gum::BayesNetFactory< GUM_SCALAR >::variableType ( const VarType & type)
overridevirtual

Tells the factory the current variable's type.

Implements gum::IBayesNetFactory.

Definition at line 224 of file BayesNetFactory_tpl.h.

224 {
226 _illegalStateError_("variableType");
227 } else {
228 switch (type) {
229 case VarType::DISCRETIZED : _stringBag_[2] = "D"; break;
230 case VarType::RANGE : _stringBag_[2] = "R"; break;
231 case VarType::INTEGER : _stringBag_[2] = "I"; break;
232 case VarType::LABELIZED : _stringBag_[2] = "L"; break;
235 "Continuous variable (" + _stringBag_[0]
236 + ") are not supported in Bayesian networks.")
237 default : GUM_ERROR(OperationNotAllowed, "Unknown type for (" + _stringBag_[0] + ")")
238 }
239 }
240 }

References _illegalStateError_(), _stringBag_, gum::CONTINUOUS, gum::DISCRETIZED, GUM_ERROR, gum::INTEGER, gum::LABELIZED, gum::RANGE, state(), and gum::IBayesNetFactory::VARIABLE.

Here is the call graph for this function:

◆ varInBN()

template<GUM_Numeric GUM_SCALAR>
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 116 of file BayesNetFactory_tpl.h.

116 {
117 return _bn_->variable(id);
118 }

References _bn_.

Member Data Documentation

◆ _bar_flag_

template<GUM_Numeric 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 426 of file BayesNetFactory.h.

Referenced by _resetParts_(), endVariableDeclaration(), and variableDescription().

◆ _bn_

◆ _foo_flag_

template<GUM_Numeric 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 422 of file BayesNetFactory.h.

Referenced by _resetParts_(), endVariableDeclaration(), and variableName().

◆ _impl_

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

Implementation of variable between two startVariableDeclaration/endVariableDeclaration calls.

Definition at line 436 of file BayesNetFactory.h.

Referenced by BayesNetFactory(), BayesNetFactory(), ~BayesNetFactory(), endVariableDeclaration(), and setVariableCPTImplementation().

◆ _parents_

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

◆ _states_

◆ _stringBag_

◆ _varNameMap_

◆ _verbose_

bool gum::IBayesNetFactory::_verbose_
privateinherited

Definition at line 151 of file IBayesNetFactory.h.

Referenced by IBayesNetFactory(), isVerbose(), resetVerbose(), and setVerbose().


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