aGrUM 3.3.2
a C++ library for (probabilistic) graphical models
O3SystemFactory_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
51
53
54namespace gum {
55 namespace prm {
56 namespace o3prm {
57
58 template < GUM_Numeric GUM_SCALAR >
60 O3PRM& o3_prm,
62 ErrorsContainer& errors) :
63 _prm_(&prm), _o3_prm_(&o3_prm), _solver_(&solver), _errors_(&errors) {
64 GUM_CONSTRUCTOR(O3SystemFactory);
65 }
66
67 template < GUM_Numeric GUM_SCALAR >
73
74 template < GUM_Numeric GUM_SCALAR >
76 _prm_(std::move(src._prm_)), _o3_prm_(std::move(src._o3_prm_)),
77 _solver_(std::move(src._solver_)), _errors_(std::move(src._errors_)),
78 _nameMap_(std::move(src._nameMap_)) {
79 GUM_CONS_MOV(O3SystemFactory);
80 }
81
82 template < GUM_Numeric GUM_SCALAR >
86
87 template < GUM_Numeric GUM_SCALAR >
90 if (this == &src) { return *this; }
91 _prm_ = src._prm_;
92 _o3_prm_ = src._o3_prm_;
93 _solver_ = src._solver_;
94 _errors_ = src._errors_;
95 return *this;
96 }
97
98 template < GUM_Numeric GUM_SCALAR >
101 if (this == &src) { return *this; }
102 _prm_ = std::move(src._prm_);
103 _o3_prm_ = std::move(src._o3_prm_);
104 _solver_ = std::move(src._solver_);
105 _errors_ = std::move(src._errors_);
106 return *this;
107 }
108
109 template < GUM_Numeric GUM_SCALAR >
112
113 for (auto& sys: _o3_prm_->systems()) {
114 // Reseting name map for each system
116
117 if (_checkSystem_(*sys)) {
118 factory.startSystem(sys->name().label());
119
120 _addInstances_(factory, *sys);
121 _addAssignments_(factory, *sys);
122 _addIncrements_(factory, *sys);
123
124 try {
125 factory.endSystem();
126 } catch (FatalError const& e) {
127 O3PRM_SYSTEM_INSTANTIATION_FAILED(*sys, O3PRM_EXCEPTION_MESSAGE(e), *_errors_);
128 }
129 }
130 }
131 }
132
133 template < GUM_Numeric GUM_SCALAR >
135 O3System& sys) {
136 for (auto& i: sys.instances()) {
137 if (i.parameters().size() > 0) {
138 auto params = HashTable< std::string, double >();
139 for (auto& p: i.parameters()) {
140 params.insert(p.name().label(), (double)p.value().value());
141 }
142 factory.addInstance(i.type().label(), i.name().label(), params);
143
144 } else {
145 if (i.size().value() > 1) {
146 factory.addArray(i.type().label(), i.name().label(), i.size().value());
147 } else {
148 factory.addInstance(i.type().label(), i.name().label());
149 }
150 }
151 }
152 }
153
154 template < GUM_Numeric GUM_SCALAR >
156 O3System& sys) {
157 const auto& real_sys = _prm_->getSystem(sys.name().label());
158
159 for (auto& ass: sys.assignments()) {
160 auto leftInstance = ass.leftInstance().label();
161 auto leftReference = ass.leftReference().label();
162 auto rightInstance = ass.rightInstance().label();
163
164 if (ass.leftIndex().value() > -1 && real_sys.isArray(leftInstance)) {
165 leftInstance = std::format("{}[{}]", leftInstance, ass.leftIndex().value());
166 }
167
168 if (ass.rightIndex().value() > -1 && real_sys.isArray(rightInstance)) {
169 rightInstance = std::format("{}[{}]", rightInstance, ass.rightIndex().value());
170 }
171
172 factory.setReferenceSlot(leftInstance, leftReference, rightInstance);
173 }
174 }
175
176 template < GUM_Numeric GUM_SCALAR >
178 O3System& sys) {
179 const auto& real_sys = _prm_->getSystem(sys.name().label());
180 for (auto& inc: sys.increments()) {
181 auto leftInstance = inc.leftInstance().label();
182 auto leftReference = inc.leftReference().label();
183 auto rightInstance = inc.rightInstance().label();
184
185 if (inc.leftIndex().value() > -1 && real_sys.isArray(leftInstance)) {
186 leftInstance = std::format("{}[{}]", leftInstance, inc.leftIndex().value());
187 }
188
189 if (inc.rightIndex().value() > -1 && real_sys.isArray(rightInstance)) {
190 rightInstance = std::format("{}[{}]", rightInstance, inc.rightIndex().value());
191 }
192
193 factory.setReferenceSlot(leftInstance, leftReference, rightInstance);
194 }
195 }
196
197 template < GUM_Numeric GUM_SCALAR >
199 if (_checkInstance_(sys) && _checkAssignments_(sys) && _checkIncrements_(sys)) {
200 return true;
201 }
202
203 return false;
204 }
205
206 template < GUM_Numeric GUM_SCALAR >
208 for (auto& i: sys.instances()) {
209 if (!_solver_->resolveClass(i.type())) { return false; }
210
211 const auto& type = _prm_->getClass(i.type().label());
212 if (type.parameters().size() > 0) {
213 if (!_checkParameters_(type, i)) { return false; }
214 }
215
216 if (_nameMap_.exists(i.name().label())) {
217 O3PRM_SYSTEM_DUPLICATE_INSTANCE(i, *_errors_);
218 return false;
219 }
220
221 _nameMap_.insert(i.name().label(), &i);
222 }
223
224 return true;
225 }
226
227 template < GUM_Numeric GUM_SCALAR >
229 const O3Instance& inst) {
230 for (const auto& param: inst.parameters()) {
231 if (!type.exists(param.name().label())) {
232 O3PRM_SYSTEM_PARAMETER_NOT_FOUND(param, *_errors_);
233 return false;
234 }
235
236 if (!PRMClassElement< GUM_SCALAR >::isParameter(type.get(param.name().label()))) {
237 O3PRM_SYSTEM_NOT_A_PARAMETER(param, *_errors_);
238 return false;
239 }
240
241 const auto& type_param
242 = static_cast< const PRMParameter< GUM_SCALAR >& >(type.get(param.name().label()));
243
244 switch (type_param.valueType()) {
246 if (!param.isInteger()) {
247 O3PRM_SYSTEM_PARAMETER_NOT_INT(param, *_errors_);
248 return false;
249 }
250 break;
251 }
252
254 if (param.isInteger()) {
255 O3PRM_SYSTEM_PARAMETER_NOT_FLOAT(param, *_errors_);
256 return false;
257 }
258 break;
259 }
260
261 default : {
262 GUM_ERROR(FatalError, "unknown parameter type")
263 }
264 }
265 }
266 return true;
267 }
268
269 template < GUM_Numeric GUM_SCALAR >
271 for (auto& ass: sys.assignments()) {
272 // if ( ass.leftInstance().label() == ass.leftReference().label() ) {
273 // O3PRM_SYSTEM_INVALID_LEFT_VALUE( ass.leftInstance(), * _errors_ );
274 // return false;
275 //}
276
277 if (!_nameMap_.exists(ass.leftInstance().label())) {
278 O3PRM_SYSTEM_INSTANCE_NOT_FOUND(ass.leftInstance(), *_errors_);
279 return false;
280 }
281
282 auto i = _nameMap_[ass.leftInstance().label()];
283 const auto& type = _prm_->getClass(i->type().label());
284 const auto& ref = ass.leftReference().label();
285
286 if (!(type.exists(ass.leftReference().label())
288 O3PRM_SYSTEM_REFERENCE_NOT_FOUND(ass.leftReference(), type.name(), *_errors_);
289 return false;
290 }
291
292 const auto& real_ref
293 = static_cast< const PRMReferenceSlot< GUM_SCALAR >& >(type.get(ref));
294
295 if (!_nameMap_.exists(ass.rightInstance().label())) {
296 O3PRM_SYSTEM_INSTANCE_NOT_FOUND(ass.rightInstance(), *_errors_);
297 return false;
298 }
299
300 if (real_ref.isArray() && _nameMap_[ass.rightInstance().label()]->size().value() == 0) {
301 O3PRM_SYSTEM_NOT_AN_ARRAY(ass.rightInstance(), *_errors_);
302 return false;
303 }
304
305 if ((!real_ref.isArray()) && _nameMap_[ass.rightInstance().label()]->size().value() > 0
306 && ass.rightIndex().value() == -1) {
307 O3PRM_SYSTEM_NOT_AN_ARRAY(ass.leftReference(), *_errors_);
308 return false;
309 }
310 }
311 return true;
312 }
313
314 template < GUM_Numeric GUM_SCALAR >
316 for (auto& inc: sys.increments()) {
317 // if ( inc.leftInstance().label() == inc.leftReference().label() ) {
318 // O3PRM_SYSTEM_INVALID_LEFT_VALUE( inc.leftInstance(), * _errors_ );
319 // return false;
320 //}
321
322 if (!_nameMap_.exists(inc.leftInstance().label())) {
323 O3PRM_SYSTEM_INSTANCE_NOT_FOUND(inc.leftInstance(), *_errors_);
324 return false;
325 }
326
327 auto i = _nameMap_[inc.leftInstance().label()];
328 const auto& type = _prm_->getClass(i->type().label());
329 const auto& ref = inc.leftReference().label();
330
331 if (!(type.exists(inc.leftReference().label())
333 O3PRM_SYSTEM_REFERENCE_NOT_FOUND(inc.leftReference(), type.name(), *_errors_);
334 return false;
335 }
336
337 const auto& real_ref
338 = static_cast< const PRMReferenceSlot< GUM_SCALAR >& >(type.get(ref));
339
340 if (!real_ref.isArray()) {
341 O3PRM_SYSTEM_NOT_AN_ARRAY(inc.leftReference(), *_errors_);
342 return false;
343 }
344 }
345
346 return true;
347 }
348 } // namespace o3prm
349 } // namespace prm
350} // namespace gum
Headers for the O3SystemFactory class.
This class is used contain and manipulate gum::ParseError.
Exception : fatal (unknown ?) error.
The class for generic Hash Tables.
Definition hashTable.h:640
virtual bool exists(std::string_view name) const
Returns true if a member with the given name exists in this PRMClassElementContainer or in the PRMCla...
static INLINE bool isReferenceSlot(const PRMClassElement< GUM_SCALAR > &elt)
Returns true if obj_ptr is of type PRMReferenceSlot.
static INLINE bool isParameter(const PRMClassElement< GUM_SCALAR > &elt)
Return true if obj is of type PRMParameter.
A PRMClass is an object of a PRM representing a fragment of a Bayesian network which can be instantia...
Definition PRMClass.h:77
PRMClassElement< GUM_SCALAR > & get(NodeId id) override
See gum::prm::PRMClassElementContainer<GUM_SCALAR>::get(NodeId).
Factory which builds a PRM<GUM_SCALAR>.
Definition PRMFactory.h:90
void setReferenceSlot(std::string_view left_instance, std::string_view left_reference, std::string_view right_instance) override
Instantiate a reference in the current model.
void startSystem(std::string_view name) override
Tells the factory that we started declaring a model.
void addInstance(std::string_view type, std::string_view name) override
Add an instance to the model.
void endSystem() override
Tells the factory that we finished declaring a model.
void addArray(std::string_view type, std::string_view name, Size size) override
Creates an array with the given number of instances of the given type.
PRMParameter is a member of a Class in a PRM.
A PRMReferenceSlot represent a relation between two PRMClassElementContainer.
This class represents a Probabilistic Relational PRMSystem<GUM_SCALAR>.
Definition PRM.h:74
The O3Instance is part of the AST of the O3PRM language.
Definition O3prm.h:813
O3InstanceParameterList & parameters()
Definition O3prm.cpp:1352
std::string & label()
Definition O3prm.cpp:286
Resolves names for the different O3PRM factories.
The O3PRM is part of the AST of the O3PRM language.
Definition O3prm.h:915
Builds gum::prm::PRMSystem from gum::prm::o3prm::O3System.
void _addInstances_(PRMFactory< GUM_SCALAR > &factory, O3System &sys)
O3SystemFactory< GUM_SCALAR > & operator=(const O3SystemFactory< GUM_SCALAR > &src)
bool _checkParameters_(const PRMClass< GUM_SCALAR > &type, const O3Instance &inst)
void _addAssignments_(PRMFactory< GUM_SCALAR > &factory, O3System &sys)
HashTable< std::string, O3Instance * > _nameMap_
void _addIncrements_(PRMFactory< GUM_SCALAR > &factory, O3System &sys)
O3NameSolver< GUM_SCALAR > * _solver_
O3SystemFactory(PRM< GUM_SCALAR > &prm, O3PRM &o3_prm, O3NameSolver< GUM_SCALAR > &solver, ErrorsContainer &errors)
The O3System is part of the AST of the O3PRM language.
Definition O3prm.h:851
O3AssignmentList & assignments()
Definition O3prm.cpp:1404
O3InstanceList & instances()
Definition O3prm.cpp:1400
O3IncrementList & increments()
Definition O3prm.cpp:1408
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
namespace for all probabilistic relational models entities
Definition agrum.h:68
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
STL namespace.