aGrUM  0.16.0
O3prmrContext_tpl.h
Go to the documentation of this file.
1 
30 #include "O3prmrContext.h"
31 
32 namespace gum {
33  namespace prm {
34  namespace o3prmr {
35 
36  /* ******************************************************************* */
37 
38  template < typename GUM_SCALAR >
39  O3prmrContext< GUM_SCALAR >::O3prmrContext(const std::string& filename) {
40  m_filename = filename;
41  m_mainImport = 0;
42  }
43 
44  template < typename GUM_SCALAR >
46  m_filename = s.m_filename;
47  m_mainImport = s.m_mainImport;
48  *this += s;
49  }
50 
51  template < typename GUM_SCALAR >
53  for (Idx i = Size(m_imports.size()); i > 0; i--)
54  delete m_imports[i - 1];
55 
56  for (Size i = Size(m_sessions.size()); i > 0; i--)
57  delete m_sessions[i - 1];
58  }
59 
60  template < typename GUM_SCALAR >
62  return m_filename;
63  }
64 
65  template < typename GUM_SCALAR >
67  return m_package;
68  }
69 
70  template < typename GUM_SCALAR >
71  void O3prmrContext< GUM_SCALAR >::setPackage(const std::string& package) {
72  m_package = package;
73  }
74 
75  template < typename GUM_SCALAR >
76  std::string
77  O3prmrContext< GUM_SCALAR >::aliasToImport(const std::string& alias) {
78  for (Idx i = Size(m_imports.size()); i > 0; i--)
79  if (m_imports[i - 1]->alias == alias) return m_imports[i - 1]->value;
80 
81  return std::string();
82  }
83 
84  template < typename GUM_SCALAR >
85  std::vector< ImportCommand* > O3prmrContext< GUM_SCALAR >::imports() const {
86  return m_imports;
87  }
88 
89  template < typename GUM_SCALAR >
91  const std::string& import,
92  const std::string& alias) {
93  m_imports.push_back(new ImportCommand(line, import, alias));
94 
95  if (alias == "default") m_mainImport = m_imports.back();
96  }
97 
98  template < typename GUM_SCALAR >
100  const std::string& import,
101  bool ismain) {
102  m_imports.push_back(new ImportCommand(line, import, import));
103 
104  if (ismain) m_mainImport = m_imports.back();
105  }
106 
107  template < typename GUM_SCALAR >
108  std::vector< O3prmrSession< GUM_SCALAR >* >
110  return m_sessions;
111  }
112 
113  template < typename GUM_SCALAR >
115  const O3prmrSession< GUM_SCALAR >& session) {
116  m_sessions.push_back(new O3prmrSession< GUM_SCALAR >(session));
117  }
118 
119  template < typename GUM_SCALAR >
121  std::string output;
122 
123  if (!m_package.empty()) {
124  output += "package " + m_package + ";\n";
125  output += "\n";
126  }
127 
128  for (auto i = m_imports.begin(); i < m_imports.end(); i++)
129  output += (*i)->toString() + "\n";
130 
131  output += "\n";
132 
133  for (auto i = m_sessions.begin(); i < m_sessions.end(); i++)
134  output += (*i)->toString() + "\n";
135 
136  return output;
137  }
138 
139  template < typename GUM_SCALAR >
142  const std::vector< ImportCommand* >& imports = c.imports();
143 
144  for (std::vector< ImportCommand* >::const_iterator i = imports.begin();
145  i != imports.end();
146  i++)
147  addImport(**i);
148 
149  const std::vector< O3prmrSession< GUM_SCALAR >* >& sessions = c.sessions();
150 
151  if (sessions.size() == 1 && sessions.back()->name() == "default") {
152  *(this->m_sessions.back()) += *(sessions.back());
153  } else
154  for (auto i = sessions.begin(); i != sessions.end(); i++)
155  addSession(**i);
156 
157  return *this;
158  }
159 
160  /* ******************************************************************* */
161 
162  template < typename GUM_SCALAR >
164  m_name = name;
165  }
166 
167  template < typename GUM_SCALAR >
169  const O3prmrSession< GUM_SCALAR >& s) {
170  m_name = s.m_name;
171  *this += s;
172  }
173 
174  template < typename GUM_SCALAR >
176  for (Idx i = Size(m_commands.size()); i >= 1; i--)
177  delete m_commands[i - 1];
178 
179  m_commands.clear();
180  }
181 
182  template < typename GUM_SCALAR >
183  std::string O3prmrSession< GUM_SCALAR >::name() const {
184  return m_name;
185  }
186 
187  template < typename GUM_SCALAR >
188  std::vector< O3prmrCommand* > O3prmrSession< GUM_SCALAR >::commands() const {
189  return m_commands;
190  }
191 
192  template < typename GUM_SCALAR >
194  m_commands.push_back(command);
195  }
196 
197  template < typename GUM_SCALAR >
199  const std::string& leftValue,
200  const std::string& rightValue) {
201  addCommand(new ObserveCommand< GUM_SCALAR >(line, leftValue, rightValue));
202  }
203 
204  template < typename GUM_SCALAR >
206  const std::string& value) {
207  addCommand(new UnobserveCommand< GUM_SCALAR >(line, value));
208  }
209 
210  template < typename GUM_SCALAR >
212  const std::string& value) {
213  addCommand(new QueryCommand< GUM_SCALAR >(line, value));
214  }
215 
216  template < typename GUM_SCALAR >
218  const std::string& value) {
219  addCommand(new SetEngineCommand(line, value));
220  }
221 
222  template < typename GUM_SCALAR >
224  const std::string& value) {
225  addCommand(new SetGndEngineCommand(line, value));
226  }
227 
228  template < typename GUM_SCALAR >
230  switch (command->type()) {
232  m_commands.push_back(
233  new SetEngineCommand(*(SetEngineCommand*)command));
234  break;
235 
237  m_commands.push_back(
238  new SetGndEngineCommand(*(SetGndEngineCommand*)command));
239  break;
240 
242  m_commands.push_back(new ObserveCommand< GUM_SCALAR >(
243  *(ObserveCommand< GUM_SCALAR >*)command));
244  break;
245 
247  m_commands.push_back(new UnobserveCommand< GUM_SCALAR >(
248  *(UnobserveCommand< GUM_SCALAR >*)command));
249  break;
250 
252  m_commands.push_back(new QueryCommand< GUM_SCALAR >(
253  *(QueryCommand< GUM_SCALAR >*)command));
254  break;
255  }
256  }
257 
258  template < typename GUM_SCALAR >
260  std::string output;
261 
262  output += "request " + m_name + " {\n";
263 
264  for (std::vector< O3prmrCommand* >::const_iterator i = m_commands.begin();
265  i < m_commands.end();
266  i++)
267  output += "\t" + (*i)->toString() + "\n";
268 
269  output += "}\n";
270 
271  return output;
272  }
273 
274  template < typename GUM_SCALAR >
277  for (std::vector< O3prmrCommand* >::const_iterator i =
278  c.m_commands.begin();
279  i < c.m_commands.end();
280  i++)
281  addCommand(*i);
282 
283  return *this;
284  }
285 
286  /* ******************************************************************* */
287 
288  } // namespace o3prmr
289  } // namespace prm
290 } // namespace gum
void addObserve(int line, const std::string &leftValue, const std::string &rightValue)
void addSession(const O3prmrSession< GUM_SCALAR > &session)
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
virtual std::string toString() const
std::vector< ImportCommand *> imports() const
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
std::vector< O3prmrCommand *> commands() const
std::string aliasToImport(const std::string &alias)
void addImport(int line, const std::string &import, const std::string &alias)
O3prmrSession(const std::string &name=std::string())
void addUnobserve(int line, const std::string &value)
O3prmrContext(const std::string &filename=std::string())
O3prmrContext & operator+=(const O3prmrContext &c)
void setPackage(const std::string &package)
This is an abstract class.
Definition: O3prmrContext.h:50
Represent a o3prmr context, with an import, and some sequencials commands.
std::vector< O3prmrSession< GUM_SCALAR > *> sessions() const
This class contains a o3prmr session.
void addQuery(int line, const std::string &value)
void addSetGndEngine(int line, const std::string &value)
Size Idx
Type for indexes.
Definition: types.h:53
virtual std::string toString() const
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
std::vector< O3prmrCommand *> m_commands
A sequence of commands.
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)