aGrUM  0.14.2
O3prmrContext_tpl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
27 #include "O3prmrContext.h"
28 
29 namespace gum {
30  namespace prm {
31  namespace o3prmr {
32 
33  /* ******************************************************************* */
34 
35  template < typename GUM_SCALAR >
36  O3prmrContext< GUM_SCALAR >::O3prmrContext(const std::string& filename) {
37  m_filename = filename;
38  m_mainImport = 0;
39  }
40 
41  template < typename GUM_SCALAR >
43  m_filename = s.m_filename;
44  m_mainImport = s.m_mainImport;
45  *this += s;
46  }
47 
48  template < typename GUM_SCALAR >
50  for (Idx i = Size(m_imports.size()); i > 0; i--)
51  delete m_imports[i - 1];
52 
53  for (Size i = Size(m_sessions.size()); i > 0; i--)
54  delete m_sessions[i - 1];
55  }
56 
57  template < typename GUM_SCALAR >
59  return m_filename;
60  }
61 
62  template < typename GUM_SCALAR >
64  return m_package;
65  }
66 
67  template < typename GUM_SCALAR >
68  void O3prmrContext< GUM_SCALAR >::setPackage(const std::string& package) {
69  m_package = package;
70  }
71 
72  template < typename GUM_SCALAR >
73  std::string
74  O3prmrContext< GUM_SCALAR >::aliasToImport(const std::string& alias) {
75  for (Idx i = Size(m_imports.size()); i > 0; i--)
76  if (m_imports[i - 1]->alias == alias) return m_imports[i - 1]->value;
77 
78  return std::string();
79  }
80 
81  template < typename GUM_SCALAR >
82  std::vector< ImportCommand* > O3prmrContext< GUM_SCALAR >::imports() const {
83  return m_imports;
84  }
85 
86  template < typename GUM_SCALAR >
88  const std::string& import,
89  const std::string& alias) {
90  m_imports.push_back(new ImportCommand(line, import, alias));
91 
92  if (alias == "default") m_mainImport = m_imports.back();
93  }
94 
95  template < typename GUM_SCALAR >
97  const std::string& import,
98  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 >* >
107  return m_sessions;
108  }
109 
110  template < typename GUM_SCALAR >
112  const O3prmrSession< GUM_SCALAR >& session) {
113  m_sessions.push_back(new O3prmrSession< GUM_SCALAR >(session));
114  }
115 
116  template < typename GUM_SCALAR >
118  std::string output;
119 
120  if (!m_package.empty()) {
121  output += "package " + m_package + ";\n";
122  output += "\n";
123  }
124 
125  for (auto i = m_imports.begin(); i < m_imports.end(); i++)
126  output += (*i)->toString() + "\n";
127 
128  output += "\n";
129 
130  for (auto i = m_sessions.begin(); i < m_sessions.end(); i++)
131  output += (*i)->toString() + "\n";
132 
133  return output;
134  }
135 
136  template < typename GUM_SCALAR >
139  const std::vector< ImportCommand* >& imports = c.imports();
140 
141  for (std::vector< ImportCommand* >::const_iterator i = imports.begin();
142  i != imports.end();
143  i++)
144  addImport(**i);
145 
146  const std::vector< O3prmrSession< GUM_SCALAR >* >& sessions = c.sessions();
147 
148  if (sessions.size() == 1 && sessions.back()->name() == "default") {
149  *(this->m_sessions.back()) += *(sessions.back());
150  } else
151  for (auto i = sessions.begin(); i != sessions.end(); i++)
152  addSession(**i);
153 
154  return *this;
155  }
156 
157  /* ******************************************************************* */
158 
159  template < typename GUM_SCALAR >
161  m_name = name;
162  }
163 
164  template < typename GUM_SCALAR >
166  const O3prmrSession< GUM_SCALAR >& s) {
167  m_name = s.m_name;
168  *this += s;
169  }
170 
171  template < typename GUM_SCALAR >
173  for (Idx i = Size(m_commands.size()); i >= 1; i--)
174  delete m_commands[i - 1];
175 
176  m_commands.clear();
177  }
178 
179  template < typename GUM_SCALAR >
180  std::string O3prmrSession< GUM_SCALAR >::name() const {
181  return m_name;
182  }
183 
184  template < typename GUM_SCALAR >
185  std::vector< O3prmrCommand* > O3prmrSession< GUM_SCALAR >::commands() const {
186  return m_commands;
187  }
188 
189  template < typename GUM_SCALAR >
191  m_commands.push_back(command);
192  }
193 
194  template < typename GUM_SCALAR >
196  const std::string& leftValue,
197  const std::string& rightValue) {
198  addCommand(new ObserveCommand< GUM_SCALAR >(line, leftValue, rightValue));
199  }
200 
201  template < typename GUM_SCALAR >
203  const std::string& value) {
204  addCommand(new UnobserveCommand< GUM_SCALAR >(line, value));
205  }
206 
207  template < typename GUM_SCALAR >
209  const std::string& value) {
210  addCommand(new QueryCommand< GUM_SCALAR >(line, value));
211  }
212 
213  template < typename GUM_SCALAR >
215  const std::string& value) {
216  addCommand(new SetEngineCommand(line, value));
217  }
218 
219  template < typename GUM_SCALAR >
221  const std::string& value) {
222  addCommand(new SetGndEngineCommand(line, value));
223  }
224 
225  template < typename GUM_SCALAR >
227  switch (command->type()) {
229  m_commands.push_back(
230  new SetEngineCommand(*(SetEngineCommand*)command));
231  break;
232 
234  m_commands.push_back(
235  new SetGndEngineCommand(*(SetGndEngineCommand*)command));
236  break;
237 
239  m_commands.push_back(new ObserveCommand< GUM_SCALAR >(
240  *(ObserveCommand< GUM_SCALAR >*)command));
241  break;
242 
244  m_commands.push_back(new UnobserveCommand< GUM_SCALAR >(
245  *(UnobserveCommand< GUM_SCALAR >*)command));
246  break;
247 
249  m_commands.push_back(new QueryCommand< GUM_SCALAR >(
250  *(QueryCommand< GUM_SCALAR >*)command));
251  break;
252  }
253  }
254 
255  template < typename GUM_SCALAR >
257  std::string output;
258 
259  output += "request " + m_name + " {\n";
260 
261  for (std::vector< O3prmrCommand* >::const_iterator i = m_commands.begin();
262  i < m_commands.end();
263  i++)
264  output += "\t" + (*i)->toString() + "\n";
265 
266  output += "}\n";
267 
268  return output;
269  }
270 
271  template < typename GUM_SCALAR >
274  for (std::vector< O3prmrCommand* >::const_iterator i =
275  c.m_commands.begin();
276  i < c.m_commands.end();
277  i++)
278  addCommand(*i);
279 
280  return *this;
281  }
282 
283  /* ******************************************************************* */
284 
285  } // namespace o3prmr
286  } // namespace prm
287 } // namespace gum
void addObserve(int line, const std::string &leftValue, const std::string &rightValue)
void addSession(const O3prmrSession< GUM_SCALAR > &session)
Headers of O3prmInterpreter.
virtual std::string toString() const
std::vector< ImportCommand *> imports() const
gum is the global namespace for all aGrUM entities
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:48
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:50
virtual std::string toString() const
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
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)