aGrUM  0.14.2
BayesNetFactory.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
27 #ifndef GUM_BAYESNET_FACTORY_H
28 #define GUM_BAYESNET_FACTORY_H
29 
30 #include <string>
31 #include <vector>
32 
33 #include <agrum/BN/BayesNet.h>
35 
36 namespace gum {
37 
58  template < typename GUM_SCALAR >
59  class BayesNetFactory : public IBayesNetFactory {
60  public:
61  // ==========================================================================
63  // ==========================================================================
65 
72  explicit BayesNetFactory(BayesNet< GUM_SCALAR >* bn);
73 
83  BayesNetFactory(const BayesNetFactory< GUM_SCALAR >& source);
84 
94  virtual ~BayesNetFactory();
95 
97  // ==========================================================================
99  // ==========================================================================
101 
107  BayesNet< GUM_SCALAR >* bayesNet();
108 
110  const DiscreteVariable& varInBN(NodeId id) final;
111 
113  factory_state state() const final;
114 
117  NodeId variableId(const std::string& name) const final;
118 
121  const DiscreteVariable& variable(const std::string& name) const;
122 
125  Size cptDomainSize(NodeId n) const final;
126 
128  // ==========================================================================
130  // ==========================================================================
132 
134  void startNetworkDeclaration() final;
135 
137  void addNetworkProperty(const std::string& propName,
138  const std::string& propValue) final;
139 
141  void endNetworkDeclaration() final;
142 
144  // ==========================================================================
146  // ==========================================================================
148 
150  void startVariableDeclaration() final;
151 
155  void variableName(const std::string& name) final;
156 
158  void variableDescription(const std::string& desc) final;
159 
161  void addModality(const std::string& name) final;
162 
179  void setVariableCPTImplementation(MultiDimAdressable* adressable) final;
180 
188 
190  // ==========================================================================
192  // ==========================================================================
194 
198  void startParentsDeclaration(const std::string& var) final;
199 
203  void addParent(const std::string& var) final;
204 
213  void endParentsDeclaration() final;
214 
216  // ==========================================================================
218  // ==========================================================================
220 
224  void startRawProbabilityDeclaration(const std::string& var) final;
225 
246  void rawConditionalTable(const std::vector< std::string >& variables,
247  const std::vector< float >& rawTable) final;
248 
256  void rawConditionalTable(const std::vector< float >& rawTable) final;
257 
260  void endRawProbabilityDeclaration() final;
261 
263  // ==========================================================================
265  // (NONE -> RAW_CPT)
266  // ==========================================================================
268 
270  void startFactorizedProbabilityDeclaration(const std::string& var) final;
271 
274  void startFactorizedEntry() final;
275 
278  void endFactorizedEntry() final;
279 
282  void setParentModality(const std::string& parent,
283  const std::string& modality) final;
284 
315  void setVariableValuesUnchecked(const std::vector< float >& values) final;
316 
322  void setVariableValues(const std::vector< float >& values) final;
323 
327 
329  // ==========================================================================
332  // ==========================================================================
334 
350  void setVariable(const DiscreteVariable& var) final;
351 
374  void setVariableCPT(const std::string& varName,
375  MultiDimAdressable* table,
376  bool redefineParents) final;
377 
379 
380  private:
381  // ==========================================================================
383  // ==========================================================================
385 
389 
393 
395  std::vector< std::string > __stringBag;
396 
399 
403 
405 
408 
410  BayesNet< GUM_SCALAR >* __bn;
411 
414 
416  BayesNetFactory< GUM_SCALAR >&
417  operator=(const BayesNetFactory< GUM_SCALAR >& source);
418 
420  void __illegalStateError(const std::string& s);
421 
424  void __checkVariableName(const std::string& name);
425 
428  Idx __checkVariableModality(const std::string& name, const std::string& mod);
429 
431  void __checkModalityInBag(const std::string& mod);
432 
435  void __setCPTAndParents(const DiscreteVariable& var,
436  Potential< GUM_SCALAR >* table);
437 
439  void __resetParts();
440 
442  void __fillProbaWithValuesTable(const std::vector< std::string >& variables,
443  const std::vector< float >& rawTable);
445  void __fillProbaWithValuesTable(const std::vector< float >& rawTable);
446 
448  bool __increment(std::vector< gum::Idx >& modCounter,
449  List< const DiscreteVariable* >& varList);
450  };
451 
452 
453 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
454  extern template class BayesNetFactory< double >;
455 #endif
456 
457 } /* namespace gum */
458 
460 
461 #endif // GUM_BAYESNET_FACTORY_H
aGrUM&#39;s Potential is a multi-dimensional array with tensor operators.
Definition: potential.h:57
Class representing a Bayesian Network.
Definition: BayesNet.h:76
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.
Header of the IBayesNetFactory class.
void endParentsDeclaration() final
Tells the factory that we&#39;ve finished declaring parents for some variable.
Class representing Bayesian networks.
Base class for discrete random variable.
Generic doubly linked lists.
Definition: list.h:369
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
The class for generic Hash Tables.
Definition: hashTable.h:676
void __fillProbaWithValuesTable(const std::vector< std::string > &variables, const std::vector< float > &rawTable)
Fill a potential from a raw CPT.
Implementation of the BayesNetFactory class.
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:80
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:50
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:45
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:97
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:43
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.