StateUtilFunctions.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * 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, see <http://www.gnu.org/licenses/>.
17 *
18 * @package ArmarX::
19 * @author Mirko Waechter ( mirko.waechter at kit dot edu)
20 * @date 2012
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24 
25 #include "StateUtilFunctions.h"
26 
31 
32 #include "Exception.h"
33 #include "StateParameter.h"
34 
35 using namespace armarx;
36 
37 std::string
39 {
40  switch (type)
41  {
43  return "Unexpected Event";
44  break;
45 
47  return "Invalid input parameters";
48  break;
49 
50  default:
51  return "undefined";
52  }
53 }
54 
55 StringVariantContainerBaseMap
57 {
58  StringVariantContainerBaseMap setValueMap;
59 
60  for (StateParameterMap::const_iterator it = paramMap.begin(); it != paramMap.end(); it++)
61  {
62  if (it->second->set)
63  {
64  setValueMap[it->first] = it->second->value; //->cloneContainer();
65  }
66  }
67 
68  return setValueMap;
69 }
70 
71 StringVariantContainerBaseMap
73 {
74  StringVariantContainerBaseMap ValueMap;
75 
76  for (StateParameterMap::const_iterator it = paramMap.begin(); it != paramMap.end(); it++)
77  {
78  ValueMap[it->first] = it->second->value->cloneContainer();
79  }
80 
81  return ValueMap;
82 }
83 
84 bool
85 StateUtilFunctions::addToDictionary(EventPtr event, const std::string key, const Variant& value)
86 {
87  bool result = true;
88  StringVariantContainerBaseMap& dictionary = event->properties;
89  StringVariantContainerBaseMap::iterator it = dictionary.find(key);
90 
91  if (it != dictionary.end())
92  {
93  result = false;
94  }
95 
96  dictionary[key] = new SingleVariant(value);
97  return result;
98 }
99 
100 bool
102  const std::string key,
103  const SingleTypeVariantListPtr& valueList)
104 {
105  bool result = true;
106  StringVariantContainerBaseMap& dictionary = event->properties;
107  StringVariantContainerBaseMap::iterator it = dictionary.find(key);
108 
109  if (it != dictionary.end())
110  {
111  result = false;
112  }
113 
114  dictionary[key] = valueList->cloneContainer();
115 
116  return result;
117 }
118 
119 bool
121 {
122  // ARMARX_INFO << "dict1" << dict1.size() << " dict2: " << dict2.size() << std::endl;
123  bool equal = true;
124 
125  for (StateParameterMap::const_iterator it = dict1.begin(); it != dict1.end(); it++)
126  {
127  StateParameterMap::const_iterator it2 = dict2.find(it->first);
128 
129  if (it2 == dict2.end() ||
130  VariantContainerType::compare(it2->second->value->getContainerType(),
131  it->second->value->getContainerType()))
132  {
133  equal = false;
134  }
135 
136  break;
137  }
138 
139  return equal;
140 }
141 
142 bool
143 StateUtilFunctions::equalKeys(const StringVariantContainerBaseMap& dict1,
144  const StringVariantContainerBaseMap& dict2)
145 {
146  bool equal = true;
147 
148  for (StringVariantContainerBaseMap::const_iterator it = dict1.begin(); it != dict1.end(); it++)
149  {
150  StringVariantContainerBaseMap::const_iterator it2 = dict2.find(it->first);
151 
152  if (it2 == dict2.end() || VariantContainerType::compare(it2->second->getContainerType(),
153  it->second->getContainerType()))
154  {
155  equal = false;
156  break;
157  }
158  }
159 
160  return equal;
161 }
162 
163 void
164 StateUtilFunctions::fillDictionary(const StringVariantContainerBaseMap& source,
165  StringVariantContainerBaseMap& destination)
166 {
167  for (StringVariantContainerBaseMap::const_iterator it = source.begin(); it != source.end();
168  it++)
169  {
170  StringVariantContainerBaseMap::iterator itDest = destination.find(it->first);
171 
172  if (itDest != destination.end())
173  {
174  *itDest->second = *it->second;
175  }
176  }
177 }
178 
179 void
180 StateUtilFunctions::fillDictionary(const StringVariantContainerBaseMap& source,
181  StateParameterMap& destination)
182 {
183  for (StringVariantContainerBaseMap::const_iterator it = source.begin(); it != source.end();
184  it++)
185  {
186  StateParameterMap::iterator itDest = destination.find(it->first);
187 
188  if (itDest != destination.end())
189  {
190  itDest->second->value = it->second->cloneContainer();
191  itDest->second->set = true;
192  }
193  }
194 }
195 
196 void
197 StateUtilFunctions::copyDictionary(const StringVariantContainerBaseMap& source,
198  StringVariantContainerBaseMap& destination)
199 {
200  destination.clear();
201 
202  for (StringVariantContainerBaseMap::const_iterator it = source.begin(); it != source.end();
203  it++)
204  {
205  destination[it->first] = it->second->cloneContainer();
206  }
207 }
208 
209 void
211 {
212  destination.clear();
213 
214  for (StateParameterMap::const_iterator it = source.begin(); it != source.end(); it++)
215  {
216  destination[it->first] = StateParameterIceBasePtr::dynamicCast(it->second->ice_clone());
217  }
218 }
219 
220 void
222  StringVariantContainerBaseMap& destination)
223 {
224  destination.clear();
225 
226  for (StateParameterMap::const_iterator it = source.begin(); it != source.end(); it++)
227  {
228  destination[it->first] = it->second->value->cloneContainer();
229  }
230 }
231 
232 void
233 StateUtilFunctions::copyDictionary(const StringVariantContainerBaseMap& source,
234  StateParameterMap& destination)
235 {
236  assert(0);
237  destination.clear();
238 
239  for (StringVariantContainerBaseMap::const_iterator it = source.begin(); it != source.end();
240  it++)
241  {
243  param->value = it->second->cloneContainer();
244  param->optionalParam = false;
245  param->set = true;
246  destination[it->first] = param;
247  }
248 }
249 
250 std::string
251 StateUtilFunctions::getDictionaryString(const StringVariantContainerBaseMap& mymap)
252 {
253  std::stringstream result;
254  int i = 0;
255 
256  for (StringVariantContainerBaseMap::const_iterator it = mymap.begin(); it != mymap.end(); it++)
257  {
258  VariantPtr var;
259  result << "\t" << it->first << " ("
260  << VariantContainerType::allTypesToString(it->second->getContainerType()) << "): ";
261 
262  if (!it->second->getContainerType()->subType)
263  {
264  SingleVariantPtr sVar = SingleVariantPtr::dynamicCast(it->second);
265 
266  if (!sVar)
267  {
268  throw LocalException("Could not cast to SingleVariant");
269  }
270 
271  var = sVar->get();
272  result << var->getOutputValueOnly() << "\n";
273  }
274  else
275  {
276  JSONObjectPtr json = new JSONObject();
277  json->serializeIceObject(it->second);
278  result << json->asString(true) << "\n";
279  }
280 
281  // break;
282  // case eVariantListParam:
283  // {
284  // SingleTypeVariantListBasePtr listPtr = it->second->getVariantList();
285  // result << "\t#" << i << it->first.c_str() << " (VariantList)" <<std::endl;
286  // for(int j = 0; j < listPtr->getSize(); ++j )
287  // {
288  // var = VariantPtr::dynamicCast(listPtr->getElementBase(j));
289  // result << "\t\t#" << j << " " << getVariantString(var) << "\n";
290  // }
291  // break;
292  // }
293  // default:
294  // break;
295  // }
296  i++;
297  }
298 
299  return result.str();
300 }
301 
302 std::string
304 {
305 
306  std::stringstream result;
307  int i = 0;
308 
309  for (StateParameterMap::const_iterator it = mymap.begin(); it != mymap.end(); it++)
310  {
311  VariantPtr var;
312  std::string setStr;
313 
314  if (it->second->set)
315  {
316  setStr = "set";
317  }
318  else
319  {
320  setStr = "not set";
321  }
322 
323  if (!it->second->value->getContainerType()->subType)
324  {
325  // case eVariantParam:
326  SingleVariantPtr sVar = SingleVariantPtr::dynamicCast(it->second->value);
327 
328  if (!sVar)
329  {
330  throw LocalException("Could not cast to SingleVariant");
331  }
332 
333  var = sVar->get();
334 
335  if (!var)
336  {
337  ARMARX_WARNING_S << "Variant is null";
338  }
339 
340  result << "\t#" << i << " " << getVariantString(var, it->first) << " (" << setStr
341  << ")\n";
342  // break;
343  }
344  else
345  {
346  JSONObjectPtr json = new JSONObject();
347  json->serializeIceObject(it->second->value);
348  result << "\t#" << i << " " << it->first << ": " << json->asString(true) << "\n";
349  }
350 
351  // case eVariantListParam:
352  // {
353  // SingleTypeVariantListBasePtr listPtr = it->second->value->getVariantList();
354  // result << "\t#" << i << it->first.c_str() << " (VariantList)" << " (" << setStr << ")" <<std::endl;
355  // for(int j = 0; j < listPtr->getSize(); ++j)
356  // {
357  // var = VariantPtr::dynamicCast(listPtr->getElementBase(j));
358  // result << "\t\t#" << j << " " << getVariantString(var) << "\n";
359  // }
360  // break;
361  // }
362  // default:
363  // break;
364  // }
365  i++;
366  }
367 
368  return result.str();
369 }
370 
371 bool
373  std::string* logOutput)
374 {
375  bool result = true;
376 
377  for (StateParameterMap::const_iterator it = paramMap.begin(); it != paramMap.end(); it++)
378  {
379  if (!it->second->optionalParam && !it->second->set)
380  {
381  if (logOutput)
382  {
383  *logOutput += "\tRequired parameter '" + it->first + "' was not set.\n";
384  }
385 
386  result = false;
387  }
388  else if (!it->second->optionalParam || it->second->set)
389  {
390  try
391  {
392  result &= it->second->value->validateElements();
393  }
395  {
396  ARMARX_DEBUG_S << "Incomplete type for key: " << it->first << " - " << e.what();
397  }
398  }
399  }
400 
401  // result &= waitForChannelRefs(paramMap);
402 
403 
404  return result;
405 }
406 
407 bool
409 {
410  IceUtil::Int64 timeoutDura = 4000;
411 
412 
413  bool allInitialized = true;
414  int waitIntervallMs = 10;
415  IceUtil::Time waitStartTime = IceUtil::Time::now();
416 
417  IceUtil::Time timeout = IceUtil::Time::milliSeconds(timeoutDura);
418  std::vector<std::string> notInitList;
419 
420  IceUtil::Time lastOutput = IceUtil::Time::milliSeconds(0);
421 
422  while (!allInitialized &&
423  IceUtil::Time(waitStartTime + timeout - IceUtil::Time::now()).toMilliSeconds() > 0)
424  {
425  allInitialized = true;
426  notInitList.clear();
427 
428  for (StateParameterMap::const_iterator it = paramMap.begin(); it != paramMap.end(); it++)
429  {
430  StateParameterPtr param = StateParameterPtr::dynamicCast(it->second);
431 
432  if (param->value->getContainerType()->typeId ==
434  {
435  if (!VariantPtr::dynamicCast(param->value)
436  ->getClass<ChannelRef>()
437  ->getInitialized())
438  {
439  allInitialized = false;
440  notInitList.push_back(it->first);
441  }
442  }
443  }
444 
445  if (!allInitialized && IceUtil::Time(IceUtil::Time::now() - lastOutput).toSeconds() >=
446  5) // output only every 5 seconds
447  {
448  ARMARX_VERBOSE_S << "Waiting for ChannelRef(s): " << notInitList << flush;
449  }
450 
451  usleep(waitIntervallMs * 1000);
452 
453  if (waitIntervallMs < timeoutDura * 0.5f) // increase wait time for lower cpu usage
454  {
455  waitIntervallMs *= 2;
456  }
457  }
458 
459  if (allInitialized)
460  {
461  return true;
462  }
463  else
464  {
465  return false;
466  }
467 }
468 
469 void
471 {
472  StateParameterMap::iterator it = paramMap.begin();
473 
474  for (; it != paramMap.end(); ++it)
475  {
476  it->second->set = false;
477  }
478 }
479 
480 std::string
481 StateUtilFunctions::getVariantString(const VariantBasePtr& var, const std::string& name)
482 {
483  return getVariantString(VariantPtr::dynamicCast(var), name);
484 }
485 
486 std::string
487 StateUtilFunctions::getVariantString(const VariantPtr& var, const std::string& name)
488 {
489  std::stringstream str;
490  str << name << ":\n" << var;
491  return str.str();
492 }
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:223
armarx::StateUtilFunctions::fillDictionary
void fillDictionary(const StringVariantContainerBaseMap &source, StringVariantContainerBaseMap &destination)
Tries to fill the destination map with matching entries of the source map. Entries that could not be ...
Definition: StateUtilFunctions.cpp:164
SingleTypeVariantList.h
str
std::string str(const T &t)
Definition: UserAssistedSegmenterGuiWidgetController.cpp:43
JSONObject.h
armarx::StateUtilFunctions::waitForChannelRefs
bool waitForChannelRefs(const StateParameterMap &paramMap)
Waits for all ChannelRefs to be initialized in the given Map.
Definition: StateUtilFunctions.cpp:408
armarx::JSONObject
The JSONObject class is used to represent and (de)serialize JSON objects.
Definition: JSONObject.h:43
armarx::ChannelRef
The ChannelRef class is a reference to a channel on an Observer. It is used to access data directly f...
Definition: ChannelRef.h:50
StateUtilFunctions.h
armarx::eTransitionErrorInput
@ eTransitionErrorInput
Definition: StateUtilFunctions.h:54
armarx::RemoteGui::ValueMap
std::map< std::string, ValueVariant > ValueMap
Definition: RemoteGuiVisitors.h:41
StateParameter.h
IceInternal::Handle< Event >
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:855
ARMARX_DEBUG_S
#define ARMARX_DEBUG_S
Definition: Logging.h:205
armarx::SingleVariant
The SingleVariant class is required to store single Variant instances in VariantContainer subclasses.
Definition: VariantContainer.h:107
armarx::statechartmodel::StateParameterMap
QMap< QString, StateParameterPtr > StateParameterMap
Definition: StateParameter.h:46
armarx::VariantContainerType::compare
static bool compare(const ContainerTypePtr &type1, const ContainerTypePtr &secondType)
Definition: VariantContainer.cpp:219
armarx::flush
const LogSender::manipulator flush
Definition: LogSender.h:251
armarx::StateUtilFunctions::getVariantString
std::string getVariantString(const VariantBasePtr &var, const std::string &name="")
armarx::VariantType::ChannelRef
const VariantTypeId ChannelRef
Definition: ChannelRef.h:169
armarx::StateUtilFunctions::copyDictionary
void copyDictionary(const StringVariantContainerBaseMap &source, StringVariantContainerBaseMap &destination)
Clears the destination map and copies the parameters of the source in it.
Definition: StateUtilFunctions.cpp:197
armarx::TransitionErrorType
TransitionErrorType
Definition: StateUtilFunctions.h:50
armarx::Variant::typeToString
static std::string typeToString(VariantTypeId typeId)
Return the name of the registered type typeId.
Definition: Variant.cpp:848
armarx::StateUtilFunctions::equalKeys
bool equalKeys(const StringVariantContainerBaseMap &dict1, const StringVariantContainerBaseMap &dict2)
Checks whether the maps have equal keys and equal Types.
Definition: StateUtilFunctions.cpp:143
armarx::StateUtilFunctions::transitionErrorToString
std::string transitionErrorToString(TransitionErrorType type)
Definition: StateUtilFunctions.cpp:38
armarx::ChannelRef::getInitialized
bool getInitialized()
Definition: ChannelRef.cpp:185
boost::source
Vertex source(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:661
ARMARX_WARNING_S
#define ARMARX_WARNING_S
Definition: Logging.h:213
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
armarx::StateUtilFunctions::getDictionaryString
std::string getDictionaryString(const StringVariantContainerBaseMap &mymap)
Converts the map into a string-representation.
Definition: StateUtilFunctions.cpp:251
armarx::StateUtilFunctions::unsetParameters
void unsetParameters(StateParameterMap &paramMap)
Sets all entries of the given dictionary to the stored default values.
Definition: StateUtilFunctions.cpp:470
armarx::eTransitionErrorUnexpectedEvent
@ eTransitionErrorUnexpectedEvent
Definition: StateUtilFunctions.h:53
armarx::StateUtilFunctions::addToDictionary
bool addToDictionary(EventPtr event, const std::string key, const Variant &value)
Adds the (key,defaulfValue) pair to the event-dictionary.
Definition: StateUtilFunctions.cpp:85
ARMARX_VERBOSE_S
#define ARMARX_VERBOSE_S
Definition: Logging.h:207
armarx::StateParameter::create
static StateParameterPtr create()
Definition: StateParameter.cpp:59
Logging.h
Exception.h
armarx::StateUtilFunctions::getValues
StringVariantContainerBaseMap getValues(const StateParameterMap &paramMap)
Definition: StateUtilFunctions.cpp:72
armarx::exceptions::local::IncompleteTypeException
Definition: InvalidDataFieldException.h:59
armarx::StateUtilFunctions::getSetValues
StringVariantContainerBaseMap getSetValues(const StateParameterMap &paramMap)
Definition: StateUtilFunctions.cpp:56
armarx::StateUtilFunctions::checkForCompleteParameters
bool checkForCompleteParameters(const StateParameterMap &paramMap, std::string *logOutput=nullptr)
Definition: StateUtilFunctions.cpp:372
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
ChannelRef.h
armarx::VariantContainerType::allTypesToString
static std::string allTypesToString(const ContainerTypePtr &type)
Definition: VariantContainer.cpp:250