aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
O3prmrContext_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 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
76  O3prmrContext< GUM_SCALAR >::aliasToImport(const std::string& alias) {
77  for (Idx i = Size(m_imports.size()); i > 0; i--)
78  if (m_imports[i - 1]->alias == alias) return m_imports[i - 1]->value;
79 
80  return std::string();
81  }
82 
83  template < typename GUM_SCALAR >
84  std::vector< ImportCommand* > O3prmrContext< GUM_SCALAR >::imports() const {
85  return m_imports;
86  }
87 
88  template < typename GUM_SCALAR >
89  void O3prmrContext< GUM_SCALAR >::addImport(int line,
90  const std::string& import,
91  const std::string& alias) {
92  m_imports.push_back(new ImportCommand(line, import, alias));
93 
94  if (alias == "default") m_mainImport = m_imports.back();
95  }
96 
97  template < typename GUM_SCALAR >
98  void O3prmrContext< GUM_SCALAR >::addImport(int line,
99  const std::string& import,
100  bool ismain) {
101  m_imports.push_back(new ImportCommand(line, import, import));
102 
103  if (ismain) m_mainImport = m_imports.back();
104  }
105 
106  template < typename GUM_SCALAR >
108  O3prmrContext< GUM_SCALAR >::sessions() const {
109  return m_sessions;
110  }
111 
112  template < typename GUM_SCALAR >
113  void O3prmrContext< GUM_SCALAR >::addSession(
114  const O3prmrSession< GUM_SCALAR >& session) {
115  m_sessions.push_back(new O3prmrSession< GUM_SCALAR >(session));
116  }
117 
118  template < typename GUM_SCALAR >
119  std::string O3prmrContext< GUM_SCALAR >::toString() const {
120  std::string output;
121 
122  if (!m_package.empty()) {
123  output += "package " + m_package + ";\n";
124  output += "\n";
125  }
126 
127  for (auto i = m_imports.begin(); i < m_imports.end(); i++)
128  output += (*i)->toString() + "\n";
129 
130  output += "\n";
131 
132  for (auto i = m_sessions.begin(); i < m_sessions.end(); i++)
133  output += (*i)->toString() + "\n";
134 
135  return output;
136  }
137 
138  template < typename GUM_SCALAR >
139  O3prmrContext< GUM_SCALAR >&
140  O3prmrContext< GUM_SCALAR >::operator+=(const O3prmrContext& c) {
141  const std::vector< ImportCommand* >& imports = c.imports();
142 
143  for (std::vector< ImportCommand* >::const_iterator i = imports.begin();
144  i != imports.end();
145  i++)
146  addImport(**i);
147 
148  const std::vector< O3prmrSession< GUM_SCALAR >* >& sessions = c.sessions();
149 
150  if (sessions.size() == 1 && sessions.back()->name() == "default") {
151  *(this->m_sessions.back()) += *(sessions.back());
152  } else
153  for (auto i = sessions.begin(); i != sessions.end(); i++)
154  addSession(**i);
155 
156  return *this;
157  }
158 
159  /* ******************************************************************* */
160 
161  template < typename GUM_SCALAR >
162  O3prmrSession< GUM_SCALAR >::O3prmrSession(const std::string& name) {
163  m_name = name;
164  }
165 
166  template < typename GUM_SCALAR >
167  O3prmrSession< GUM_SCALAR >::O3prmrSession(
168  const O3prmrSession< GUM_SCALAR >& s) {
169  m_name = s.m_name;
170  *this += s;
171  }
172 
173  template < typename GUM_SCALAR >
174  O3prmrSession< GUM_SCALAR >::~O3prmrSession() {
175  for (Idx i = Size(m_commands.size()); i >= 1; i--)
176  delete m_commands[i - 1];
177 
178  m_commands.clear();
179  }
180 
181  template < typename GUM_SCALAR >
182  std::string O3prmrSession< GUM_SCALAR >::name() const {
183  return m_name;
184  }
185 
186  template < typename GUM_SCALAR >
187  std::vector< O3prmrCommand* > O3prmrSession< GUM_SCALAR >::commands() const {
188  return m_commands;
189  }
190 
191  template < typename GUM_SCALAR >
192  void O3prmrSession< GUM_SCALAR >::addCommand(O3prmrCommand* command) {
193  m_commands.push_back(command);
194  }
195 
196  template < typename GUM_SCALAR >
197  void O3prmrSession< GUM_SCALAR >::addObserve(int line,
198  const std::string& leftValue,
199  const std::string& rightValue) {
200  addCommand(new ObserveCommand< GUM_SCALAR >(line, leftValue, rightValue));
201  }
202 
203  template < typename GUM_SCALAR >
204  void O3prmrSession< GUM_SCALAR >::addUnobserve(int line,
205  const std::string& value) {
206  addCommand(new UnobserveCommand< GUM_SCALAR >(line, value));
207  }
208 
209  template < typename GUM_SCALAR >
210  void O3prmrSession< GUM_SCALAR >::addQuery(int line,
211  const std::string& value) {
212  addCommand(new QueryCommand< GUM_SCALAR >(line, value));
213  }
214 
215  template < typename GUM_SCALAR >
216  void O3prmrSession< GUM_SCALAR >::addSetEngine(int line,
217  const std::string& value) {
218  addCommand(new SetEngineCommand(line, value));
219  }
220 
221  template < typename GUM_SCALAR >
222  void O3prmrSession< GUM_SCALAR >::addSetGndEngine(int line,
223  const std::string& value) {
224  addCommand(new SetGndEngineCommand(line, value));
225  }
226 
227  template < typename GUM_SCALAR >
228  void O3prmrSession< GUM_SCALAR >::addCommand(const O3prmrCommand* command) {
229  switch (command->type()) {
230  case O3prmrCommand::RequestType::SetEngine:
231  m_commands.push_back(
232  new SetEngineCommand(*(SetEngineCommand*)command));
233  break;
234 
235  case O3prmrCommand::RequestType::SetGndEngine:
236  m_commands.push_back(
237  new SetGndEngineCommand(*(SetGndEngineCommand*)command));
238  break;
239 
240  case O3prmrCommand::RequestType::Observe:
241  m_commands.push_back(new ObserveCommand< GUM_SCALAR >(
242  *(ObserveCommand< GUM_SCALAR >*)command));
243  break;
244 
245  case O3prmrCommand::RequestType::Unobserve:
246  m_commands.push_back(new UnobserveCommand< GUM_SCALAR >(
247  *(UnobserveCommand< GUM_SCALAR >*)command));
248  break;
249 
250  case O3prmrCommand::RequestType::Query:
251  m_commands.push_back(new QueryCommand< GUM_SCALAR >(
252  *(QueryCommand< GUM_SCALAR >*)command));
253  break;
254  }
255  }
256 
257  template < typename GUM_SCALAR >
258  std::string O3prmrSession< GUM_SCALAR >::toString() const {
259  std::string output;
260 
261  output += "request " + m_name + " {\n";
262 
263  for (std::vector< O3prmrCommand* >::const_iterator i = m_commands.begin();
264  i < m_commands.end();
265  i++)
266  output += "\t" + (*i)->toString() + "\n";
267 
268  output += "}\n";
269 
270  return output;
271  }
272 
273  template < typename GUM_SCALAR >
274  O3prmrSession< GUM_SCALAR >& O3prmrSession< GUM_SCALAR >::operator+=(
275  const O3prmrSession< GUM_SCALAR >& c) {
276  for (std::vector< O3prmrCommand* >::const_iterator i
277  = c.m_commands.begin();
278  i < c.m_commands.end();
279  i++)
280  addCommand(*i);
281 
282  return *this;
283  }
284 
285  /* ******************************************************************* */
286 
287  } // namespace o3prmr
288  } // namespace prm
289 } // 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:669
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:92
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)