aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
O3prmrContext_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
50
51#include <agrum/PRM/o3prmr/O3prmrContext.h> // to ease IDE parser
52
53#include "O3prmrContext.h"
54
55namespace gum {
56 namespace prm {
57 namespace o3prmr {
58
59 /* ******************************************************************* */
60
61 template < GUM_Numeric GUM_SCALAR >
66
67 template < GUM_Numeric GUM_SCALAR >
71
72 template < GUM_Numeric GUM_SCALAR >
76
77 template < GUM_Numeric GUM_SCALAR >
79 return leftValue + " = " + rightValue + ";";
80 }
81
82 /* ******************************************************************* */
83
84 template < GUM_Numeric GUM_SCALAR >
87
88 template < GUM_Numeric GUM_SCALAR >
91
92 template < GUM_Numeric GUM_SCALAR >
96
97 template < GUM_Numeric GUM_SCALAR >
99 return "unobserve " + value + ";";
100 }
101
102 /* ******************************************************************* */
103
104 template < GUM_Numeric GUM_SCALAR >
106 O3prmrCommand(line), value(val), system(nullptr) {}
107
108 template < GUM_Numeric GUM_SCALAR >
112
113 template < GUM_Numeric GUM_SCALAR >
115 return "? " + value + ";";
116 }
117
118 /* ******************************************************************* */
119
120 template < GUM_Numeric GUM_SCALAR >
124
125 template < GUM_Numeric GUM_SCALAR >
127 m_imports.push_back(new ImportCommand(i.line, i.value, i.alias));
128
129 if (i.alias == "default") m_mainImport = m_imports.back();
130 }
131
132 /* ******************************************************************* */
133
134 template < GUM_Numeric GUM_SCALAR >
139
140 template < GUM_Numeric GUM_SCALAR >
146
147 template < GUM_Numeric GUM_SCALAR >
149 for (Idx i = Size(m_imports.size()); i > 0; i--)
150 delete m_imports[i - 1];
151
152 for (Size i = Size(m_sessions.size()); i > 0; i--)
153 delete m_sessions[i - 1];
154 }
155
156 template < GUM_Numeric GUM_SCALAR >
158 return m_filename;
159 }
160
161 template < GUM_Numeric GUM_SCALAR >
163 return m_package;
164 }
165
166 template < GUM_Numeric GUM_SCALAR >
170
171 template < GUM_Numeric GUM_SCALAR >
172 std::string O3prmrContext< GUM_SCALAR >::aliasToImport(std::string_view alias) {
173 for (Idx i = Size(m_imports.size()); i > 0; i--)
174 if (m_imports[i - 1]->alias == alias) return m_imports[i - 1]->value;
175
176 return {};
177 }
178
179 template < GUM_Numeric GUM_SCALAR >
180 std::vector< ImportCommand* > O3prmrContext< GUM_SCALAR >::imports() const {
181 return m_imports;
182 }
183
184 template < GUM_Numeric GUM_SCALAR >
186 std::string_view import,
187 std::string_view alias) {
188 m_imports.push_back(new ImportCommand(line, import, alias));
189
190 if (alias == "default") m_mainImport = m_imports.back();
191 }
192
193 template < GUM_Numeric GUM_SCALAR >
194 void O3prmrContext< GUM_SCALAR >::addImport(int line, std::string_view import, bool ismain) {
195 m_imports.push_back(new ImportCommand(line, import, import));
196
197 if (ismain) m_mainImport = m_imports.back();
198 }
199
200 template < GUM_Numeric GUM_SCALAR >
201 std::vector< O3prmrSession< GUM_SCALAR >* > O3prmrContext< GUM_SCALAR >::sessions() const {
202 return m_sessions;
203 }
204
205 template < GUM_Numeric GUM_SCALAR >
209
210 template < GUM_Numeric GUM_SCALAR >
212 std::string output;
213
214 if (!m_package.empty()) {
215 output += "package " + m_package + ";\n";
216 output += "\n";
217 }
218
219 for (auto i = m_imports.begin(); i < m_imports.end(); i++)
220 output += (*i)->toString() + "\n";
221
222 output += "\n";
223
224 for (auto i = m_sessions.begin(); i < m_sessions.end(); i++)
225 output += (*i)->toString() + "\n";
227 return output;
229
230 template < GUM_Numeric GUM_SCALAR >
232 const std::vector< ImportCommand* >& imports = c.imports();
234 for (std::vector< ImportCommand* >::const_iterator i = imports.begin(); i != imports.end();
235 i++)
238 const std::vector< O3prmrSession< GUM_SCALAR >* >& sessions = c.sessions();
239
240 if (sessions.size() == 1 && sessions.back()->name() == "default") {
241 *(this->m_sessions.back()) += *(sessions.back());
242 } else
243 for (auto i = sessions.begin(); i != sessions.end(); i++)
244 addSession(**i);
246 return *this;
247 }
248
249 /* ******************************************************************* */
250
251 template < GUM_Numeric GUM_SCALAR >
255
256 template < GUM_Numeric GUM_SCALAR >
261
262 template < GUM_Numeric GUM_SCALAR >
264 for (Idx i = Size(m_commands.size()); i >= 1; i--)
265 delete m_commands[i - 1];
266
267 m_commands.clear();
268 }
269
270 template < GUM_Numeric GUM_SCALAR >
272 return m_name;
273 }
274
275 template < GUM_Numeric GUM_SCALAR >
276 std::vector< O3prmrCommand* > O3prmrSession< GUM_SCALAR >::commands() const {
277 return m_commands;
278 }
279
280 template < GUM_Numeric GUM_SCALAR >
282 m_commands.push_back(command);
283 }
284
285 template < GUM_Numeric GUM_SCALAR >
287 std::string_view leftValue,
288 std::string_view rightValue) {
289 addCommand(new ObserveCommand< GUM_SCALAR >(line, leftValue, rightValue));
290 }
291
292 template < GUM_Numeric GUM_SCALAR >
293 void O3prmrSession< GUM_SCALAR >::addUnobserve(int line, std::string_view value) {
295 }
296
297 template < GUM_Numeric GUM_SCALAR >
298 void O3prmrSession< GUM_SCALAR >::addQuery(int line, std::string_view value) {
299 addCommand(new QueryCommand< GUM_SCALAR >(line, value));
300 }
301
302 template < GUM_Numeric GUM_SCALAR >
303 void O3prmrSession< GUM_SCALAR >::addSetEngine(int line, std::string_view value) {
304 addCommand(new SetEngineCommand(line, value));
305 }
306
307 template < GUM_Numeric GUM_SCALAR >
308 void O3prmrSession< GUM_SCALAR >::addSetGndEngine(int line, std::string_view value) {
309 addCommand(new SetGndEngineCommand(line, value));
310 }
311
312 template < GUM_Numeric GUM_SCALAR >
314 switch (command->type()) {
316 m_commands.push_back(new SetEngineCommand(*(SetEngineCommand*)command));
317 break;
318
320 m_commands.push_back(new SetGndEngineCommand(*(SetGndEngineCommand*)command));
321 break;
322
324 m_commands.push_back(
326 break;
327
329 m_commands.push_back(
331 break;
332
334 m_commands.push_back(
336 break;
337 }
338 }
339
340 template < GUM_Numeric GUM_SCALAR >
342 std::string output;
343
344 output += "request " + m_name + " {\n";
345
346 for (std::vector< O3prmrCommand* >::const_iterator i = m_commands.begin();
347 i < m_commands.end();
348 i++) {
349 output += "\t";
350 output += (*i)->toString() + "\n";
351 }
352
353 output += "}\n";
354
355 return output;
356 }
357
358 template < GUM_Numeric GUM_SCALAR >
361 for (std::vector< O3prmrCommand* >::const_iterator i = c.m_commands.begin();
362 i < c.m_commands.end();
363 i++)
364 addCommand(*i);
365
366 return *this;
367 }
368
369 /* ******************************************************************* */
370
371 } // namespace o3prmr
372 } // namespace prm
373} // namespace gum
Headers of O3prmInterpreter.
This is an abstract class.
virtual RequestType type() const =0
std::vector< ImportCommand * > m_imports
void addImport(int line, std::string_view import, std::string_view alias)
std::vector< O3prmrSession< GUM_SCALAR > * > sessions() const
void addSession(const O3prmrSession< GUM_SCALAR > &session)
virtual std::string toString() const
std::vector< ImportCommand * > imports() const
std::vector< O3prmrSession< GUM_SCALAR > * > m_sessions
O3prmrContext(std::string_view filename="")
const ImportCommand * mainImport() const
std::string aliasToImport(std::string_view alias)
void setPackage(std::string_view package)
O3prmrContext & operator+=(const O3prmrContext &c)
This class contains a o3prmr session.
O3prmrSession & operator+=(const O3prmrSession &c)
O3prmrSession(std::string_view name="")
std::vector< O3prmrCommand * > commands() const
void addSetEngine(int line, std::string_view value)
void addUnobserve(int line, std::string_view value)
std::string m_name
The session name;.
virtual std::string toString() const
void addObserve(int line, std::string_view leftValue, std::string_view rightValue)
void addQuery(int line, std::string_view value)
void addSetGndEngine(int line, std::string_view value)
std::vector< O3prmrCommand * > m_commands
A sequence of commands.
void addCommand(const O3prmrCommand *command)
std::string toString() const override
RequestType type() const override
PRMInference< GUM_SCALAR >::Chain chain
const PRMSystem< GUM_SCALAR > * system
ObserveCommand(int line, std::string_view leftValue, std::string_view rightValue)
const PRMSystem< GUM_SCALAR > * system
RequestType type() const override
std::string toString() const override
QueryCommand(int line, std::string_view val)
UnobserveCommand(int line, std::string_view value)
PRMInference< GUM_SCALAR >::Chain chain
const PRMSystem< GUM_SCALAR > * system
std::string toString() const override
RequestType type() const override
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Size Idx
Type for indexes.
Definition types.h:79
namespace for all probabilistic relational models entities
Definition agrum.h:68
gum is the global namespace for all aGrUM entities
Definition agrum.h:46