aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
O3prmrContext_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief Implementation of O3prmReader.
25  *
26  * @author Pierre-Henri WUILLEMIN(@LIP6), Ni NI, Lionel TORTI & Vincent RENAUDINEAU
27  */
28 
29 #include "O3prmrContext.h"
30 
31 namespace gum {
32  namespace prm {
33  namespace o3prmr {
34 
35  /* ******************************************************************* */
36 
37  template < typename GUM_SCALAR >
38  O3prmrContext< GUM_SCALAR >::O3prmrContext(const std::string& filename) {
39  m_filename = filename;
40  m_mainImport = 0;
41  }
42 
43  template < typename GUM_SCALAR >
44  O3prmrContext< GUM_SCALAR >::O3prmrContext(const O3prmrContext& s) {
45  m_filename = s.m_filename;
46  m_mainImport = s.m_mainImport;
47  *this += s;
48  }
49 
50  template < typename GUM_SCALAR >
51  O3prmrContext< GUM_SCALAR >::~O3prmrContext() {
52  for (Idx i = Size(m_imports.size()); i > 0; i--)
53  delete m_imports[i - 1];
54 
55  for (Size i = Size(m_sessions.size()); i > 0; i--)
56  delete m_sessions[i - 1];
57  }
58 
59  template < typename GUM_SCALAR >
60  std::string O3prmrContext< GUM_SCALAR >::filename() const {
61  return m_filename;
62  }
63 
64  template < typename GUM_SCALAR >
65  std::string O3prmrContext< GUM_SCALAR >::package() const {
66  return m_package;
67  }
68 
69  template < typename GUM_SCALAR >
70  void O3prmrContext< GUM_SCALAR >::setPackage(const std::string& package) {
71  m_package = package;
72  }
73 
74  template < typename GUM_SCALAR >
75  std::string O3prmrContext< GUM_SCALAR >::aliasToImport(const std::string& alias) {
76  for (Idx i = Size(m_imports.size()); i > 0; i--)
77  if (m_imports[i - 1]->alias == alias) return m_imports[i - 1]->value;
78 
79  return std::string();
80  }
81 
82  template < typename GUM_SCALAR >
83  std::vector< ImportCommand* > O3prmrContext< GUM_SCALAR >::imports() const {
84  return m_imports;
85  }
86 
87  template < typename GUM_SCALAR >
88  void O3prmrContext< GUM_SCALAR >::addImport(int line,
89  const std::string& import,
90  const std::string& alias) {
91  m_imports.push_back(new ImportCommand(line, import, alias));
92 
93  if (alias == "default") m_mainImport = m_imports.back();
94  }
95 
96  template < typename GUM_SCALAR >
97  void
98  O3prmrContext< GUM_SCALAR >::addImport(int line, const std::string& import, bool ismain) {
99  m_imports.push_back(new ImportCommand(line, import, import));
100 
101  if (ismain) m_mainImport = m_imports.back();
102  }
103 
104  template < typename GUM_SCALAR >
105  std::vector< O3prmrSession< GUM_SCALAR >* > O3prmrContext< GUM_SCALAR >::sessions() const {
106  return m_sessions;
107  }
108 
109  template < typename GUM_SCALAR >
110  void O3prmrContext< GUM_SCALAR >::addSession(const O3prmrSession< GUM_SCALAR >& session) {
111  m_sessions.push_back(new O3prmrSession< GUM_SCALAR >(session));
112  }
113 
114  template < typename GUM_SCALAR >
115  std::string O3prmrContext< GUM_SCALAR >::toString() const {
116  std::string output;
117 
118  if (!m_package.empty()) {
119  output += "package " + m_package + ";\n";
120  output += "\n";
121  }
122 
123  for (auto i = m_imports.begin(); i < m_imports.end(); i++)
124  output += (*i)->toString() + "\n";
125 
126  output += "\n";
127 
128  for (auto i = m_sessions.begin(); i < m_sessions.end(); i++)
129  output += (*i)->toString() + "\n";
130 
131  return output;
132  }
133 
134  template < typename GUM_SCALAR >
135  O3prmrContext< GUM_SCALAR >& O3prmrContext< GUM_SCALAR >::operator+=(const O3prmrContext& c) {
136  const std::vector< ImportCommand* >& imports = c.imports();
137 
138  for (std::vector< ImportCommand* >::const_iterator i = imports.begin(); i != imports.end();
139  i++)
140  addImport(**i);
141 
142  const std::vector< O3prmrSession< GUM_SCALAR >* >& sessions = c.sessions();
143 
144  if (sessions.size() == 1 && sessions.back()->name() == "default") {
145  *(this->m_sessions.back()) += *(sessions.back());
146  } else
147  for (auto i = sessions.begin(); i != sessions.end(); i++)
148  addSession(**i);
149 
150  return *this;
151  }
152 
153  /* ******************************************************************* */
154 
155  template < typename GUM_SCALAR >
156  O3prmrSession< GUM_SCALAR >::O3prmrSession(const std::string& name) {
157  m_name = name;
158  }
159 
160  template < typename GUM_SCALAR >
161  O3prmrSession< GUM_SCALAR >::O3prmrSession(const O3prmrSession< GUM_SCALAR >& s) {
162  m_name = s.m_name;
163  *this += s;
164  }
165 
166  template < typename GUM_SCALAR >
167  O3prmrSession< GUM_SCALAR >::~O3prmrSession() {
168  for (Idx i = Size(m_commands.size()); i >= 1; i--)
169  delete m_commands[i - 1];
170 
171  m_commands.clear();
172  }
173 
174  template < typename GUM_SCALAR >
175  std::string O3prmrSession< GUM_SCALAR >::name() const {
176  return m_name;
177  }
178 
179  template < typename GUM_SCALAR >
180  std::vector< O3prmrCommand* > O3prmrSession< GUM_SCALAR >::commands() const {
181  return m_commands;
182  }
183 
184  template < typename GUM_SCALAR >
185  void O3prmrSession< GUM_SCALAR >::addCommand(O3prmrCommand* command) {
186  m_commands.push_back(command);
187  }
188 
189  template < typename GUM_SCALAR >
190  void O3prmrSession< GUM_SCALAR >::addObserve(int line,
191  const std::string& leftValue,
192  const std::string& rightValue) {
193  addCommand(new ObserveCommand< GUM_SCALAR >(line, leftValue, rightValue));
194  }
195 
196  template < typename GUM_SCALAR >
197  void O3prmrSession< GUM_SCALAR >::addUnobserve(int line, const std::string& value) {
198  addCommand(new UnobserveCommand< GUM_SCALAR >(line, value));
199  }
200 
201  template < typename GUM_SCALAR >
202  void O3prmrSession< GUM_SCALAR >::addQuery(int line, const std::string& value) {
203  addCommand(new QueryCommand< GUM_SCALAR >(line, value));
204  }
205 
206  template < typename GUM_SCALAR >
207  void O3prmrSession< GUM_SCALAR >::addSetEngine(int line, const std::string& value) {
208  addCommand(new SetEngineCommand(line, value));
209  }
210 
211  template < typename GUM_SCALAR >
212  void O3prmrSession< GUM_SCALAR >::addSetGndEngine(int line, const std::string& value) {
213  addCommand(new SetGndEngineCommand(line, value));
214  }
215 
216  template < typename GUM_SCALAR >
217  void O3prmrSession< GUM_SCALAR >::addCommand(const O3prmrCommand* command) {
218  switch (command->type()) {
219  case O3prmrCommand::RequestType::SetEngine:
220  m_commands.push_back(new SetEngineCommand(*(SetEngineCommand*)command));
221  break;
222 
223  case O3prmrCommand::RequestType::SetGndEngine:
224  m_commands.push_back(new SetGndEngineCommand(*(SetGndEngineCommand*)command));
225  break;
226 
227  case O3prmrCommand::RequestType::Observe:
228  m_commands.push_back(
229  new ObserveCommand< GUM_SCALAR >(*(ObserveCommand< GUM_SCALAR >*)command));
230  break;
231 
232  case O3prmrCommand::RequestType::Unobserve:
233  m_commands.push_back(
234  new UnobserveCommand< GUM_SCALAR >(*(UnobserveCommand< GUM_SCALAR >*)command));
235  break;
236 
237  case O3prmrCommand::RequestType::Query:
238  m_commands.push_back(
239  new QueryCommand< GUM_SCALAR >(*(QueryCommand< GUM_SCALAR >*)command));
240  break;
241  }
242  }
243 
244  template < typename GUM_SCALAR >
245  std::string O3prmrSession< GUM_SCALAR >::toString() const {
246  std::string output;
247 
248  output += "request " + m_name + " {\n";
249 
250  for (std::vector< O3prmrCommand* >::const_iterator i = m_commands.begin();
251  i < m_commands.end();
252  i++)
253  output += "\t" + (*i)->toString() + "\n";
254 
255  output += "}\n";
256 
257  return output;
258  }
259 
260  template < typename GUM_SCALAR >
261  O3prmrSession< GUM_SCALAR >&
262  O3prmrSession< GUM_SCALAR >::operator+=(const O3prmrSession< GUM_SCALAR >& c) {
263  for (std::vector< O3prmrCommand* >::const_iterator i = c.m_commands.begin();
264  i < c.m_commands.end();
265  i++)
266  addCommand(*i);
267 
268  return *this;
269  }
270 
271  /* ******************************************************************* */
272 
273  } // namespace o3prmr
274  } // namespace prm
275 } // namespace gum
void addObserve(int line, const std::string &leftValue, const std::string &rightValue)
std::map< const PRMSystem< GUM_SCALAR > *, PRMInference< GUM_SCALAR > *> m_infEngineMap
void addSession(const O3prmrSession< GUM_SCALAR > &session)
virtual std::string toString() const
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
void addImport(int line, const std::string &import, bool ismain)
PRMInference< GUM_SCALAR >::Chain chain
void addCommand(O3prmrCommand *command)
std::vector< ImportCommand *> imports() const
std::vector< O3prmrCommand *> commands() const
std::string aliasToImport(const std::string &alias)
void addImport(int line, const std::string &import, const std::string &alias)
SetGndEngineCommand(int line, const std::string &value)
O3prmrSession(const std::string &name=std::string())
void addUnobserve(int line, const std::string &value)
O3prmrSession(const O3prmrSession &s)
SetEngineCommand(int line, const std::string &value)
Definition: O3prmrContext.h:88
O3prmrContext(const std::string &filename=std::string())
O3prmrContext & operator+=(const O3prmrContext &c)
ParamScopeData(const std::string &s, const PRMReferenceSlot< GUM_SCALAR > &ref, Idx d)
PRMInference< GUM_SCALAR >::Chain chain
O3prmrContext(const O3prmrContext &s)
void setPackage(const std::string &package)
This is an abstract class.
Definition: O3prmrContext.h:49
Represent a o3prmr context, with an import, and some sequencials commands.
std::vector< O3prmrSession< GUM_SCALAR > *> sessions() const
void addQuery(int line, const std::string &value)
void addSetGndEngine(int line, const std::string &value)
virtual std::string toString() const
PRMInference< GUM_SCALAR >::Chain chain
void addSetEngine(int line, const std::string &value)
virtual RequestType type() const =0
std::string m_name
The session name;.
void addCommand(const O3prmrCommand *command)
O3prmrSession & operator+=(const O3prmrSession &c)