aGrUM 3.1.1
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&) { O3PRM_SYSTEM_INSTANTIATION_FAILED(*sys, *_errors_); }
127 }
128 }
129 }
130
131 template < GUM_Numeric GUM_SCALAR >
133 O3System& sys) {
134 for (auto& i: sys.instances()) {
135 if (i.parameters().size() > 0) {
136 auto params = HashTable< std::string, double >();
137 for (auto& p: i.parameters()) {
138 params.insert(p.name().label(), (double)p.value().value());
139 }
140 factory.addInstance(i.type().label(), i.name().label(), params);
141
142 } else {
143 if (i.size().value() > 1) {
144 factory.addArray(i.type().label(), i.name().label(), i.size().value());
145 } else {
146 factory.addInstance(i.type().label(), i.name().label());
147 }
148 }
149 }
150 }
151
152 template < GUM_Numeric GUM_SCALAR >
154 O3System& sys) {
155 const auto& real_sys = _prm_->getSystem(sys.name().label());
156
157 for (auto& ass: sys.assignments()) {
158 auto leftInstance = ass.leftInstance().label();
159 auto leftReference = ass.leftReference().label();
160 auto rightInstance = ass.rightInstance().label();
161
162 if (ass.leftIndex().value() > -1 && real_sys.isArray(leftInstance)) {
163 leftInstance = std::format("{}[{}]", leftInstance, ass.leftIndex().value());
164 }
165
166 if (ass.rightIndex().value() > -1 && real_sys.isArray(rightInstance)) {
167 rightInstance = std::format("{}[{}]", rightInstance, ass.rightIndex().value());
168 }
169
170 factory.setReferenceSlot(leftInstance, leftReference, rightInstance);
171 }
172 }
173
174 template < GUM_Numeric GUM_SCALAR >
176 O3System& sys) {
177 const auto& real_sys = _prm_->getSystem(sys.name().label());
178 for (auto& inc: sys.increments()) {
179 auto leftInstance = inc.leftInstance().label();
180 auto leftReference = inc.leftReference().label();
181 auto rightInstance = inc.rightInstance().label();
182
183 if (inc.leftIndex().value() > -1 && real_sys.isArray(leftInstance)) {
184 leftInstance = std::format("{}[{}]", leftInstance, inc.leftIndex().value());
185 }
186
187 if (inc.rightIndex().value() > -1 && real_sys.isArray(rightInstance)) {
188 rightInstance = std::format("{}[{}]", rightInstance, inc.rightIndex().value());
189 }
190
191 factory.setReferenceSlot(leftInstance, leftReference, rightInstance);
192 }
193 }
194
195 template < GUM_Numeric GUM_SCALAR >
197 if (_checkInstance_(sys) && _checkAssignments_(sys) && _checkIncrements_(sys)) {
198 return true;
199 }
200
201 return false;
202 }
203
204 template < GUM_Numeric GUM_SCALAR >
206 for (auto& i: sys.instances()) {
207 if (!_solver_->resolveClass(i.type())) { return false; }
208
209 const auto& type = _prm_->getClass(i.type().label());
210 if (type.parameters().size() > 0) {
211 if (!_checkParameters_(type, i)) { return false; }
212 }
213
214 if (_nameMap_.exists(i.name().label())) {
215 O3PRM_SYSTEM_DUPLICATE_INSTANCE(i, *_errors_);
216 return false;
217 }
218
219 _nameMap_.insert(i.name().label(), &i);
220 }
221
222 return true;
223 }
224
225 template < GUM_Numeric GUM_SCALAR >
227 const O3Instance& inst) {
228 for (const auto& param: inst.parameters()) {
229 if (!type.exists(param.name().label())) {
230 O3PRM_SYSTEM_PARAMETER_NOT_FOUND(param, *_errors_);
231 return false;
232 }
233
234 if (!PRMClassElement< GUM_SCALAR >::isParameter(type.get(param.name().label()))) {
235 O3PRM_SYSTEM_NOT_A_PARAMETER(param, *_errors_);
236 return false;
237 }
238
239 const auto& type_param
240 = static_cast< const PRMParameter< GUM_SCALAR >& >(type.get(param.name().label()));
241
242 switch (type_param.valueType()) {
244 if (!param.isInteger()) {
245 O3PRM_SYSTEM_PARAMETER_NOT_INT(param, *_errors_);
246 return false;
247 }
248 break;
249 }
250
252 if (param.isInteger()) {
253 O3PRM_SYSTEM_PARAMETER_NOT_FLOAT(param, *_errors_);
254 return false;
255 }
256 break;
257 }
258
259 default : {
260 GUM_ERROR(FatalError, "unknown parameter type")
261 }
262 }
263 }
264 return true;
265 }
266
267 template < GUM_Numeric GUM_SCALAR >
269 for (auto& ass: sys.assignments()) {
270 // if ( ass.leftInstance().label() == ass.leftReference().label() ) {
271 // O3PRM_SYSTEM_INVALID_LEFT_VALUE( ass.leftInstance(), * _errors_ );
272 // return false;
273 //}
274
275 if (!_nameMap_.exists(ass.leftInstance().label())) {
276 O3PRM_SYSTEM_INSTANCE_NOT_FOUND(ass.leftInstance(), *_errors_);
277 return false;
278 }
279
280 auto i = _nameMap_[ass.leftInstance().label()];
281 const auto& type = _prm_->getClass(i->type().label());
282 const auto& ref = ass.leftReference().label();
283
284 if (!(type.exists(ass.leftReference().label())
286 O3PRM_SYSTEM_REFERENCE_NOT_FOUND(ass.leftReference(), type.name(), *_errors_);
287 return false;
288 }
289
290 const auto& real_ref
291 = static_cast< const PRMReferenceSlot< GUM_SCALAR >& >(type.get(ref));
292
293 if (!_nameMap_.exists(ass.rightInstance().label())) {
294 O3PRM_SYSTEM_INSTANCE_NOT_FOUND(ass.rightInstance(), *_errors_);
295 return false;
296 }
297
298 if (real_ref.isArray() && _nameMap_[ass.rightInstance().label()]->size().value() == 0) {
299 O3PRM_SYSTEM_NOT_AN_ARRAY(ass.rightInstance(), *_errors_);
300 return false;
301 }
302
303 if ((!real_ref.isArray()) && _nameMap_[ass.rightInstance().label()]->size().value() > 0
304 && ass.rightIndex().value() == -1) {
305 O3PRM_SYSTEM_NOT_AN_ARRAY(ass.leftReference(), *_errors_);
306 return false;
307 }
308 }
309 return true;
310 }
311
312 template < GUM_Numeric GUM_SCALAR >
314 for (auto& inc: sys.increments()) {
315 // if ( inc.leftInstance().label() == inc.leftReference().label() ) {
316 // O3PRM_SYSTEM_INVALID_LEFT_VALUE( inc.leftInstance(), * _errors_ );
317 // return false;
318 //}
319
320 if (!_nameMap_.exists(inc.leftInstance().label())) {
321 O3PRM_SYSTEM_INSTANCE_NOT_FOUND(inc.leftInstance(), *_errors_);
322 return false;
323 }
324
325 auto i = _nameMap_[inc.leftInstance().label()];
326 const auto& type = _prm_->getClass(i->type().label());
327 const auto& ref = inc.leftReference().label();
328
329 if (!(type.exists(inc.leftReference().label())
331 O3PRM_SYSTEM_REFERENCE_NOT_FOUND(inc.leftReference(), type.name(), *_errors_);
332 return false;
333 }
334
335 const auto& real_ref
336 = static_cast< const PRMReferenceSlot< GUM_SCALAR >& >(type.get(ref));
337
338 if (!real_ref.isArray()) {
339 O3PRM_SYSTEM_NOT_AN_ARRAY(inc.leftReference(), *_errors_);
340 return false;
341 }
342 }
343
344 return true;
345 }
346 } // namespace o3prm
347 } // namespace prm
348} // 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.