aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
PRMType.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 Class.
25  *
26  * @author Lionel TORTI and Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 
29 #ifndef GUM_RANDOM_VARIABLE_TYPE_H
30 #define GUM_RANDOM_VARIABLE_TYPE_H
31 
32 #include <sstream>
33 #include <vector>
34 
35 #include <agrum/agrum.h>
36 
37 #include <agrum/tools/multidim/implementations/multiDimArray.h>
38 #include <agrum/tools/multidim/implementations/multiDimImplementation.h>
39 #include <agrum/tools/variables/discreteVariable.h>
40 #include <agrum/tools/variables/labelizedVariable.h>
41 
42 #include <agrum/PRM/elements/PRMObject.h>
43 #include <agrum/PRM/utils_prm.h>
44 
45 namespace gum {
46  namespace prm {
47 
48  template < typename GUM_SCALAR >
49  class PRMFactory;
50 
51  /**
52  * @class PRMType
53  * @brief This is a decoration of the DiscreteVariable class.
54  * The name of the DiscreteVariable will be the name of the type (i.e. of
55  * the PRMObject).
56  * As for all PRMObject, a type's name is unique in a given PRM, so equality
57  * tests will be based on the types names.
58  *
59  * Since MultiDim use pointers to handle DiscreteVariables, it is necessary
60  * to create a new instance of a type for each PRMAttribute.
61  */
62  class PRMType: public PRMObject {
63  public:
64  // ==========================================================================
65  /// @name Friends of PRMType
66  // ==========================================================================
67  /// @{
68 
69  template < typename GUM_SCALAR >
70  friend class PRMFactory;
71 
72  /// @}
73  // ==========================================================================
74  /// @name Static methods for primitive types
75  // ==========================================================================
76  /// @{
77 
78  /// Returns a pointer on type boolean.
79  static PRMType* boolean() {
80  LabelizedVariable var("boolean", "Boolean variable", 0);
81  var.addLabel("false");
82  var.addLabel("true");
83  return new PRMType(var);
84  }
85 
86  /// @}
87  // ==========================================================================
88  /// @name Constructors & destructor
89  // ==========================================================================
90  /// @{
91 
92  /**
93  * Default Constructor.
94  * A copy is made of var.
95  */
96  explicit PRMType(const DiscreteVariable& var);
97 
98  /**
99  * Sub type constructor.
100  * A copy is made of var.
101  * @throw OperationNotAllowed Raised if label_map is invalid.
102  */
103  PRMType(PRMType& super_type,
104  const std::vector< Idx >& label_map,
105  const DiscreteVariable& var);
106 
107  /**
108  * Copy constructor.
109  * The DiscreteVariable is copied.
110  */
111  PRMType(const PRMType& from);
112 
113  /**
114  * Destructor.
115  */
116  virtual ~PRMType();
117 
118  /// @}
119  // ==========================================================================
120  /// @name Getters & setters
121  // ==========================================================================
122  /// @{
123 
124  /// Return a reference on the DiscreteVariable contained in this.
125  DiscreteVariable& variable();
126 
127  /// Return a reference on the DiscreteVariable contained in this.
128  const DiscreteVariable& variable() const;
129 
130  /// @}
131  // ==========================================================================
132  /// @name Operators
133  // ==========================================================================
134  /// @{
135 
136  /**
137  * Indirection on the DiscreteVariable contained in this.
138  */
139  DiscreteVariable& operator*();
140 
141  /**
142  * Constant indirection on the DiscreteVariable contained in this.
143  */
144  const DiscreteVariable& operator*() const;
145 
146  /**
147  * Dereference the DiscreteVariable contained in this.
148  */
149  DiscreteVariable* operator->();
150 
151  /**
152  * Constant dereference the DiscreteVariable contained in this.
153  */
154  DiscreteVariable const* operator->() const;
155 
156  /**
157  * Equality operator.
158  */
159  bool operator==(const PRMObject& from) const;
160 
161  /**
162  * Difference operator.
163  */
164  bool operator!=(const PRMObject& from) const;
165 
166  /// @}
167  // ==========================================================================
168  /// @name Getters & setters
169  // ==========================================================================
170  /// @{
171 
172  /**
173  * Implementation of the pure virtual method of PRMObject.
174  */
175  virtual prm_type obj_type() const;
176 
177  /**
178  * Returns the name of this object.
179  */
180  const std::string& name() const;
181 
182  /**
183  * Returns true if this type is a sub-type.
184  */
185  bool isSubType() const;
186 
187  /**
188  * Returns true if this is a subtype of super.
189  * Note that two types that are equal are also subtypes,
190  * if a == b then a.isSubTypeOf(b) == b.isSubTypeOf(a) == true.
191  */
192  bool isSubTypeOf(const PRMType& super) const;
193 
194  /**
195  * Returns true if this is a super type of t.
196  */
197  bool isSuperTypeOf(const PRMType& t) const;
198 
199  /**
200  * Returns the super type of this type.
201  * @throw NotFound Raised if this type has no super type.
202  */
203  PRMType& superType();
204 
205  /**
206  * Returns the super type of this type.
207  * @throw NotFound Raised if this type has no super type.
208  */
209  const PRMType& superType() const;
210 
211  /**
212  * @brief Changes the PRMType of this PRMType super.
213  *
214  * You can only change this PRMType super only if t and this->superType()
215  * are
216  * equal. Thus you should use this method only if you want to change the
217  * DiscreteVariable pointer of this PRMType super.
218  *
219  * This is useful to maintain consistence between PRMAttribute's PRMType
220  * and
221  * their CPF.
222  *
223  * @param t The PRMType to replace this PRMType super.
224  *
225  * @throw OperationNotAllowed If this PRMType has no super.
226  * @throw PRMTypeError If t is not equal to this PRMType super.
227  */
228  void setSuper(PRMType& t);
229 
230  /**
231  * Returns the vector in which the i-th element is the Idx of the super
232  * type's label for the i-th label of this.
233  * @throw NotFound Raised if this type has no super type.
234  */
235  const std::vector< Idx >& label_map() const;
236 
237  /// @}
238  // ==========================================================================
239  private:
240  // ==========================================================================
241  /// @name Private methods
242  // ==========================================================================
243  /// @{
244 
245  /**
246  * Move constructor.
247  * Not implemented, will raise an exception.
248  */
249  PRMType(PRMType&& from);
250 
251  /**
252  * Copy operator. Not implemented.
253  */
254  PRMType& operator=(const PRMType& from);
255 
256  /**
257  * Move operator. Not implemented.
258  */
259  PRMType& operator=(PRMType&& from);
260 
261  /// @}
262  // ==========================================================================
263  /// @name Private members.
264  // ==========================================================================
265  /// @{
266 
267  /// Returns true if this is a valid type or subtype.
268  bool _isValid_() const;
269 
270  /// Used at construction to set a unique name to this class underlying
271  /// DiscreteVariable.
272  void _updateName_();
273 
274  /// The discrete variable
275  DiscreteVariable* _var_;
276 
277  /// The super type of this, if any.
278  PRMType* _superType_;
279 
280  /// A vector in which the i-th element is the Idx of the super
281  /// type's label for the i-th label of this.
282  std::vector< Idx >* _label_map_;
283 
284  /// @}
285  };
286 
287  } /* namespace prm */
288 
289 } /* namespace gum */
290 
291 #ifndef GUM_NO_INLINE
292 # include <agrum/PRM/elements/PRMType_inl.h>
293 #endif // GUM_NO_INLINE
294 
295 #endif /* GUM_CLASS_H */