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 #include "StateParameter.h"
27 #include "Exception.h"
32 
33 using namespace armarx;
34 
35 std::string
37 {
38  switch (type)
39  {
41  return "Unexpected Event";
42  break;
43 
45  return "Invalid input parameters";
46  break;
47 
48  default:
49  return "undefined";
50  }
51 }
52 
53 StringVariantContainerBaseMap
55 {
56  StringVariantContainerBaseMap setValueMap;
57 
58  for (StateParameterMap::const_iterator it = paramMap.begin(); it != paramMap.end(); it++)
59  {
60  if (it->second->set)
61  {
62  setValueMap[it->first] = it->second->value; //->cloneContainer();
63  }
64  }
65 
66  return setValueMap;
67 }
68 
69 
70 StringVariantContainerBaseMap StateUtilFunctions::getValues(const StateParameterMap& paramMap)
71 {
72  StringVariantContainerBaseMap ValueMap;
73 
74  for (StateParameterMap::const_iterator it = paramMap.begin(); it != paramMap.end(); it++)
75  {
76  ValueMap[it->first] = it->second->value->cloneContainer();
77  }
78 
79  return ValueMap;
80 }
81 
82 bool
83 StateUtilFunctions::addToDictionary(EventPtr event, const std::string key, const Variant& value)
84 {
85  bool result = true;
86  StringVariantContainerBaseMap& dictionary = event->properties;
87  StringVariantContainerBaseMap::iterator it = dictionary.find(key);
88 
89  if (it != dictionary.end())
90  {
91  result = false;
92  }
93 
94  dictionary[key] = new SingleVariant(value);
95  return result;
96 }
97 
98 bool StateUtilFunctions::addToDictionary(EventPtr event, const std::string key, const SingleTypeVariantListPtr& valueList)
99 {
100  bool result = true;
101  StringVariantContainerBaseMap& dictionary = event->properties;
102  StringVariantContainerBaseMap::iterator it = dictionary.find(key);
103 
104  if (it != dictionary.end())
105  {
106  result = false;
107  }
108 
109  dictionary[key] = valueList->cloneContainer();
110 
111  return result;
112 }
113 
114 bool
116 {
117  // ARMARX_INFO << "dict1" << dict1.size() << " dict2: " << dict2.size() << std::endl;
118  bool equal = true;
119 
120  for (StateParameterMap::const_iterator it = dict1.begin(); it != dict1.end(); it++)
121  {
122  StateParameterMap::const_iterator it2 = dict2.find(it->first);
123 
124  if (it2 == dict2.end()
125  || VariantContainerType::compare(it2->second->value->getContainerType(), it->second->value->getContainerType()))
126  {
127  equal = false;
128  }
129 
130  break;
131  }
132 
133  return equal;
134 }
135 bool
136 StateUtilFunctions::equalKeys(const StringVariantContainerBaseMap& dict1, const StringVariantContainerBaseMap& dict2)
137 {
138  bool equal = true;
139 
140  for (StringVariantContainerBaseMap::const_iterator it = dict1.begin(); it != dict1.end(); it++)
141  {
142  StringVariantContainerBaseMap::const_iterator it2 = dict2.find(it->first);
143 
144  if (it2 == dict2.end()
145  || VariantContainerType::compare(it2->second->getContainerType(), it->second->getContainerType()))
146  {
147  equal = false;
148  break;
149  }
150  }
151 
152  return equal;
153 }
154 void
155 StateUtilFunctions::fillDictionary(const StringVariantContainerBaseMap& source, StringVariantContainerBaseMap& destination)
156 {
157  for (StringVariantContainerBaseMap::const_iterator it = source.begin(); it != source.end(); it++)
158  {
159  StringVariantContainerBaseMap::iterator itDest = destination.find(it->first);
160 
161  if (itDest != destination.end())
162  {
163  *itDest->second = *it->second;
164  }
165  }
166 }
167 
168 void
169 StateUtilFunctions::fillDictionary(const StringVariantContainerBaseMap& source, StateParameterMap& destination)
170 {
171  for (StringVariantContainerBaseMap::const_iterator it = source.begin(); it != source.end(); it++)
172  {
173  StateParameterMap::iterator itDest = destination.find(it->first);
174 
175  if (itDest != destination.end())
176  {
177  itDest->second->value = it->second->cloneContainer();
178  itDest->second->set = true;
179  }
180  }
181 }
182 
183 void
184 StateUtilFunctions::copyDictionary(const StringVariantContainerBaseMap& source, StringVariantContainerBaseMap& destination)
185 {
186  destination.clear();
187 
188  for (StringVariantContainerBaseMap::const_iterator it = source.begin(); it != source.end(); it++)
189  {
190  destination[it->first] = it->second->cloneContainer();
191  }
192 }
193 void
195 {
196  destination.clear();
197 
198  for (StateParameterMap::const_iterator it = source.begin(); it != source.end(); it++)
199  {
200  destination[it->first] = StateParameterIceBasePtr::dynamicCast(it->second->ice_clone());
201  }
202 }
203 
204 void
205 StateUtilFunctions::copyDictionary(const StateParameterMap& source, StringVariantContainerBaseMap& destination)
206 {
207  destination.clear();
208 
209  for (StateParameterMap::const_iterator it = source.begin(); it != source.end(); it++)
210  {
211  destination[it->first] = it->second->value->cloneContainer();
212  }
213 }
214 void
215 StateUtilFunctions::copyDictionary(const StringVariantContainerBaseMap& source, StateParameterMap& destination)
216 {
217  assert(0);
218  destination.clear();
219 
220  for (StringVariantContainerBaseMap::const_iterator it = source.begin(); it != source.end(); it++)
221  {
223  param->value = it->second->cloneContainer();
224  param->optionalParam = false;
225  param->set = true;
226  destination[it->first] = param;
227  }
228 }
229 
230 
231 std::string
232 StateUtilFunctions::getDictionaryString(const StringVariantContainerBaseMap& mymap)
233 {
234  std::stringstream result;
235  int i = 0;
236 
237  for (StringVariantContainerBaseMap::const_iterator it = mymap.begin(); it != mymap.end(); it++)
238  {
239  VariantPtr var;
240  result << "\t" << it->first << " (" << VariantContainerType::allTypesToString(it->second->getContainerType()) << "): ";
241 
242  if (!it->second->getContainerType()->subType)
243  {
244  SingleVariantPtr sVar = SingleVariantPtr::dynamicCast(it->second);
245 
246  if (!sVar)
247  {
248  throw LocalException("Could not cast to SingleVariant");
249  }
250 
251  var = sVar->get();
252  result << var->getOutputValueOnly() << "\n";
253  }
254  else
255  {
256  JSONObjectPtr json = new JSONObject();
257  json->serializeIceObject(it->second);
258  result << json->asString(true) << "\n";
259  }
260 
261  // break;
262  // case eVariantListParam:
263  // {
264  // SingleTypeVariantListBasePtr listPtr = it->second->getVariantList();
265  // result << "\t#" << i << it->first.c_str() << " (VariantList)" <<std::endl;
266  // for(int j = 0; j < listPtr->getSize(); ++j )
267  // {
268  // var = VariantPtr::dynamicCast(listPtr->getElementBase(j));
269  // result << "\t\t#" << j << " " << getVariantString(var) << "\n";
270  // }
271  // break;
272  // }
273  // default:
274  // break;
275  // }
276  i++;
277 
278 
279  }
280 
281  return result.str();
282 }
283 
284 
285 std::string
287 {
288 
289  std::stringstream result;
290  int i = 0;
291 
292  for (StateParameterMap::const_iterator it = mymap.begin(); it != mymap.end(); it++)
293  {
294  VariantPtr var;
295  std::string setStr;
296 
297  if (it->second->set)
298  {
299  setStr = "set";
300  }
301  else
302  {
303  setStr = "not set";
304  }
305 
306  if (!it->second->value->getContainerType()->subType)
307  {
308  // case eVariantParam:
309  SingleVariantPtr sVar = SingleVariantPtr::dynamicCast(it->second->value);
310 
311  if (!sVar)
312  {
313  throw LocalException("Could not cast to SingleVariant");
314  }
315 
316  var = sVar->get();
317 
318  if (!var)
319  {
320  ARMARX_WARNING_S << "Variant is null";
321  }
322 
323  result << "\t#" << i << " " << getVariantString(var, it->first) << " (" << setStr << ")\n";
324  // break;
325  }
326  else
327  {
328  JSONObjectPtr json = new JSONObject();
329  json->serializeIceObject(it->second->value);
330  result << "\t#" << i << " " << it->first << ": " << json->asString(true) << "\n";
331  }
332 
333  // case eVariantListParam:
334  // {
335  // SingleTypeVariantListBasePtr listPtr = it->second->value->getVariantList();
336  // result << "\t#" << i << it->first.c_str() << " (VariantList)" << " (" << setStr << ")" <<std::endl;
337  // for(int j = 0; j < listPtr->getSize(); ++j)
338  // {
339  // var = VariantPtr::dynamicCast(listPtr->getElementBase(j));
340  // result << "\t\t#" << j << " " << getVariantString(var) << "\n";
341  // }
342  // break;
343  // }
344  // default:
345  // break;
346  // }
347  i++;
348  }
349 
350  return result.str();
351 }
352 
353 
354 bool StateUtilFunctions::checkForCompleteParameters(const StateParameterMap& paramMap, std::string* logOutput)
355 {
356  bool result = true;
357 
358  for (StateParameterMap::const_iterator it = paramMap.begin(); it != paramMap.end(); it++)
359  {
360  if (!it->second->optionalParam && !it->second->set)
361  {
362  if (logOutput)
363  {
364  *logOutput += "\tRequired parameter '" + it->first + "' was not set.\n";
365  }
366 
367  result = false;
368 
369  }
370  else if (!it->second->optionalParam || it->second->set)
371  {
372  try
373  {
374  result &= it->second->value->validateElements();
375  }
377  {
378  ARMARX_DEBUG_S << "Incomplete type for key: " << it->first << " - " << e.what();
379  }
380  }
381  }
382 
383  // result &= waitForChannelRefs(paramMap);
384 
385 
386  return result;
387 }
388 
389 
391 {
392  IceUtil::Int64 timeoutDura = 4000;
393 
394 
395  bool allInitialized = true;
396  int waitIntervallMs = 10;
397  IceUtil::Time waitStartTime = IceUtil::Time::now();
398 
399  IceUtil::Time timeout = IceUtil::Time::milliSeconds(timeoutDura);
400  std::vector<std::string> notInitList;
401 
402  IceUtil::Time lastOutput = IceUtil::Time::milliSeconds(0);
403 
404  while (!allInitialized
405  && IceUtil::Time(waitStartTime + timeout - IceUtil::Time::now()).toMilliSeconds() > 0)
406  {
407  allInitialized = true;
408  notInitList.clear();
409 
410  for (StateParameterMap::const_iterator it = paramMap.begin(); it != paramMap.end(); it++)
411  {
412  StateParameterPtr param = StateParameterPtr::dynamicCast(it->second);
413 
414  if (param->value->getContainerType()->typeId == Variant::typeToString(VariantType::ChannelRef))
415  {
416  if (!VariantPtr::dynamicCast(param->value)->getClass<ChannelRef>()->getInitialized())
417  {
418  allInitialized = false;
419  notInitList.push_back(it->first);
420  }
421  }
422  }
423 
424  if (!allInitialized
425  && IceUtil::Time(IceUtil::Time::now() - lastOutput).toSeconds() >= 5) // output only every 5 seconds
426  {
427  ARMARX_VERBOSE_S << "Waiting for ChannelRef(s): " << notInitList << flush;
428  }
429 
430  usleep(waitIntervallMs * 1000);
431 
432  if (waitIntervallMs < timeoutDura * 0.5f) // increase wait time for lower cpu usage
433  {
434  waitIntervallMs *= 2;
435  }
436  }
437 
438  if (allInitialized)
439  {
440  return true;
441  }
442  else
443  {
444  return false;
445  }
446 }
447 
448 
449 void
451 {
452  StateParameterMap::iterator it = paramMap.begin();
453 
454  for (; it != paramMap.end(); ++it)
455  {
456  it->second->set = false;
457  }
458 }
459 
460 std::string
461 StateUtilFunctions::getVariantString(const VariantBasePtr& var, const std::string& name)
462 {
463  return getVariantString(VariantPtr::dynamicCast(var), name);
464 }
465 
466 std::string
467 StateUtilFunctions::getVariantString(const VariantPtr& var, const std::string& name)
468 {
469  std::stringstream str;
470  str << name << ":\n" << var;
471  return str.str();
472 }
473 
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:224
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:155
SingleTypeVariantList.h
str
std::string str(const T &t)
Definition: UserAssistedSegmenterGuiWidgetController.cpp:42
JSONObject.h
armarx::StateUtilFunctions::waitForChannelRefs
bool waitForChannelRefs(const StateParameterMap &paramMap)
Waits for all ChannelRefs to be initialized in the given Map.
Definition: StateUtilFunctions.cpp:390
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:51
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:926
ARMARX_DEBUG_S
#define ARMARX_DEBUG_S
Definition: Logging.h:198
armarx::SingleVariant
The SingleVariant class is required to store single Variant instances in VariantContainer subclasses.
Definition: VariantContainer.h:109
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:203
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:162
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:184
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:732
armarx::StateUtilFunctions::equalKeys
bool equalKeys(const StringVariantContainerBaseMap &dict1, const StringVariantContainerBaseMap &dict2)
Checks whether the maps have equal keys and equal Types.
Definition: StateUtilFunctions.cpp:136
armarx::StateUtilFunctions::transitionErrorToString
std::string transitionErrorToString(TransitionErrorType type)
Definition: StateUtilFunctions.cpp:36
armarx::ChannelRef::getInitialized
bool getInitialized()
Definition: ChannelRef.cpp:175
boost::source
Vertex source(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:681
ARMARX_WARNING_S
#define ARMARX_WARNING_S
Definition: Logging.h:206
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:232
armarx::StateUtilFunctions::unsetParameters
void unsetParameters(StateParameterMap &paramMap)
Sets all entries of the given dictionary to the stored default values.
Definition: StateUtilFunctions.cpp:450
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:83
ARMARX_VERBOSE_S
#define ARMARX_VERBOSE_S
Definition: Logging.h:200
armarx::StateParameter::create
static StateParameterPtr create()
Definition: StateParameter.cpp:58
Logging.h
Exception.h
armarx::StateUtilFunctions::getValues
StringVariantContainerBaseMap getValues(const StateParameterMap &paramMap)
Definition: StateUtilFunctions.cpp:70
armarx::exceptions::local::IncompleteTypeException
Definition: InvalidDataFieldException.h:56
armarx::StateUtilFunctions::getSetValues
StringVariantContainerBaseMap getSetValues(const StateParameterMap &paramMap)
Definition: StateUtilFunctions.cpp:54
armarx::StateUtilFunctions::checkForCompleteParameters
bool checkForCompleteParameters(const StateParameterMap &paramMap, std::string *logOutput=nullptr)
Definition: StateUtilFunctions.cpp:354
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
ChannelRef.h
armarx::VariantContainerType::allTypesToString
static std::string allTypesToString(const ContainerTypePtr &type)
Definition: VariantContainer.cpp:232