aGrUM 2.3.2
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-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
49
50#include "O3prmrContext.h"
51
52namespace gum {
53 namespace prm {
54 namespace o3prmr {
55
56 /* ******************************************************************* */
57
58 template < typename GUM_SCALAR >
63
64 template < typename GUM_SCALAR >
70
71 template < typename GUM_SCALAR >
73 for (Idx i = Size(m_imports.size()); i > 0; i--)
74 delete m_imports[i - 1];
75
76 for (Size i = Size(m_sessions.size()); i > 0; i--)
77 delete m_sessions[i - 1];
78 }
79
80 template < typename GUM_SCALAR >
82 return m_filename;
83 }
84
85 template < typename GUM_SCALAR >
87 return m_package;
88 }
89
90 template < typename GUM_SCALAR >
94
95 template < typename GUM_SCALAR >
96 std::string O3prmrContext< GUM_SCALAR >::aliasToImport(const std::string& alias) {
97 for (Idx i = Size(m_imports.size()); i > 0; i--)
98 if (m_imports[i - 1]->alias == alias) return m_imports[i - 1]->value;
99
100 return std::string();
101 }
102
103 template < typename GUM_SCALAR >
104 std::vector< ImportCommand* > O3prmrContext< GUM_SCALAR >::imports() const {
105 return m_imports;
106 }
107
108 template < typename GUM_SCALAR >
110 const std::string& import,
111 const std::string& alias) {
112 m_imports.push_back(new ImportCommand(line, import, alias));
113
114 if (alias == "default") m_mainImport = m_imports.back();
115 }
116
117 template < typename GUM_SCALAR >
118 void
119 O3prmrContext< GUM_SCALAR >::addImport(int line, const std::string& import, bool ismain) {
120 m_imports.push_back(new ImportCommand(line, import, import));
121
122 if (ismain) m_mainImport = m_imports.back();
123 }
124
125 template < typename GUM_SCALAR >
126 std::vector< O3prmrSession< GUM_SCALAR >* > O3prmrContext< GUM_SCALAR >::sessions() const {
127 return m_sessions;
128 }
129
130 template < typename GUM_SCALAR >
134
135 template < typename GUM_SCALAR >
137 std::string output;
138
139 if (!m_package.empty()) {
140 output += "package " + m_package + ";\n";
141 output += "\n";
142 }
143
144 for (auto i = m_imports.begin(); i < m_imports.end(); i++)
145 output += (*i)->toString() + "\n";
146
147 output += "\n";
148
149 for (auto i = m_sessions.begin(); i < m_sessions.end(); i++)
150 output += (*i)->toString() + "\n";
151
152 return output;
153 }
154
155 template < typename GUM_SCALAR >
157 const std::vector< ImportCommand* >& imports = c.imports();
158
159 for (std::vector< ImportCommand* >::const_iterator i = imports.begin(); i != imports.end();
160 i++)
161 addImport(**i);
162
163 const std::vector< O3prmrSession< GUM_SCALAR >* >& sessions = c.sessions();
164
165 if (sessions.size() == 1 && sessions.back()->name() == "default") {
166 *(this->m_sessions.back()) += *(sessions.back());
167 } else
168 for (auto i = sessions.begin(); i != sessions.end(); i++)
169 addSession(**i);
170
171 return *this;
172 }
173
174 /* ******************************************************************* */
175
176 template < typename GUM_SCALAR >
178 m_name = name;
179 }
180
181 template < typename GUM_SCALAR >
186
187 template < typename GUM_SCALAR >
189 for (Idx i = Size(m_commands.size()); i >= 1; i--)
190 delete m_commands[i - 1];
191
192 m_commands.clear();
193 }
194
195 template < typename GUM_SCALAR >
197 return m_name;
199
200 template < typename GUM_SCALAR >
201 std::vector< O3prmrCommand* > O3prmrSession< GUM_SCALAR >::commands() const {
203 }
205 template < typename GUM_SCALAR >
207 m_commands.push_back(command);
210 template < typename GUM_SCALAR >
212 const std::string& leftValue,
213 const std::string& rightValue) {
214 addCommand(new ObserveCommand< GUM_SCALAR >(line, leftValue, rightValue));
215 }
216
217 template < typename GUM_SCALAR >
218 void O3prmrSession< GUM_SCALAR >::addUnobserve(int line, const std::string& value) {
220 }
221
222 template < typename GUM_SCALAR >
223 void O3prmrSession< GUM_SCALAR >::addQuery(int line, const std::string& value) {
224 addCommand(new QueryCommand< GUM_SCALAR >(line, value));
225 }
226
227 template < typename GUM_SCALAR >
228 void O3prmrSession< GUM_SCALAR >::addSetEngine(int line, const std::string& value) {
229 addCommand(new SetEngineCommand(line, value));
230 }
231
232 template < typename GUM_SCALAR >
233 void O3prmrSession< GUM_SCALAR >::addSetGndEngine(int line, const std::string& value) {
234 addCommand(new SetGndEngineCommand(line, value));
235 }
236
237 template < typename GUM_SCALAR >
239 switch (command->type()) {
241 m_commands.push_back(new SetEngineCommand(*(SetEngineCommand*)command));
242 break;
245 m_commands.push_back(new SetGndEngineCommand(*(SetGndEngineCommand*)command));
246 break;
247
249 m_commands.push_back(
251 break;
252
254 m_commands.push_back(
256 break;
259 m_commands.push_back(
261 break;
262 }
263 }
264
265 template < typename GUM_SCALAR >
267 std::string output;
268
269 output += "request " + m_name + " {\n";
270
271 for (std::vector< O3prmrCommand* >::const_iterator i = m_commands.begin();
272 i < m_commands.end();
273 i++) {
274 output += "\t";
275 output += (*i)->toString() + "\n";
276 }
277
278 output += "}\n";
279
280 return output;
281 }
282
283 template < typename GUM_SCALAR >
286 for (std::vector< O3prmrCommand* >::const_iterator i = c.m_commands.begin();
287 i < c.m_commands.end();
288 i++)
289 addCommand(*i);
290
291 return *this;
292 }
293
294 /* ******************************************************************* */
295
296 } // namespace o3prmr
297 } // namespace prm
298} // namespace gum
Headers of O3prmInterpreter.
This is an abstract class.
virtual RequestType type() const =0
std::vector< ImportCommand * > m_imports
std::string aliasToImport(const std::string &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
void addImport(int line, const std::string &import, const std::string &alias)
void setPackage(const std::string &package)
O3prmrContext(const std::string &filename=std::string())
O3prmrContext & operator+=(const O3prmrContext &c)
This class contains a o3prmr session.
void addSetEngine(int line, const std::string &value)
O3prmrSession & operator+=(const O3prmrSession &c)
std::vector< O3prmrCommand * > commands() const
O3prmrSession(const std::string &name=std::string())
std::string m_name
The session name;.
void addQuery(int line, const std::string &value)
virtual std::string toString() const
std::vector< O3prmrCommand * > m_commands
A sequence of commands.
void addUnobserve(int line, const std::string &value)
void addSetGndEngine(int line, const std::string &value)
void addCommand(const O3prmrCommand *command)
void addObserve(int line, const std::string &leftValue, const std::string &rightValue)
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