aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
O3prmrContext.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 Headers of O3prmInterpreter
25  *
26  * @author Pierre-Henri WUILLEMIN(@LIP6), Ni NI, Lionel TORTI & Vincent RENAUDINEAU
27  */
28 
29 #ifndef SKOORSYNTAXTREE_H
30 #define SKOORSYNTAXTREE_H
31 
32 #include <map>
33 #include <string>
34 #include <vector>
35 
36 #include <agrum/PRM/elements/PRMSystem.h>
37 
38 #include <agrum/PRM/inference/PRMInference.h>
39 
40 namespace gum {
41  namespace prm {
42  namespace o3prmr {
43 
44  /**
45  * This is an abstract class.
46  * It represent a o3prmr command.
47  * There five types of command, such sub-classes.
48  * */
49  class O3prmrCommand {
50  public:
51  int line;
52  enum class RequestType : char
53  {
54  Observe,
55  Unobserve,
56  Query,
57  SetEngine,
59  };
60 
61  explicit O3prmrCommand(int line) : line(line) {}
63  virtual ~O3prmrCommand() {}
64 
65  virtual RequestType type() const = 0;
66  virtual std::string toString() const = 0;
67  };
68 
69  ///
70  class ImportCommand {
71  public:
72  ImportCommand(int line,
73  const std::string& value,
74  const std::string& alias) :
75  line(line),
76  value(value), alias(alias) {}
78  line(c.line), value(c.value), alias(c.alias) {}
79 
80  int line;
81  std::string value;
82  std::string alias;
83 
84  std::string toString() const {
85  return "import " + value + (alias.empty() ? "" : "as " + alias) + ";";
86  }
87  };
88 
89  ///
91  public:
92  SetEngineCommand(int line, const std::string& value) :
93  O3prmrCommand(line), value(value) {}
96 
97  std::string value;
98 
100  std::string toString() const { return "engine " + value + ";"; }
101  };
102 
103  ///
105  public:
106  SetGndEngineCommand(int line, const std::string& value) :
107  O3prmrCommand(line), value(value) {}
110 
111  std::string value;
112 
114  std::string toString() const { return "grd_engine " + value + ";"; }
115  };
116 
117  ///
118  template < typename GUM_SCALAR >
120  public:
121  ObserveCommand(int line,
122  const std::string& leftValue,
123  const std::string& rightValue) :
124  O3prmrCommand(line),
125  leftValue(leftValue), rightValue(rightValue), system(0) {}
127  O3prmrCommand(c), leftValue(c.leftValue), rightValue(c.rightValue),
128  system(c.system), chain(c.chain) {}
129 
130  std::string leftValue;
131  std::string rightValue;
133  typename PRMInference< GUM_SCALAR >::Chain chain;
135 
137  std::string toString() const {
138  return leftValue + " = " + rightValue + ";";
139  }
140  };
141 
142  ///
143  template < typename GUM_SCALAR >
145  public:
146  std::string value;
148  typename PRMInference< GUM_SCALAR >::Chain chain;
149 
150  UnobserveCommand(int line, const std::string& value) :
151  O3prmrCommand(line), value(value), system(0) {}
153  O3prmrCommand(c), value(c.value), system(c.system), chain(c.chain) {}
154 
156  std::string toString() const { return "unobserve " + value + ";"; }
157  };
158 
159  ///
160  template < typename GUM_SCALAR >
161  class QueryCommand: public O3prmrCommand {
162  public:
163  QueryCommand(int line, const std::string& val) :
164  O3prmrCommand(line), value(val), system(nullptr) {}
165 
166  std::string value;
168  typename PRMInference< GUM_SCALAR >::Chain chain;
169 
170  RequestType type() const { return RequestType::Query; }
171  std::string toString() const { return "? " + value + ";"; }
172  };
173 
174  /**
175  * This class contains a o3prmr session.
176  * It have a name and a sequence of commands.
177  * */
178  template < typename GUM_SCALAR >
180  /// The session name;
181  std::string m_name;
182  /// A sequence of commands.
186 
187  public:
188  explicit O3prmrSession(const std::string& name = std::string());
189  O3prmrSession(const O3prmrSession& s);
190  virtual ~O3prmrSession();
191 
192  std::string name() const;
193 
194  std::vector< O3prmrCommand* > commands() const;
195  void addObserve(int line,
196  const std::string& leftValue,
197  const std::string& rightValue);
198  void addUnobserve(int line, const std::string& value);
199  void addQuery(int line, const std::string& value);
200  void addSetEngine(int line, const std::string& value);
201  void addSetGndEngine(int line, const std::string& value);
202  void addCommand(const O3prmrCommand* command);
203 
204  virtual std::string toString() const;
205  O3prmrSession& operator+=(const O3prmrSession& c);
206 
207  private:
208  void addCommand(O3prmrCommand* command);
209  };
210 
211  /**
212  * Represent a o3prmr context, with an import, and some sequencials
213  * commands.
214  */
215  template < typename GUM_SCALAR >
217  std::string m_filename;
218  std::string m_package;
222 
223  public:
224  explicit O3prmrContext(const std::string& filename = std::string());
225  O3prmrContext(const O3prmrContext& s);
226  virtual ~O3prmrContext();
227 
228  const ImportCommand* mainImport() const { return m_mainImport; }
229 
230  std::string filename() const;
231 
232  std::string package() const;
233  void setPackage(const std::string& package);
234 
235  std::string aliasToImport(const std::string& alias);
236  std::vector< ImportCommand* > imports() const;
237  void addImport(int line,
238  const std::string& import,
239  const std::string& alias);
240  void addImport(int line, const std::string& import, bool ismain);
241  void addImport(const ImportCommand& i) {
242  m_imports.push_back(new ImportCommand(i.line, i.value, i.alias));
243 
244  if (i.alias == "default") m_mainImport = m_imports.back();
245  }
246 
247  std::vector< O3prmrSession< GUM_SCALAR >* > sessions() const;
248  void addSession(const O3prmrSession< GUM_SCALAR >& session);
249 
250  virtual std::string toString() const;
251  O3prmrContext& operator+=(const O3prmrContext& c);
252  };
253 
254 
255 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
256 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
257 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
258 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
259 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
260  extern template class ObserveCommand< double >;
261 # endif
262 # endif
263 # endif
264 # endif
265 #endif
266 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
267 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
268 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
269 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
270 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
271  extern template class UnobserveCommand< double >;
272 # endif
273 # endif
274 # endif
275 # endif
276 #endif
277 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
278 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
279 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
280 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
281 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
282  extern template class QueryCommand< double >;
283 # endif
284 # endif
285 # endif
286 # endif
287 #endif
288 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
289 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
290 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
291 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
292 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
293  extern template class O3prmrSession< double >;
294 # endif
295 # endif
296 # endif
297 # endif
298 #endif
299 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
300 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
301 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
302 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
303 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
304  extern template class O3prmrContext< double >;
305 # endif
306 # endif
307 # endif
308 # endif
309 #endif
310 
311 
312  } // namespace o3prmr
313  } // namespace prm
314 } // namespace gum
315 
316 #include <agrum/PRM/o3prmr/O3prmrContext_tpl.h>
317 
318 #endif // SKOORSYNTAXTREE_H
const PRMSystem< GUM_SCALAR > * system
Potential< GUM_SCALAR > potentiel
void addObserve(int line, const std::string &leftValue, const std::string &rightValue)
SetEngineCommand(const SetEngineCommand &c)
Definition: O3prmrContext.h:94
std::map< const PRMSystem< GUM_SCALAR > *, PRMInference< GUM_SCALAR > *> m_infEngineMap
const ImportCommand * mainImport() const
void addSession(const O3prmrSession< GUM_SCALAR > &session)
QueryCommand(int line, const std::string &val)
O3prmrCommand(const O3prmrCommand &c)
Definition: O3prmrContext.h:62
virtual std::string toString() const
const PRMSystem< GUM_SCALAR > * system
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)
ObserveCommand(int line, const std::string &leftValue, const std::string &rightValue)
UnobserveCommand(const UnobserveCommand &c)
std::vector< ImportCommand *> imports() const
std::vector< O3prmrCommand *> commands() const
ImportCommand(int line, const std::string &value, const std::string &alias)
Definition: O3prmrContext.h:72
std::string aliasToImport(const std::string &alias)
std::vector< O3prmrSession< GUM_SCALAR > *> m_sessions
void addImport(int line, const std::string &import, const std::string &alias)
SetGndEngineCommand(int line, const std::string &value)
const PRMSystem< GUM_SCALAR > * system
std::vector< ImportCommand *> m_imports
O3prmrSession(const std::string &name=std::string())
UnobserveCommand(int line, const std::string &value)
void addUnobserve(int line, const std::string &value)
std::string toString() const
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)
void addImport(const ImportCommand &i)
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
ObserveCommand(const ObserveCommand &c)
ImportCommand(const ImportCommand &c)
Definition: O3prmrContext.h:77
void addQuery(int line, const std::string &value)
void addSetGndEngine(int line, const std::string &value)
SetGndEngineCommand(const SetGndEngineCommand &c)
virtual std::string toString() const
virtual std::string toString() const =0
std::vector< O3prmrCommand *> m_commands
A sequence of commands.
PRMInference< GUM_SCALAR >::Chain chain
std::string toString() const
Definition: O3prmrContext.h:84
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)