aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
PRMAggregate.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 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,
143  AggregateType aggType,
144  const PRMType& rvType);
145 
146  /**
147  * Default constructor.
148  * @param name The name of this aggregate.
149  * @param aggType The aggregate type of this aggregate.
150  * @param rvType The random variable type of this aggregate, which is
151  * copied.
152  * @param label The index of the label on which this aggregate applies.
153  */
154  PRMAggregate(const std::string& name,
155  AggregateType aggType,
156  const PRMType& rvType,
157  Idx label);
158 
159  /// Destructor.
160  virtual ~PRMAggregate();
161 
162  /// @}
163  // ========================================================================
164  /// @name Getters and Setters.
165  // ========================================================================
166  /// @{
167 
168  /// See gum::PRMClassElement::elt_type().
169  virtual typename PRMClassElement< GUM_SCALAR >::ClassElementType
170  elt_type() const;
171 
172  /// Returns the aggregate of *this.
173  AggregateType agg_type() const;
174 
175  /**
176  * Returns the label's index on which this aggregate applies.
177  * @throw OperationNotAllowed Raised if the aggregate does not applies
178  * on a label.
179  */
180  Idx label() const;
181  const std::string& labelValue() const;
182 
183  /**
184  * @brief Returns the shared_ptr holding this Aggregate label.
185  *
186  * This is used for inherited Aggregates to share labels in O3PRM.
187  */
188  std::shared_ptr< Idx > sharedLabel() const;
189 
190  /**
191  * @brief Sets the shared_ptr of this Aggregate.
192  *
193  * This is used for inherited aggregates to share labels in O3PRM.
194  */
195  void sharedLabel(std::shared_ptr< Idx > label);
196 
197  /**
198  * @brief Set the aggregator's label.
199  */
200  void setLabel(Idx idx);
201  void setLabel(const std::string& label);
202 
203  /**
204  * @brief Returns true if the label is defined.
205  */
206  bool hasLabel() const;
207 
208  /**
209  * @brief Returns true if the aggregator is decomposable.
210  */
211  bool isDecomposable() const;
212 
213  /// See gum::PRMClassElement::addParent_().
214  virtual void addParent(const PRMClassElement< GUM_SCALAR >& elt);
215 
216  /// See gum::PRMClassElement::addChild_().
217  virtual void addChild(const PRMClassElement< GUM_SCALAR >& elt);
218 
219  /// See gum::PRMClassElement::type().
220  virtual PRMType& type();
221 
222  /// See gum::PRMClassElement::type().
223  virtual const PRMType& type() const;
224 
225  /**
226  * @brief Aggregates don't have Potential until they are instantiated as
227  * PRMAttribute, so this will raise an OperationNotAllowed exception.
228  * See gum::PRMClassElement::cpf().
229  */
230  virtual Potential< GUM_SCALAR >& cpf();
231 
232  /**
233  * @brief Aggregates don't have Potential until they are instantiated as
234  * PRMAttribute, so this will raise an OperationNotAllowed exception.
235  * See gum::PRMClassElement::cpf().
236  */
237  virtual const Potential< GUM_SCALAR >& cpf() const;
238 
239  /**
240  * Returns a pointer over an empty gum::MultiDimImplementation of the good
241  * type for this PRMAggregate.
242  *
243  * This should be use when manipulating instantiations of aggregates.
244  */
246 
247  virtual PRMAttribute< GUM_SCALAR >* getCastDescendant() const;
248  /// @}
249  private:
250  // ========================================================================
251  /// @name Private methods.
252  // ========================================================================
253  /// @{
254 
255  /// Copy constructor. Don't use it.
256  PRMAggregate< GUM_SCALAR >(const PRMAggregate< GUM_SCALAR >& source);
257 
258  /// Copy operator. Don't use it.
259  PRMAggregate< GUM_SCALAR >&
260  operator=(const PRMAggregate< GUM_SCALAR >& source);
261 
262  /// @}
263  // ========================================================================
264  /// @name Private members.
265  // ========================================================================
266  /// @{
267 
268  /// The AggregateType of this aggregate.
270 
271  /// The random variable type of this aggregate
272  /// It is deleted with the aggregate.
274 
275  /// Some aggregators applies only on a given label. This attribute must
276  /// have the concerned Idx. If not initialized the pointer equals 0.
277  /// It is deleted with the aggregate.
279  // Idx* label__;
280  std::string label_value__;
281 
282  /// @}
283  };
284 
285 
286 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
287  extern template class PRMAggregate< double >;
288 #endif
289 
290  } /* namespace prm */
291 } // namespace gum
292 
293 #include <agrum/PRM/elements/PRMAggregate_tpl.h>
294 
295 #endif /* GUM_AGGREGATE_H */
virtual PRMClassElement< GUM_SCALAR >::ClassElementType elt_type() const
See gum::PRMClassElement::elt_type().
AggregateType agg_type__
The AggregateType of this aggregate.
Definition: PRMAggregate.h:269
static AggregateType str2enum(const std::string &str)
Static method which returns the AggregateType given its string representation.
Definition: PRMAggregate.h:101
std::shared_ptr< Idx > label__
Some aggregators applies only on a given label. This attribute must have the concerned Idx...
Definition: PRMAggregate.h:278
MultiDimImplementation< GUM_SCALAR > * buildImpl() const
Returns a pointer over an empty gum::MultiDimImplementation of the good type for this PRMAggregate...
std::string label_value__
The AggregateType of this aggregate.
Definition: PRMAggregate.h:280
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
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().
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...
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:273
Idx label() const
Returns the label&#39;s index on which this aggregate applies.