aGrUM  0.16.0
BayesNetFactory.h
Go to the documentation of this file.
1 
30 #ifndef GUM_BAYESNET_FACTORY_H
31 #define GUM_BAYESNET_FACTORY_H
32 
33 #include <string>
34 #include <vector>
35 
36 #include <agrum/BN/BayesNet.h>
38 
39 namespace gum {
40 
61  template < typename GUM_SCALAR >
62  class BayesNetFactory : public IBayesNetFactory {
63  public:
64  // ==========================================================================
66  // ==========================================================================
68 
75  explicit BayesNetFactory(BayesNet< GUM_SCALAR >* bn);
76 
86  BayesNetFactory(const BayesNetFactory< GUM_SCALAR >& source);
87 
97  virtual ~BayesNetFactory();
98 
100  // ==========================================================================
102  // ==========================================================================
104 
110  BayesNet< GUM_SCALAR >* bayesNet();
111 
113  const DiscreteVariable& varInBN(NodeId id) final;
114 
116  factory_state state() const final;
117 
120  NodeId variableId(const std::string& name) const final;
121 
124  const DiscreteVariable& variable(const std::string& name) const;
125 
128  Size cptDomainSize(NodeId n) const final;
129 
131  // ==========================================================================
133  // ==========================================================================
135 
137  void startNetworkDeclaration() final;
138 
140  void addNetworkProperty(const std::string& propName,
141  const std::string& propValue) final;
142 
144  void endNetworkDeclaration() final;
145 
147  // ==========================================================================
149  // ==========================================================================
151 
153  void startVariableDeclaration() final;
154 
158  void variableName(const std::string& name) final;
159 
161  void variableDescription(const std::string& desc) final;
162 
164  void addModality(const std::string& name) final;
165 
182  void setVariableCPTImplementation(MultiDimAdressable* adressable) final;
183 
191 
193  // ==========================================================================
195  // ==========================================================================
197 
201  void startParentsDeclaration(const std::string& var) final;
202 
206  void addParent(const std::string& var) final;
207 
216  void endParentsDeclaration() final;
217 
219  // ==========================================================================
221  // ==========================================================================
223 
227  void startRawProbabilityDeclaration(const std::string& var) final;
228 
249  void rawConditionalTable(const std::vector< std::string >& variables,
250  const std::vector< float >& rawTable) final;
251 
259  void rawConditionalTable(const std::vector< float >& rawTable) final;
260 
263  void endRawProbabilityDeclaration() final;
264 
266  // ==========================================================================
268  // (NONE -> RAW_CPT)
269  // ==========================================================================
271 
273  void startFactorizedProbabilityDeclaration(const std::string& var) final;
274 
277  void startFactorizedEntry() final;
278 
281  void endFactorizedEntry() final;
282 
285  void setParentModality(const std::string& parent,
286  const std::string& modality) final;
287 
318  void setVariableValuesUnchecked(const std::vector< float >& values) final;
319 
325  void setVariableValues(const std::vector< float >& values) final;
326 
330 
332  // ==========================================================================
335  // ==========================================================================
337 
353  void setVariable(const DiscreteVariable& var) final;
354 
377  void setVariableCPT(const std::string& varName,
378  MultiDimAdressable* table,
379  bool redefineParents) final;
380 
382 
383  private:
384  // ==========================================================================
386  // ==========================================================================
388 
392 
396 
398  std::vector< std::string > __stringBag;
399 
402 
406 
408 
411 
413  BayesNet< GUM_SCALAR >* __bn;
414 
417 
419  BayesNetFactory< GUM_SCALAR >&
420  operator=(const BayesNetFactory< GUM_SCALAR >& source);
421 
423  void __illegalStateError(const std::string& s);
424 
427  void __checkVariableName(const std::string& name);
428 
431  Idx __checkVariableModality(const std::string& name, const std::string& mod);
432 
434  void __checkModalityInBag(const std::string& mod);
435 
438  void __setCPTAndParents(const DiscreteVariable& var,
439  Potential< GUM_SCALAR >* table);
440 
442  void __resetParts();
443 
445  void __fillProbaWithValuesTable(const std::vector< std::string >& variables,
446  const std::vector< float >& rawTable);
448  void __fillProbaWithValuesTable(const std::vector< float >& rawTable);
449 
451  bool __increment(std::vector< gum::Idx >& modCounter,
452  List< const DiscreteVariable* >& varList);
453  };
454 
455 
456 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
457  extern template class BayesNetFactory< double >;
458 #endif
459 
460 } /* namespace gum */
461 
463 
464 #endif // GUM_BAYESNET_FACTORY_H
aGrUM&#39;s Potential is a multi-dimensional array with tensor operators.
Definition: potential.h:60
Class representing a Bayesian Network.
Definition: BayesNet.h:78
void endRawProbabilityDeclaration() final
Tells the factory that we finished declaring a conditional probability table.
void variableName(const std::string &name) final
Tells the factory the current variable&#39;s name.
void endFactorizedEntry() final
Tells the factory that we end an entry of a factorized conditional probability table.
void setVariableCPTImplementation(MultiDimAdressable *adressable) final
Defines the implementation to use for var&#39;s Potential.
void startFactorizedEntry() final
Tells the factory that we start an entry of a factorized conditional probability table.
BayesNetFactory(BayesNet< GUM_SCALAR > *bn)
Use this constructor if you want to use an already created BayesNet.
void addParent(const std::string &var) final
Tells the factory for which variable we&#39;re declaring parents.
void setVariable(const DiscreteVariable &var) final
Define a variable.
BayesNet< GUM_SCALAR > * bayesNet()
Returns the BayesNet created by this factory.
factory_state state() const final
Returns the current state of the factory.
void addNetworkProperty(const std::string &propName, const std::string &propValue) final
Tells the factory to add a property to the current network.
void startParentsDeclaration(const std::string &var) final
Tells the factory that we&#39;re declaring parents for some variable.
STL namespace.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
void endParentsDeclaration() final
Tells the factory that we&#39;ve finished declaring parents for some variable.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Base class for discrete random variable.
Generic doubly linked lists.
Definition: list.h:372
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
The class for generic Hash Tables.
Definition: hashTable.h:679
void __fillProbaWithValuesTable(const std::vector< std::string > &variables, const std::vector< float > &rawTable)
Fill a potential from a raw CPT.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
void __illegalStateError(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
void startFactorizedProbabilityDeclaration(const std::string &var) final
Tells the factory that we&#39;re starting a factorized declaration.
NodeId variableId(const std::string &name) const final
Returns the NodeId of a variable given it&#39;s name.
void startNetworkDeclaration() final
Tells the factory that we&#39;re in a network declaration.
HashTable< std::string, NodeId > __varNameMap
Mapping between a declared variable&#39;s name and it&#39;s node id.
void startVariableDeclaration() final
Tells the factory that we&#39;re in a variable declaration.
factory_state
The enumeration of states in which the factory can be in.
BayesNet< GUM_SCALAR > * __bn
The constructed BayesNet.
Idx __checkVariableModality(const std::string &name, const std::string &mod)
Check if var exists and if mod is one of it&#39;s modality, if not raise an NotFound exception.
NodeId endVariableDeclaration() final
Tells the factory that we&#39;re out of a variable declaration.
std::vector< factory_state > __states
State stack.
Size cptDomainSize(NodeId n) const final
Returns the domainSize of the cpt for the node n.
void startRawProbabilityDeclaration(const std::string &var) final
Tells the factory that we&#39;re declaring a conditional probability table for some variable.
std::vector< std::string > __stringBag
Just to keep track of strings between two start/end calls.
const DiscreteVariable & varInBN(NodeId id) final
short-cut accessor for a DiscreveVariable in the BN
void setVariableValuesUnchecked(const std::vector< float > &values) final
Gives the values of the variable with respect to precedent parents modality.
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:83
void setVariableCPT(const std::string &varName, MultiDimAdressable *table, bool redefineParents) final
Define a variable&#39;s CPT.
void endFactorizedProbabilityDeclaration() final
Tells the factory that we finished declaring a conditional probability table.
bool __foo_flag
Depending on the context this flag is used for some VERY important reasons.
Instantiation * __parents
Used when a factorized CPT is built.
void variableDescription(const std::string &desc) final
Tells the factory the current variable&#39;s description.
void __checkVariableName(const std::string &name)
Check if a variable with the given name exists, if not raise an NotFound exception.
void rawConditionalTable(const std::vector< std::string > &variables, const std::vector< float > &rawTable) final
Fills the variable&#39;s table with the values in rawTable.
<agrum/multidim/multiDimImplementation.h>
Size Idx
Type for indexes.
Definition: types.h:53
void endNetworkDeclaration() final
Tells the factory that we&#39;re out of a network declaration.
const DiscreteVariable & variable(const std::string &name) const
Returns a constant reference on a variable given it&#39;s name.
void __checkModalityInBag(const std::string &mod)
Check if in __stringBag there is no other modality with the same name.
void addModality(const std::string &name) final
Adds a modality to the current variable.
bool __increment(std::vector< gum::Idx > &modCounter, List< const DiscreteVariable * > &varList)
Increment a modality counter for the __fillProbaWithValuesTable method.
bool __bar_flag
Depending on the context this flag is used for some VERY important reasons.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
MultiDimImplementation< GUM_SCALAR > * __impl
Implementation of variable between two startVariableDeclaration/endVariableDeclaration calls...
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
void setVariableValues(const std::vector< float > &values) final
same than below with gum::OperationNotAllowed exception if value&#39;s size not OK.
void __resetParts()
Reset the different parts used to constructed the BayesNet.
A factory class to ease BayesNet construction.
Definition: BayesNet.h:45
virtual ~BayesNetFactory()
Destructor.
void setParentModality(const std::string &parent, const std::string &modality) final
Tells the factory on which modality we want to instantiate one of variable&#39;s parent.