aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
PRMAggregate.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 gum::PRMAggregate.
25  *
26  * @author Lionel TORTI and Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 
29 #ifndef GUM_AGGREGATE_H
30 #define GUM_AGGREGATE_H
31 
32 #include <memory>
33 #include <string>
34 
35 #include <agrum/tools/multidim/aggregators/multiDimAggregator.h>
36 #include <agrum/tools/multidim/implementations/multiDimImplementation.h>
37 #include <agrum/tools/multidim/potential.h>
38 
39 #include <agrum/PRM/elements/PRMType.h>
40 #include <agrum/PRM/elements/PRMClassElement.h>
41 #include <agrum/PRM/elements/PRMAttribute.h>
42 #include <agrum/PRM/elements/PRMAggregate.h>
43 #include <agrum/PRM/elements/PRMScalarAttribute.h>
44 
45 
46 namespace gum {
47  namespace prm {
48 
49  /**
50  * @class gum::PRMAggregate
51  * @headerfile aggregate.h <agrum/PRM/elements/aggregate.h>
52  * @brief Defines an aggregate in a PRM.
53  *
54  * An PRMAggregate is defined by its type, and by the slot chain on which it
55  * is applied.
56  *
57  * The name of an aggregate is always of the form:
58  * AggregateType::type(slot_chain[, param])
59  *
60  * An aggregate is a member of a class.
61  *
62  * An aggregate in a PRM is used to summarized information about n-ary
63  * relations.
64  *
65  * @see PRM PRMFactory Class PRMSlotChain
66  * @ingroup prm_group
67  */
68  template < typename GUM_SCALAR >
70  friend class PRMClass< GUM_SCALAR >;
71 
72  public:
73  // ========================================================================
74  /// @name Built-in types.
75  // ========================================================================
76  /// @{
77 
78  /// The different type of aggregates we can have.
79  enum class AggregateType : char
80  {
81  MIN,
82  MAX,
83  COUNT,
84  EXISTS,
85  FORALL,
86  OR,
87  AND,
88  AMPLITUDE,
89  MEDIAN,
90  SUM
91  };
92 
93  /**
94  * Static method which returns the AggregateType given its string
95  * representation.
96  * We suppose that str is given either entirely in lower case or upper
97  * case. It will also match if only the first letter is in upper case.
98  *
99  * @throw Raise NotFound exception if no matches is found.
100  */
101  static AggregateType str2enum(const std::string& str) {
102  if (toLower(str) == "min") {
103  return AggregateType::MIN;
104  } else if (toLower(str) == "max") {
105  return AggregateType::MAX;
106  } else if (toLower(str) == "count") {
107  return AggregateType::COUNT;
108  } else if (toLower(str) == "exists") {
109  return AggregateType::EXISTS;
110  } else if (toLower(str) == "or") {
111  return AggregateType::OR;
112  } else if (toLower(str) == "and") {
113  return AggregateType::AND;
114  } else if (toLower(str) == "forall") {
115  return AggregateType::FORALL;
116  } else if (toLower(str) == "amplitude") {
117  return AggregateType::AMPLITUDE;
118  } else if (toLower(str) == "median") {
119  return AggregateType::MEDIAN;
120  } else if (toLower(str) == "sum") {
121  return AggregateType::SUM;
122  } else {
123  std::string msg = "Unknown aggregate: ";
124  msg.append(str);
125  GUM_ERROR(NotFound, msg)
126  }
127  }
128 
129  /// @}
130  // ========================================================================
131  /// @name Constructor & destructor.
132  // ========================================================================
133  /// @{
134 
135  /**
136  * Default constructor.
137  * @param name The name of this aggregate.
138  * @param aggType The aggregate type of this aggregate.
139  * @param rvType The random variable type of this aggregate, which is
140  * copied.
141  */
142  PRMAggregate(const std::string& name, AggregateType aggType, const PRMType& rvType);
143 
144  /**
145  * Default constructor.
146  * @param name The name of this aggregate.
147  * @param aggType The aggregate type of this aggregate.
148  * @param rvType The random variable type of this aggregate, which is
149  * copied.
150  * @param label The index of the label on which this aggregate applies.
151  */
152  PRMAggregate(const std::string& name,
153  AggregateType aggType,
154  const PRMType& rvType,
155  Idx label);
156 
157  /// Destructor.
158  virtual ~PRMAggregate();
159 
160  /// @}
161  // ========================================================================
162  /// @name Getters and Setters.
163  // ========================================================================
164  /// @{
165 
166  /// See gum::PRMClassElement::elt_type().
167  virtual typename PRMClassElement< GUM_SCALAR >::ClassElementType elt_type() const;
168 
169  /// Returns the aggregate of *this.
170  AggregateType agg_type() const;
171 
172  /**
173  * Returns the label's index on which this aggregate applies.
174  * @throw OperationNotAllowed Raised if the aggregate does not applies
175  * on a label.
176  */
177  Idx label() const;
178  const std::string& labelValue() const;
179 
180  /**
181  * @brief Returns the shared_ptr holding this Aggregate label.
182  *
183  * This is used for inherited Aggregates to share labels in O3PRM.
184  */
185  std::shared_ptr< Idx > sharedLabel() const;
186 
187  /**
188  * @brief Sets the shared_ptr of this Aggregate.
189  *
190  * This is used for inherited aggregates to share labels in O3PRM.
191  */
192  void sharedLabel(std::shared_ptr< Idx > label);
193 
194  /**
195  * @brief Set the aggregator's label.
196  */
197  void setLabel(Idx idx);
198  void setLabel(const std::string& label);
199 
200  /**
201  * @brief Returns true if the label is defined.
202  */
203  bool hasLabel() const;
204 
205  /**
206  * @brief Returns true if the aggregator is decomposable.
207  */
208  bool isDecomposable() const;
209 
210  /// See gum::PRMClassElement::addParent_().
211  virtual void addParent(const PRMClassElement< GUM_SCALAR >& elt);
212 
213  /// See gum::PRMClassElement::addChild_().
214  virtual void addChild(const PRMClassElement< GUM_SCALAR >& elt);
215 
216  /// See gum::PRMClassElement::type().
217  virtual PRMType& type();
218 
219  /// See gum::PRMClassElement::type().
220  virtual const PRMType& type() const;
221 
222  /**
223  * @brief Aggregates don't have Potential until they are instantiated as
224  * PRMAttribute, so this will raise an OperationNotAllowed exception.
225  * See gum::PRMClassElement::cpf().
226  */
227  virtual Potential< GUM_SCALAR >& cpf();
228 
229  /**
230  * @brief Aggregates don't have Potential until they are instantiated as
231  * PRMAttribute, so this will raise an OperationNotAllowed exception.
232  * See gum::PRMClassElement::cpf().
233  */
234  virtual const Potential< GUM_SCALAR >& cpf() const;
235 
236  /**
237  * Returns a pointer over an empty gum::MultiDimImplementation of the good
238  * type for this PRMAggregate.
239  *
240  * This should be use when manipulating instantiations of aggregates.
241  */
243 
244  virtual PRMAttribute< GUM_SCALAR >* getCastDescendant() const;
245  /// @}
246  private:
247  // ========================================================================
248  /// @name Private methods.
249  // ========================================================================
250  /// @{
251 
252  /// Copy constructor. Don't use it.
253  PRMAggregate< GUM_SCALAR >(const PRMAggregate< GUM_SCALAR >& source);
254 
255  /// Copy operator. Don't use it.
256  PRMAggregate< GUM_SCALAR >& operator=(const PRMAggregate< GUM_SCALAR >& source);
257 
258  /// @}
259  // ========================================================================
260  /// @name Private members.
261  // ========================================================================
262  /// @{
263 
264  /// The AggregateType of this aggregate.
266 
267  /// The random variable type of this aggregate
268  /// It is deleted with the aggregate.
270 
271  /// Some aggregators applies only on a given label. This attribute must
272  /// have the concerned Idx. If not initialized the pointer equals 0.
273  /// It is deleted with the aggregate.
275  // Idx* _label_;
276  std::string _label_value_;
277 
278  /// @}
279  };
280 
281 
282 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
283  extern template class PRMAggregate< double >;
284 #endif
285 
286  } /* namespace prm */
287 } // namespace gum
288 
289 #include <agrum/PRM/elements/PRMAggregate_tpl.h>
290 
291 #endif /* GUM_AGGREGATE_H */
virtual PRMClassElement< GUM_SCALAR >::ClassElementType elt_type() const
See gum::PRMClassElement::elt_type().
static AggregateType str2enum(const std::string &str)
Static method which returns the AggregateType given its string representation.
Definition: PRMAggregate.h:101
MultiDimImplementation< GUM_SCALAR > * buildImpl() const
Returns a pointer over an empty gum::MultiDimImplementation of the good type for this PRMAggregate...
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
virtual ~PRMAggregate()
Destructor.
virtual void addParent(const PRMClassElement< GUM_SCALAR > &elt)
See gum::PRMClassElement::addParent_().
void setLabel(const std::string &label)
See gum::PRMClassElement::elt_type().
std::shared_ptr< Idx > _label_
Some aggregators applies only on a given label. This attribute must have the concerned Idx...
Definition: PRMAggregate.h:274
void sharedLabel(std::shared_ptr< Idx > label)
Sets the shared_ptr of this Aggregate.
void setLabel(Idx idx)
Set the aggregator&#39;s label.
virtual PRMAttribute< GUM_SCALAR > * getCastDescendant() const
See gum::PRMClassElement::elt_type().
PRMAggregate< GUM_SCALAR > & operator=(const PRMAggregate< GUM_SCALAR > &source)
Copy operator. Don&#39;t use it.
std::shared_ptr< Idx > sharedLabel() const
Returns the shared_ptr holding this Aggregate label.
PRMAggregate(const std::string &name, AggregateType aggType, const PRMType &rvType)
Default constructor.
virtual void addChild(const PRMClassElement< GUM_SCALAR > &elt)
See gum::PRMClassElement::addChild_().
const std::string & labelValue() const
See gum::PRMClassElement::elt_type().
AggregateType agg_type() const
Returns the aggregate of *this.
ParamScopeData(const std::string &s, const PRMReferenceSlot< GUM_SCALAR > &ref, Idx d)
bool isDecomposable() const
Returns true if the aggregator is decomposable.
virtual const PRMType & type() const
See gum::PRMClassElement::type().
virtual Potential< GUM_SCALAR > & cpf()
Aggregates don&#39;t have Potential until they are instantiated as PRMAttribute, so this will raise an Op...
std::string _label_value_
The AggregateType of this aggregate.
Definition: PRMAggregate.h:276
AggregateType
The different type of aggregates we can have.
Definition: PRMAggregate.h:79
virtual PRMType & type()
See gum::PRMClassElement::type().
virtual const Potential< GUM_SCALAR > & cpf() const
Aggregates don&#39;t have Potential until they are instantiated as PRMAttribute, so this will raise an Op...
bool hasLabel() const
Returns true if the label is defined.
PRMAggregate(const std::string &name, AggregateType aggType, const PRMType &rvType, Idx label)
Default constructor.
PRMType * _type_
The random variable type of this aggregate It is deleted with the aggregate.
Definition: PRMAggregate.h:269
AggregateType _agg_type_
The AggregateType of this aggregate.
Definition: PRMAggregate.h:265
Idx label() const
Returns the label&#39;s index on which this aggregate applies.