aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
PRMAttribute.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::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 >*
78  newFactory(const PRMClass< GUM_SCALAR >& c) const = 0;
79  virtual PRMAttribute< GUM_SCALAR >*
80  copy(Bijection< const DiscreteVariable*, const DiscreteVariable* > bij)
81  const = 0;
82 
83  virtual void copyCpf(
84  const Bijection< const DiscreteVariable*, const DiscreteVariable* >& bif,
85  const PRMAttribute< GUM_SCALAR >& source)
86  = 0;
87 
88  /// See gum::PRMClassElement::elt_type().
89  virtual typename PRMClassElement< GUM_SCALAR >::ClassElementType
90  elt_type() const = 0;
91 
92  /// See gum::PRMClassElement::type().
93  virtual PRMType& type() = 0;
94 
95  /// See gum::PRMClassElement::type().
96  virtual const PRMType& type() const = 0;
97 
98  /// See gum::PRMClassElement::cpf().
99  virtual const Potential< GUM_SCALAR >& cpf() const = 0;
100 
101  /// See gum::PRMClassElement::addParent_().
102  virtual void addParent(const PRMClassElement< GUM_SCALAR >& elt) = 0;
103 
104  /// See gum::PRMClassElement::addChild_().
105  virtual void addChild(const PRMClassElement< GUM_SCALAR >& elt) = 0;
106 
107  /**
108  * @brief Returns a proper cast descendant of this PRMAttribute.
109  *
110  * A cast descendant is an PRMAttribute depending on this one which
111  * cast it in this->type().super().
112  *
113  * The pointer is not deleted by this PRMAttribute, so delete it yourself
114  * after use.
115  *
116  * A new cast descendant is created for each call of this method.
117  *
118  * @return The cast descendant of this PRMAttribute.
119  *
120  * @throw OperationNotAllowed Raised if it is not possible to create a
121  * cast descendant for this PRMAttribute.
122  */
123  virtual PRMAttribute< GUM_SCALAR >* getCastDescendant() const = 0;
124 
125  /**
126  * @brief Define attr as a cast descendant of this PRMAttribute.
127  *
128  * When overloading an inherited PRMAttribute using of subtype of it,
129  * it is necessary to change the inherited PRMAttribute CPF to make it
130  * a proper cast descendant.
131  *
132  * Furthermore it is necessary to change the DiscreteVariable used
133  * by this PRMAttribute's super PRMType in order to have the
134  *same
135  *pointers in
136  * both super PRMType (i.e. this->type().super().variable())
137  *and
138  *the
139  * cast descendant CPF (i.e. attr->cpf()).
140  *
141  * This can only be done if attr PRMType is a direct subtype
142  *of
143  *this
144  * PRMAttribute PRMType (i.e. this->type().super() ==
145  *attr->type()).
146  *
147  * @param attr The PRMAttribute which is transformed to be this
148  *PRMAttribute
149  * cast descendant.
150  *
151  * @throw OperationNotAllowed Raised if this PRMAttribute can not have any
152  * cast descendant.
153  * @throw PRMTypeError Raised if attr's PRMType is not a direct
154  *descendant of
155  * this PRMAttribute's PRMType.
156  */
157  virtual void setAsCastDescendant(PRMAttribute< GUM_SCALAR >* attr) = 0;
158 
159  /**
160  * @brief Change this attribute to be a cast descendant of a an attribute
161  * with type
162  * subtype.
163  */
164  virtual void becomeCastDescendant(PRMType& subtype) = 0;
165 
166  /// Swap old_type with new_type in the PRMClassElement cpt.
167  virtual void swap(const PRMType& old_type, const PRMType& new_type) = 0;
168 
169  /// Set this as overload of source (necessayr to preserver internal
170  /// pointers for MultiDims).
171  virtual void overload(PRMAttribute< GUM_SCALAR >* source);
172 
173  /// @}
174 
175  protected:
176  PRMAttribute(const PRMAttribute< GUM_SCALAR >& source);
177 
178  virtual PRMType* type_() = 0;
179  virtual void type_(PRMType* t) = 0;
180  };
181 
182 
183 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
184  extern template class PRMAttribute< double >;
185 #endif
186 
187 
188  } /* namespace prm */
189 } // namespace gum
190 
191 #include <agrum/PRM/elements/PRMAttribute_tpl.h>
192 
193 #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:669
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