aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
DBTranslator4IntegerVariable_inl.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2026 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2026 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41#pragma once
42
43
49
50
51#include <agrum/base/database/DBTranslator4IntegerVariable.h> // to ease IDE parser
52#ifndef DOXYGEN_SHOULD_SKIP_THIS
53
55
56namespace gum {
57
58 namespace learning {
59
62 // for debugging purposes
63 GUM_DESTRUCTOR(DBTranslator4IntegerVariable);
64 }
65
68 const bool b(isInteger(str));
69 const std::string s(str);
70 if (b)
71 if (this->back_dico_.existsSecond(s)) return {this->back_dico_.first(s)};
72
73 if (this->isMissingSymbol(str))
74 return DBTranslatedValue{std::numeric_limits< std::size_t >::max()};
75
76 if (!b) {
77 GUM_ERROR(TypeError,
78 "String \"" << str << "\" cannot be translated because it is not value for "
79 << _variable_);
80 } else {
81 GUM_ERROR(UnknownLabelInDatabase,
82 "The translation of \"" << str << "\" could not be found for " << _variable_)
83 }
84 }
85
87 INLINE std::string
89 try {
90 return this->back_dico_.second(translated_val.discr_val);
91 } catch (Exception const&) {
92 // check if this is a missing value
93 if ((translated_val.discr_val == std::numeric_limits< std::size_t >::max())
94 && !this->missing_symbols_.empty())
95 return *(this->missing_symbols_.begin());
96 else
97 GUM_ERROR(UnknownLabelInDatabase,
98 "The back translation of \"" << translated_val.discr_val
99 << "\" could not be found");
100 }
101 }
102
104 INLINE bool DBTranslator4IntegerVariable::hasEditableDictionary() const { return false; }
105
108
110 INLINE bool DBTranslator4IntegerVariable::needsReordering() const { return false; }
111
113 INLINE HashTable< std::size_t, std::size_t > DBTranslator4IntegerVariable::reorder() {
114 return HashTable< std::size_t, std::size_t >();
115 }
116
118 INLINE std::size_t DBTranslator4IntegerVariable::domainSize() const {
119 return _variable_.domainSize();
120 }
121
123 INLINE const IntegerVariable* DBTranslator4IntegerVariable::variable() const {
124 return &_variable_;
125 }
126
129 return DBTranslatedValue{std::numeric_limits< std::size_t >::max()};
130 }
131
132
133 } /* namespace learning */
134
135} /* namespace gum */
136
137#endif /* DOXYGEN_SHOULD_SKIP_THIS */
The databases' cell translators for integer variables.
std::size_t domainSize() const final
returns the domain size of the variable used for translations
DBTranslatedValue translate(std::string_view str) final
returns the translation of a string
const IntegerVariable * variable() const final
returns the variable stored into the translator
DBTranslator4IntegerVariable(const IntegerVariable &var, const std::vector< std::string > &missing_symbols, std::size_t max_dico_entries=std::numeric_limits< std::size_t >::max())
default constructor with an integer variable as translator
~DBTranslator4IntegerVariable() override
destructor
void setEditableDictionaryMode(bool new_mode) final
sets/unset the editable dictionary mode
std::string translateBack(const DBTranslatedValue translated_val) const final
returns the original value for a given translation
HashTable< std::size_t, std::size_t > reorder() final
returns an empty HashTable to indicate that no reordering is needed.
bool needsReordering() const final
indicates that the translations should never be reordered
bool hasEditableDictionary() const final
indicates that the translator is never in editable dictionary mode
DBTranslatedValue missingValue() const final
returns the translation of a missing value
Bijection< std::size_t, std::string > back_dico_
the bijection relating back translated values and their original strings.
Set< std::string > missing_symbols_
the set of missing symbols
bool isMissingSymbol(std::string_view str) const
indicates whether a string corresponds to a missing symbol
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
bool isInteger(std::string_view val)
return true is a string contains an integer value
include the inlined functions if necessary
Definition CSVParser.h:55
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
The union class for storing the translated values in learning databases.