aGrUM 3.1.1
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-2026 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-2026 *
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
62#include <string_view>
63
64namespace gum {
65
66 namespace prm {
88 // ==========================================================================
89 template < GUM_Numeric GUM_SCALAR >
90 class PRMFactory: public IPRMFactory {
91 public:
92 // ======================================================================
94 // ======================================================================
96
100 PRMFactory();
101
106
108 PRMFactory(const PRMFactory& from) = delete;
109
111 PRMFactory& operator=(const PRMFactory& from) = delete;
115 ~PRMFactory() override;
116
118 // ========================================================================
120 // ========================================================================
122
129 PRM< GUM_SCALAR >* prm() const;
130
135 PRMObject::prm_type currentType() const override;
136
141 PRMObject* getCurrent() override;
142 const PRMObject* getCurrent() const override;
143
155 PRMObject* closeCurrent() override;
156
160 std::string currentPackage() const override;
161
170 PRMClass< GUM_SCALAR >& retrieveClass(std::string_view name);
171
180 PRMType& retrieveType(std::string_view name);
181
182
183 /* @brief Retrieve the common PRMType of a vector of
184 *PRMClassElement<GUM_SCALAR>.
185 *
186 * The vector elts must only contains PRMClassElement<GUM_SCALAR> with a
187 *PRMType, i.e.
188 * PRMAttribute<GUM_SCALAR>, PRMAggregate and PRMSlotChain<GUM_SCALAR>. If
189 *not, a
190 *WrongClassElement is
191 * raised.
192 *
193 * A common PRMType is PRMType t such as
194 *t.isSuperTypeOf(elts[i]) for
195 * 0 < i < elts.size(), where elts is a PRMType container.
196 *
197 * @param elts A vector of PRMClassElement<GUM_SCALAR>.
198 * @return the common super PRMType of all
199 *PRMClassElement<GUM_SCALAR> un elts.
200 *
201 * @throw WrongClassElement Raised if elts contains a
202 *PRMClassElement<GUM_SCALAR>
203 * without a PRMType.
204 * @throw NotFound Raised if there exists no common super type of all
205 * PRMClassElement<GUM_SCALAR> in elts.
206 */
207 PRMType& retrieveCommonType(const std::vector< PRMClassElement< GUM_SCALAR >* >& elts);
208
210 // ======================================================================
212 // ======================================================================
214
223 void pushPackage(std::string_view name) override;
224
230 std::string popPackage() override;
231
233 // ======================================================================
235 // ======================================================================
237
251 void addImport(std::string_view name) override;
252
254 // ======================================================================
256 // ======================================================================
258
268 void startDiscreteType(std::string_view name, std::string_view super = "") override;
269
283 void addLabel(std::string_view l, std::string_view ext = "") override;
284
291 void endDiscreteType() override;
292
301 void startDiscretizedType(std::string_view name) override;
302
308 void addTick(double tick) override;
309
316 void endDiscretizedType() override;
317
330 void addRangeType(std::string_view name, long minVal, long maxVal) override;
332
335 bool isClassOrInterface(std::string_view type) const override;
336 bool isArrayInCurrentSystem(std::string_view name) const override;
338
339
342
357 void startClass(std::string_view c,
358 std::string_view ext = "",
359 const Set< std::string >* implements = nullptr,
360 bool delayInheritance = false) override;
361
365 void continueClass(std::string_view c) override;
366
374 void endClass(bool checkImplementations = true) override;
375
377 // ======================================================================
379 // ======================================================================
381
396 void startInterface(std::string_view i,
397 std::string_view ext = "",
398 bool delayInheritance = false) override;
399
403 void continueInterface(std::string_view name) override;
404
409 void addAttribute(std::string_view type, std::string_view name) override;
410
414 void endInterface() override;
415
417 // ======================================================================
419 // ======================================================================
421
442 virtual void addAttribute(PRMAttribute< GUM_SCALAR >* attr);
443
448 void startAttribute(std::string_view type,
449 std::string_view name,
450 bool scalar_atttr = false) override;
451
455 void continueAttribute(std::string_view name) override;
456
465 void addParent(std::string_view name) override;
466
486 void setRawCPFByLines(const std::vector< GUM_SCALAR >& array);
487
508 void setRawCPFByColumns(const std::vector< GUM_SCALAR >& array);
509
522 virtual void setCPFByRule(const std::vector< std::string >& labels,
523 const std::vector< GUM_SCALAR >& values);
524
537 void setCPFByRule(const std::vector< std::string >& labels,
538 const std::vector< std::string >& values) override;
539
559 void setRawCPFByFloatLines(const std::vector< float >& array) override;
560
581 void setRawCPFByFloatColumns(const std::vector< float >& array) override;
582
603 void setRawCPFByColumns(const std::vector< std::string >& array) override;
604
624 void setRawCPFByLines(const std::vector< std::string >& array) override;
625
638 void setCPFByFloatRule(const std::vector< std::string >& labels,
639 const std::vector< float >& values) override;
640
645 void endAttribute() override;
646
648 // ======================================================================
650 // ======================================================================
652
664 void addParameter(std::string_view type, std::string_view name, double value) override;
666 // ======================================================================
668 // ======================================================================
670
693 void addAggregator(std::string_view name,
694 std::string_view agg_type,
695 const std::vector< std::string >& chains,
696 const std::vector< std::string >& params,
697 std::string_view type = "") override;
698
708 void startAggregator(std::string_view name,
709 std::string_view agg_type,
710 std::string_view rv_type,
711 const std::vector< std::string >& params);
712
716 void continueAggregator(std::string_view name);
717
718
722 void endAggregator();
723
748 void addNoisyOrCompound(std::string_view name,
749 const std::vector< std::string >& chains,
750 const std::vector< float >& numbers,
751 float leak,
752 const std::vector< std::string >& label) override;
754 // ======================================================================
756 // ======================================================================
758
763 void addReferenceSlot(std::string_view type, std::string_view name, bool isArray) override;
764
766 // ======================================================================
768 // ======================================================================
770
775 void startSystem(std::string_view name) override;
776
781 void endSystem() override;
782
786 void addInstance(std::string_view type, std::string_view name) override;
787
791 void addInstance(std::string_view type,
792 std::string_view name,
793 const HashTable< std::string, double >& params) override;
794
801 void addArray(std::string_view type, std::string_view name, Size size) override;
802
806 void incArray(std::string_view l_i, std::string_view r_i) override;
807
822 void setReferenceSlot(std::string_view left_instance,
823 std::string_view left_reference,
824 std::string_view right_instance) override;
825
840 void setReferenceSlot(std::string_view l_i, std::string_view r_i) override;
841
843
844 private:
845 // TODELETE /// Decompose a string in v using dots ('.') as delimiters.
846 // TODELETE void _fill_sc_in_vector_(std::vector< std::string >& v,
847 // TODELETE const std::string& str);
848
849 // ======================================================================
851 // ======================================================================
853
855 std::string _addPrefix_(std::string_view str) const;
856
861 // current declaration.
863
866
868
870 // ======================================================================
872 // ======================================================================
874
891 PRMType* _retrieveType_(std::string_view name) const;
892
898 PRMClass< GUM_SCALAR >* _retrieveClass_(std::string_view name) const;
899
905 PRMInterface< GUM_SCALAR >* _retrieveInterface_(std::string_view name) const;
906
908 // ======================================================================
911 // ======================================================================
913
921 std::string_view name);
922
945 const std::vector< std::string >& chains,
946 std::vector< PRMClassElement< GUM_SCALAR >* >& inputs);
947
965 //
970 PRMType* _retrieveCommonType_(const std::vector< PRMClassElement< GUM_SCALAR >* >& elts);
971
979 int _typeDepth_(const PRMType* t);
980
983
987 std::string_view name);
988
992 std::string_view name);
994 // ======================================================================
997 // ======================================================================
999
1001 void _addInstance_(PRMClass< GUM_SCALAR >* type, std::string_view name);
1002
1003 // Builds all PRMSlotChain<GUM_SCALAR><PRMInstance<GUM_SCALAR>> in the
1004 // given model.
1005 // @throw OperationNotAllowed If reference slots are left un affected
1006 // ERROR? void _buildSlotChains_(PRMSystem< GUM_SCALAR >* model);
1007
1011
1017
1026
1028 // ======================================================================
1030 // ======================================================================
1032
1035 std::vector< std::string > _packages_;
1036
1038 std::vector< List< std::string >* > _namespaces_;
1039
1042
1044 std::vector< PRMObject* > _stack_;
1045
1048
1050 };
1051 } /* namespace prm */
1052} /* namespace gum */
1053
1055
1056#endif /* GUM_PRM_FACTORY_H */
Headers of IPRMFactory.
Inline implementation of PRMFactory.
The generic class for storing (ordered) sequences of objects.
Definition sequence.h:994
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:77
void addLabel(std::string_view l, std::string_view ext="") override
Add a label to the current discrete type.
void addNoisyOrCompound(std::string_view 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>.
std::string _addPrefix_(std::string_view str) const
Adds prefix to str iff prefix != "".
PRMType & retrieveType(std::string_view name)
Returns a reference over a PRMType given its name.
void startDiscreteType(std::string_view name, std::string_view super="") override
Start a discrete subtype declaration.
void addReferenceSlot(std::string_view type, std::string_view name, bool isArray) override
Tells the factory that we started declaring a slot.
void endAggregator()
Finishes an aggregate declaration.
void setRawCPFByColumns(const std::vector< GUM_SCALAR > &array)
Gives the factory the CPF in its raw form.
~PRMFactory() override
Destructor.
void endInterface() override
Tells the factory that we finished an interface declaration.
HashTable< PRMAggregate< GUM_SCALAR > *, std::vector< std::string > > _agg_params_
A mapping between aggregators and their parameters.
void setReferenceSlot(std::string_view left_instance, std::string_view left_reference, std::string_view right_instance) override
Instantiate a reference in the current model.
void startInterface(std::string_view i, std::string_view ext="", bool delayInheritance=false) override
Tells the factory that we start an interface declaration.
PRMFactory()
Default constructor.
std::string popPackage() override
Pop the current package from the package stack.
PRMSlotChain< GUM_SCALAR > * _buildSlotChain_(PRMClassElementContainer< GUM_SCALAR > *start, std::string_view name)
This methods build a PRMSlotChain<GUM_SCALAR> given a starting element and a string.
void _checkInterfaceImplementation_(PRMClass< GUM_SCALAR > *c)
Check if c implements correctly all his interfaces.
void setCPFByFloatRule(const std::vector< std::string > &labels, const std::vector< float > &values) override
Fills the CPF using a rule.
void endDiscretizedType() override
End the current discretized type declaration.
PRMFactory & operator=(const PRMFactory &from)=delete
Copy operator. Don't use it.
void continueAttribute(std::string_view name) override
Continues the declaration of an attribute.
void startDiscretizedType(std::string_view name) override
Start a discretized type declaration.
PRM< GUM_SCALAR > * prm() const
Returns a pointer on the PRM<GUM_SCALAR> created by this factory.
void addAttribute(std::string_view type, std::string_view name) override
Add an attribute to an interface.
bool _retrieveInputs_(PRMClass< GUM_SCALAR > *c, const std::vector< std::string > &chains, std::vector< PRMClassElement< GUM_SCALAR > * > &inputs)
Retrieve inputs for an PRMAggregate.
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.
std::vector< PRMObject * > _stack_
A stack used to keep track of created PRMObject.
void setRawCPFByLines(const std::vector< GUM_SCALAR > &array)
Gives the factory the CPF in its raw form.
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...
PRMClass< GUM_SCALAR > * _retrieveClass_(std::string_view name) const
Returns a pointer on a class given it's name. Used when building models, meaning that the class name ...
PRMClass< GUM_SCALAR > & retrieveClass(std::string_view name)
Returns a reference over a Class<GUM_SCALAR> given its name.
void startSystem(std::string_view name) override
Tells the factory that we started declaring a model.
void _addInstance_(PRMClass< GUM_SCALAR > *type, std::string_view name)
Adds an instance to the current model.
void pushPackage(std::string_view name) override
Define the current package.
int _typeDepth_(const PRMType *t)
Returns the inheritance depth of a PRMType.
void addInstance(std::string_view type, std::string_view name) override
Add an instance to the model.
void addAggregator(std::string_view name, std::string_view agg_type, const std::vector< std::string > &chains, const std::vector< std::string > &params, std::string_view type="") override
Add an aggregator in the current declared class.
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...
virtual void setCPFByRule(const std::vector< std::string > &labels, const std::vector< GUM_SCALAR > &values)
Fills the CPF using a rule.
void continueInterface(std::string_view name) override
Continue the declaration of an interface.
PRMObject * getCurrent() override
PRMFactory(const PRMFactory &from)=delete
Copy constructor. Don't use it.
void setRawCPFByFloatLines(const std::vector< float > &array) override
Gives the factory the CPF in its raw form.
bool isArrayInCurrentSystem(std::string_view name) const override
void startAttribute(std::string_view type, std::string_view name, bool scalar_atttr=false) override
Tells the factory that we start an attribute declaration.
PRMObject::prm_type currentType() const override
void addImport(std::string_view name) override
Add an import for namespace lookup.
std::string currentPackage() const override
void addTick(double tick) override
Add a tick to the current discretized type.
std::vector< std::string > _packages_
The prefix used for classes and types names. It is normally the namespace of the corresponding compil...
void endClass(bool checkImplementations=true) override
Tells the factory that we finished a class declaration.
PRMType * _retrieveCommonType_(const std::vector< PRMClassElement< GUM_SCALAR > * > &elts)
Retrieve the common PRMType of a vector of PRMClassElement<GUM_SCALAR>.
void continueAggregator(std::string_view name)
Conitnues an aggregator declaration.
PRMObject * closeCurrent() override
Close current object being built.
PRMClassElementContainer< GUM_SCALAR > * _checkStackContainter_(Idx i)
Adds prefix to str iff prefix != "".
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...
void addRangeType(std::string_view name, long minVal, long maxVal) override
Add a range variable type declaration.
void addParameter(std::string_view type, std::string_view name, double value) override
Add a parameter to the current class with a default value.
void startClass(std::string_view c, std::string_view ext="", const Set< std::string > *implements=nullptr, bool delayInheritance=false) override
Tells the factory that we start a class declaration.
PRMType & retrieveCommonType(const std::vector< PRMClassElement< GUM_SCALAR > * > &elts)
Returns a pointer on the PRM<GUM_SCALAR> created by this factory.
void _addParent_(PRMClassElementContainer< GUM_SCALAR > *c, PRMAttribute< GUM_SCALAR > *agg, std::string_view name)
Add a parent to an attribute.
std::vector< List< std::string > * > _namespaces_
Set of all declared namespaces.
void endAttribute() override
Tells the factory that we finished declaring an attribute.
bool isClassOrInterface(std::string_view type) const override
void startAggregator(std::string_view name, std::string_view agg_type, std::string_view rv_type, const std::vector< std::string > &params)
Start an aggregator declaration.
void endSystem() override
Tells the factory that we finished declaring a model.
PRMType * _retrieveType_(std::string_view name) const
Returns a pointer on a PRMType given it's name. Since the type can be given either with it's local na...
void addArray(std::string_view type, std::string_view name, Size size) override
Creates an array with the given number of instances of the given type.
PRMInterface< GUM_SCALAR > * _retrieveInterface_(std::string_view name) const
Returns a pointer on an interface given it's name. Used when building models, meaning that the interf...
void incArray(std::string_view l_i, std::string_view r_i) override
Add an instance to an array.
void setRawCPFByFloatColumns(const std::vector< float > &array) override
Gives the factory the CPF in its raw form.
void addParent(std::string_view name) override
Tells the factory that we add a parent to the current declared attribute.
void continueClass(std::string_view c) override
Continue the declaration of a class.
void endDiscreteType() override
End the current discrete type declaration.
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:77
prm_type
Enumeration of the different types of objects handled by a PRM.
Definition PRMObject.h:90
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:72
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:73