datapairs.cpp
Go to the documentation of this file.
1 
18 #include "datapairs.h"
19 //#include "manipulation/mathexpr.h"
20 #include "muParser.h"
21 
22 #include "log.h"
23 #include "utils.h"
24 #include "vecmath.h"
25 
26 
27 bool DataPairs::getItem(unsigned int index, std::vector<std::string> &item)
28 {
29  if (index >= values.size())
30  return false;
31 
32  item = values[index];
33 
34  return true;
35 }
36 
37 
39 {
40  clear();
41  for (unsigned int i=0; i<other.getLength(); i++)
42  {
43  std::vector<std::string> item ;
44  if (other.getItem(i, item) && item.size() > 1)
45  add(item[0], item[1]);
46  }
47 }
48 
50 {
51  for (unsigned int i=0; i<values.size(); i++)
52  {
53  LOG_VERBOSE("%s: ", values[i][0].c_str());
54  for (unsigned int j=1; j<values[i].size(); j++)
55  LOG_VERBOSE("%s ", values[i][j].c_str());
56  LOG_VERBOSE("\n");
57  }
58 }
59 
60 // sets max array size
61 void DataPairs::setMaxSize(unsigned int size)
62 {
63  values.reserve(size);
64 }
65 
66 // returns first index of item with name "str"
67 // name[10] = "hund" value[10] = "dobermann"
68 // name[12] = "hund" value[12] = "dackel"
69 // -> getFirstIndex("hund") = 10
70 int DataPairs::getFirstIndex(std::string str)
71 {
72  for (unsigned int i=0; i<values.size(); i++)
73  if (values[i].size() > 0 && strcasecmp(values[i][0].c_str(), str.c_str()) == 0)
74  return i;
75 
76  return -1;
77 }
78 
79 // returns array with indize of all items with name "str"
80 int DataPairs::getIndex(std::string str, std::vector<unsigned int> &ids)
81 {
82  ids.clear();
83 
84  for (unsigned int i=0; i<values.size(); i++)
85  if (strcasecmp(values[i][0].c_str(), str.c_str()) == 0)
86  ids.push_back(i);
87 
88  if (ids.size() == 0)
89  return -1;
90 
91  return ids[0];
92 }
93 
94 // below: get item with name "str" and return its value as type "Int", "Bool", etc
95 int DataPairs::getInt(std::string str, int def)
96 {
97  int id = getFirstIndex(str);
98 
99  if (id >= 0)
100  return atoi(values[id][1].c_str());
101 
102  return def;
103 }
104 
105 float DataPairs::getFloat(std::string str, float def)
106 {
107  int id = getFirstIndex(str);
108 
109  if (id >= 0)
110  return atof(values[id][1].c_str());
111 
112  return def;
113 }
114 
115 
116 std::string DataPairs::getString(std::string str, std::string def)
117 {
118  int id = getFirstIndex(str);
119 
120  if (id >= 0)
121  return values[id][1];
122 
123  return def;
124 }
125 
127 {
128  values.clear();
129 }
130 
131 unsigned int DataPairs::getLength()
132 {
133  return values.size();
134 }
135 
136 // get all strings
137 void DataPairs::getStrings(std::string str, std::vector<std::string> &result)
138 {
139  result.clear();
140  std::vector<unsigned int> ids;
141  getIndex(str, ids);
142 
143  for (unsigned int i=0;i<ids.size();i++)
144  result.push_back(values[ids[i]][1]);
145 
146  return;
147 }
148 
149 
150 bool DataPairs::getBool(std::string str, bool def)
151 {
152  int id = getFirstIndex(str);
153 
154  if (id >= 0)
155  return strcasecmp(values[id][1].c_str(), "true") == 0;
156 
157  return def;
158 }
159 
160 void DataPairs::add(std::string n, std::string v, bool replace)
161 {
162  int id = getFirstIndex(n);
163 
164  if (id >= 0 && replace)
165  {
166  values[id].clear();
167  values[id].push_back(n);
168  values[id].push_back(v);
169  return;
170  }
171 
172  std::vector<std::string> newItem;
173  newItem.push_back(n);
174  newItem.push_back(v);
175 
176  values.push_back(newItem);
177 }
178 
179 
180 bool DataPairs::getIntValue(std::string str, int &result)
181 {
182  int id = getFirstIndex(str);
183  if (id < 0)
184  return false;
185 
186  try
187  {
188  result = (int) atof(values[id][1].c_str());
189  }
190  catch (...)
191  {
192  return false;
193  }
194  return true;
195 }
196 
197 bool DataPairs::getFloatValue(std::string str, float &result)
198 {
199  int id = getFirstIndex(str);
200  if (id < 0)
201  return false;
202 
203  try
204  {
205  result = atof(values[id][1].c_str());
206  }
207  catch (...)
208  {
209  return false;
210  }
211  return true;
212 }
213 
214 bool DataPairs::getStringValue(std::string str, std::string &result)
215 {
216  int id = getFirstIndex(str);
217  if (id < 0)
218  return false;
219 
220  result = values[id][1];
221  return true;
222 }
223 
225 {
226  return robotLibPbD::getUniform();
227 }
228 
229 double parseMathExprRandomInteger(double first, double second)
230 {
231  return (double)robotLibPbD::getUniform((int) first, (int) second);
232 }
233 
234 std::string parseMathExpr(std::string output)
235 {
236  using namespace mu;
237 
238  std::string result;
239 
240  std::vector<std::string> tokens;
241  robotLibPbD::strtokenize(output, tokens, " ");
242 
243  Parser p;
244  //p.DefineFun("rand", parseMathExprRandomInteger, false);
245  //p.DefineFun("uniform", parseMathExprRandomDouble01, false);
246 
247  result = "";
248  for (unsigned int i=0; i<tokens.size(); i++)
249  {
250  //printf("%s ->", tokens[i].c_str());
251 
252  try
253  {
254  p.SetExpr(tokens[i]);
255  double value = p.Eval();
256  //printf("Math: %s = %g\n", tokens[i].c_str(), value);
257 
258  tokens[i] = robotLibPbD::printToString("%g", value);
259  }
260  catch (Parser::exception_type &e)
261  {
262  }
263 
264  if (i+1 < tokens.size())
265  result += tokens[i] + " ";
266  else
267  result += tokens[i];
268  }
269 
270  return result;
271 }
272 
273 bool DataPairs::getBoolValue(std::string str, bool &result)
274 {
275  int id = getFirstIndex(str);
276  if (id < 0)
277  return false;
278 
279  result = strcasecmp(values[id][1].c_str(), "true") == 0;
280  return true;
281 }
282 
283 
284 void DataPairs::resolveString(std::string input, std::string &output)
285 {
286  char buffer[1024], buffer2[1024], buffer3[1024];
287  sprintf(buffer, "%s", input.c_str());
288  char *start, *end;
289 
290 
291 
292  start = &buffer[0];
293  while (start != NULL && *start != '\0')
294  {
295  if (*start == '[')
296  {
297  bool replaced = false;
298  start++;
299  end = start;
300  while (end != NULL && *end != '\0')
301  {
302  if (*end == ']')
303  {
304  *end = '\0';
305  sprintf(buffer3, "%s", start);
306 
307  if (getFirstIndex(buffer3) >= 0)
308  sprintf(buffer3, "%s", getString(buffer3).c_str());
309  else
310  sprintf(buffer3, "%s", buffer3);
311 
312  end++;
313  sprintf(buffer2, "%s", end);
314 
315  start--;
316  sprintf(start, "%s%s", buffer3, buffer2);
317 
318  replaced = true;
319  break;
320  }
321  end++;
322  }
323 
324  if (replaced)
325  {
326  start = &buffer[0];
327  continue;
328  }
329  }
330 
331  start++;
332  }
333 
334  output = buffer;
335 
336  if (true)
337  {
338  output = parseMathExpr(output);
339  }
340 }
341 
342 
343 std::string DataPairs::resolveString(std::string input)
344 {
345  std::string output;
346  resolveString(input, output);
347  return output;
348 }
349 
351 {
352  for (unsigned int i=0; i<values.size(); i++)
353  for (unsigned int j=1; j<values[i].size(); j++)
354  values[i][j] = resolveString(values[i][j]);
355 }
356 
357 
358 
359 void DataPairs::loadFromXml(CConfiguration &config, TiXmlElement* node, DataPairs* replace)
360 {
361  std::vector<TiXmlElement*> result;
362  config.findNodes("Value", result, node);
363  for (unsigned int i=0; i<result.size(); i++)
364  {
365  std::string name, value;
366  name = config.getAttributeString(result[i], "name", "");
367  value = config.getAttributeString(result[i], "value", "");
368 
369  if (replace != NULL)
370  {
371  name = replace->resolveString(name);
372  value = replace->resolveString(value);
373  }
374 
375  if (name != "")
376  add(name, value, true);
377  }
378 }
void strtokenize(const std::string &str, std::vector< std::string > &tokens, const std::string &delimiters)
Definition: utils.cpp:87
double parseMathExprRandomInteger(double first, double second)
Definition: datapairs.cpp:229
std::string printToString(const char *format,...)
Definition: utils.cpp:149
bool getFloatValue(std::string str, float &result)
Definition: datapairs.cpp:197
double parseMathExprRandomDouble01()
Definition: datapairs.cpp:224
void copyFrom(DataPairs &other)
Copies all Attribute-Value pairs.
Definition: datapairs.cpp:38
bool getItem(unsigned int index, std::vector< std::string > &item)
Definition: datapairs.cpp:27
int getInt(std::string str, int defaultValue=0)
Definition: datapairs.cpp:95
static const char * getAttributeString(TiXmlElement *node, const char *str, const char *def="")
int getIndex(std::string attr, std::vector< unsigned int > &ids)
Retrieves all indexes of Attribute-Value pairs with Attribute name equal to attr. ...
Definition: datapairs.cpp:80
bool getBoolValue(std::string str, bool &result)
Definition: datapairs.cpp:273
std::string getString(std::string str, std::string defaultValue="")
Definition: datapairs.cpp:116
void resolve()
Definition: datapairs.cpp:350
void resolveString(std::string input, std::string &output)
Definition: datapairs.cpp:284
Data Storage Class (Attribute-Value Pairs)
Definition: datapairs.h:33
int getFirstIndex(std::string str)
Definition: datapairs.cpp:70
void clear()
Clears storage buffer.
Definition: datapairs.cpp:126
bool getBool(std::string str, bool defaultValue=false)
Definition: datapairs.cpp:150
std::string parseMathExpr(std::string output)
Definition: datapairs.cpp:234
#define LOG_VERBOSE(format,...)
Definition: log.h:34
void getStrings(std::string str, std::vector< std::string > &result)
Retrieves all Attribute-Value pairs with Attribute name equal to attr.
Definition: datapairs.cpp:137
double getUniform()
Definition: vecmath.h:429
bool getStringValue(std::string str, std::string &result)
Definition: datapairs.cpp:214
void findNodes(const char *name, std::vector< TiXmlElement * > &result, TiXmlElement *start, unsigned int level)
void print()
Prints all Attribute-Value pairs.
Definition: datapairs.cpp:49
float getFloat(std::string str, float defaultValue=0.0)
Definition: datapairs.cpp:105
bool getIntValue(std::string str, int &result)
Definition: datapairs.cpp:180
void setMaxSize(unsigned int size)
Sets number of storable items to size.
Definition: datapairs.cpp:61
std::vector< std::vector< std::string > > values
Attribute Names and Values.
Definition: datapairs.h:37
Configuration file wrapper.
Definition: configuration.h:34
void loadFromXml(CConfiguration &config, TiXmlElement *node, DataPairs *replace=NULL)
Definition: datapairs.cpp:359
unsigned int getLength()
Returns Number of pairs stored.
Definition: datapairs.cpp:131
void add(std::string attribute, std::string value, bool replace=false)
Adds the attribute-value pair attribute, value.
Definition: datapairs.cpp:160


asr_kinematic_chain_optimizer
Author(s): Aumann Florian, Heller Florian, Jäkel Rainer, Wittenbeck Valerij
autogenerated on Mon Jun 10 2019 12:35:36