aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
O3prmrContext.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 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, const std::string& value, const std::string& alias) :
73  line(line), value(value), alias(alias) {}
75 
76  int line;
77  std::string value;
78  std::string alias;
79 
80  std::string toString() const {
81  return "import " + value + (alias.empty() ? "" : "as " + alias) + ";";
82  }
83  };
84 
85  ///
87  public:
88  SetEngineCommand(int line, const std::string& value) : O3prmrCommand(line), value(value) {}
90 
91  std::string value;
92 
94  std::string toString() const { return "engine " + value + ";"; }
95  };
96 
97  ///
99  public:
100  SetGndEngineCommand(int line, const std::string& value) :
101  O3prmrCommand(line), value(value) {}
103 
104  std::string value;
105 
107  std::string toString() const { return "grd_engine " + value + ";"; }
108  };
109 
110  ///
111  template < typename GUM_SCALAR >
113  public:
114  ObserveCommand(int line, const std::string& leftValue, const std::string& rightValue) :
115  O3prmrCommand(line), leftValue(leftValue), rightValue(rightValue), system(0) {}
117  O3prmrCommand(c), leftValue(c.leftValue), rightValue(c.rightValue), system(c.system),
118  chain(c.chain) {}
119 
120  std::string leftValue;
121  std::string rightValue;
123  typename PRMInference< GUM_SCALAR >::Chain chain;
125 
127  std::string toString() const { return leftValue + " = " + rightValue + ";"; }
128  };
129 
130  ///
131  template < typename GUM_SCALAR >
133  public:
134  std::string value;
136  typename PRMInference< GUM_SCALAR >::Chain chain;
137 
138  UnobserveCommand(int line, const std::string& value) :
139  O3prmrCommand(line), value(value), system(0) {}
141  O3prmrCommand(c), value(c.value), system(c.system), chain(c.chain) {}
142 
144  std::string toString() const { return "unobserve " + value + ";"; }
145  };
146 
147  ///
148  template < typename GUM_SCALAR >
149  class QueryCommand: public O3prmrCommand {
150  public:
151  QueryCommand(int line, const std::string& val) :
152  O3prmrCommand(line), value(val), system(nullptr) {}
153 
154  std::string value;
156  typename PRMInference< GUM_SCALAR >::Chain chain;
157 
158  RequestType type() const { return RequestType::Query; }
159  std::string toString() const { return "? " + value + ";"; }
160  };
161 
162  /**
163  * This class contains a o3prmr session.
164  * It have a name and a sequence of commands.
165  * */
166  template < typename GUM_SCALAR >
168  /// The session name;
169  std::string m_name;
170  /// A sequence of commands.
173 
174  public:
175  explicit O3prmrSession(const std::string& name = std::string());
176  O3prmrSession(const O3prmrSession& s);
177  virtual ~O3prmrSession();
178 
179  std::string name() const;
180 
181  std::vector< O3prmrCommand* > commands() const;
182  void addObserve(int line, const std::string& leftValue, const std::string& rightValue);
183  void addUnobserve(int line, const std::string& value);
184  void addQuery(int line, const std::string& value);
185  void addSetEngine(int line, const std::string& value);
186  void addSetGndEngine(int line, const std::string& value);
187  void addCommand(const O3prmrCommand* command);
188 
189  virtual std::string toString() const;
190  O3prmrSession& operator+=(const O3prmrSession& c);
191 
192  private:
193  void addCommand(O3prmrCommand* command);
194  };
195 
196  /**
197  * Represent a o3prmr context, with an import, and some sequencials
198  * commands.
199  */
200  template < typename GUM_SCALAR >
202  std::string m_filename;
203  std::string m_package;
207 
208  public:
209  explicit O3prmrContext(const std::string& filename = std::string());
210  O3prmrContext(const O3prmrContext& s);
211  virtual ~O3prmrContext();
212 
213  const ImportCommand* mainImport() const { return m_mainImport; }
214 
215  std::string filename() const;
216 
217  std::string package() const;
218  void setPackage(const std::string& package);
219 
220  std::string aliasToImport(const std::string& alias);
221  std::vector< ImportCommand* > imports() const;
222  void addImport(int line, const std::string& import, const std::string& alias);
223  void addImport(int line, const std::string& import, bool ismain);
224  void addImport(const ImportCommand& i) {
225  m_imports.push_back(new ImportCommand(i.line, i.value, i.alias));
226 
227  if (i.alias == "default") m_mainImport = m_imports.back();
228  }
229 
230  std::vector< O3prmrSession< GUM_SCALAR >* > sessions() const;
231  void addSession(const O3prmrSession< GUM_SCALAR >& session);
232 
233  virtual std::string toString() const;
234  O3prmrContext& operator+=(const O3prmrContext& c);
235  };
236 
237 
238 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
239 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
240 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
241 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
242 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
243  extern template class ObserveCommand< double >;
244 # endif
245 # endif
246 # endif
247 # endif
248 #endif
249 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
250 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
251 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
252 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
253 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
254  extern template class UnobserveCommand< double >;
255 # endif
256 # endif
257 # endif
258 # endif
259 #endif
260 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
261 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
262 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
263 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
264 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
265  extern template class QueryCommand< double >;
266 # endif
267 # endif
268 # endif
269 # endif
270 #endif
271 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
272 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
273 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
274 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
275 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
276  extern template class O3prmrSession< double >;
277 # endif
278 # endif
279 # endif
280 # endif
281 #endif
282 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
283 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
284 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
285 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
286 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
287  extern template class O3prmrContext< double >;
288 # endif
289 # endif
290 # endif
291 # endif
292 #endif
293 
294 
295  } // namespace o3prmr
296  } // namespace prm
297 } // namespace gum
298 
299 #include <agrum/PRM/o3prmr/O3prmrContext_tpl.h>
300 
301 #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:89
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:643
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: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)
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:74
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:80
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)