aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
PRMAttribute.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::PRMAttribute.
25  *
26  * @author Lionel TORTI and Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 
29 #ifndef GUM_ATTRIBUTE_H
30 #define GUM_ATTRIBUTE_H
31 
32 #include <agrum/PRM/elements/PRMClassElement.h>
33 #include <agrum/tools/multidim/implementations/multiDimImplementation.h>
34 
35 namespace gum {
36  namespace prm {
37 
38  /**
39  * @class PRMAttribute
40  * @headerfile attribute.h <agrum/PRM/attribute.h>
41  * @brief PRMAttribute is a member of a Class in a PRM.
42  *
43  * A PRMAttribute is defined by its name, its containing class, its type and
44  * by a Conditional Probability Function (aka CPF but represented by a
45  * Potential).
46  *
47  * An attribute in a PRM is the equivalent of a random variable in a
48  * Bayesian network.
49  *
50  * This class is constructed by a gum::PRMFactory and is deleted by its
51  * gum::Class.
52  *
53  * Built-in copies (copy constructor and copy operator) are illegal due to
54  * various problems raised by redondant information.
55  *
56  * @see PRM PRMFactory Class PRMClassElement PRMType Potential
57  * @ingroup prm_group
58  */
59  template < typename GUM_SCALAR >
61  public:
62  // ========================================================================
63  /// @name Constructors & destructor
64  // ========================================================================
65  /// @{
66  explicit PRMAttribute(const std::string& name);
67 
68  /// Destructor.
69  virtual ~PRMAttribute();
70 
71  /// @}
72  // ========================================================================
73  /// @name Getters & setters
74  // ========================================================================
75  /// @{
76 
77  virtual PRMAttribute< GUM_SCALAR >* newFactory(const PRMClass< GUM_SCALAR >& c) const = 0;
78  virtual PRMAttribute< GUM_SCALAR >*
79  copy(Bijection< const DiscreteVariable*, const DiscreteVariable* > bij) const = 0;
80 
81  virtual void copyCpf(const Bijection< const DiscreteVariable*, const DiscreteVariable* >& bif,
82  const PRMAttribute< GUM_SCALAR >& source)
83  = 0;
84 
85  /// See gum::PRMClassElement::elt_type().
86  virtual typename PRMClassElement< GUM_SCALAR >::ClassElementType elt_type() const = 0;
87 
88  /// See gum::PRMClassElement::type().
89  virtual PRMType& type() = 0;
90 
91  /// See gum::PRMClassElement::type().
92  virtual const PRMType& type() const = 0;
93 
94  /// See gum::PRMClassElement::cpf().
95  virtual const Potential< GUM_SCALAR >& cpf() const = 0;
96 
97  /// See gum::PRMClassElement::addParent_().
98  virtual void addParent(const PRMClassElement< GUM_SCALAR >& elt) = 0;
99 
100  /// See gum::PRMClassElement::addChild_().
101  virtual void addChild(const PRMClassElement< GUM_SCALAR >& elt) = 0;
102 
103  /**
104  * @brief Returns a proper cast descendant of this PRMAttribute.
105  *
106  * A cast descendant is an PRMAttribute depending on this one which
107  * cast it in this->type().super().
108  *
109  * The pointer is not deleted by this PRMAttribute, so delete it yourself
110  * after use.
111  *
112  * A new cast descendant is created for each call of this method.
113  *
114  * @return The cast descendant of this PRMAttribute.
115  *
116  * @throw OperationNotAllowed Raised if it is not possible to create a
117  * cast descendant for this PRMAttribute.
118  */
119  virtual PRMAttribute< GUM_SCALAR >* getCastDescendant() const = 0;
120 
121  /**
122  * @brief Define attr as a cast descendant of this PRMAttribute.
123  *
124  * When overloading an inherited PRMAttribute using of subtype of it,
125  * it is necessary to change the inherited PRMAttribute CPF to make it
126  * a proper cast descendant.
127  *
128  * Furthermore it is necessary to change the DiscreteVariable used
129  * by this PRMAttribute's super PRMType in order to have the
130  *same
131  *pointers in
132  * both super PRMType (i.e. this->type().super().variable())
133  *and
134  *the
135  * cast descendant CPF (i.e. attr->cpf()).
136  *
137  * This can only be done if attr PRMType is a direct subtype
138  *of
139  *this
140  * PRMAttribute PRMType (i.e. this->type().super() ==
141  *attr->type()).
142  *
143  * @param attr The PRMAttribute which is transformed to be this
144  *PRMAttribute
145  * cast descendant.
146  *
147  * @throw OperationNotAllowed Raised if this PRMAttribute can not have any
148  * cast descendant.
149  * @throw PRMTypeError Raised if attr's PRMType is not a direct
150  *descendant of
151  * this PRMAttribute's PRMType.
152  */
153  virtual void setAsCastDescendant(PRMAttribute< GUM_SCALAR >* attr) = 0;
154 
155  /**
156  * @brief Change this attribute to be a cast descendant of a an attribute
157  * with type
158  * subtype.
159  */
160  virtual void becomeCastDescendant(PRMType& subtype) = 0;
161 
162  /// Swap old_type with new_type in the PRMClassElement cpt.
163  virtual void swap(const PRMType& old_type, const PRMType& new_type) = 0;
164 
165  /// Set this as overload of source (necessayr to preserver internal
166  /// pointers for MultiDims).
167  virtual void overload(PRMAttribute< GUM_SCALAR >* source);
168 
169  /// @}
170 
171  protected:
172  PRMAttribute(const PRMAttribute< GUM_SCALAR >& source);
173 
174  virtual PRMType* type_() = 0;
175  virtual void type_(PRMType* t) = 0;
176  };
177 
178 
179 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
180  extern template class PRMAttribute< double >;
181 #endif
182 
183 
184  } /* namespace prm */
185 } // namespace gum
186 
187 #include <agrum/PRM/elements/PRMAttribute_tpl.h>
188 
189 #endif /* GUM_ATTRIBUTE_H */
PRMAttribute(const PRMAttribute< GUM_SCALAR > &source)
virtual PRMAttribute< GUM_SCALAR > * getCastDescendant() const =0
Returns a proper cast descendant of this PRMAttribute.
PRMAttribute(const std::string &name)
Destructor.
virtual void addParent(const PRMClassElement< GUM_SCALAR > &elt)=0
See gum::PRMClassElement::addParent_().
virtual PRMAttribute< GUM_SCALAR > * copy(Bijection< const DiscreteVariable *, const DiscreteVariable * > bij) const =0
See gum::PRMClassElement::elt_type().
virtual PRMAttribute< GUM_SCALAR > * newFactory(const PRMClass< GUM_SCALAR > &c) const =0
See gum::PRMClassElement::elt_type().
virtual PRMType & type()=0
See gum::PRMClassElement::type().
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
virtual ~PRMAttribute()
Destructor.
virtual void becomeCastDescendant(PRMType &subtype)=0
Change this attribute to be a cast descendant of a an attribute with type subtype.
virtual void overload(PRMAttribute< GUM_SCALAR > *source)
Set this as overload of source (necessayr to preserver internal pointers for MultiDims).
virtual const Potential< GUM_SCALAR > & cpf() const =0
See gum::PRMClassElement::cpf().
ParamScopeData(const std::string &s, const PRMReferenceSlot< GUM_SCALAR > &ref, Idx d)
virtual PRMClassElement< GUM_SCALAR >::ClassElementType elt_type() const =0
See gum::PRMClassElement::elt_type().
virtual void addChild(const PRMClassElement< GUM_SCALAR > &elt)=0
See gum::PRMClassElement::addChild_().
virtual void swap(const PRMType &old_type, const PRMType &new_type)=0
Swap old_type with new_type in the PRMClassElement cpt.
virtual PRMType * type_()=0
virtual void setAsCastDescendant(PRMAttribute< GUM_SCALAR > *attr)=0
Define attr as a cast descendant of this PRMAttribute.
virtual void copyCpf(const Bijection< const DiscreteVariable *, const DiscreteVariable * > &bif, const PRMAttribute< GUM_SCALAR > &source)=0
See gum::PRMClassElement::elt_type().
virtual const PRMType & type() const =0
See gum::PRMClassElement::type().
virtual void type_(PRMType *t)=0