aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
PRMFactory.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2025 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41
48
49#ifndef GUM_PRM_FACTORY_H
50#define GUM_PRM_FACTORY_H
51
52#include <iostream>
53#include <limits>
54#include <sstream>
55#include <string>
56#include <vector>
57
58#include <agrum/agrum.h>
59
61
62namespace gum {
63
64 namespace prm {
86 // ==========================================================================
87 template < typename GUM_SCALAR >
88 class PRMFactory: public IPRMFactory {
89 public:
90 // ======================================================================
92 // ======================================================================
94
98 PRMFactory();
99
104
106 PRMFactory(const PRMFactory& from) = delete;
107
109 PRMFactory& operator=(const PRMFactory& from) = delete;
113 virtual ~PRMFactory();
114
116 // ========================================================================
118 // ========================================================================
120
127 PRM< GUM_SCALAR >* prm() const;
128
133 virtual PRMObject::prm_type currentType() const override;
134
139 virtual PRMObject* getCurrent() override;
140 virtual const PRMObject* getCurrent() const override;
141
153 virtual PRMObject* closeCurrent() override;
154
158 virtual std::string currentPackage() const override;
159
168 PRMClass< GUM_SCALAR >& retrieveClass(const std::string& name);
169
178 PRMType& retrieveType(const std::string& name);
179
180
181 /* @brief Retrieve the common PRMType of a vector of
182 *PRMClassElement<GUM_SCALAR>.
183 *
184 * The vector elts must only contains PRMClassElement<GUM_SCALAR> with a
185 *PRMType, i.e.
186 * PRMAttribute<GUM_SCALAR>, PRMAggregate and PRMSlotChain<GUM_SCALAR>. If
187 *not, a
188 *WrongClassElement is
189 * raised.
190 *
191 * A common PRMType is PRMType t such as
192 *t.isSuperTypeOf(elts[i]) for
193 * 0 < i < elts.size(), where elts is a PRMType container.
194 *
195 * @param elts A vector of PRMClassElement<GUM_SCALAR>.
196 * @return the common super PRMType of all
197 *PRMClassElement<GUM_SCALAR> un elts.
198 *
199 * @throw WrongClassElement Raised if elts contains a
200 *PRMClassElement<GUM_SCALAR>
201 * without a PRMType.
202 * @throw NotFound Raised if there exists no common super type of all
203 * PRMClassElement<GUM_SCALAR> in elts.
204 */
205 PRMType& retrieveCommonType(const std::vector< PRMClassElement< GUM_SCALAR >* >& elts);
206
208 // ======================================================================
210 // ======================================================================
212
221 virtual void pushPackage(const std::string& name) override;
222
228 virtual std::string popPackage() override;
229
231 // ======================================================================
233 // ======================================================================
235
249 virtual void addImport(const std::string& name) override;
250
252 // ======================================================================
254 // ======================================================================
256
266 virtual void startDiscreteType(const std::string& name, std::string super = "") override;
267
281 virtual void addLabel(const std::string& l, std::string ext = "") override;
282
289 virtual void endDiscreteType() override;
290
299 virtual void startDiscretizedType(const std::string& name) override;
300
306 virtual void addTick(double tick) override;
307
314 virtual void endDiscretizedType() override;
315
328 virtual void addRangeType(const std::string& name, long minVal, long maxVal) override;
330
333 virtual bool isClassOrInterface(const std::string& type) const override;
334 virtual bool isArrayInCurrentSystem(const std::string& name) const override;
336
337
340
355 virtual void startClass(const std::string& c,
356 const std::string& ext = "",
357 const Set< std::string >* implements = nullptr,
358 bool delayInheritance = false) override;
359
363 virtual void continueClass(const std::string& c) override;
364
372 virtual void endClass(bool checkImplementations = true) override;
373
375 // ======================================================================
377 // ======================================================================
379
394 virtual void startInterface(const std::string& i,
395 const std::string& ext = "",
396 bool delayInheritance = false) override;
397
401 virtual void continueInterface(const std::string& name) override;
402
407 virtual void addAttribute(const std::string& type, const std::string& name) override;
408
412 virtual void endInterface() override;
413
415 // ======================================================================
417 // ======================================================================
419
440 virtual void addAttribute(PRMAttribute< GUM_SCALAR >* attr);
441
446 virtual void startAttribute(const std::string& type,
447 const std::string& name,
448 bool scalar_atttr = false) override;
449
453 virtual void continueAttribute(const std::string& name) override;
454
463 virtual void addParent(const std::string& name) override;
464
484 void setRawCPFByLines(const std::vector< GUM_SCALAR >& array);
485
506 void setRawCPFByColumns(const std::vector< GUM_SCALAR >& array);
507
520 virtual void setCPFByRule(const std::vector< std::string >& labels,
521 const std::vector< GUM_SCALAR >& values);
522
535 virtual void setCPFByRule(const std::vector< std::string >& labels,
536 const std::vector< std::string >& values) override;
537
557 virtual void setRawCPFByFloatLines(const std::vector< float >& array) override;
558
579 virtual void setRawCPFByFloatColumns(const std::vector< float >& array) override;
580
601 virtual void setRawCPFByColumns(const std::vector< std::string >& array) override;
602
622 virtual void setRawCPFByLines(const std::vector< std::string >& array) override;
623
636 virtual void setCPFByFloatRule(const std::vector< std::string >& labels,
637 const std::vector< float >& values) override;
638
643 virtual void endAttribute() override;
644
646 // ======================================================================
648 // ======================================================================
650
662 void addParameter(const std::string& type, const std::string& name, double value) override;
664 // ======================================================================
666 // ======================================================================
668
691 virtual void addAggregator(const std::string& name,
692 const std::string& agg_type,
693 const std::vector< std::string >& chains,
694 const std::vector< std::string >& params,
695 std::string type = "") override;
696
706 void startAggregator(const std::string& name,
707 const std::string& agg_type,
708 const std::string& rv_type,
709 const std::vector< std::string >& params);
710
714 void continueAggregator(const std::string& name);
715
716
720 void endAggregator();
721
746 virtual void addNoisyOrCompound(const std::string& name,
747 const std::vector< std::string >& chains,
748 const std::vector< float >& numbers,
749 float leak,
750 const std::vector< std::string >& label) override;
752 // ======================================================================
754 // ======================================================================
756
761 virtual void
762 addReferenceSlot(const std::string& type, const std::string& name, bool isArray) override;
763
765 // ======================================================================
767 // ======================================================================
769
774 virtual void startSystem(const std::string& name) override;
775
780 virtual void endSystem() override;
781
785 virtual void addInstance(const std::string& type, 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, const std::string& name, Size size) override;
801
805 virtual void incArray(const std::string& l_i, 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, const std::string& r_i) override;
840
842
843 private:
844 // TODELETE /// Decompose a string in v using dots ('.') as delimiters.
845 // TODELETE void _fill_sc_in_vector_(std::vector< std::string >& v,
846 // TODELETE const std::string& str);
847
848 // ======================================================================
850 // ======================================================================
852
854 std::string _addPrefix_(const std::string& str) const;
855
860 // current declaration.
862
865
867
869 // ======================================================================
871 // ======================================================================
873
890 PRMType* _retrieveType_(const std::string& name) const;
891
897 PRMClass< GUM_SCALAR >* _retrieveClass_(const std::string& name) const;
898
904 PRMInterface< GUM_SCALAR >* _retrieveInterface_(const std::string& name) const;
905
907 // ======================================================================
910 // ======================================================================
912
920 const std::string& name);
921
944 const std::vector< std::string >& chains,
945 std::vector< PRMClassElement< GUM_SCALAR >* >& inputs);
946
964 //
969 PRMType* _retrieveCommonType_(const std::vector< PRMClassElement< GUM_SCALAR >* >& elts);
970
978 int _typeDepth_(const PRMType* t);
979
982
986 const std::string& name);
987
991 const std::string& name);
993 // ======================================================================
996 // ======================================================================
998
1000 void _addInstance_(PRMClass< GUM_SCALAR >* type, const std::string& name);
1001
1002 // Builds all PRMSlotChain<GUM_SCALAR><PRMInstance<GUM_SCALAR>> in the
1003 // given model.
1004 // @throw OperationNotAllowed If reference slots are left un affected
1005 // ERROR? void _buildSlotChains_(PRMSystem< GUM_SCALAR >* model);
1006
1010
1016
1025
1027 // ======================================================================
1029 // ======================================================================
1031
1034 std::vector< std::string > _packages_;
1035
1037 std::vector< List< std::string >* > _namespaces_;
1038
1041
1043 std::vector< PRMObject* > _stack_;
1044
1047
1049 };
1050 } /* namespace prm */
1051} /* namespace gum */
1052
1054
1055#endif /* GUM_PRM_FACTORY_H */
Headers of IPRMFactory.
Inline implementation of PRMFactory.
The generic class for storing (ordered) sequences of objects.
Definition sequence.h:972
PRMAttribute is a member of a Class in a PRM.
<agrum/PRM/classElementContainer.h>
Abstract class representing an element of PRM class.
ClassElementType
Returns true if obj_ptr is of type PRMReferenceSlot.
A PRMClass is an object of a PRM representing a fragment of a Bayesian network which can be instantia...
Definition PRMClass.h:75
virtual bool isArrayInCurrentSystem(const std::string &name) const override
PRMClassElementContainer< GUM_SCALAR > * _checkStackContainter_(Idx i)
Adds prefix to str iff prefix != "".
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.
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 gi...
virtual ~PRMFactory()
Destructor.
void endAggregator()
Finishes an aggregate declaration.
virtual void addImport(const std::string &name) override
Add an import for namespace lookup.
HashTable< PRMAggregate< GUM_SCALAR > *, std::vector< std::string > > _agg_params_
A mapping between aggregators and their parameters.
std::string _addPrefix_(const std::string &str) const
Adds prefix to str iff prefix != "".
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 addInstance(const std::string &type, const std::string &name) override
Add an instance to the model.
void addParameter(const std::string &type, const std::string &name, double value) override
Add a parameter to the current class with a default value.
virtual void incArray(const std::string &l_i, const std::string &r_i) override
Add an instance to an array.
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.
virtual void addParent(const std::string &name) override
Tells the factory that we add a parent to the current declared attribute.
virtual void pushPackage(const std::string &name) override
Define the current package.
virtual std::string currentPackage() const override
virtual bool isClassOrInterface(const std::string &type) const override
void setRawCPFByLines(const std::vector< GUM_SCALAR > &array)
Gives the factory the CPF in its raw form.
PRMFactory & operator=(const PRMFactory &from)=delete
Copy operator. Don't use it.
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 addTick(double tick) override
Add a tick to the current discretized type.
PRMType & retrieveType(const std::string &name)
Returns a reference over a PRMType given its name.
virtual void endSystem() override
Tells the factory that we finished declaring a model.
virtual void addRangeType(const std::string &name, long minVal, long maxVal) override
Add a range variable type 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.
PRMType * _retrieveCommonType_(const std::vector< PRMClassElement< GUM_SCALAR > * > &elts)
Retrieve the common PRMType of a vector of PRMClassElement<GUM_SCALAR>.
virtual void setCPFByFloatRule(const std::vector< std::string > &labels, const std::vector< float > &values) override
Fills the CPF using a rule.
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.
PRMFactory()
Default constructor.
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.
PRMClass< GUM_SCALAR > & retrieveClass(const std::string &name)
Returns a reference over a Class<GUM_SCALAR> given its name.
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>.
PRMInterface< GUM_SCALAR > * _retrieveInterface_(const std::string &name) const
Returns a pointer on an interface given it's name. Used when building models, meaning that the interf...
std::vector< PRMObject * > _stack_
A stack used to keep track of created PRMObject.
PRM< GUM_SCALAR > * _prm_
The pointer on the PRM<GUM_SCALAR> built by this factory.
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 seekin...
virtual void setRawCPFByFloatColumns(const std::vector< float > &array) override
Gives the factory the CPF in its raw form.
PRMClass< GUM_SCALAR > * _retrieveClass_(const std::string &name) const
Returns a pointer on a class given it's name. Used when building models, meaning that the class name ...
PRMType & retrieveCommonType(const std::vector< PRMClassElement< GUM_SCALAR > * > &elts)
Returns a pointer on the PRM<GUM_SCALAR> created by this factory.
bool _retrieveInputs_(PRMClass< GUM_SCALAR > *c, const std::vector< std::string > &chains, std::vector< PRMClassElement< GUM_SCALAR > * > &inputs)
Retrieve inputs for an PRMAggregate.
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...
void _addParent_(PRMClassElementContainer< GUM_SCALAR > *c, PRMAttribute< GUM_SCALAR > *agg, const std::string &name)
Add a parent to an attribute.
PRMFactory(const PRMFactory &from)=delete
Copy constructor. Don't use it.
virtual void setCPFByRule(const std::vector< std::string > &labels, const std::vector< GUM_SCALAR > &values)
Fills the CPF using a rule.
virtual void addReferenceSlot(const std::string &type, const std::string &name, bool isArray) override
Tells the factory that we started declaring a slot.
virtual void endAttribute() override
Tells the factory that we finished declaring an attribute.
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.
virtual std::string popPackage() override
Pop the current package from the package stack.
virtual PRMObject::prm_type currentType() const override
void setRawCPFByColumns(const std::vector< GUM_SCALAR > &array)
Gives the factory the CPF in its raw form.
int _typeDepth_(const PRMType *t)
Returns the inheritance depth of a PRMType.
virtual PRMObject * getCurrent() override
virtual void continueClass(const std::string &c) override
Continue the declaration of a class.
virtual void endClass(bool checkImplementations=true) override
Tells the factory that we finished a class declaration.
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.
std::vector< std::string > _packages_
The prefix used for classes and types names. It is normally the namespace of the corresponding compil...
void continueAggregator(const std::string &name)
Conitnues an aggregator declaration.
virtual void addAttribute(const std::string &type, const std::string &name) override
Add an attribute to an interface.
virtual void continueAttribute(const std::string &name) override
Continues the declaration of an attribute.
virtual void endDiscretizedType() override
End the current discretized type declaration.
virtual void endInterface() override
Tells the factory that we finished an interface declaration.
virtual void startDiscreteType(const std::string &name, std::string super="") override
Start a discrete subtype declaration.
PRM< GUM_SCALAR > * prm() const
Returns a pointer on the PRM<GUM_SCALAR> created by this factory.
PRMType * _retrieveType_(const std::string &name) const
Returns a pointer on a PRMType given it's name. Since the type can be given either with it's local na...
virtual void startDiscretizedType(const std::string &name) override
Start a discretized type declaration.
void _addInstance_(PRMClass< GUM_SCALAR > *type, const std::string &name)
Adds a instance to the current model.
std::vector< List< std::string > * > _namespaces_
Set of all declared namespaces.
virtual PRMObject * closeCurrent() override
Close current object being built.
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 endDiscreteType() override
End the current discrete type declaration.
virtual void continueInterface(const std::string &name) override
Continue the declaration of an interface.
void _checkInterfaceImplementation_(PRMClass< GUM_SCALAR > *c)
Check if c implements correctly all his interfaces.
virtual void addLabel(const std::string &l, std::string ext="") override
Add a label to the current discrete type.
An PRMInstance is a Bayesian network fragment defined by a Class and used in a PRMSystem.
Definition PRMInstance.h:79
An PRMInterface is implemented by a Class<GUM_SCALAR> and defines a set of PRMReferenceSlot<GUM_SCALA...
Abstract base class for any element defined in a PRM.
Definition PRMObject.h:75
prm_type
Enumeration of the different types of objects handled by a PRM.
Definition PRMObject.h:88
A PRMReferenceSlot represent a relation between two PRMClassElementContainer.
A PRMSlotChain represents a sequence of gum::prm::PRMClassElement<GUM_SCALAR> where the n-1 first gum...
A PRMSystem is a container of PRMInstance and describe a relational skeleton.
Definition PRMSystem.h:70
This is a decoration of the DiscreteVariable class.
Definition PRMType.h:78
This class represents a Probabilistic Relational PRMSystem<GUM_SCALAR>.
Definition PRM.h:74
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Size Idx
Type for indexes.
Definition types.h:79
namespace for all probabilistic relational models entities
Definition agrum.h:68
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
non-template interface-like parent for every PRM Factory
Definition IPRMFactory.h:71