aGrUM  0.14.2
PRMFactory.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_PRM_FACTORY_H
28 #define GUM_PRM_FACTORY_H
29 
30 #include <iostream>
31 #include <limits>
32 #include <sstream>
33 #include <string>
34 #include <vector>
35 
36 #include <agrum/agrum.h>
37 #include <agrum/core/list.h>
38 
41 
42 #include <agrum/PRM/IPRMFactory.h>
43 #include <agrum/PRM/PRM.h>
44 #include <agrum/PRM/utils_prm.h>
45 
46 namespace gum {
47 
48  namespace prm {
70  // ==========================================================================
71  template < typename GUM_SCALAR >
72  class PRMFactory : public IPRMFactory {
73  public:
74  // ======================================================================
76  // ======================================================================
78 
82  PRMFactory();
83 
87  PRMFactory(PRM< GUM_SCALAR >* prm);
88 
90  PRMFactory(const PRMFactory& from) = delete;
91 
93  PRMFactory& operator=(const PRMFactory& from) = delete;
97  virtual ~PRMFactory();
98 
100  // ========================================================================
102  // ========================================================================
104 
111  PRM< GUM_SCALAR >* prm() const;
112 
117  virtual PRMObject::prm_type currentType() const override;
118 
123  virtual PRMObject* getCurrent() override;
124  virtual const PRMObject* getCurrent() const override;
125 
137  virtual PRMObject* closeCurrent() override;
138 
142  virtual std::string currentPackage() const override;
143 
152  PRMClass< GUM_SCALAR >& retrieveClass(const std::string& name);
153 
162  PRMType& retrieveType(const std::string& name);
163 
164 
165  /* @brief Retrieve the common PRMType of a vector of
166  *PRMClassElement<GUM_SCALAR>.
167  *
168  * The vector elts must only contains PRMClassElement<GUM_SCALAR> with a
169  *PRMType, i.e.
170  * PRMAttribute<GUM_SCALAR>, PRMAggregate and PRMSlotChain<GUM_SCALAR>. If
171  *not, a
172  *WrongClassElement is
173  * raised.
174  *
175  * A common PRMType is PRMType t such as
176  *t.isSuperTypeOf(elts[i]) for
177  * 0 < i < elts.size(), where elts is a PRMType container.
178  *
179  * @param elts A vector of PRMClassElement<GUM_SCALAR>.
180  * @return the common super PRMType of all
181  *PRMClassElement<GUM_SCALAR> un elts.
182  *
183  * @throw WrongClassElement Raised if elts contains a
184  *PRMClassElement<GUM_SCALAR>
185  * without a PRMType.
186  * @throw NotFound Raised if there exists no common super type of all
187  * PRMClassElement<GUM_SCALAR> in elts.
188  */
189  PRMType& retrieveCommonType(
190  const std::vector< PRMClassElement< GUM_SCALAR >* >& elts);
191 
193  // ======================================================================
195  // ======================================================================
197 
206  virtual void pushPackage(const std::string& name) override;
207 
213  virtual std::string popPackage() override;
214 
216  // ======================================================================
218  // ======================================================================
220 
234  virtual void addImport(const std::string& name) override;
235 
237  // ======================================================================
239  // ======================================================================
241 
251  virtual void startDiscreteType(const std::string& name,
252  std::string super = "") override;
253 
267  virtual void addLabel(const std::string& l, std::string ext = "") override;
268 
275  virtual void endDiscreteType() override;
276 
285  virtual void startDiscretizedType(const std::string& name) override;
286 
292  virtual void addTick(double tick) override;
293 
300  virtual void endDiscretizedType() override;
301 
314  virtual void
315  addRangeType(const std::string& name, long minVal, long maxVal) override;
317 
320  virtual bool isClassOrInterface(const std::string& type) const override;
321  virtual bool isArrayInCurrentSystem(const std::string& name) const override;
323 
324 
327 
342  virtual void startClass(const std::string& c,
343  const std::string& ext = "",
344  const Set< std::string >* implements = nullptr,
345  bool delayInheritance = false) override;
346 
350  virtual void continueClass(const std::string& c) override;
351 
359  virtual void endClass(bool checkImplementations = true) override;
360 
362  // ======================================================================
364  // ======================================================================
366 
381  virtual void startInterface(const std::string& i,
382  const std::string& ext = "",
383  bool delayInheritance = false) override;
384 
388  virtual void continueInterface(const std::string& name) override;
389 
394  virtual void addAttribute(const std::string& type,
395  const std::string& name) override;
396 
400  virtual void endInterface() override;
401 
403  // ======================================================================
405  // ======================================================================
407 
428  virtual void addAttribute(PRMAttribute< GUM_SCALAR >* attr);
429 
434  virtual void startAttribute(const std::string& type,
435  const std::string& name,
436  bool scalar_atttr = false) override;
437 
441  virtual void continueAttribute(const std::string& name) override;
442 
451  virtual void addParent(const std::string& name) override;
452 
472  void setRawCPFByLines(const std::vector< GUM_SCALAR >& array);
473 
494  void setRawCPFByColumns(const std::vector< GUM_SCALAR >& array);
495 
508  virtual void setCPFByRule(const std::vector< std::string >& labels,
509  const std::vector< GUM_SCALAR >& values);
510 
523  virtual void setCPFByRule(const std::vector< std::string >& labels,
524  const std::vector< std::string >& values) override;
525 
545  virtual void
546  setRawCPFByFloatLines(const std::vector< float >& array) override;
547 
568  virtual void
569  setRawCPFByFloatColumns(const std::vector< float >& array) override;
570 
591  virtual void
592  setRawCPFByColumns(const std::vector< std::string >& array) override;
593 
613  virtual void
614  setRawCPFByLines(const std::vector< std::string >& array) override;
615 
628  virtual void setCPFByFloatRule(const std::vector< std::string >& labels,
629  const std::vector< float >& values) override;
630 
635  virtual void endAttribute() override;
636 
638  // ======================================================================
640  // ======================================================================
642 
654  void addParameter(const std::string& type,
655  const std::string& name,
656  double value) override;
658  // ======================================================================
660  // ======================================================================
662 
685  virtual void addAggregator(const std::string& name,
686  const std::string& agg_type,
687  const std::vector< std::string >& chains,
688  const std::vector< std::string >& params,
689  std::string type = "") override;
690 
700  void startAggregator(const std::string& name,
701  const std::string& agg_type,
702  const std::string& rv_type,
703  const std::vector< std::string >& params);
704 
708  void continueAggregator(const std::string& name);
709 
710 
714  void endAggregator();
715 
740  virtual void
741  addNoisyOrCompound(const std::string& name,
742  const std::vector< std::string >& chains,
743  const std::vector< float >& numbers,
744  float leak,
745  const std::vector< std::string >& label) override;
747  // ======================================================================
749  // ======================================================================
751 
756  virtual void addReferenceSlot(const std::string& type,
757  const std::string& name,
758  bool isArray) override;
759 
761  // ======================================================================
763  // ======================================================================
765 
770  virtual void startSystem(const std::string& name) override;
771 
776  virtual void endSystem() override;
777 
781  virtual void addInstance(const std::string& type,
782  const std::string& name) override;
783 
787  void addInstance(const std::string& type,
788  const std::string& name,
789  const HashTable< std::string, double >& params) override;
790 
797  virtual void addArray(const std::string& type,
798  const std::string& name,
799  Size size) override;
800 
804  virtual void incArray(const std::string& l_i,
805  const std::string& r_i) override;
806 
821  virtual void setReferenceSlot(const std::string& left_instance,
822  const std::string& left_reference,
823  const std::string& right_instance) override;
824 
839  virtual void setReferenceSlot(const std::string& l_i,
840  const std::string& r_i) override;
841 
843 
844  private:
846  void __fill_sc_in_vector(std::vector< std::string >& v,
847  const std::string& str);
848 
849  // ======================================================================
851  // ======================================================================
853 
855  std::string __addPrefix(const std::string& str) const;
856 
861  // current declaration.
862  PRMObject* __checkStack(Idx i, PRMObject::prm_type obj_type);
863 
864  PRMClassElement< GUM_SCALAR >* __checkStack(
866 
867  PRMClassElementContainer< GUM_SCALAR >* __checkStackContainter(Idx i);
868 
870  // ======================================================================
872  // ======================================================================
874 
891  PRMType* __retrieveType(const std::string& name) const;
892 
898  PRMClass< GUM_SCALAR >* __retrieveClass(const std::string& name) const;
899 
905  PRMInterface< GUM_SCALAR >*
906  __retrieveInterface(const std::string& name) const;
907 
909  // ======================================================================
912  // ======================================================================
914 
921  PRMSlotChain< GUM_SCALAR >*
922  __buildSlotChain(PRMClassElementContainer< GUM_SCALAR >* start,
923  const std::string& name);
924 
946  bool __retrieveInputs(PRMClass< GUM_SCALAR >* c,
947  const std::vector< std::string >& chains,
948  std::vector< PRMClassElement< GUM_SCALAR >* >& inputs);
949 
967  //
972  PRMType* __retrieveCommonType(
973  const std::vector< PRMClassElement< GUM_SCALAR >* >& elts);
974 
982  int __typeDepth(const PRMType* t);
983 
985  void __checkInterfaceImplementation(PRMClass< GUM_SCALAR >* c);
986 
988  void __addParent(PRMClassElementContainer< GUM_SCALAR >* c,
989  PRMAttribute< GUM_SCALAR >* agg,
990  const std::string& name);
991 
993  void __addParent(PRMClass< GUM_SCALAR >* c,
994  PRMAggregate< GUM_SCALAR >* agg,
995  const std::string& name);
997  // ======================================================================
1000  // ======================================================================
1002 
1004  void __addInstance(PRMClass< GUM_SCALAR >* type, const std::string& name);
1005 
1010  void __buildSlotChains(PRMSystem< GUM_SCALAR >* model);
1011 
1014  void __buildAggregateCPF(PRMSystem< GUM_SCALAR >* model);
1015 
1017  void __instantiateSlotChain(PRMSystem< GUM_SCALAR >* model,
1018  PRMInstance< GUM_SCALAR >* inst,
1019  PRMReferenceSlot< GUM_SCALAR >* ref,
1020  PRMSlotChain< GUM_SCALAR >* sc);
1021 
1027  std::string
1028  __retrieveInstanceSequence(PRMInstance< GUM_SCALAR >* inst,
1029  Sequence< PRMInstance< GUM_SCALAR >* >& seq,
1030  PRMSlotChain< GUM_SCALAR >* sc);
1031 
1033  // ======================================================================
1035  // ======================================================================
1037 
1040  std::vector< std::string > __packages;
1041 
1043  std::vector< List< std::string >* > __namespaces;
1044 
1047 
1049  std::vector< PRMObject* > __stack;
1050 
1052  HashTable< PRMAggregate< GUM_SCALAR >*, std::vector< std::string > >
1054 
1056  };
1057  } /* namespace prm */
1058 } /* namespace gum */
1059 
1060 #include <agrum/PRM/PRMFactory_tpl.h>
1061 
1062 #endif /* GUM_PRM_FACTORY_H */
PRM< GUM_SCALAR > * __prm
The pointer on the PRM<GUM_SCALAR> built by this factory.
Definition: PRMFactory.h:1046
std::vector< PRMObject *> __stack
A stack used to keep track of created PRMObject.
Definition: PRMFactory.h:1049
virtual void endInterface() override
Tells the factory that we finished an interface declaration.
virtual void pushPackage(const std::string &name) override
Define the current package.
virtual void addAggregator(const std::string &name, const std::string &agg_type, const std::vector< std::string > &chains, const std::vector< std::string > &params, std::string type="") override
Add an aggregator in the current declared class.
virtual void addRangeType(const std::string &name, long minVal, long maxVal) override
Add a range variable type declaration.
HashTable< PRMAggregate< GUM_SCALAR > *, std::vector< std::string > > __agg_params
A mapping between aggregators and their parameters.
Definition: PRMFactory.h:1053
Base class for discrete random variable.
void endAggregator()
Finishes an aggregate declaration.
virtual void setReferenceSlot(const std::string &left_instance, const std::string &left_reference, const std::string &right_instance) override
Instantiate a reference in the current model.
virtual ~PRMFactory()
Destructor.
virtual void addNoisyOrCompound(const std::string &name, const std::vector< std::string > &chains, const std::vector< float > &numbers, float leak, const std::vector< std::string > &label) override
Add a compound noisy-or as an PRMAttribute<GUM_SCALAR> to the current Class<GUM_SCALAR>.
virtual std::string popPackage() override
Pop the current package from the package stack.
virtual void addInstance(const std::string &type, const std::string &name) override
Add an instance to the model.
virtual void setRawCPFByFloatColumns(const std::vector< float > &array) override
Gives the factory the CPF in its raw form.
virtual void addArray(const std::string &type, const std::string &name, Size size) override
Creates an array with the given number of instances of the given type.
virtual void setCPFByFloatRule(const std::vector< std::string > &labels, const std::vector< float > &values) override
Fills the CPF using a rule.
Headers of MultiDimSparse.
PRMType * __retrieveType(const std::string &name) const
Returns a pointer on a PRMType given it&#39;s name. Since the type can be given either with it&#39;s local na...
virtual void continueClass(const std::string &c) override
Continue the declaration of a class.
PRMClassElementContainer< GUM_SCALAR > * __checkStackContainter(Idx i)
Adds __prefix to str iff __prefix != "".
virtual void continueInterface(const std::string &name) override
Continue the declaration of an interface.
Headers of PRM.
virtual void addImport(const std::string &name) override
Add an import for namespace lookup.
virtual void startDiscretizedType(const std::string &name) override
Start a discretized type declaration.
void __buildAggregateCPF(PRMSystem< GUM_SCALAR > *model)
Builds all Aggregates CPF in the given model. This must be called after all the slot chains have been...
PRMClass< GUM_SCALAR > & retrieveClass(const std::string &name)
Returns a reference over a Class<GUM_SCALAR> given its name.
virtual void endAttribute() override
Tells the factory that we finished declaring an attribute.
virtual void startInterface(const std::string &i, const std::string &ext="", bool delayInheritance=false) override
Tells the factory that we start an interface declaration.
virtual void addReferenceSlot(const std::string &type, const std::string &name, bool isArray) override
Tells the factory that we started declaring a slot.
PRMType & retrieveCommonType(const std::vector< PRMClassElement< GUM_SCALAR > * > &elts)
Returns a pointer on the PRM<GUM_SCALAR> created by this factory.
PRMType & retrieveType(const std::string &name)
Returns a reference over a PRMType given its name.
Headers of IPRMFactory.
void __addInstance(PRMClass< GUM_SCALAR > *type, const std::string &name)
Adds a instance to the current model.
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
virtual void addAttribute(const std::string &type, const std::string &name) override
Add an attribute to an interface.
virtual void startClass(const std::string &c, const std::string &ext="", const Set< std::string > *implements=nullptr, bool delayInheritance=false) override
Tells the factory that we start a class declaration.
void __instantiateSlotChain(PRMSystem< GUM_SCALAR > *model, PRMInstance< GUM_SCALAR > *inst, PRMReferenceSlot< GUM_SCALAR > *ref, PRMSlotChain< GUM_SCALAR > *sc)
Instantiate a slot chain in the given instance.
void __buildSlotChains(PRMSystem< GUM_SCALAR > *model)
Builds all PRMSlotChain<GUM_SCALAR><PRMInstance<GUM_SCALAR>> in the given model.
void continueAggregator(const std::string &name)
Conitnues an aggregator declaration.
virtual void incArray(const std::string &l_i, const std::string &r_i) override
Add an instance to an array.
virtual void startSystem(const std::string &name) override
Tells the factory that we started declaring a model.
virtual void setRawCPFByFloatLines(const std::vector< float > &array) override
Gives the factory the CPF in its raw form.
virtual void startAttribute(const std::string &type, const std::string &name, bool scalar_atttr=false) override
Tells the factory that we start an attribute declaration.
virtual std::string currentPackage() const override
PRMType * __retrieveCommonType(const std::vector< PRMClassElement< GUM_SCALAR > * > &elts)
Retrieve the common PRMType of a vector of PRMClassElement<GUM_SCALAR>.
void setRawCPFByColumns(const std::vector< GUM_SCALAR > &array)
Gives the factory the CPF in its raw form.
prm_type
Enumeration of the different types of objects handled by a PRM.
Definition: PRMObject.h:66
virtual PRMObject::prm_type currentType() const override
Generic class for manipulating lists.
void __fill_sc_in_vector(std::vector< std::string > &v, const std::string &str)
Decompose a string in v using dots (&#39;.&#39;) as delimiters.
virtual void setCPFByRule(const std::vector< std::string > &labels, const std::vector< GUM_SCALAR > &values)
Fills the CPF using a rule.
PRMSlotChain< GUM_SCALAR > * __buildSlotChain(PRMClassElementContainer< GUM_SCALAR > *start, const std::string &name)
This methods build a PRMSlotChain<GUM_SCALAR> given a starting element and a string.
void __addParent(PRMClassElementContainer< GUM_SCALAR > *c, PRMAttribute< GUM_SCALAR > *agg, const std::string &name)
Add a parent to an attribute.
virtual PRMObject * getCurrent() override
std::vector< std::string > __packages
The prefix used for classes and types names. It is normally the namespace of the corresponding compil...
Definition: PRMFactory.h:1040
PRMFactory()
Default constructor.
std::string __retrieveInstanceSequence(PRMInstance< GUM_SCALAR > *inst, Sequence< PRMInstance< GUM_SCALAR > * > &seq, PRMSlotChain< GUM_SCALAR > *sc)
Fill seq with the sequence of instance build using inst as the instantiation of sc->__class and seeki...
Inline implementation of PRMFactory.
virtual void endClass(bool checkImplementations=true) override
Tells the factory that we finished a class declaration.
virtual PRMObject * closeCurrent() override
Close current object being built.
void setRawCPFByLines(const std::vector< GUM_SCALAR > &array)
Gives the factory the CPF in its raw form.
void __checkInterfaceImplementation(PRMClass< GUM_SCALAR > *c)
Check if c implements correctly all his interfaces.
This class represents a Probabilistic Relational PRMSystem<GUM_SCALAR>.
Definition: PRM.h:63
std::string __addPrefix(const std::string &str) const
Adds __prefix to str iff __prefix != "".
virtual void startDiscreteType(const std::string &name, std::string super="") override
Start a discrete subtype declaration.
PRMClass< GUM_SCALAR > * __retrieveClass(const std::string &name) const
Returns a pointer on a class given it&#39;s name. Used when building models, meaning that the class name ...
virtual void endDiscretizedType() override
End the current discretized type declaration.
PRMObject * __checkStack(Idx i, PRMObject::prm_type obj_type)
Return a pointer on a PRMObject at __stack.size() - i position after checking the type of the object ...
virtual void endDiscreteType() override
End the current discrete type declaration.
virtual void continueAttribute(const std::string &name) override
Continues the declaration of an attribute.
void addParameter(const std::string &type, const std::string &name, double value) override
Add a parameter to the current class with a default value.
std::vector< List< std::string > *> __namespaces
Set of all declared namespaces.
Definition: PRMFactory.h:1043
int __typeDepth(const PRMType *t)
Returns the inheritance depth of a PRMType.
virtual bool isClassOrInterface(const std::string &type) const override
virtual void addLabel(const std::string &l, std::string ext="") override
Add a label to the current discrete type.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
PRMFactory & operator=(const PRMFactory &from)=delete
Copy operator. Don&#39;t use it.
virtual void endSystem() override
Tells the factory that we finished declaring a model.
PRMInterface< GUM_SCALAR > * __retrieveInterface(const std::string &name) const
Returns a pointer on an interface given it&#39;s name. Used when building models, meaning that the interf...
PRM< GUM_SCALAR > * prm() const
Returns a pointer on the PRM<GUM_SCALAR> created by this factory.
virtual bool isArrayInCurrentSystem(const std::string &name) const override
void startAggregator(const std::string &name, const std::string &agg_type, const std::string &rv_type, const std::vector< std::string > &params)
Start an aggregator declaration.
virtual void addParent(const std::string &name) override
Tells the factory that we add a parent to the current declared attribute.
virtual void addTick(double tick) override
Add a tick to the current discretized type.
ClassElementType
Returns true if obj_ptr is of type PRMReferenceSlot.
bool __retrieveInputs(PRMClass< GUM_SCALAR > *c, const std::vector< std::string > &chains, std::vector< PRMClassElement< GUM_SCALAR > * > &inputs)
Retrieve inputs for an PRMAggregate.