aGrUM  0.16.0
PRMFactory.h
Go to the documentation of this file.
1 
30 #ifndef GUM_PRM_FACTORY_H
31 #define GUM_PRM_FACTORY_H
32 
33 #include <iostream>
34 #include <limits>
35 #include <sstream>
36 #include <string>
37 #include <vector>
38 
39 #include <agrum/agrum.h>
40 #include <agrum/core/list.h>
41 
44 
45 #include <agrum/PRM/IPRMFactory.h>
46 #include <agrum/PRM/PRM.h>
47 #include <agrum/PRM/utils_prm.h>
48 
49 namespace gum {
50 
51  namespace prm {
73  // ==========================================================================
74  template < typename GUM_SCALAR >
75  class PRMFactory : public IPRMFactory {
76  public:
77  // ======================================================================
79  // ======================================================================
81 
85  PRMFactory();
86 
90  PRMFactory(PRM< GUM_SCALAR >* prm);
91 
93  PRMFactory(const PRMFactory& from) = delete;
94 
96  PRMFactory& operator=(const PRMFactory& from) = delete;
100  virtual ~PRMFactory();
101 
103  // ========================================================================
105  // ========================================================================
107 
114  PRM< GUM_SCALAR >* prm() const;
115 
120  virtual PRMObject::prm_type currentType() const override;
121 
126  virtual PRMObject* getCurrent() override;
127  virtual const PRMObject* getCurrent() const override;
128 
140  virtual PRMObject* closeCurrent() override;
141 
145  virtual std::string currentPackage() const override;
146 
155  PRMClass< GUM_SCALAR >& retrieveClass(const std::string& name);
156 
165  PRMType& retrieveType(const std::string& name);
166 
167 
168  /* @brief Retrieve the common PRMType of a vector of
169  *PRMClassElement<GUM_SCALAR>.
170  *
171  * The vector elts must only contains PRMClassElement<GUM_SCALAR> with a
172  *PRMType, i.e.
173  * PRMAttribute<GUM_SCALAR>, PRMAggregate and PRMSlotChain<GUM_SCALAR>. If
174  *not, a
175  *WrongClassElement is
176  * raised.
177  *
178  * A common PRMType is PRMType t such as
179  *t.isSuperTypeOf(elts[i]) for
180  * 0 < i < elts.size(), where elts is a PRMType container.
181  *
182  * @param elts A vector of PRMClassElement<GUM_SCALAR>.
183  * @return the common super PRMType of all
184  *PRMClassElement<GUM_SCALAR> un elts.
185  *
186  * @throw WrongClassElement Raised if elts contains a
187  *PRMClassElement<GUM_SCALAR>
188  * without a PRMType.
189  * @throw NotFound Raised if there exists no common super type of all
190  * PRMClassElement<GUM_SCALAR> in elts.
191  */
192  PRMType& retrieveCommonType(
193  const std::vector< PRMClassElement< GUM_SCALAR >* >& elts);
194 
196  // ======================================================================
198  // ======================================================================
200 
209  virtual void pushPackage(const std::string& name) override;
210 
216  virtual std::string popPackage() override;
217 
219  // ======================================================================
221  // ======================================================================
223 
237  virtual void addImport(const std::string& name) override;
238 
240  // ======================================================================
242  // ======================================================================
244 
254  virtual void startDiscreteType(const std::string& name,
255  std::string super = "") override;
256 
270  virtual void addLabel(const std::string& l, std::string ext = "") override;
271 
278  virtual void endDiscreteType() override;
279 
288  virtual void startDiscretizedType(const std::string& name) override;
289 
295  virtual void addTick(double tick) override;
296 
303  virtual void endDiscretizedType() override;
304 
317  virtual void
318  addRangeType(const std::string& name, long minVal, long maxVal) override;
320 
323  virtual bool isClassOrInterface(const std::string& type) const override;
324  virtual bool isArrayInCurrentSystem(const std::string& name) const override;
326 
327 
330 
345  virtual void startClass(const std::string& c,
346  const std::string& ext = "",
347  const Set< std::string >* implements = nullptr,
348  bool delayInheritance = false) override;
349 
353  virtual void continueClass(const std::string& c) override;
354 
362  virtual void endClass(bool checkImplementations = true) override;
363 
365  // ======================================================================
367  // ======================================================================
369 
384  virtual void startInterface(const std::string& i,
385  const std::string& ext = "",
386  bool delayInheritance = false) override;
387 
391  virtual void continueInterface(const std::string& name) override;
392 
397  virtual void addAttribute(const std::string& type,
398  const std::string& name) override;
399 
403  virtual void endInterface() override;
404 
406  // ======================================================================
408  // ======================================================================
410 
431  virtual void addAttribute(PRMAttribute< GUM_SCALAR >* attr);
432 
437  virtual void startAttribute(const std::string& type,
438  const std::string& name,
439  bool scalar_atttr = false) override;
440 
444  virtual void continueAttribute(const std::string& name) override;
445 
454  virtual void addParent(const std::string& name) override;
455 
475  void setRawCPFByLines(const std::vector< GUM_SCALAR >& array);
476 
497  void setRawCPFByColumns(const std::vector< GUM_SCALAR >& array);
498 
511  virtual void setCPFByRule(const std::vector< std::string >& labels,
512  const std::vector< GUM_SCALAR >& values);
513 
526  virtual void setCPFByRule(const std::vector< std::string >& labels,
527  const std::vector< std::string >& values) override;
528 
548  virtual void
549  setRawCPFByFloatLines(const std::vector< float >& array) override;
550 
571  virtual void
572  setRawCPFByFloatColumns(const std::vector< float >& array) override;
573 
594  virtual void
595  setRawCPFByColumns(const std::vector< std::string >& array) override;
596 
616  virtual void
617  setRawCPFByLines(const std::vector< std::string >& array) override;
618 
631  virtual void setCPFByFloatRule(const std::vector< std::string >& labels,
632  const std::vector< float >& values) override;
633 
638  virtual void endAttribute() override;
639 
641  // ======================================================================
643  // ======================================================================
645 
657  void addParameter(const std::string& type,
658  const std::string& name,
659  double value) override;
661  // ======================================================================
663  // ======================================================================
665 
688  virtual void addAggregator(const std::string& name,
689  const std::string& agg_type,
690  const std::vector< std::string >& chains,
691  const std::vector< std::string >& params,
692  std::string type = "") override;
693 
703  void startAggregator(const std::string& name,
704  const std::string& agg_type,
705  const std::string& rv_type,
706  const std::vector< std::string >& params);
707 
711  void continueAggregator(const std::string& name);
712 
713 
717  void endAggregator();
718 
743  virtual void
744  addNoisyOrCompound(const std::string& name,
745  const std::vector< std::string >& chains,
746  const std::vector< float >& numbers,
747  float leak,
748  const std::vector< std::string >& label) override;
750  // ======================================================================
752  // ======================================================================
754 
759  virtual void addReferenceSlot(const std::string& type,
760  const std::string& name,
761  bool isArray) override;
762 
764  // ======================================================================
766  // ======================================================================
768 
773  virtual void startSystem(const std::string& name) override;
774 
779  virtual void endSystem() override;
780 
784  virtual void addInstance(const std::string& type,
785  const std::string& name) override;
786 
790  void addInstance(const std::string& type,
791  const std::string& name,
792  const HashTable< std::string, double >& params) override;
793 
800  virtual void addArray(const std::string& type,
801  const std::string& name,
802  Size size) override;
803 
807  virtual void incArray(const std::string& l_i,
808  const std::string& r_i) override;
809 
824  virtual void setReferenceSlot(const std::string& left_instance,
825  const std::string& left_reference,
826  const std::string& right_instance) override;
827 
842  virtual void setReferenceSlot(const std::string& l_i,
843  const std::string& r_i) override;
844 
846 
847  private:
849  void __fill_sc_in_vector(std::vector< std::string >& v,
850  const std::string& str);
851 
852  // ======================================================================
854  // ======================================================================
856 
858  std::string __addPrefix(const std::string& str) const;
859 
864  // current declaration.
865  PRMObject* __checkStack(Idx i, PRMObject::prm_type obj_type);
866 
867  PRMClassElement< GUM_SCALAR >* __checkStack(
869 
870  PRMClassElementContainer< GUM_SCALAR >* __checkStackContainter(Idx i);
871 
873  // ======================================================================
875  // ======================================================================
877 
894  PRMType* __retrieveType(const std::string& name) const;
895 
901  PRMClass< GUM_SCALAR >* __retrieveClass(const std::string& name) const;
902 
908  PRMInterface< GUM_SCALAR >*
909  __retrieveInterface(const std::string& name) const;
910 
912  // ======================================================================
915  // ======================================================================
917 
924  PRMSlotChain< GUM_SCALAR >*
925  __buildSlotChain(PRMClassElementContainer< GUM_SCALAR >* start,
926  const std::string& name);
927 
949  bool __retrieveInputs(PRMClass< GUM_SCALAR >* c,
950  const std::vector< std::string >& chains,
951  std::vector< PRMClassElement< GUM_SCALAR >* >& inputs);
952 
970  //
975  PRMType* __retrieveCommonType(
976  const std::vector< PRMClassElement< GUM_SCALAR >* >& elts);
977 
985  int __typeDepth(const PRMType* t);
986 
988  void __checkInterfaceImplementation(PRMClass< GUM_SCALAR >* c);
989 
991  void __addParent(PRMClassElementContainer< GUM_SCALAR >* c,
992  PRMAttribute< GUM_SCALAR >* agg,
993  const std::string& name);
994 
996  void __addParent(PRMClass< GUM_SCALAR >* c,
997  PRMAggregate< GUM_SCALAR >* agg,
998  const std::string& name);
1000  // ======================================================================
1003  // ======================================================================
1005 
1007  void __addInstance(PRMClass< GUM_SCALAR >* type, const std::string& name);
1008 
1013  void __buildSlotChains(PRMSystem< GUM_SCALAR >* model);
1014 
1017  void __buildAggregateCPF(PRMSystem< GUM_SCALAR >* model);
1018 
1020  void __instantiateSlotChain(PRMSystem< GUM_SCALAR >* model,
1021  PRMInstance< GUM_SCALAR >* inst,
1022  PRMReferenceSlot< GUM_SCALAR >* ref,
1023  PRMSlotChain< GUM_SCALAR >* sc);
1024 
1030  std::string
1031  __retrieveInstanceSequence(PRMInstance< GUM_SCALAR >* inst,
1032  Sequence< PRMInstance< GUM_SCALAR >* >& seq,
1033  PRMSlotChain< GUM_SCALAR >* sc);
1034 
1036  // ======================================================================
1038  // ======================================================================
1040 
1043  std::vector< std::string > __packages;
1044 
1046  std::vector< List< std::string >* > __namespaces;
1047 
1050 
1052  std::vector< PRMObject* > __stack;
1053 
1055  HashTable< PRMAggregate< GUM_SCALAR >*, std::vector< std::string > >
1057 
1059  };
1060  } /* namespace prm */
1061 } /* namespace gum */
1062 
1063 #include <agrum/PRM/PRMFactory_tpl.h>
1064 
1065 #endif /* GUM_PRM_FACTORY_H */
PRM< GUM_SCALAR > * __prm
The pointer on the PRM<GUM_SCALAR> built by this factory.
Definition: PRMFactory.h:1049
std::vector< PRMObject *> __stack
A stack used to keep track of created PRMObject.
Definition: PRMFactory.h:1052
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:1056
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
void __addInstance(PRMClass< GUM_SCALAR > *type, const std::string &name)
Adds a instance to the current model.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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:69
virtual PRMObject::prm_type currentType() const override
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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:1043
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...
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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:66
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:1046
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:48
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.