aGrUM 2.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-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
50
52
53namespace gum {
54 namespace prm {
55 namespace o3prm {
56
57 template < typename GUM_SCALAR >
59 O3PRM& o3_prm,
61 ErrorsContainer& errors) :
62 _prm_(&prm), _o3_prm_(&o3_prm), _solver_(&solver), _errors_(&errors) {
63 GUM_CONSTRUCTOR(O3SystemFactory);
64 }
65
66 template < typename GUM_SCALAR >
67 INLINE
73
74 template < typename 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 < typename GUM_SCALAR >
86
87 template < typename 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 < typename 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 < typename 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 < typename 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 < typename 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 std::stringstream sBuff;
164 sBuff << leftInstance << "[" << ass.leftIndex().value() << "]";
165 leftInstance = sBuff.str();
166 }
167
168 if (ass.rightIndex().value() > -1 && real_sys.isArray(rightInstance)) {
169 std::stringstream sBuff;
170 sBuff << rightInstance << "[" << ass.rightIndex().value() << "]";
171 rightInstance = sBuff.str();
172 }
173
174 factory.setReferenceSlot(leftInstance, leftReference, rightInstance);
175 }
176 }
177
178 template < typename GUM_SCALAR >
180 O3System& sys) {
181 const auto& real_sys = _prm_->getSystem(sys.name().label());
182 for (auto& inc: sys.increments()) {
183 auto leftInstance = inc.leftInstance().label();
184 auto leftReference = inc.leftReference().label();
185 auto rightInstance = inc.rightInstance().label();
186
187 if (inc.leftIndex().value() > -1 && real_sys.isArray(leftInstance)) {
188 std::stringstream sBuff;
189 sBuff << leftInstance << "[" << inc.leftIndex().value() << "]";
190 leftInstance = sBuff.str();
191 }
192
193 if (inc.rightIndex().value() > -1 && real_sys.isArray(rightInstance)) {
194 std::stringstream sBuff;
195 sBuff << rightInstance << "[" << inc.rightIndex().value() << "]";
196 rightInstance = sBuff.str();
197 }
198
199 factory.setReferenceSlot(leftInstance, leftReference, rightInstance);
200 }
201 }
202
203 template < typename GUM_SCALAR >
205 if (_checkInstance_(sys) && _checkAssignments_(sys) && _checkIncrements_(sys)) {
206 return true;
207 }
208
209 return false;
210 }
211
212 template < typename GUM_SCALAR >
214 for (auto& i: sys.instances()) {
215 if (!_solver_->resolveClass(i.type())) { return false; }
216
217 const auto& type = _prm_->getClass(i.type().label());
218 if (type.parameters().size() > 0) {
219 if (!_checkParameters_(type, i)) { return false; }
220 }
221
222 if (_nameMap_.exists(i.name().label())) {
223 O3PRM_SYSTEM_DUPLICATE_INSTANCE(i, *_errors_);
224 return false;
225 }
226
227 _nameMap_.insert(i.name().label(), &i);
228 }
229
230 return true;
231 }
232
233 template < typename GUM_SCALAR >
234 INLINE bool
236 const O3Instance& inst) {
237 for (const auto& param: inst.parameters()) {
238 if (!type.exists(param.name().label())) {
239 O3PRM_SYSTEM_PARAMETER_NOT_FOUND(param, *_errors_);
240 return false;
241 }
242
243 if (!PRMClassElement< GUM_SCALAR >::isParameter(type.get(param.name().label()))) {
244 O3PRM_SYSTEM_NOT_A_PARAMETER(param, *_errors_);
245 return false;
246 }
247
248 const auto& type_param
249 = static_cast< const PRMParameter< GUM_SCALAR >& >(type.get(param.name().label()));
250
251 switch (type_param.valueType()) {
253 if (!param.isInteger()) {
254 O3PRM_SYSTEM_PARAMETER_NOT_INT(param, *_errors_);
255 return false;
256 }
257 break;
258 }
259
261 if (param.isInteger()) {
262 O3PRM_SYSTEM_PARAMETER_NOT_FLOAT(param, *_errors_);
263 return false;
264 }
265 break;
266 }
267
268 default : {
269 GUM_ERROR(FatalError, "unknown parameter type")
270 }
271 }
272 }
273 return true;
274 }
275
276 template < typename GUM_SCALAR >
278 for (auto& ass: sys.assignments()) {
279 // if ( ass.leftInstance().label() == ass.leftReference().label() ) {
280 // O3PRM_SYSTEM_INVALID_LEFT_VALUE( ass.leftInstance(), * _errors_ );
281 // return false;
282 //}
283
284 if (!_nameMap_.exists(ass.leftInstance().label())) {
285 O3PRM_SYSTEM_INSTANCE_NOT_FOUND(ass.leftInstance(), *_errors_);
286 return false;
287 }
288
289 auto i = _nameMap_[ass.leftInstance().label()];
290 const auto& type = _prm_->getClass(i->type().label());
291 const auto& ref = ass.leftReference().label();
292
293 if (!(type.exists(ass.leftReference().label())
295 O3PRM_SYSTEM_REFERENCE_NOT_FOUND(ass.leftReference(), type.name(), *_errors_);
296 return false;
297 }
298
299 const auto& real_ref
300 = static_cast< const PRMReferenceSlot< GUM_SCALAR >& >(type.get(ref));
301
302 if (!_nameMap_.exists(ass.rightInstance().label())) {
303 O3PRM_SYSTEM_INSTANCE_NOT_FOUND(ass.rightInstance(), *_errors_);
304 return false;
305 }
306
307 if (real_ref.isArray() && _nameMap_[ass.rightInstance().label()]->size().value() == 0) {
308 O3PRM_SYSTEM_NOT_AN_ARRAY(ass.rightInstance(), *_errors_);
309 return false;
310 }
311
312 if ((!real_ref.isArray()) && _nameMap_[ass.rightInstance().label()]->size().value() > 0
313 && ass.rightIndex().value() == -1) {
314 O3PRM_SYSTEM_NOT_AN_ARRAY(ass.leftReference(), *_errors_);
315 return false;
316 }
317 }
318 return true;
319 }
320
321 template < typename GUM_SCALAR >
323 for (auto& inc: sys.increments()) {
324 // if ( inc.leftInstance().label() == inc.leftReference().label() ) {
325 // O3PRM_SYSTEM_INVALID_LEFT_VALUE( inc.leftInstance(), * _errors_ );
326 // return false;
327 //}
328
329 if (!_nameMap_.exists(inc.leftInstance().label())) {
330 O3PRM_SYSTEM_INSTANCE_NOT_FOUND(inc.leftInstance(), *_errors_);
331 return false;
332 }
333
334 auto i = _nameMap_[inc.leftInstance().label()];
335 const auto& type = _prm_->getClass(i->type().label());
336 const auto& ref = inc.leftReference().label();
337
338 if (!(type.exists(inc.leftReference().label())
340 O3PRM_SYSTEM_REFERENCE_NOT_FOUND(inc.leftReference(), type.name(), *_errors_);
341 return false;
342 }
343
344 const auto& real_ref
345 = static_cast< const PRMReferenceSlot< GUM_SCALAR >& >(type.get(ref));
346
347 if (!real_ref.isArray()) {
348 O3PRM_SYSTEM_NOT_AN_ARRAY(inc.leftReference(), *_errors_);
349 return false;
350 }
351 }
352
353 return true;
354 }
355 } // namespace o3prm
356 } // namespace prm
357} // 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:637
virtual bool exists(const std::string &name) const
Returns true if a member with the given name exists in this PRMClassElementContainer or in the PRMCla...
static INLINE bool isParameter(const PRMClassElement< GUM_SCALAR > &elt)
Return true if obj is of type PRMParameter.
static INLINE bool isReferenceSlot(const PRMClassElement< GUM_SCALAR > &elt)
Returns true if obj_ptr is of type PRMReferenceSlot.
A PRMClass is an object of a PRM representing a fragment of a Bayesian network which can be instantia...
Definition PRMClass.h:75
PRMClassElement< GUM_SCALAR > & get(NodeId id)
See gum::prm::PRMClassElementContainer<GUM_SCALAR>::get(NodeId).
Factory which builds a PRM<GUM_SCALAR>.
Definition PRMFactory.h:88
virtual void addInstance(const std::string &type, const std::string &name) override
Add an instance to the model.
virtual void endSystem() override
Tells the factory that we finished declaring a model.
virtual void setReferenceSlot(const std::string &left_instance, const std::string &left_reference, const std::string &right_instance) override
Instantiate a reference in the current model.
virtual void addArray(const std::string &type, const std::string &name, Size size) override
Creates an array with the given number of instances of the given type.
virtual void startSystem(const std::string &name) override
Tells the factory that we started declaring a model.
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:811
O3InstanceParameterList & parameters()
Definition O3prm.cpp:1353
std::string & label()
Definition O3prm.cpp:287
Resolves names for the different O3PRM factories.
The O3PRM is part of the AST of the O3PRM language.
Definition O3prm.h:913
Builds gum::prm::PRMSystem from gum::prm::o3prm::O3System.
bool _checkParameters_(const PRMClass< GUM_SCALAR > &type, const O3Instance &inst)
void _addIncrements_(PRMFactory< GUM_SCALAR > &factory, O3System &sys)
O3SystemFactory(PRM< GUM_SCALAR > &prm, O3PRM &o3_prm, O3NameSolver< GUM_SCALAR > &solver, ErrorsContainer &errors)
O3SystemFactory< GUM_SCALAR > & operator=(const O3SystemFactory< GUM_SCALAR > &src)
void _addAssignments_(PRMFactory< GUM_SCALAR > &factory, O3System &sys)
HashTable< std::string, O3Instance * > _nameMap_
void _addInstances_(PRMFactory< GUM_SCALAR > &factory, O3System &sys)
O3NameSolver< GUM_SCALAR > * _solver_
The O3System is part of the AST of the O3PRM language.
Definition O3prm.h:849
O3AssignmentList & assignments()
Definition O3prm.cpp:1405
O3InstanceList & instances()
Definition O3prm.cpp:1401
O3IncrementList & increments()
Definition O3prm.cpp:1409
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
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.