aGrUM  0.14.2
PRMAggregate_tpl.h
Go to the documentation of this file.
1 /**************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
27 #include <climits>
28 
38 
39 namespace gum {
40  namespace prm {
41 
42  template < typename GUM_SCALAR >
44  AggregateType aggType,
45  const PRMType& rvType) :
46  PRMClassElement< GUM_SCALAR >(name),
47  __agg_type(aggType), __type(new PRMType(rvType)),
48  __label(std::shared_ptr< Idx >(new Idx(INT_MAX))) {
49  GUM_CONSTRUCTOR(PRMAggregate);
50  this->_safeName =
51  PRMObject::LEFT_CAST() + __type->name() + PRMObject::RIGHT_CAST() + name;
52  this->__type->variable().setName(name);
53  }
54 
55  template < typename GUM_SCALAR >
57  AggregateType aggType,
58  const PRMType& rvType,
59  Idx label) :
60  PRMClassElement< GUM_SCALAR >(name),
61  __agg_type(aggType), __type(new PRMType(rvType)),
62  __label(std::shared_ptr< Idx >(new Idx(label))) {
63  GUM_CONSTRUCTOR(PRMAggregate);
64  this->_safeName =
65  PRMObject::LEFT_CAST() + __type->name() + PRMObject::RIGHT_CAST() + name;
66  this->__type->variable().setName(name);
67  }
68 
69  template < typename GUM_SCALAR >
71  GUM_DESTRUCTOR(PRMAggregate);
72  delete __type;
73  }
74 
75  template < typename GUM_SCALAR >
77  const PRMAggregate< GUM_SCALAR >& source) :
78  PRMClassElement< GUM_SCALAR >(source) {
79  GUM_CONS_CPY(PRMAggregate);
80  GUM_ERROR(FatalError, "illegal call to gum::PRMAggregate copy constructor.");
81  }
82 
83  template < typename GUM_SCALAR >
86  GUM_ERROR(FatalError, "illegal call to gum::PRMAggregate copy operator.");
87  }
88 
89  template < typename GUM_SCALAR >
92  return this->prm_aggregate;
93  }
94 
95  template < typename GUM_SCALAR >
98  return __agg_type;
99  }
100 
101  template < typename GUM_SCALAR >
103  if (*__label != INT_MAX) return *__label;
104  GUM_ERROR(OperationNotAllowed, "no label defined for this aggregate");
105  }
106 
107  template < typename GUM_SCALAR >
108  INLINE const std::string& PRMAggregate< GUM_SCALAR >::labelValue() const {
109  return __label_value;
110  }
111 
112  template < typename GUM_SCALAR >
114  (*__label) = idx;
115  }
116 
117  template < typename GUM_SCALAR >
118  INLINE void PRMAggregate< GUM_SCALAR >::setLabel(const std::string& value) {
119  __label_value = value;
120  }
121 
122  template < typename GUM_SCALAR >
124  return *__label != INT_MAX;
125  }
126 
127  template < typename GUM_SCALAR >
129  return *__type;
130  }
131 
132  template < typename GUM_SCALAR >
134  return *__type;
135  }
136 
137  template < typename GUM_SCALAR >
139  GUM_ERROR(OperationNotAllowed, "This is an aggregate.");
140  }
141 
142  template < typename GUM_SCALAR >
144  GUM_ERROR(OperationNotAllowed, "This is an aggregate.");
145  }
146 
147  template < typename GUM_SCALAR >
150  switch (agg_type()) {
151  case AggregateType::MIN: {
152  return new aggregator::Min< GUM_SCALAR >();
153  }
154  case AggregateType::MAX: {
155  return new aggregator::Max< GUM_SCALAR >();
156  }
157  case AggregateType::OR: {
158  return new aggregator::Or< GUM_SCALAR >();
159  }
160  case AggregateType::AND: {
161  return new aggregator::And< GUM_SCALAR >();
162  }
163  case AggregateType::AMPLITUDE: {
165  }
166  case AggregateType::MEDIAN: {
168  }
169  case AggregateType::EXISTS: {
170  return new aggregator::Exists< GUM_SCALAR >(label());
171  }
172  case AggregateType::FORALL: {
173  return new aggregator::Forall< GUM_SCALAR >(label());
174  }
175  case AggregateType::COUNT: {
176  return new aggregator::Count< GUM_SCALAR >(label());
177  }
178  default: { GUM_ERROR(OperationNotAllowed, "Unknown aggregator."); }
179  }
180  return nullptr;
181  }
182 
183  // See gum::PRMClassElement<GUM_SCALAR>::_addParent().
184  template < typename GUM_SCALAR >
186  const PRMClassElement< GUM_SCALAR >& elt) {}
187 
188  // See gum::PRMClassElement<GUM_SCALAR>::_addChild().
189  template < typename GUM_SCALAR >
191  const PRMClassElement< GUM_SCALAR >& elt) {}
192 
193  template < typename GUM_SCALAR >
197 
198  try {
199  cast =
200  new PRMScalarAttribute< GUM_SCALAR >(this->name(), type().superType());
201  } catch (NotFound&) {
203  "this PRMAggregate can not have cast descendant");
204  }
205 
206  cast->addParent(*this);
207  const DiscreteVariable& my_var = type().variable();
208  DiscreteVariable& cast_var = cast->type().variable();
209  Instantiation inst(cast->cpf());
210 
211  for (inst.setFirst(); !inst.end(); inst.inc()) {
212  if (type().label_map()[inst.val(my_var)] == inst.val(cast_var)) {
213  cast->cpf().set(inst, 1);
214  } else {
215  cast->cpf().set(inst, 0);
216  }
217  }
218 
219  return cast;
220  }
221 
222  template < typename GUM_SCALAR >
223  INLINE std::shared_ptr< Idx > PRMAggregate< GUM_SCALAR >::sharedLabel() const {
224  return __label;
225  }
226 
227  template < typename GUM_SCALAR >
228  INLINE void
229  PRMAggregate< GUM_SCALAR >::sharedLabel(std::shared_ptr< Idx > label) {
230  this->__label = label;
231  }
232 
233  } /* namespace prm */
234 } /* namespace gum */
aGrUM&#39;s Potential is a multi-dimensional array with tensor operators.
Definition: potential.h:57
or aggregator
Definition: or.h:53
forall aggregator
STL namespace.
Abstract class representing an element of PRM class.
median aggregator
Base class for discrete random variable.
virtual PRMType & type()
See gum::PRMClassElement::type().
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
min aggregator
forall aggregator
Definition: forall.h:52
virtual const Potential< GUM_SCALAR > & cpf() const
See gum::PRMClassElement::cpf().
PRMAggregate(const std::string &name, AggregateType aggType, const PRMType &rvType)
Default constructor.
And aggregator.
Definition: and.h:52
or aggregator
exists aggregator
Definition: exists.h:51
forall aggregator
max aggregator
This is a decoration of the DiscreteVariable class.
Definition: PRMType.h:60
count aggregator
AggregateType
The different type of aggregates we can have.
Definition: PRMAggregate.h:77
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:80
count aggregator
median aggregator
<agrum/multidim/multiDimImplementation.h>
Size Idx
Type for indexes.
Definition: types.h:50
max aggregator
Definition: max.h:51
PRMAttribute is a member of a Class in a PRM.
Definition: PRMAttribute.h:58
median aggregator
Definition: median.h:57
virtual void addParent(const PRMClassElement< GUM_SCALAR > &elt)
See gum::PRMClassElement::_addParent().
<agrum/PRM/elements/scalarAttribute.h>
amplitude aggregator
Definition: amplitude.h:52
min aggregator
Definition: min.h:50
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
ClassElementType
Returns true if obj_ptr is of type PRMReferenceSlot.
count aggregator
Definition: count.h:54