aGrUM  0.18.1
a C++ library for (probabilistic) graphical models
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 variableType(const VarType& type);
141 
143  void addNetworkProperty(const std::string& propName,
144  const std::string& propValue) final;
145 
147  void endNetworkDeclaration() final;
148 
150  // ==========================================================================
152  // ==========================================================================
154 
156  void startVariableDeclaration() final;
157 
161  void variableName(const std::string& name) final;
162 
164  void variableDescription(const std::string& desc) final;
165 
167  void addModality(const std::string& name) final;
168 
170  void addMin(const long& min);
171 
173  void addMax(const long& max);
174 
176  void addTick(const GUM_SCALAR& tick);
177 
194  void setVariableCPTImplementation(MultiDimAdressable* adressable) final;
195 
203 
205  // ==========================================================================
207  // ==========================================================================
209 
213  void startParentsDeclaration(const std::string& var) final;
214 
218  void addParent(const std::string& var) final;
219 
228  void endParentsDeclaration() final;
229 
231  // ==========================================================================
233  // ==========================================================================
235 
239  void startRawProbabilityDeclaration(const std::string& var) final;
240 
261  void rawConditionalTable(const std::vector< std::string >& variables,
262  const std::vector< float >& rawTable) final;
263 
271  void rawConditionalTable(const std::vector< float >& rawTable) final;
272 
275  void endRawProbabilityDeclaration() final;
276 
278  // ==========================================================================
280  // (NONE -> RAW_CPT)
281  // ==========================================================================
283 
285  void startFactorizedProbabilityDeclaration(const std::string& var) final;
286 
289  void startFactorizedEntry() final;
290 
293  void endFactorizedEntry() final;
294 
297  void setParentModality(const std::string& parent,
298  const std::string& modality) final;
299 
330  void setVariableValuesUnchecked(const std::vector< float >& values) final;
331 
337  void setVariableValues(const std::vector< float >& values) final;
338 
342 
344  // ==========================================================================
347  // ==========================================================================
349 
365  void setVariable(const DiscreteVariable& var) final;
366 
389  void setVariableCPT(const std::string& varName,
390  MultiDimAdressable* table,
391  bool redefineParents) final;
392 
394 
395  private:
396  // ==========================================================================
398  // ==========================================================================
400 
404 
408 
410  std::vector< std::string > stringBag__;
411 
414 
418 
420 
423 
425  BayesNet< GUM_SCALAR >* bn__;
426 
429 
431  BayesNetFactory< GUM_SCALAR >&
432  operator=(const BayesNetFactory< GUM_SCALAR >& source);
433 
435  void illegalStateError__(const std::string& s);
436 
439  void checkVariableName__(const std::string& name);
440 
443  Idx checkVariableModality__(const std::string& name, const std::string& mod);
444 
446  void checkModalityInBag__(const std::string& mod);
447 
450  void setCPTAndParents__(const DiscreteVariable& var,
451  Potential< GUM_SCALAR >* table);
452 
454  void resetParts__();
455 
457  void fillProbaWithValuesTable__(const std::vector< std::string >& variables,
458  const std::vector< float >& rawTable);
460  void fillProbaWithValuesTable__(const std::vector< float >& rawTable);
461 
463  bool increment__(std::vector< gum::Idx >& modCounter,
464  List< const DiscreteVariable* >& varList);
465  };
466 
467 
468 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
469  extern template class BayesNetFactory< double >;
470 #endif
471 
472 } /* namespace gum */
473 
475 
476 #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
bool foo_flag__
Depending on the context this flag is used for some VERY important reasons.
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.
bool bar_flag__
Depending on the context this flag is used for some VERY important reasons.
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.
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.
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.
MultiDimImplementation< GUM_SCALAR > * impl__
Implementation of variable between two startVariableDeclaration/endVariableDeclaration calls...
void checkVariableName__(const std::string &name)
Check if a variable with the given name exists, if not raise an NotFound exception.
Copyright 2005-2020 Pierre-Henri WUILLEMIN() & Christophe GONZALES() info_at_agrum_dot_org.
void checkModalityInBag__(const std::string &mod)
Check if in stringBag__ there is no other modality with the same name.
void endParentsDeclaration() final
Tells the factory that we&#39;ve finished declaring parents for some variable.
Copyright 2005-2020 Pierre-Henri WUILLEMIN() & Christophe GONZALES() info_at_agrum_dot_org.
void illegalStateError__(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
Base class for discrete random variable.
Generic doubly linked lists.
Definition: list.h:372
Copyright 2005-2020 Pierre-Henri WUILLEMIN() & Christophe GONZALES() info_at_agrum_dot_org.
Definition: agrum.h:25
BayesNet< GUM_SCALAR > * bn__
The constructed BayesNet.
The class for generic Hash Tables.
Definition: hashTable.h:679
void variableType(const VarType &type)
Tells the factory the current variable&#39;s type.
Copyright 2005-2020 Pierre-Henri WUILLEMIN() & Christophe GONZALES() info_at_agrum_dot_org.
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.
std::vector< std::string > stringBag__
Just to keep track of strings between two start/end calls.
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.
std::vector< factory_state > states__
State stack.
VarType
Definition: variable.h:41
HashTable< std::string, NodeId > varNameMap__
Mapping between a declared variable&#39;s name and it&#39;s node id.
void addMax(const long &max)
Adds the max value of the current range variable.
NodeId endVariableDeclaration() final
Tells the factory that we&#39;re out of a variable declaration.
Instantiation * parents__
Used when a factorized CPT is built.
bool increment__(std::vector< gum::Idx > &modCounter, List< const DiscreteVariable * > &varList)
Increment a modality counter for the fillProbaWithValuesTable__ method.
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.
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.
void fillProbaWithValuesTable__(const std::vector< std::string > &variables, const std::vector< float > &rawTable)
Fill a potential from a raw CPT.
void addMin(const long &min)
Adds the min value of the current range variable.
void variableDescription(const std::string &desc) final
Tells the factory the current variable&#39;s description.
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.
void addTick(const GUM_SCALAR &tick)
Adds a tick to the current Discretized variable.
<agrum/tools/multidim/multiDimImplementation.h>
Size Idx
Type for indexes.
Definition: types.h:53
void setCPTAndParents__(const DiscreteVariable &var, Potential< GUM_SCALAR > *table)
Sub method of setVariableCPT() which redefine the BayesNet&#39;s DAG with respect to table.
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 addModality(const std::string &name) final
Adds a modality to the current labelized variable.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
void resetParts__()
Reset the different parts used to constructed the BayesNet.
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.
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.