aGrUM  0.21.0
a C++ library for (probabilistic) graphical models
PRMSystem.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief Headers of PRMSystem.
25  *
26  * @author Lionel TORTI and Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 
29 #ifndef GUM_SYSTEM_H
30 #define GUM_SYSTEM_H
31 
32 #include <utility>
33 #include <string>
34 
35 #include <agrum/tools/core/hashTable.h>
36 #include <agrum/tools/core/sequence.h>
37 #include <agrum/tools/core/set.h>
38 
39 #include <agrum/tools/graphs/diGraph.h>
40 
41 #include <agrum/tools/variables/integerVariable.h>
42 
43 #include <agrum/BN/BayesNetFactory.h>
44 
45 #include <agrum/PRM/elements/PRMObject.h>
46 #include <agrum/PRM/elements/PRMInstance.h>
47 #include <agrum/PRM/elements/PRMClass.h>
48 
49 namespace gum {
50  namespace prm {
51 
52  /**
53  * @class PRMSystem
54  * @headerfile system.h <agrum/PRM/system.h>
55  * @brief A PRMSystem is a container of PRMInstance and describe a
56  * relational
57  * skeleton.
58  */
59  template < typename GUM_SCALAR >
60  class PRMSystem: public PRMObject {
61  public:
62  // ========================================================================
63  /// @name Constructors & destructor.
64  // ========================================================================
65  /// @{
66 
67  /// Default constructor.
68  explicit PRMSystem(const std::string& name);
69 
70  /// Destructor.
71  virtual ~PRMSystem();
72 
73  /// @}
74  // ========================================================================
75  /// @name Getters & setters over the relational skeleton.
76  // ========================================================================
77  /// @{
78 
79  /// Returns the relation skeleton of this PRMSystem.
80  const DiGraph& skeleton() const;
81 
82  /**
83  * Returns an PRMInstance given it's NodeId in the relational skeleton.
84  *
85  * @throw NotFound Raised if no PRMInstance matches the given NodeId.
86  */
88 
89  /**
90  * Returns an PRMInstance given it's NodeId in the relational skeleton.
91  *
92  * @throw NotFound Raised if no PRMInstance matches the given NodeId.
93  */
94  const PRMInstance< GUM_SCALAR >& get(NodeId id) const;
95 
96  NodeId get(const PRMInstance< GUM_SCALAR >& i) const;
97 
98  /// @}
99  // ========================================================================
100  /// @name Getters & setters over PRMInstance and array of PRMInstance.
101  // ========================================================================
102  /// @{
103 
104  /// Returns the PRM type of this object.
105  virtual PRMObject::prm_type obj_type() const;
106 
107  /// Returns the number of PRMInstance in this PRMSystem.
108  Size size() const;
109 
110  /// Retruns true either if name is an instance or an array in this
111  /// PRMSystem.
112  bool exists(const std::string& name) const;
113 
114  /// Returns true if the given Class<GUM_SCALAR> has at least one
115  /// PRMInstance
116  /// in this PRMSystem.
117  bool isInstantiated(const PRMClass< GUM_SCALAR >& c) const;
118 
119  /// Returns true if an PRMInstance with the given name exists.
120  bool isInstance(const std::string& name) const;
121 
122  /// Returns true if an array with the given name exists.
123  bool isArray(const std::string& name) const;
124 
125  /**
126  * Returns the grounded Bayesian network of this system.
127  * @param factory The factory used to build the grounded Bayesian network.
128  */
129  void groundedBN(BayesNetFactory< GUM_SCALAR >& factory) const;
130 
131  /// Instantiate all the PRMInstance in this PRMSystem.
132  void instantiate();
133 
134  /// @}
135  // ========================================================================
136  /// @name PRMInstance handling.
137  // ========================================================================
138  /// @{
139 
140  /// Returns a reference over an PRMInstance given it's name
141  /// @throw NotFound Raised if the no PRMInstance matches name.
142  PRMInstance< GUM_SCALAR >& get(const std::string& name);
143 
144  /// Returns a constant reference over an PRMInstance given it's name
145  /// @throw NotFound Raised if the no PRMInstance matches name.
146  const PRMInstance< GUM_SCALAR >& get(const std::string& name) const;
147 
148  /// Returns the sequence of all instances of the given type
149  /// @throw NotFound Raised if there is instantiation of type.
150  const Set< PRMInstance< GUM_SCALAR >* >& get(const PRMClass< GUM_SCALAR >& type) const;
151 
152  /// Add an PRMInstance to this system.
153  /// @throw DuplicateElement Raised if an PRMInstance with the same name
154  /// already exists.
156 
157  /// @}
158  // ========================================================================
159  /// @name Array handling.
160  // ========================================================================
161  /// @{
162 
163  /// Returns the sequence of instances of a given array.
164  /// @throw NotFound Raised if no array matches name.
165  const Sequence< PRMInstance< GUM_SCALAR >* >& getArray(const std::string& name) const;
166 
167  /// Returns the type of the given array.
168  /// @throw NotFound Raised if no array matches name.
170 
171  /// Returns the type of the given array.
172  /// @throw NotFound Raised if no array matches name.
174 
175  /// @brief Add an PRMInstance to an array in this system.
176  /// If the array doesn't exists it is created.
177  /// @throw PRMTypeError Raised if i is not of the good type.
178  /// @throw DuplicateElement Raised if an PRMInstance with same name
179  /// already
180  /// exists.
182 
183  /// @brief Add an PRMInstance to an array in this system.
184  /// If the array doesn't exists it is created.
185  /// @throw PRMTypeError Raised if i is not of the good type.
186  /// @throw DuplicateElement Raised if an PRMInstance with same name
187  /// already
188  /// exists.
190 
191  /// @brief Add an array of instances in this system.
192  /// If the array doesn't exists it is created.
193  /// @throw DuplicateElement Raised if an existing array with the same name
194  /// already exists.
195  void addArray(const std::string& array, PRMClassElementContainer< GUM_SCALAR >& type);
196 
197  /// @}
198  // ========================================================================
199  /// @name Iterators.
200  // ========================================================================
201  /// @{
202 
203  /// Iterator over the PRMInstance of this PRMSystem.
204  typedef typename NodeProperty< PRMInstance< GUM_SCALAR >* >::iterator iterator;
205 
206  /// Returns an iterator over the instances in this system.
207  iterator begin();
208 
209  /// Returns a iterator at the end of the set of PRMInstance
210  /// in this PRMSystem.
211  const iterator& end();
212 
213  /// Constant Iterator over the PRMInstance of this PRMSystem.
215 
216  /// Returns a constant iterator over the instances in this system.
217  const_iterator begin() const;
218 
219  /// Returns a constant iterator at the end of the set of PRMInstance
220  /// in this PRMSystem.
221  const const_iterator& end() const;
222 
223  /// Iterator over the PRMInstance in an array in this PRMSystem.
224  typedef typename Sequence< PRMInstance< GUM_SCALAR >* >::iterator array_iterator;
225 
226  /// Returns an iterator at the beginning of the Sequence of PRMInstance
227  /// in the array named a;
228  /// @throw NotFound Raised if no array matches a.
229  array_iterator begin(const std::string& a);
230 
231  /// Returns an iterator at the end of the Sequence of PRMInstance
232  /// in the array named a.
233  /// @throw NotFound Raised if no array matches a.
234  const array_iterator& end(const std::string& a);
235 
236  /// Iterator over the PRMInstance in an array in this PRMSystem.
238 
239  /// Returns an iterator at the beginning of the Sequence of PRMInstance
240  /// in the array named a;
241  /// @throw NotFound Raised if no array matches a.
242  const_array_iterator begin(const std::string& a) const;
243 
244  /// Returns an iterator at the end of the Sequence of PRMInstance
245  /// in the array named a.
246  /// @throw NotFound Raised if no array matches a.
247  const const_array_iterator& end(const std::string& a) const;
248 
249  /// @}
250  protected:
251  /// Copy constructor. Don't use it.
252  PRMSystem(const PRMSystem< GUM_SCALAR >& from);
253 
254  /// Copy operator. Don't use it.
255  PRMSystem< GUM_SCALAR >& operator=(const PRMSystem< GUM_SCALAR >& from) = delete;
256 
257  // ========================================================================
258  /// @name Private PRMInstance handling methods and members.
259  // ========================================================================
260  /// @{
261 
262  /// The relational skeleton of this PRMSystem.
264 
265  /// The mapping between PRMInstance and their NodeId in the relational
266  /// skeleton of this PRMSystem.
268 
269  /// The mapping between PRMInstance and their names.
271 
272  /// Mapping between a class and all it's PRMInstance in this system
274 
275  /// Typedef of the pair of a Class<GUM_SCALAR> and the sequence of it's
276  /// instantiation.
280 
281  /// Mapping between arrays and their name. The first element of the pair
282  /// is the type of the array.
284 
285  /// @}
286  // ========================================================================
287  /// @name Ground BN private methods.
288  // ========================================================================
289  /// @{
290 
291  /// @brief Method which ground PRMReferenceSlot of an PRMInstance and add
292  /// arcs
293  /// in the IBayesNet.
294  /// @param instance The PRMInstance grounded by this method.
295  /// @param factory The factory used to build the grounded IBayesNet.
296  void _groundRef_(const PRMInstance< GUM_SCALAR >& instance,
297  BayesNetFactory< GUM_SCALAR >& factory) const;
298 
299  /// @brief Method which ground Atttributes and Aggregators of
300  /// an PRMInstance.
301  /// @param instance The PRMInstance grounded by this method.
302  /// @param factory The factory used to build the grounded IBayesNet.
303  void _groundAttr_(const PRMInstance< GUM_SCALAR >& instance,
304  BayesNetFactory< GUM_SCALAR >& factory) const;
305 
306  /// @brief Method which copy node's Potential of an PRMInstance to the
307  /// grounded
308  /// Bayesian network.
309  /// @param instance The PRMInstance currently grounded.
310  /// @param attr The PRMAttribute<GUM_SCALAR> for which the Potential
311  /// is
312  /// grounded.
313  /// @param factory The factory used to build the grounded IBayesNet.
314  void _groundPotential_(const PRMInstance< GUM_SCALAR >& instance,
315  const PRMAttribute< GUM_SCALAR >& attr,
316  BayesNetFactory< GUM_SCALAR >& factory) const;
317 
318  /// @brief Ground an aggregator with the given name in the grounded
319  /// IBayesNet.
320  /// @param elt The aggregator grounded.
321  /// @param name The aggregator's name in the grounded IBayesNet.
322  /// @param factory The factory used to build the grounded IBayesNet.
323  void _groundAgg_(const PRMClassElement< GUM_SCALAR >& elt,
324  const std::string& name,
325  BayesNetFactory< GUM_SCALAR >& factory) const;
326  /// @}
327  };
328 
329 
330 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
331  extern template class PRMSystem< double >;
332 #endif
333 
334 
335  } /* namespace prm */
336 
337 } /* namespace gum */
338 
339 #include <agrum/PRM/elements/PRMSystem_tpl.h>
340 
341 #endif // GUM_SYSTEM_H
const PRMClassElementContainer< GUM_SCALAR > & getArrayType(const std::string &name) const
Returns the type of the given array.
PRMClassElementContainer< GUM_SCALAR > & getArrayType(const std::string &name)
Returns the type of the given array.
void _groundAgg_(const PRMClassElement< GUM_SCALAR > &elt, const std::string &name, BayesNetFactory< GUM_SCALAR > &factory) const
Ground an aggregator with the given name in the grounded IBayesNet.
iterator begin()
Returns an iterator over the instances in this system.
PRMSystem(const PRMSystem< GUM_SCALAR > &from)
Copy constructor. Don&#39;t use it.
Definition: PRMSystem_tpl.h:60
virtual ~PRMSystem()
Destructor.
Definition: PRMSystem_tpl.h:46
NodeId add(const std::string &array, PRMInstance< GUM_SCALAR > *i)
Add an PRMInstance to an array in this system. If the array doesn&#39;t exists it is created.
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
PRMInstance< GUM_SCALAR > & get(const std::string &name)
Returns a reference over an PRMInstance given it&#39;s name.
bool isInstantiated(const PRMClass< GUM_SCALAR > &c) const
Returns true if the given Class<GUM_SCALAR> has at least one PRMInstance in this PRMSystem.
const PRMInstance< GUM_SCALAR > & get(NodeId id) const
Returns an PRMInstance given it&#39;s NodeId in the relational skeleton.
const const_array_iterator & end(const std::string &a) const
Returns an iterator at the end of the Sequence of PRMInstance in the array named a.
const PRMInstance< GUM_SCALAR > & get(const std::string &name) const
Returns a constant reference over an PRMInstance given it&#39;s name.
NodeId add(const std::string &array, PRMInstance< GUM_SCALAR > &i)
Add an PRMInstance to an array in this system. If the array doesn&#39;t exists it is created.
HashTable< std::string, model_pair > arrayMap_
Mapping between arrays and their name. The first element of the pair is the type of the array...
Definition: PRMSystem.h:283
const_iterator begin() const
Returns a constant iterator over the instances in this system.
PRMSystem(const std::string &name)
Default constructor.
Definition: PRMSystem_tpl.h:41
DiGraph skeleton_
The relational skeleton of this PRMSystem.
Definition: PRMSystem.h:263
NodeId add(PRMInstance< GUM_SCALAR > *i)
Add an PRMInstance to this system.
Definition: PRMSystem_tpl.h:67
const DiGraph & skeleton() const
Returns the relation skeleton of this PRMSystem.
bool isInstance(const std::string &name) const
Returns true if an PRMInstance with the given name exists.
virtual PRMObject::prm_type obj_type() const
Returns the PRM type of this object.
PRMInstance< GUM_SCALAR > & get(NodeId id)
Returns an PRMInstance given it&#39;s NodeId in the relational skeleton.
NodeId get(const PRMInstance< GUM_SCALAR > &i) const
Returns the relation skeleton of this PRMSystem.
void groundedBN(BayesNetFactory< GUM_SCALAR > &factory) const
Returns the grounded Bayesian network of this system.
Definition: PRMSystem_tpl.h:88
ParamScopeData(const std::string &s, const PRMReferenceSlot< GUM_SCALAR > &ref, Idx d)
void instantiate()
Instantiate all the PRMInstance in this PRMSystem.
const const_iterator & end() const
Returns a constant iterator at the end of the set of PRMInstance in this PRMSystem.
void _groundAttr_(const PRMInstance< GUM_SCALAR > &instance, BayesNetFactory< GUM_SCALAR > &factory) const
Method which ground Atttributes and Aggregators of an PRMInstance.
bool exists(const std::string &name) const
Retruns true either if name is an instance or an array in this PRMSystem.
Size size() const
Returns the number of PRMInstance in this PRMSystem.
const array_iterator & end(const std::string &a)
Returns an iterator at the end of the Sequence of PRMInstance in the array named a.
bool isArray(const std::string &name) const
Returns true if an array with the given name exists.
const Sequence< PRMInstance< GUM_SCALAR > *> & getArray(const std::string &name) const
Returns the sequence of instances of a given array.
void _groundPotential_(const PRMInstance< GUM_SCALAR > &instance, const PRMAttribute< GUM_SCALAR > &attr, BayesNetFactory< GUM_SCALAR > &factory) const
Method which copy node&#39;s Potential of an PRMInstance to the grounded Bayesian network.
PRMSystem< GUM_SCALAR > & operator=(const PRMSystem< GUM_SCALAR > &from)=delete
Copy operator. Don&#39;t use it.
void _groundRef_(const PRMInstance< GUM_SCALAR > &instance, BayesNetFactory< GUM_SCALAR > &factory) const
Method which ground PRMReferenceSlot of an PRMInstance and add arcs in the IBayesNet.
const iterator & end()
Returns a iterator at the end of the set of PRMInstance in this PRMSystem.
void addArray(const std::string &array, PRMClassElementContainer< GUM_SCALAR > &type)
Add an array of instances in this system. If the array doesn&#39;t exists it is created.
array_iterator begin(const std::string &a)
Returns an iterator at the beginning of the Sequence of PRMInstance in the array named a;...
const_array_iterator begin(const std::string &a) const
Returns an iterator at the beginning of the Sequence of PRMInstance in the array named a;...