aGrUM  0.20.3
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/BN/BayesNetFactory.h>
42 
43 #include <agrum/PRM/elements/PRMObject.h>
44 #include <agrum/PRM/elements/PRMInstance.h>
45 #include <agrum/PRM/elements/PRMClass.h>
46 
47 namespace gum {
48  namespace prm {
49 
50  /**
51  * @class PRMSystem
52  * @headerfile system.h <agrum/PRM/system.h>
53  * @brief A PRMSystem is a container of PRMInstance and describe a
54  * relational
55  * skeleton.
56  */
57  template < typename GUM_SCALAR >
58  class PRMSystem: public PRMObject {
59  public:
60  // ========================================================================
61  /// @name Constructors & destructor.
62  // ========================================================================
63  /// @{
64 
65  /// Default constructor.
66  explicit PRMSystem(const std::string& name);
67 
68  /// Destructor.
69  virtual ~PRMSystem();
70 
71  /// @}
72  // ========================================================================
73  /// @name Getters & setters over the relational skeleton.
74  // ========================================================================
75  /// @{
76 
77  /// Returns the relation skeleton of this PRMSystem.
78  const DiGraph& skeleton() const;
79 
80  /**
81  * Returns an PRMInstance given it's NodeId in the relational skeleton.
82  *
83  * @throw NotFound Raised if no PRMInstance matches the given NodeId.
84  */
86 
87  /**
88  * Returns an PRMInstance given it's NodeId in the relational skeleton.
89  *
90  * @throw NotFound Raised if no PRMInstance matches the given NodeId.
91  */
92  const PRMInstance< GUM_SCALAR >& get(NodeId id) const;
93 
94  NodeId get(const PRMInstance< GUM_SCALAR >& i) const;
95 
96  /// @}
97  // ========================================================================
98  /// @name Getters & setters over PRMInstance and array of PRMInstance.
99  // ========================================================================
100  /// @{
101 
102  /// Returns the PRM type of this object.
103  virtual PRMObject::prm_type obj_type() const;
104 
105  /// Returns the number of PRMInstance in this PRMSystem.
106  Size size() const;
107 
108  /// Retruns true either if name is an instance or an array in this
109  /// PRMSystem.
110  bool exists(const std::string& name) const;
111 
112  /// Returns true if the given Class<GUM_SCALAR> has at least one
113  /// PRMInstance
114  /// in this PRMSystem.
115  bool isInstantiated(const PRMClass< GUM_SCALAR >& c) const;
116 
117  /// Returns true if an PRMInstance with the given name exists.
118  bool isInstance(const std::string& name) const;
119 
120  /// Returns true if an array with the given name exists.
121  bool isArray(const std::string& name) const;
122 
123  /**
124  * Returns the grounded Bayesian network of this system.
125  * @param factory The factory used to build the grounded Bayesian network.
126  */
127  void groundedBN(BayesNetFactory< GUM_SCALAR >& factory) const;
128 
129  /// Instantiate all the PRMInstance in this PRMSystem.
130  void instantiate();
131 
132  /// @}
133  // ========================================================================
134  /// @name PRMInstance handling.
135  // ========================================================================
136  /// @{
137 
138  /// Returns a reference over an PRMInstance given it's name
139  /// @throw NotFound Raised if the no PRMInstance matches name.
140  PRMInstance< GUM_SCALAR >& get(const std::string& name);
141 
142  /// Returns a constant reference over an PRMInstance given it's name
143  /// @throw NotFound Raised if the no PRMInstance matches name.
144  const PRMInstance< GUM_SCALAR >& get(const std::string& name) const;
145 
146  /// Returns the sequence of all instances of the given type
147  /// @throw NotFound Raised if there is instantiation of type.
148  const Set< PRMInstance< GUM_SCALAR >* >& get(const PRMClass< GUM_SCALAR >& type) const;
149 
150  /// Add an PRMInstance to this system.
151  /// @throw DuplicateElement Raised if an PRMInstance with the same name
152  /// already exists.
154 
155  /// @}
156  // ========================================================================
157  /// @name Array handling.
158  // ========================================================================
159  /// @{
160 
161  /// Returns the sequence of instances of a given array.
162  /// @throw NotFound Raised if no array matches name.
163  const Sequence< PRMInstance< GUM_SCALAR >* >& getArray(const std::string& name) const;
164 
165  /// Returns the type of the given array.
166  /// @throw NotFound Raised if no array matches name.
168 
169  /// Returns the type of the given array.
170  /// @throw NotFound Raised if no array matches name.
172 
173  /// @brief Add an PRMInstance to an array in this system.
174  /// If the array doesn't exists it is created.
175  /// @throw PRMTypeError Raised if i is not of the good type.
176  /// @throw DuplicateElement Raised if an PRMInstance with same name
177  /// already
178  /// exists.
180 
181  /// @brief Add an PRMInstance to an array in this system.
182  /// If the array doesn't exists it is created.
183  /// @throw PRMTypeError Raised if i is not of the good type.
184  /// @throw DuplicateElement Raised if an PRMInstance with same name
185  /// already
186  /// exists.
188 
189  /// @brief Add an array of instances in this system.
190  /// If the array doesn't exists it is created.
191  /// @throw DuplicateElement Raised if an existing array with the same name
192  /// already exists.
193  void addArray(const std::string& array, PRMClassElementContainer< GUM_SCALAR >& type);
194 
195  /// @}
196  // ========================================================================
197  /// @name Iterators.
198  // ========================================================================
199  /// @{
200 
201  /// Iterator over the PRMInstance of this PRMSystem.
202  typedef typename NodeProperty< PRMInstance< GUM_SCALAR >* >::iterator iterator;
203 
204  /// Returns an iterator over the instances in this system.
205  iterator begin();
206 
207  /// Returns a iterator at the end of the set of PRMInstance
208  /// in this PRMSystem.
209  const iterator& end();
210 
211  /// Constant Iterator over the PRMInstance of this PRMSystem.
213 
214  /// Returns a constant iterator over the instances in this system.
215  const_iterator begin() const;
216 
217  /// Returns a constant iterator at the end of the set of PRMInstance
218  /// in this PRMSystem.
219  const const_iterator& end() const;
220 
221  /// Iterator over the PRMInstance in an array in this PRMSystem.
222  typedef typename Sequence< PRMInstance< GUM_SCALAR >* >::iterator array_iterator;
223 
224  /// Returns an iterator at the beginning of the Sequence of PRMInstance
225  /// in the array named a;
226  /// @throw NotFound Raised if no array matches a.
227  array_iterator begin(const std::string& a);
228 
229  /// Returns an iterator at the end of the Sequence of PRMInstance
230  /// in the array named a.
231  /// @throw NotFound Raised if no array matches a.
232  const array_iterator& end(const std::string& a);
233 
234  /// Iterator over the PRMInstance in an array in this PRMSystem.
236 
237  /// Returns an iterator at the beginning of the Sequence of PRMInstance
238  /// in the array named a;
239  /// @throw NotFound Raised if no array matches a.
240  const_array_iterator begin(const std::string& a) const;
241 
242  /// Returns an iterator at the end of the Sequence of PRMInstance
243  /// in the array named a.
244  /// @throw NotFound Raised if no array matches a.
245  const const_array_iterator& end(const std::string& a) const;
246 
247  /// @}
248  protected:
249  /// Copy constructor. Don't use it.
250  PRMSystem(const PRMSystem< GUM_SCALAR >& from);
251 
252  /// Copy operator. Don't use it.
253  PRMSystem< GUM_SCALAR >& operator=(const PRMSystem< GUM_SCALAR >& from) = delete;
254 
255  // ========================================================================
256  /// @name Private PRMInstance handling methods and members.
257  // ========================================================================
258  /// @{
259 
260  /// The relational skeleton of this PRMSystem.
262 
263  /// The mapping between PRMInstance and their NodeId in the relational
264  /// skeleton of this PRMSystem.
266 
267  /// The mapping between PRMInstance and their names.
269 
270  /// Mapping between a class and all it's PRMInstance in this system
272 
273  /// Typedef of the pair of a Class<GUM_SCALAR> and the sequence of it's
274  /// instantiation.
278 
279  /// Mapping between arrays and their name. The first element of the pair
280  /// is the type of the array.
282 
283  /// @}
284  // ========================================================================
285  /// @name Ground BN private methods.
286  // ========================================================================
287  /// @{
288 
289  /// @brief Method which ground PRMReferenceSlot of an PRMInstance and add
290  /// arcs
291  /// in the IBayesNet.
292  /// @param instance The PRMInstance grounded by this method.
293  /// @param factory The factory used to build the grounded IBayesNet.
294  void _groundRef_(const PRMInstance< GUM_SCALAR >& instance,
295  BayesNetFactory< GUM_SCALAR >& factory) const;
296 
297  /// @brief Method which ground Atttributes and Aggregators of
298  /// an PRMInstance.
299  /// @param instance The PRMInstance grounded by this method.
300  /// @param factory The factory used to build the grounded IBayesNet.
301  void _groundAttr_(const PRMInstance< GUM_SCALAR >& instance,
302  BayesNetFactory< GUM_SCALAR >& factory) const;
303 
304  /// @brief Method which copy node's Potential of an PRMInstance to the
305  /// grounded
306  /// Bayesian network.
307  /// @param instance The PRMInstance currently grounded.
308  /// @param attr The PRMAttribute<GUM_SCALAR> for which the Potential
309  /// is
310  /// grounded.
311  /// @param factory The factory used to build the grounded IBayesNet.
312  void _groundPotential_(const PRMInstance< GUM_SCALAR >& instance,
313  const PRMAttribute< GUM_SCALAR >& attr,
314  BayesNetFactory< GUM_SCALAR >& factory) const;
315 
316  /// @brief Ground an aggregator with the given name in the grounded
317  /// IBayesNet.
318  /// @param elt The aggregator grounded.
319  /// @param name The aggregator's name in the grounded IBayesNet.
320  /// @param factory The factory used to build the grounded IBayesNet.
321  void _groundAgg_(const PRMClassElement< GUM_SCALAR >& elt,
322  const std::string& name,
323  BayesNetFactory< GUM_SCALAR >& factory) const;
324  /// @}
325  };
326 
327 
328 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
329  extern template class PRMSystem< double >;
330 #endif
331 
332 
333  } /* namespace prm */
334 
335 } /* namespace gum */
336 
337 #include <agrum/PRM/elements/PRMSystem_tpl.h>
338 
339 #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:281
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:261
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;...