aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
discreteVariable.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 discrete random variable.
24  *
25  * This class is used as an interface.
26  * @author Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
27  */
28 #ifndef GUM_DISCRETE_VARIABLE_H
29 #define GUM_DISCRETE_VARIABLE_H
30 
31 #include <ostream>
32 #include <string>
33 
34 #include <agrum/agrum.h>
35 
36 #include <agrum/tools/core/hashFunc.h>
37 #include <agrum/tools/variables/variable.h>
38 
39 namespace gum {
40 
41  /* ===========================================================================
42  */
43  /* ===========================================================================
44  */
45  /* === GUM_DISCRETE_VARIABLE ===
46  */
47  /* ===========================================================================
48  */
49  /* ===========================================================================
50  */
51  /** @class DiscreteVariable
52  * @brief Base class for discrete random variable.
53  * @ingroup multidim_group
54  *
55  * This class is used as an interface. */
56  /* ===========================================================================
57  */
58 
59  class DiscreteVariable: public Variable {
60  public:
61  // ############################################################################
62  /// @name Constructors / Destructors
63  // ############################################################################
64  /// @{
65 
66  /// Default constructor
67 
68  DiscreteVariable(const std::string& aName, const std::string& aDesc);
69 
70  /// Copy constructor
71 
72  /** Copy Constructor.
73  *
74  * If aDRV haves any listener, it will not be copied.
75  *
76  * @param aDRV the variable we copy
77  */
78  DiscreteVariable(const DiscreteVariable& aDRV);
79 
80  /// destructor
81 
82  virtual ~DiscreteVariable();
83 
84  /// Copy Factory.
85  /// @return Returns a pointer on a new copy of this.
86 
87  virtual DiscreteVariable* clone() const = 0;
88 
89  /// @}
90 
91  // ############################################################################
92  /// @name Accessors / Modifiers
93  // ############################################################################
94  /// @{
95 
96  /// @return true if the domainSize() < 2;
97 
98  bool empty() const;
99 
100  /// @return the number of modalities of the random discrete
101 
102  virtual Size domainSize() const = 0;
103 
104  /// vector of labels
105  std::vector< std::string > labels() const;
106 
107  /// get a numerical representation of the indice-th value.
108  virtual double numerical(Idx indice) const = 0;
109 
110  /// returns the varType of variable
111 
112  virtual VarType varType() const = 0;
113  /// @}
114 
115  // ############################################################################
116  /// @name Operators
117  // ############################################################################
118  /// @{
119 
120  /// Copy operator
121  /** @param aRV to be copied
122  * @return a ref to *this */
123 
124  DiscreteVariable& operator=(const DiscreteVariable& aRV);
125 
126  /// equality operator
127  virtual bool operator==(const DiscreteVariable& aRV) const;
128 
129  /// inequality operator
130 
131  virtual bool operator!=(const DiscreteVariable& aRV) const;
132 
133  /// @}
134 
135  /// from the label to its index in var.
136  /// @warning This operation may have different complexity in different
137  /// subclasses.
138  /// @throws NotFound
139  Idx operator[](const std::string& label) const { return index(label); };
140  virtual Idx index(const std::string& label) const = 0;
141 
142  /// get the indice-th label. This method is pure virtual.
143  /** @param indice the index of the label we wish to return
144  * @throw OutOfBound
145  */
146  virtual std::string label(Idx i) const = 0;
147 
148  /// string version of *this
149  std::string toString() const;
150 
151  /// string version of *this using description attribute instead of name.
152  std::string toStringWithDescription() const;
153 
154  /// string represent the domain of the variable
155  virtual const std::string domain() const = 0;
156 
157  protected:
158  /// (protected) Default constructor
159  DiscreteVariable() {
160  GUM_CONSTRUCTOR(DiscreteVariable);
161  ;
162  }
163  };
164 
165  /// for friendly displaying the content of the variable
166 
167  std::ostream& operator<<(std::ostream&, const DiscreteVariable&);
168 
169 } /* namespace gum */
170 
171 #ifndef GUM_NO_INLINE
172 # include <agrum/tools/variables/discreteVariable_inl.h>
173 #endif /* GUM_NO_INLINE */
174 
175 #endif /* GUM_DISCRETE_VARIABLE_H */