aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
DBRowGeneratorSet_tpl.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 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-2025 *
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#pragma once
41
42
48
49#ifndef DOXYGEN_SHOULD_SKIP_THIS
50
51namespace gum {
52
53 namespace learning {
54
56 template < class Generator >
57 void DBRowGeneratorSet::insertGenerator(const Generator& generator) {
58 // check that no output row generation is still active
59 if (_output_row_ != nullptr)
60 GUM_ERROR(OperationNotAllowed,
61 "you cannot insert a new generator while a generation is "
62 "still being processed");
63
64 _generators_.push_back(generator.clone());
65
66 try {
67 _setInputRow_performed_.push_back(0);
68 } catch (...) {
69 delete _generators_.back();
70 _generators_.pop_back();
71 throw;
72 }
73
74 ++_nb_generators_;
75 _output_row_ = nullptr;
76 }
77
79 template < class Generator >
80 void DBRowGeneratorSet::insertGenerator(const Generator& generator, const std::size_t i) {
81 // check that no output row generation is still active
82 if (_output_row_ != nullptr)
83 GUM_ERROR(OperationNotAllowed,
84 "you cannot insert a new generator while a generation is "
85 "still being processed");
86
87 _generators_.insert(_generators_.begin() + i, generator.clone());
88
89 try {
90 _setInputRow_performed_.push_back(0);
91 } catch (...) {
92 delete *(_generators_.begin() + i);
93 _generators_.erase(_generators_.begin() + i);
94 throw;
95 }
96
97 ++_nb_generators_;
98 _output_row_ = nullptr;
99 }
100
102 template < typename GUM_SCALAR >
103 void DBRowGeneratorSet::setBayesNet(const BayesNet< GUM_SCALAR >& new_bn) {
104 HashTable< DBRowGeneratorWithBN< GUM_SCALAR >*, const BayesNet< GUM_SCALAR >* > old_bns;
105
106 for (auto xgen: _generators_) {
107 // check if the generator relies on a Bayes net
108 DBRowGeneratorWithBN< GUM_SCALAR >* gen = nullptr;
109 try {
110 gen = dynamic_cast< DBRowGeneratorWithBN< GUM_SCALAR >* >(xgen);
111 } catch (std::bad_cast&) {}
112
113 if (gen != nullptr) {
114 // try to assign the new BN to the generator
115 try {
116 const BayesNet< GUM_SCALAR >* bn = &(gen->getBayesNet());
117 old_bns.insert(gen, bn);
118 gen->setBayesNet(new_bn);
119 } catch (...) {
120 // if we could not assign the new BN to the generator, then
121 // make all the generators that were successfully assigned this
122 // BN revert to the old BN they had
123 for (auto& generator: old_bns) {
124 generator.first->setBayesNet(*(generator.second));
125 }
126 throw;
127 }
128 }
129 }
130 }
131
132 } /* namespace learning */
133
134} /* namespace gum */
135
136#endif /* DOXYGEN_SHOULD_SKIP_THIS */
void insertGenerator(const Generator &generator)
inserts a new generator at the end of the set
void setBayesNet(const BayesNet< GUM_SCALAR > &new_bn)
assign a new Bayes net to all the generators that depend on a BN
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
std::mt19937 & generator()
include the inlined functions if necessary
Definition CSVParser.h:54
gum is the global namespace for all aGrUM entities
Definition agrum.h:46