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
35using namespace armarx;
36
37std::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
55StringVariantContainerBaseMap
56StateUtilFunctions::getSetValues(const StateParameterMap& paramMap)
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
71StringVariantContainerBaseMap
72StateUtilFunctions::getValues(const StateParameterMap& paramMap)
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
84bool
85StateUtilFunctions::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
100bool
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
119bool
120StateUtilFunctions::equalKeys(const StateParameterMap& dict1, const StateParameterMap& dict2)
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
142bool
143StateUtilFunctions::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
163void
164StateUtilFunctions::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
179void
180StateUtilFunctions::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
196void
197StateUtilFunctions::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
209void
210StateUtilFunctions::copyDictionary(const StateParameterMap& source, StateParameterMap& destination)
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
220void
221StateUtilFunctions::copyDictionary(const StateParameterMap& source,
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
232void
233StateUtilFunctions::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
250std::string
251StateUtilFunctions::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
302std::string
303StateUtilFunctions::getDictionaryString(const StateParameterMap& mymap)
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
371bool
372StateUtilFunctions::checkForCompleteParameters(const StateParameterMap& paramMap,
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
407bool
408StateUtilFunctions::waitForChannelRefs(const StateParameterMap& paramMap)
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
469void
470StateUtilFunctions::unsetParameters(StateParameterMap& paramMap)
471{
472 StateParameterMap::iterator it = paramMap.begin();
473
474 for (; it != paramMap.end(); ++it)
475 {
476 it->second->set = false;
477 }
478}
479
480std::string
481StateUtilFunctions::getVariantString(const VariantBasePtr& var, const std::string& name)
482{
483 return getVariantString(VariantPtr::dynamicCast(var), name);
484}
485
486std::string
487StateUtilFunctions::getVariantString(const VariantPtr& var, const std::string& name)
488{
489 std::stringstream str;
490 str << name << ":\n" << var;
491 return str.str();
492}
std::string str(const T &t)
The ChannelRef class is a reference to a channel on an Observer.
Definition ChannelRef.h:51
The JSONObject class is used to represent and (de)serialize JSON objects.
Definition JSONObject.h:44
The SingleVariant class is required to store single Variant instances in VariantContainer subclasses.
static StateParameterPtr create()
static std::string allTypesToString(const ContainerTypePtr &type)
static bool compare(const ContainerTypePtr &type1, const ContainerTypePtr &secondType)
The Variant class is described here: Variants.
Definition Variant.h:224
static std::string typeToString(VariantTypeId typeId)
Return the name of the registered type typeId.
Definition Variant.cpp:848
#define ARMARX_DEBUG_S
The logging level for output that is only interesting while debugging.
Definition Logging.h:205
#define ARMARX_VERBOSE_S
Definition Logging.h:207
#define ARMARX_WARNING_S
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:213
std::map< std::string, ValueVariant > ValueMap
std::string getVariantString(const VariantBasePtr &var, const std::string &name="")
void copyDictionary(const StringVariantContainerBaseMap &source, StringVariantContainerBaseMap &destination)
Clears the destination map and copies the parameters of the source in it.
std::string transitionErrorToString(TransitionErrorType type)
std::string getDictionaryString(const StringVariantContainerBaseMap &mymap)
Converts the map into a string-representation.
StringVariantContainerBaseMap getValues(const StateParameterMap &paramMap)
bool checkForCompleteParameters(const StateParameterMap &paramMap, std::string *logOutput=nullptr)
void unsetParameters(StateParameterMap &paramMap)
Sets all entries of the given dictionary to the stored default values.
StringVariantContainerBaseMap getSetValues(const StateParameterMap &paramMap)
bool equalKeys(const StringVariantContainerBaseMap &dict1, const StringVariantContainerBaseMap &dict2)
Checks whether the maps have equal keys and equal Types.
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 ...
bool waitForChannelRefs(const StateParameterMap &paramMap)
Waits for all ChannelRefs to be initialized in the given Map.
bool addToDictionary(EventPtr event, const std::string key, const Variant &value)
Adds the (key,defaulfValue) pair to the event-dictionary.
const VariantTypeId ChannelRef
Definition ChannelRef.h:169
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceInternal::Handle< SingleTypeVariantList > SingleTypeVariantListPtr
@ eTransitionErrorUnexpectedEvent
IceInternal::Handle< StateParameter > StateParameterPtr
IceInternal::Handle< SingleVariant > SingleVariantPtr
IceInternal::Handle< Variant > VariantPtr
Definition Variant.h:41
IceInternal::Handle< JSONObject > JSONObjectPtr
Definition JSONObject.h:34
const LogSender::manipulator flush
Definition LogSender.h:251
::IceInternal::Handle<::armarx::VariantBase > VariantBasePtr
IceInternal::Handle< Event > EventPtr
Typedef of EventPtr as IceInternal::Handle<Event> for convenience.
Definition Event.h:40