aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
labelizedVariable.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 /** @file
23  * @brief Base class for labelized discrete random variables
24  *
25  * @author Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
26  */
27 #ifndef GUM_LABELIZED_DISCRETE_VARIABLE_H
28 #define GUM_LABELIZED_DISCRETE_VARIABLE_H
29 
30 #include <iostream>
31 #include <sstream>
32 #include <string>
33 
34 #include <agrum/agrum.h>
35 
36 #include <agrum/tools/core/sequence.h>
37 #include <agrum/tools/variables/discreteVariable.h>
38 
39 namespace gum {
40  /* =========================================================================*/
41  /* =========================================================================*/
42  /* === GUM_LABELIZED_VARIABLE === */
43  /* =========================================================================*/
44  /* =========================================================================*/
45  /** class LabelizedVariable
46  * @brief Base class for labelized discrete random variables
47  * @ingroup multidim_group
48  */
49  /* =========================================================================*/
50 
51  class LabelizedVariable: public DiscreteVariable {
52  public:
53  // ############################################################################
54  /// @name Constructors / Destructors
55  // ############################################################################
56  /// @{
57 
58  /** @brief constructor
59  * @param aName the name
60  * @param aDesc the Description
61  * @param nbrLabel the domainSize (2 by default)
62  */
63 
64  LabelizedVariable(const std::string& aName,
65  const std::string& aDesc = "",
66  const Size nbrLabel = 2);
67 
68  /** @brief constructor
69  * @param aName the name
70  * @param aDesc the Description
71  * @param labels the labels
72  */
73 
74  LabelizedVariable(const std::string& aName,
75  const std::string& aDesc,
76  const std::vector< std::string >& labels);
77 
78  /// Copy constructor
79  /**
80  * @param aLDRV the variable we copy
81  */
82 
83  LabelizedVariable(const LabelizedVariable& aLDRV);
84 
85  /// destructor
86 
87  virtual ~LabelizedVariable();
88 
89  /// a virtual clone ...
90  virtual LabelizedVariable* clone() const;
91 
92  /// @}
93 
94  // ############################################################################
95  /// @name Operators
96  // ############################################################################
97  /// @{
98 
99  /// copy operator
100  /** @param aLDRV the labelized discrete random variable we copy */
101  LabelizedVariable& operator=(const LabelizedVariable& aLDRV);
102 
103 
104  /// returns the index of a given label
105  /** @param label searched label
106  * @return the index of this label
107  * @throw NotFound */
108  virtual Idx index(const std::string& label) const;
109 
110  /// @}
111 
112  // ############################################################################
113  /// @name Accessors / Modifiers
114  // ############################################################################
115  /// @{
116 
117  /**
118  * @brief indicates whether the variable already has the label passed in
119  * argument
120  * @param aLabel The label
121  * @return true if the label already exists */
122  bool isLabel(const std::string& aLabel) const;
123 
124  /// add a label with a new index (we assume that we will NEVER remove a
125  /// label)
126  /** @param aLabel the label to be added to the labelized variable
127  * @throw DuplicateElement is raised if the variable already contains
128  * the label
129  * @return *this which allows : v.addLabel("1").addLabel("2")...;
130  */
131  LabelizedVariable& addLabel(const std::string& aLabel);
132 
133  /// change a label for this index
134  /** @param pos the index of the label to be changed
135  * @param aLabel the label to be added to the labelized variable
136  * @throw DuplicateElement is raised if the variable already contains
137  * @throw OutOfBounds
138  * @warning Since this change does not affect the variable domain but only
139  * the
140  * variable representation,
141  * this method is allowed on const LabelizedVariable.
142  */
143  void changeLabel(Idx pos, const std::string& aLabel) const;
144 
145  /// erase all the labels
146  void eraseLabels();
147 
148  /// returns the ith label
149  /** @param i
150  * @return the ith label */
151  virtual std::string label(Idx i) const;
152 
153  /// return the pos from label
154  Idx posLabel(const std::string& label) const;
155 
156  /// get a numerical representation of he indice-the value.
157  virtual double numerical(Idx indice) const;
158 
159  /// returns the size of the random discrete variable domain
160  virtual Size domainSize() const;
161 
162  /// returns the type of variable
163  virtual VarType varType() const;
164 
165  /// Returns the domain
166  virtual const std::string domain() const;
167 
168  /// @}
169 
170  protected:
171  /// (protected) Default constructor
172  LabelizedVariable() {
173  GUM_CONSTRUCTOR(LabelizedVariable);
174  ;
175  }
176 
177  /// copies the content of aLDRV
178 
179  void copy_(const LabelizedVariable& aLDRV);
180 
181  private:
182  /// the set of labels contained in the variable
183  mutable Sequence< std::string > _labels_;
184  };
185 
186 } /* namespace gum */
187 
188 /* ==============================================================================
189  */
190 /* ==============================================================================
191  */
192 /* === LabelizedVariable IMPLEMENTATION === */
193 /* ==============================================================================
194  */
195 /* ==============================================================================
196  */
197 #ifndef GUM_NO_INLINE
198 # include <agrum/tools/variables/labelizedVariable_inl.h>
199 #endif /* GUM_NO_INLINE */
200 
201 #endif /* GUM_LABELIZED_DISCRETE_VARIABLE_H */