aGrUM 3.1.1
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-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#include <agrum/base/database/DBRowGeneratorSet.h> // to ease IDE parser
51#ifndef DOXYGEN_SHOULD_SKIP_THIS
52
53namespace gum {
54
55 namespace learning {
56
58 template < class Generator >
59 void DBRowGeneratorSet::insertGenerator(const Generator& generator) {
60 // check that no output row generation is still active
61 if (_output_row_ != nullptr)
62 GUM_ERROR(OperationNotAllowed,
63 "you cannot insert a new generator while a generation is "
64 "still being processed");
65
66 _generators_.push_back(generator.clone());
67
68 try {
69 _setInputRow_performed_.push_back(0);
70 } catch (...) {
71 delete _generators_.back();
72 _generators_.pop_back();
73 throw;
74 }
75
76 ++_nb_generators_;
77 _output_row_ = nullptr;
78 }
79
81 template < class Generator >
82 void DBRowGeneratorSet::insertGenerator(const Generator& generator, const std::size_t i) {
83 // check that no output row generation is still active
84 if (_output_row_ != nullptr)
85 GUM_ERROR(OperationNotAllowed,
86 "you cannot insert a new generator while a generation is "
87 "still being processed");
88
89 _generators_.insert(_generators_.begin() + i, generator.clone());
90
91 try {
92 _setInputRow_performed_.push_back(0);
93 } catch (...) {
94 delete *(_generators_.begin() + i);
95 _generators_.erase(_generators_.begin() + i);
96 throw;
97 }
98
99 ++_nb_generators_;
100 _output_row_ = nullptr;
101 }
102
104 template < GUM_Numeric GUM_SCALAR >
105 void DBRowGeneratorSet::setBayesNet(const BayesNet< GUM_SCALAR >& new_bn) {
106 HashTable< DBRowGeneratorWithBN< GUM_SCALAR >*, const BayesNet< GUM_SCALAR >* > old_bns;
107
108 for (auto xgen: _generators_) {
109 // check if the generator relies on a Bayes net
110 DBRowGeneratorWithBN< GUM_SCALAR >* gen = nullptr;
111 try {
112 gen = dynamic_cast< DBRowGeneratorWithBN< GUM_SCALAR >* >(xgen);
113 } catch (std::bad_cast&) {}
114
115 if (gen != nullptr) {
116 // try to assign the new BN to the generator
117 try {
118 const BayesNet< GUM_SCALAR >* bn = &(gen->getBayesNet());
119 old_bns.insert(gen, bn);
120 gen->setBayesNet(new_bn);
121 } catch (...) {
122 // if we could not assign the new BN to the generator, then
123 // make all the generators that were successfully assigned this
124 // BN revert to the old BN they had
125 for (auto& generator: old_bns) {
126 generator.first->setBayesNet(*(generator.second));
127 }
128 throw;
129 }
130 }
131 }
132 }
133
134 } /* namespace learning */
135
136} /* namespace gum */
137
138#endif /* DOXYGEN_SHOULD_SKIP_THIS */
class for packing sets of generators
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:76
std::mt19937 & generator()
include the inlined functions if necessary
Definition CSVParser.h:55
gum is the global namespace for all aGrUM entities
Definition agrum.h:46