StateBase.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 ArmarXCore::Statechart
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 // Statechart Includes
26 #include "StateBase.h"
27 #include "StateBaseImpl.h"
29 #include "RemoteState.h"
30 #include "StateParameter.h"
31 #include "StateUtilFunctions.h"
32 #include "StatechartManager.h"
33 
34 // ArmarX core Includes
36 
37 // boost Includes
38 #include <boost/regex.hpp>
39 #include <filesystem>
40 
41 
42 
43 namespace armarx
44 {
45  using namespace StateUtilFunctions;
46 
49  HiddenTimedMutex* StateBase::Impl::__StateInstancesMutex = new HiddenTimedMutex();
50 
52  :
53  StateIceBase(),
54  impl(new Impl)
55 
56  {
57  impl->visitCounter = 0;
58  impl->context = nullptr;
59  impl->manager = nullptr;
60 
61  setTag(std::string("Statechart"));
62 
63  initialStateMapping = nullptr;
64 
65  reset();
67  impl->localUniqueId = Impl::__LocalIdCounter++;
68  impl->stateInstancesPtr = Impl::StateInstances;
69  (*impl->stateInstancesPtr)[impl->localUniqueId] = this;
70  }
71 
73  IceUtil::Shared(source),
74  Ice::Object(source),
75  StateIceBase(source),
76  Logging(source),
77  impl(new Impl)
78  {
79  impl->statePhase = source.impl->statePhase;
80  impl->__useRunFunction = true;
81  impl->__parentState = nullptr;
82  impl->visitCounter = 0;
83  impl->context = nullptr;
84  impl->manager = nullptr;
85 
86  setTag(std::string("Statechart"));
87  initialStateMapping = nullptr;
88 
89 
91 
93  impl->localUniqueId = Impl::__LocalIdCounter++;
94  impl->stateInstancesPtr = Impl::StateInstances;
95  (*impl->stateInstancesPtr)[impl->localUniqueId] = this;
96  }
97 
99  {
100  assert(0); //this function shouldnt be called, since it is not tested
101  deepCopy(source);
102  return *this;
103  }
104 
105 
107  {
108  // ARMARX_INFO << "~StateBase " << stateClassName << std::endl;
109  for (unsigned int i = 0; i < subStateList.size(); i++)
110  {
111  RemoteStatePtr remoteStatePtr = RemoteStatePtr::dynamicCast(subStateList.at(i));
112 
113  if (remoteStatePtr
114  && remoteStatePtr->getArmarXManager()) // if the remotestate was not added with addRemoteState(),
115  // this pointer is NULL
116  // (e.g. in case of creation through ObjectFactories)
117  {
118  try
119  {
120  remoteStatePtr->getArmarXManager()->removeObjectNonBlocking(remoteStatePtr->getName());
121  }
122  // destructor must never throw
123  catch (...) {}
124  }
125 
126  }
127 
128  {
130  impl->stateInstancesPtr->erase(impl->localUniqueId);
131  }
132  }
133 
134  void StateBase::__checkPhase(StateBase::StatePhase allowedType, const char* functionName) const
135  {
136  if (allowedType != getStatePhase())
137  {
138  std::string hint;
139 
140  if (allowedType == eSubstatesDefinitions)
141  {
142  hint = "This function must be called in defineSubstates().";
143  }
144  else if (allowedType == eParametersDefinitions)
145  {
146  hint = "This function must be called in defineParameters().";
147  }
148 
149  else if (allowedType == eStatechartDefinitions)
150  {
151  hint = "This function must be called in defineState().";
152  }
153 
154  else if (allowedType == eEntering)
155  {
156  hint = "This function must be called in onEnter().";
157  }
158 
159  if (!hint.empty())
160  {
161  hint = "\n" + hint;
162  }
163 
164  std::stringstream typeStr;
165  typeStr << (int) getStatePhase() << "/" << int(allowedType) ;
166  throw LocalException("It is not allowed to call the function " + std::string(functionName) + " here! (Current Phase/Allowed Phase): " + typeStr.str() + ") " + hint);
167  }
168  }
169 
170  void StateBase::__checkPhase(const std::vector<StateBase::StatePhase>& allowedTypes, const char* functionName) const
171  {
172  if (!std::any_of(allowedTypes.begin(), allowedTypes.end(), [&](StatePhase value)
173  {
174  return value == getStatePhase();
175  }))
176  {
177  std::string hint;
178 
179  for (auto& allowedType : allowedTypes)
180  {
181  if (!hint.empty())
182  {
183  hint += " or ";
184  }
185 
186  if (allowedType == eSubstatesDefinitions)
187  {
188  hint += "defineSubstates()";
189  }
190  else if (allowedType == eParametersDefinitions)
191  {
192  hint += "defineParameters()";
193  }
194 
195  if (allowedType == eStatechartDefinitions)
196  {
197  hint += "defineState()";
198  }
199 
200  if (allowedType == eEntering)
201  {
202  hint += "onEnter()";
203  }
204 
205  if (allowedType == eEntered)
206  {
207  hint += "onRun()";
208  }
209 
210 
211  }
212 
213  if (!hint.empty())
214  {
215  hint = "\nThis function must be called in " + hint;
216  }
217 
218  std::stringstream typeStr;
219  typeStr << (int) getStatePhase();
220  throw LocalException("It is not allowed to call the function " + std::string(functionName) + " here! (Current Phase: " + typeStr.str() + ") " + hint);
221 
222  }
223  }
224 
225  void StateBase::__checkPhaseMin(StateBase::StatePhase allowedType, const char* functionName) const
226  {
227  if (allowedType > getStatePhase())
228  {
229  std::stringstream typeStr;
230  typeStr << (int) getStatePhase() << "/" << int(allowedType) ;
231  throw LocalException("It is not allowed to call the function " + std::string(functionName) + " here! (Current Phase/min. Allowed Phase): " + typeStr.str() + ") ");
232  }
233  }
234 
235 
236 
237 
238 
239 
241  {
242  // currently deprecated since shared_from_this is a normal pointer
243  // Shared::__setNoDelete(true);
244  // __shared_from_this = NULL;
245  // Shared::__setNoDelete(false);
246  }
247 
249  {
250  impl->__parentState = parentState;
252  }
253 
255  {
256  if (impl->__parentState)
257  {
258  if (!impl->__parentState->globalStateIdentifier.empty())
259  {
260  globalStateIdentifier = impl->__parentState->getGlobalHierarchyString() + "->" + stateName;
261  }
262  else
263  {
264  globalStateIdentifier = getLocalHierarchyString(); // generate local hierarchy string
265  }
266  }
267  else
268  {
269  globalStateIdentifier = stateName;
270  }
271  }
272 
274  {
276 
277  for (unsigned int i = 0; i < subStateList.size(); i++)
278  {
279  StateBasePtr::dynamicCast(subStateList.at(i))->__updateGlobalStateIdRecursive();
280  }
281  }
282 
283  void StateBase::setInitialized(bool enable)
284  {
285  ScopedLock lock(impl->initMutex);
286  initialized = enable;
287  impl->initCondition.notify_all();
288  }
289 
291  {
292  if (subStateList.size() > 0)
293  {
294  return true;
295  }
296 
297  return false;
298  }
299 
301  {
302  boost::recursive_mutex::scoped_lock lock(impl->__processEventMutex);
303 
304  if (activeSubstate._ptr)
305  {
306  return true;
307  }
308 
309  return false;
310  }
311 
312 
313 
314 
315  void
316  StateBase::deepCopy(const StateBase& sourceState, bool reset)
317  {
318  try
319  {
320  if (!sourceState.initialized)
321  {
323  }
324 
325 
326  StateUtilFunctions::copyDictionary(sourceState.inputParameters, inputParameters);
327  StateUtilFunctions::copyDictionary(sourceState.localParameters, localParameters);
328  StateUtilFunctions::copyDictionary(sourceState.outputParameters, outputParameters);
329  initState = nullptr;
330 
331  if (sourceState.initialStateMapping)
332  {
333  initialStateMapping = PMPtr::dynamicCast(sourceState.initialStateMapping)->clone();
334  }
335 
336  impl->__useRunFunction = sourceState.impl->__useRunFunction;
337 
338  impl->statePhase = sourceState.impl->statePhase;
339  impl->__parentState = nullptr;
340  activeSubstate = nullptr;
341  impl->context = sourceState.getContext(false);
342  impl->manager = sourceState.impl->manager;
343  impl->cancelEnteringSubstates = sourceState.impl->cancelEnteringSubstates;
344  // __eventBufferedDueToUnbreakableState = sourceState.__eventBufferedDueToUnbreakableState;
345  impl->triggeredEndstateEvent = sourceState.impl->triggeredEndstateEvent;
346 
347  if (sourceState.subStateList.size() > 0)
348  {
349  for (unsigned int i = 0; i < sourceState.subStateList.size(); i++)
350  {
351  StateBasePtr curSubstate = StateBasePtr::dynamicCast(sourceState.subStateList.at(i))->clone();
352  subStateList.at(i) = curSubstate;
353 
354  if (!curSubstate._ptr)
355  {
356  throw LocalException("StatePtrePtr::dynamicCast failed in deepCopy() in file " + std::string(__FILE__));
357  }
358 
359  curSubstate->impl->__parentState = this;
360 
361  if (sourceState.initState._ptr == sourceState.subStateList.at(i)._ptr)
362  {
363  initState = curSubstate;
364  }
365 
366  if (sourceState.activeSubstate._ptr == sourceState.subStateList.at(i)._ptr)
367  {
368  activeSubstate = curSubstate;
369  }
370 
371  // recreate transitionTable
372 
373  for (unsigned int j = 0; j < sourceState.transitions.size(); j++)
374  {
375  if (sourceState.transitions[j].mappingToNextStatesInput)
376  {
377  transitions[j].mappingToNextStatesInput = PMPtr::dynamicCast(sourceState.transitions[j].mappingToNextStatesInput)->clone();
378  }
379 
380  if (sourceState.transitions[j].mappingToParentStatesLocal)
381  {
382  transitions[j].mappingToParentStatesLocal = PMPtr::dynamicCast(sourceState.transitions[j].mappingToParentStatesLocal)->clone();
383  }
384 
385  if (sourceState.transitions[j].mappingToParentStatesOutput)
386  {
387  transitions[j].mappingToParentStatesOutput = PMPtr::dynamicCast(sourceState.transitions[j].mappingToParentStatesOutput)->clone();
388  }
389 
390  if (sourceState.transitions[j].sourceState._ptr == sourceState.subStateList.at(i)._ptr)
391  {
392  transitions[j].sourceState = curSubstate;
393  }
394 
395  if (sourceState.transitions[j].destinationState._ptr == sourceState.subStateList.at(i)._ptr)
396  {
397  transitions[j].destinationState = curSubstate;
398  }
399  }
400  }
401 
402  if (reset)
403  {
404  activeSubstate = nullptr;
405  }
406  }
407  else
408  {
409  initState = nullptr;
410  }
411  }
412  catch (...)
413  {
415  }
416 
417  }
418 
419  bool
421  {
422  if (!context)
423  {
424  ARMARX_WARNING << "The StatechartContext should not be null" << std::endl;
425  }
426 
427  if (!manager)
428  {
429  ARMARX_WARNING << "The StatechartManager should not be null" << std::endl;
430  }
431 
432  this->impl->manager = manager;
433  this->impl->context = context;
434 
435 
437  defineState();
438  setTag("State: " + stateName);
442  defineSubstates();
446 
447 
449 
450  if (subStateList.size() > 0)
451  {
452  // // perform some logic checks
453  // if (subStateList.size() == 1)
454  // {
455  // throw exceptions::local::eStatechartLogicError("A state must contain atleast 2 substates if it has any.");
456  // }
457 
458  // bool foundFinalState = false;
459  // for(unsigned int i= 0; i < subStateList.size(); i++)
460  // if(dynamic_cast<FinalStateBase<SuccessState>*>(subStateList.at(i)._ptr) != NULL
461  // ||dynamic_cast<FinalStateBase<FailureState>*>(subStateList.at(i)._ptr) != NULL )
462  // foundFinalState = true;
463  // if(!foundFinalState)
464  // throw exceptions::local::eLogicError("A state with substates must contain a state of type FinalStateBase!");
465  if (!initState._ptr)
466  {
467  throw exceptions::local::eNullPointerException("The initial state of state '" + stateName + "' was not set!");
468  }
469  }
470 
471  activeSubstate = nullptr;
472  setInitialized(true);
473  return true;
474  }
475 
477  StateBase::getContext(bool checkNULL) const
478  {
479  if (!impl->context && checkNULL)
480  {
481  throw exceptions::local::eNullPointerException("Statechart Context is NULL");
482  }
483 
484  return impl->context;
485  }
486 
487 
488  void
490  {
491  onExit(); //if OnBreak is not overridden, class-standard OnExit() is called
492  }
493 
494 
495  void
497  {
498  }
499 
501  {
502  impl->__useRunFunction = false; // if not implemented, this function does not need to be called, so disable it after first run
503  }
504 
505  void
507  {
508  }
509 
510 
511 
512 
514  {
515  this->impl->context = context;
516  }
517 
519  {
520  return outputParameters;
521  }
522 
523  std::string StateBase::getStateName() const
524  {
525  return stateName;
526  }
527 
529  {
530  return impl->localUniqueId;
531  }
532 
533  const std::string& StateBase::getStateClassName() const
534  {
535  return stateClassName;
536  }
537 
539  {
540  StateIceBase* curParent = impl->__parentState;
541  std::string result;
542 
543  while (curParent)
544  {
545  result = curParent->stateName + "->" + result;
546  curParent = StateBasePtr::dynamicCast(curParent)->impl->__parentState;
547  }
548 
549  result += stateName;
550 
551  return result;
552  }
553 
555  {
556  return globalStateIdentifier;
557  }
558 
559  bool StateBase::waitForInitialization(int timeoutMS) const
560  {
561  ScopedLock lock(impl->initMutex);
562  while (!initialized)
563  {
564  if (timeoutMS >= 0)
565  {
566  if (!impl->initCondition.timed_wait(lock, boost::posix_time::milliseconds(timeoutMS)))
567  {
568  return false;
569  }
570  }
571  else
572  {
573  impl->initCondition.wait(lock);
574  }
575  }
576  return true;
577  }
578 
580  {
581  ScopedLock lock(impl->initMutex);
582  return initialized;
583  }
584 
585  bool StateBase::addParameter(StateParameterMap& paramMap, const std::string& key, VariantTypeId type, bool optional, VariantPtr defaultValue) const
586  {
587 
588  Variant variant;
589  variant.setType(type);
590  VariantContainerBasePtr variantContainer = new SingleVariant(static_cast<const Variant&>(variant));
591  VariantContainerBasePtr defaultContainer;
592 
593  if (defaultValue)
594  {
595  const auto& data = *defaultValue;
596  defaultContainer = new SingleVariant(data);
597  }
598 
599  return addParameterContainer(paramMap, key, *variantContainer->getContainerType(), optional, defaultContainer);
600  }
601 
602  bool StateBase::addParameterContainer(StateParameterMap& paramMap, const std::string& key, const ContainerType& containerType, bool optional, VariantContainerBasePtr defaultValue) const
603  {
604  if (key.empty())
605  {
606  throw LocalException("The key-string of a parameter must not be empty.");
607  }
608 
609  const boost::regex e("^([a-zA-Z0-9_\\.]+)$");
610 
611  if (!boost::regex_match(key, e))
612  {
613  throw LocalException("Invalid character in new key '" + key + "'. Only a-zA-Z0-9_ are allowed.");
614  }
615 
616 
618  // std::string containerStrId = containerType.typeId;
619 
620  // Ice::ObjectFactoryPtr objFac = getContext()->getIceManager()->getCommunicator()->findObjectFactory(containerStrId);
621  // if(objFac)
622  // {
623  // param->value = VariantContainerBasePtr::dynamicCast(objFac->create(containerStrId));
624 
625 
626  // }
627  if (containerType.subType)
628  {
629  // it has a complex container type. the type will be set afterwards
630  param->value = new ContainerDummy();
631  }
632  else
633  {
634  Variant var = Variant();
635  var.setType(Variant::hashTypeName(containerType.typeId));
636  param->value = new SingleVariant(static_cast<const Variant&>(var));
637 
638  }
639 
640  // else
641  // throw exceptions::local::eNullPointerException("Could not find ObjectFactory for string '"+containerStrId+"' and could not identiy VariantTypeId") << ;
642 
643  param->value->setContainerType(containerType.clone());
644 
645  if (defaultValue)
646  {
647  param->defaultValue = defaultValue->cloneContainer();
648  }
649 
650  param->optionalParam = optional;
651  param->set = false;
652 
653  if (paramMap.find(key) != paramMap.end())
654  {
655  return false;
656  }
657 
658  paramMap[key] = param;
659  return true;
660  }
661 
662  void StateBase::setParameter(StateParameterMap& paramMap, const std::string& key, const Variant& variant)
663  {
664  StateParameterMap::iterator it = paramMap.find(/*stateName + ".out." + */key);
665 
666  if (it == paramMap.end())
667  {
668  __throwUnknownParameter(paramMap, key);
669  }
670 
671  // if(it->second->value->getContainerType() != eVariantParam)
672  // throw exceptions::local::eLogicError("Cannot assign a VariantParameter to another type of a Parameter");
673  if ((it->second->value->getContainerType()->typeId != Variant::typeToString(variant.getType())))
674  throw exceptions::local::eStatechartLogicError("Cannot assign the parameter '"
675  + key + "' with type '"
676  + Variant::typeToString(variant.getType())
677  + "' to another type of a Variant ("
678  + it->second->value->getContainerType()->typeId + ")!");
679 
680  it->second->value = new SingleVariant(variant);
681  it->second->set = true;
682 
683  }
684 
685  void StateBase::setParameterContainer(StateParameterMap& paramMap, const std::string& key, const VariantContainerBasePtr& valueContainer)
686  {
687  setParameterContainer(paramMap, key, *valueContainer);
688  }
689 
690  void StateBase::setParameterContainer(StateParameterMap& paramMap, const std::string& key, const VariantContainerBase& valueContainer)
691  {
692  StateParameterMap::iterator it = paramMap.find(/*stateName + ".out." + */key);
693 
694  if (it == paramMap.end())
695  {
696  __throwUnknownParameter(paramMap, key);
697  }
698 
699  if (valueContainer.getSize() > 0 && VariantContainerType::allTypesToString(it->second->value->getContainerType()).find(::armarx::InvalidVariantData::ice_staticId()) == std::string::npos)
700  {
701  if (!VariantContainerType::compare(it->second->value->getContainerType(), valueContainer.getContainerType()))
702  throw exceptions::local::eStatechartLogicError("Cannot assign the parameter container '"
703  + key + "' with type '"
704  + VariantContainerType::allTypesToString(valueContainer.getContainerType())
705  + "' to another type of container ("
706  + VariantContainerType::allTypesToString(it->second->value->getContainerType()) + ")!");
707  }
708  auto type = it->second->value->getContainerType();
709  it->second->value = valueContainer.cloneContainer();
710  it->second->value->setContainerType(type);
711  it->second->set = true;
712  }
713 
714  void StateBase::getParameterContainer(const StateParameterMap& paramMap, const std::string& key, VariantContainerBasePtr& valueContainer) const
715  {
716  StateParameterMap::const_iterator it = paramMap.find(key);
717 
718  if (it == paramMap.end())
719  {
720  __throwUnknownParameter(paramMap, key);
721  }
722 
723  if (!it->second->set)
724  {
725  throw LocalException("Requested parameter '" + key + "' was not set in state " + stateName);
726  }
727 
728  valueContainer = it->second->value;
729  }
730 
731  bool StateBase::isParameterSet(const StateParameterMap& paramMap, const std::string& key) const
732  {
733  StateParameterMap::const_iterator it = paramMap.find(key);
734 
735  if (it == paramMap.end())
736  {
737  throw LocalException("Requested parameter '" + key + "' not found in state " + stateName);
738  }
739 
740  return it->second->set;
741  }
742 
743  void StateBase::__throwUnknownParameter(const StateParameterMap& paramMap, const std::string& key) const
744  {
745  std::string parameters;
746 
747  for (StateParameterMap::const_iterator it = paramMap.begin(); it != paramMap.end(); it++, parameters += ",\n")
748  {
749  parameters += it->first + "(" + VariantContainerType::allTypesToString(it->second->value->getContainerType()) + ")";
750  }
751 
752  throw exceptions::local::eUnknownParameter(stateName, key, "Available parameters:\n" + parameters);
753  }
754 
756  {
757  for (size_t i = 0; i < impl->inputInheritance.size(); i++)
758  {
759  StateBasePtr state = findSubstateByName(impl->inputInheritance.at(i));
760 
761  if (state)
762  {
763  StateParameterMap substateInput = state->getInputParameters();
764 
765  for (StateParameterMap::iterator it = substateInput.begin(); it != substateInput.end(); it++)
766  {
767  StateParameterIceBasePtr param = it->second;
768  addParameterContainer(inputParameters, impl->inputInheritance.at(i) + "." + it->first, *param->value->getContainerType(), param->optionalParam);
769  }
770  }
771  }
772  }
773 
775  {
776  for (StateParameterMap::iterator i = inputParameters.begin(); i != inputParameters.end(); i++)
777  {
778  StateParameterIceBasePtr p = i->second;
779 
780  if (p->defaultValue)
781  {
782  p->value = p->defaultValue->cloneContainer();
783  p->set = true;
784  }
785  }
786  }
787 
788  StateBasePtr StateBase::findSubstateByName(const std::string& substateName)
789  {
790  for (unsigned int i = 0; i < subStateList.size(); i++)
791  {
792  if (StateBasePtr::dynamicCast(subStateList.at(i))->stateName == substateName)
793  {
794  return StateBasePtr::dynamicCast(subStateList.at(i));
795  }
796  }
797 
798  return nullptr;
799  }
800 
801  void StateBase::getParameter(const StateParameterMap& paramMap, const std::string& key, VariantPtr& value) const
802  {
803  StateParameterMap::const_iterator it = paramMap.find(/*stateName + ".in." +*/ key);
804 
805  if (it == paramMap.end())
806  {
807  __throwUnknownParameter(paramMap, key);
808  }
809 
810  if (!it->second->set)
811  {
812  throw LocalException("Requested parameter '" + key + "' was not set in state " + stateName);
813  }
814 
815  SingleVariantPtr sVar = SingleVariantPtr::dynamicCast(it->second->value);
816  if (!sVar)
817  {
818  throw InvalidTypeException("Parameter '" + key + "' is not a single variant!");
819  }
820 
821  value = sVar->get();
822  }
823 
824  void StateBase::setStateClassName(std::string className)
825  {
826  // __checkPhase(eStatechartDefinitions, __PRETTY_FUNCTION__);
827  stateClassName = className;
828  }
829 
831  {
832  for (auto s : subStateList)
833  {
834  StateBasePtr state = StateBasePtr::dynamicCast(s);
835  state->refetchSubstates();
836  }
837  }
838 
840  {
841  return impl->triggeredEndstateEvent;
842  }
843 
845  {
846  ScopedLock lock(impl->statePhaseMutex);
847  return impl->statePhase;
848  }
849 
851  {
852  ScopedLock lock(impl->statePhaseMutex);
853  impl->statePhase = newPhase;
854  }
855 
856  StringVariantContainerBaseMap StateBase::__getSetInputAndLocalParameters() const
857  {
858  StringVariantContainerBaseMap result = StateUtilFunctions::getSetValues(localParameters);
859  StringVariantContainerBaseMap inputSetValues = StateUtilFunctions::getSetValues(inputParameters);
860  result.insert(inputSetValues.begin(), inputSetValues.end());
861  return result;
862  }
863 
864  std::vector<StateBasePtr> StateBase::GetActiveStateLeafs(StateBasePtr toplevelState)
865  {
866  if (!toplevelState)
867  {
868  throw LocalException("The toplevelState must not be NULL");
869  }
870 
871  std::vector<StateBasePtr> activeStates;
872  StateBasePtr activeState = toplevelState;
873  boost::recursive_mutex::scoped_lock lock(toplevelState->impl->__processEventMutex);
874 
875  while (activeState->activeSubstate)
876  {
877  activeState = StateBasePtr::dynamicCast(activeState->activeSubstate);
878  }
879 
880  activeStates.push_back(activeState);
881  return activeStates;
882  }
883 
884 
885 
886 
888  {
889  impl->cancelEnteringSubstates = false;
890  stateName = "";
891  initialized = false;
892  unbreakable = false;
893  eventsDelayed = false;
894  greedyInputDictionary = false;
895  // __eventBufferedDueToUnbreakableState = false;
896  inputParameters.clear();
897  localParameters.clear();
898  outputParameters.clear();
899  subStateList.clear();
900  transitions.clear();
901  initialStateMapping = nullptr;
902  initState = nullptr;
903  activeSubstate = nullptr;
904  impl->__parentState = nullptr;
905  impl->triggeredEndstateEvent = nullptr;
906  // __unbreakableBuffer = queue<EventPtr>();
907  // __eventBuffer = vector< pair<StateBasePtr, EventPtr> >();
908  }
909 
910 }
911 
912 
913 
914 
915 //void armarx::StateBase::__decRef()
916 //{
917 // ARMARX_DEBUG << "DecRef of " << stateName << " of class " << stateClassName;
918 // GCShared::__decRef();
919 //}
armarx::StateBase::getOutputParameters
virtual StateParameterMap & getOutputParameters()
Definition: StateBase.cpp:518
armarx::StateBase::eEntered
@ eEntered
Definition: StateBase.h:273
armarx::StateBase::getParameterContainer
void getParameterContainer(const StateParameterMap &paramMap, const std::string &key, VariantContainerBasePtr &valueContainer) const
Definition: StateBase.cpp:714
armarx::StateBase::getGlobalHierarchyString
std::string getGlobalHierarchyString() const
Definition: StateBase.cpp:554
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:224
armarx::StateBase::getContext
ContextType * getContext() const
Definition: StateBase.h:74
armarx::StateBase::eEntering
@ eEntering
Definition: StateBase.h:272
armarx::StateBase::defineParameters
virtual void defineParameters()
Virtual function, in which input/local/output parameters can be specified.
Definition: StateBase.h:174
armarx::StateBase::deepCopy
virtual void deepCopy(const StateBase &sourceState, bool reset=true)
Function to copy the states with all it substates and transitions.
Definition: StateBase.cpp:316
armarx::StateBase::setStatePhase
void setStatePhase(StatePhase newPhase)
Definition: StateBase.cpp:850
armarx::StateBase::__getSetInputAndLocalParameters
StringVariantContainerBaseMap __getSetInputAndLocalParameters() const
Combines both maps to one map and returns a new map of only the set parameters.
Definition: StateBase.cpp:856
armarx::StateBase::waitForInitialization
virtual bool waitForInitialization(int timeoutMS=-1) const
Definition: StateBase.cpp:559
armarx::StateBase::onExit
virtual void onExit()
Virtual function, in which the behaviour of state is defined, when it is exited. Can be overridden,...
Definition: StateBase.cpp:506
armarx::exceptions::local::eNotInitialized
Definition: Exception.h:55
armarx::Variant::getType
VariantTypeId getType(const Ice::Current &c=Ice::emptyCurrent) const override
Return the Variant's internal type.
Definition: Variant.cpp:574
armarx::StateBase::eStatechartDefinitions
@ eStatechartDefinitions
Definition: StateBase.h:268
StateBaseImpl.h
armarx::StateBase::onBreak
virtual void onBreak()
Virtual function, in which the behaviour of state is defined, when it is abnormally exited....
Definition: StateBase.cpp:489
armarx::exceptions::local::eUnknownParameter
Definition: Exception.h:74
armarx::StateBase::operator=
StateBase & operator=(const StateBase &source)
Definition: StateBase.cpp:98
armarx::StateBase::StatePhase
StatePhase
enum that specifies the phase in which the state is currently in used to control the usage of state-f...
Definition: StateBase.h:265
armarx::StateBase::defineState
virtual void defineState()
Virtual function, in which this state can be configured.
Definition: StateBase.h:162
armarx::StateBase::__checkPhaseMin
void __checkPhaseMin(StatePhase allowedType, const char *functionName) const
Definition: StateBase.cpp:225
armarx::StateBase::isInitialized
virtual bool isInitialized() const
Returns the status of this state. Only if a state is initialized, it can be used.
Definition: StateBase.cpp:579
armarx::StateBase::setInitialized
void setInitialized(bool enable)
Definition: StateBase.cpp:283
armarx::StateBase::getStateName
std::string getStateName() const
getStateName
Definition: StateBase.cpp:523
armarx::StatechartContextInterface
Definition: StatechartContextInterface.h:46
armarx::StateBase::GetActiveStateLeafs
static std::vector< StateBasePtr > GetActiveStateLeafs(StateBasePtr toplevelState)
Definition: StateBase.cpp:864
armarx::StateBase::getLocalUniqueId
Ice::Int getLocalUniqueId() const
Definition: StateBase.cpp:528
armarx::exceptions::local::eNullPointerException
Definition: Exception.h:42
armarx::StateBase::StateBase
StateBase()
Definition: StateBase.cpp:51
StateUtilFunctions.h
armarx::StateBase::clearSelfPointer
void clearSelfPointer()
Definition: StateBase.cpp:240
armarx::ScopedLock
Mutex::scoped_lock ScopedLock
Definition: Synchronization.h:132
IceUtil
Definition: Instance.h:21
StateParameter.h
armarx::exceptions::local::eStatechartLogicError
Definition: Exception.h:30
armarx::StateBase::eDefined
@ eDefined
Definition: StateBase.h:271
armarx::StateBase::Impl::StateInstanceMap
std::map< int, StateBase * > StateInstanceMap
Definition: StateBaseImpl.h:57
armarx::StateBase::__updateGlobalStateId
void __updateGlobalStateId()
Definition: StateBase.cpp:254
armarx::StateBase::__updateGlobalStateIdRecursive
virtual void __updateGlobalStateIdRecursive()
Definition: StateBase.cpp:273
IceInternal::Handle
Definition: forward_declarations.h:8
armarx::StateBase::Impl::StateInstances
static StateInstanceMapPtr StateInstances
Static map that contains all the states that are alive in this process.
Definition: StateBaseImpl.h:60
armarx::StateBase::setParameterContainer
void setParameterContainer(StateParameterMap &paramMap, const std::string &key, const VariantContainerBasePtr &valueContainer)
Definition: StateBase.cpp:685
armarx::StateBase::setStateClassName
void setStateClassName(std::string className)
setStateClassName() sets the string, that contains a stringrepresentation of this class....
Definition: StateBase.cpp:824
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::StateBase::reset
void reset()
Function to reset the state: clear name, clear substatesList, clear transition etc.
Definition: StateBase.cpp:887
armarx::SingleVariant
The SingleVariant class is required to store single Variant instances in VariantContainer subclasses.
Definition: VariantContainer.h:109
armarx::StateBase::Impl::__LocalIdCounter
static int __LocalIdCounter
Unique Identifier counter for this process for identifing states.
Definition: StateBaseImpl.h:38
armarx::StateBase::__copyDefaultValuesToInput
void __copyDefaultValuesToInput()
Definition: StateBase.cpp:774
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
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
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
RemoteState.h
armarx::StateBase::~StateBase
~StateBase() override
Definition: StateBase.cpp:106
armarx::Variant::typeToString
static std::string typeToString(VariantTypeId typeId)
Return the name of the registered type typeId.
Definition: Variant.cpp:732
armarx::StateBase::inheritInputParameters
void inheritInputParameters()
Definition: StateBase.cpp:755
armarx::VariantTypeId
Ice::Int VariantTypeId
Definition: Variant.h:44
boost::source
Vertex source(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:681
armarx::StateBase::addParameter
bool addParameter(StateParameterMap &paramMap, const std::string &key, VariantTypeId type, bool optional, VariantPtr defaultValue=VariantPtr()) const
Definition: StateBase.cpp:585
armarx::StateBase::run
virtual void run()
Virtual function, that can be reimplemented to calculate complex operations.
Definition: StateBase.cpp:500
armarx::StateBase
Definition: StateBase.h:61
armarx::StateBase::__throwUnknownParameter
void __throwUnknownParameter(const StateParameterMap &paramMap, const std::string &key) const
Definition: StateBase.cpp:743
armarx::StateBase::defineSubstates
virtual void defineSubstates()
Virtual function, in which substates, transition and mappings can be added.
Definition: StateBase.h:168
armarx::StateBase::getLocalHierarchyString
std::string getLocalHierarchyString() const
Function to get a string that contains als parent states and this state. (e.g. "Robot->Functional->Id...
Definition: StateBase.cpp:538
armarx::StateBase::eSubstatesDefinitions
@ eSubstatesDefinitions
Definition: StateBase.h:269
armarx::StateBase::Impl::StateInstanceMapPtr
std::shared_ptr< StateInstanceMap > StateInstanceMapPtr
Definition: StateBaseImpl.h:58
StatechartManager.h
Ice
Definition: DBTypes.cpp:64
StateBase.h
armarx::Logging
Base Class for all Logging classes.
Definition: Logging.h:232
armarx::StateBase::setParameter
void setParameter(StateParameterMap &paramMap, const std::string &key, const Variant &variant)
Definition: StateBase.cpp:662
armarx::StateBase::setContext
void setContext(StatechartContextInterface *context)
Definition: StateBase.cpp:513
armarx::StateBase::refetchSubstates
virtual void refetchSubstates()
This functions updates the substates.
Definition: StateBase.cpp:830
armarx::StateBase::onEnter
virtual void onEnter()
Virtual function, in which the behaviour of state is defined, when it is entered. Can be overridden,...
Definition: StateBase.cpp:496
armarx::Variant::setType
void setType(VariantTypeId typeId, const Ice::Current &c=Ice::emptyCurrent) override
Sets the Variant's type to typeId.
Definition: Variant.cpp:264
armarx::StateBase::__setParentState
void __setParentState(StateBase *parentState)
Definition: StateBase.cpp:248
armarx::VariantType::Int
const VariantTypeId Int
Definition: Variant.h:916
armarx::StateBase::addParameterContainer
bool addParameterContainer(StateParameterMap &paramMap, const std::string &key, const ContainerType &containerType, bool optional, VariantContainerBasePtr defaultValue=VariantContainerBasePtr()) const
Definition: StateBase.cpp:602
armarx::StateBase::__hasActiveSubstate
virtual bool __hasActiveSubstate()
Virtual function to indicate wheter a state has an active substate or not. To be overridden by Remote...
Definition: StateBase.cpp:300
armarx::Variant::hashTypeName
static int hashTypeName(const std::string &typeName)
Compute and return a hash value for a given type name.
Definition: Variant.cpp:699
armarx::StateParameter::create
static StateParameterPtr create()
Definition: StateParameter.cpp:58
armarx::StateBase::getStateClassName
const std::string & getStateClassName() const
Definition: StateBase.cpp:533
armarx::StateBase::getContext
StatechartContextInterface * getContext(bool checkNULL=true) const
Definition: StateBase.cpp:477
armarx::StateBase::isParameterSet
bool isParameterSet(const StateParameterMap &paramMap, const std::string &key) const
Definition: StateBase.cpp:731
armarx::Logging::setTag
void setTag(const LogTag &tag)
Definition: Logging.cpp:55
armarx::StateBase::getStatePhase
StatePhase getStatePhase() const
Definition: StateBase.cpp:844
armarx::handleExceptions
void handleExceptions()
Definition: Exception.cpp:141
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::StateBase::eParametersDefinitions
@ eParametersDefinitions
Definition: StateBase.h:270
armarx::HiddenTimedMutex::ScopedLock
boost::unique_lock< HiddenTimedMutex > ScopedLock
Definition: Synchronization.h:122
armarx::StateBase::getTriggeredEndstateEvent
EventPtr getTriggeredEndstateEvent() const
This function returns the event that was triggered by entering an endstate.
Definition: StateBase.cpp:839
armarx::StateBase::__hasSubstates
virtual bool __hasSubstates()
Virtual function to indicate wheter a state has substates or not. To be overridden by RemoteState to ...
Definition: StateBase.cpp:290
armarx::StateUtilFunctions::getSetValues
StringVariantContainerBaseMap getSetValues(const StateParameterMap &paramMap)
Definition: StateUtilFunctions.cpp:54
armarx::StateBase::__checkPhase
void __checkPhase(StatePhase allowedType, const char *functionName) const
Helper function for checking if a function was called in valid position of the statechart.
Definition: StateBase.cpp:134
armarx::StateBase::impl
std::unique_ptr< Impl > impl
Definition: StateBase.h:258
armarx::ContainerDummy
Definition: VariantContainer.h:154
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
armarx::StateBase::findSubstateByName
StateBasePtr findSubstateByName(const std::string &substateName)
Utility function to find a substate of this state by the name.
Definition: StateBase.cpp:788
armarx::StateBase::Impl
Definition: StateBaseImpl.h:10
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::StatechartManager
Definition: StatechartManager.h:42
Application.h
armarx::StateBase::getParameter
void getParameter(const StateParameterMap &paramMap, const std::string &key, VariantPtr &value) const
Definition: StateBase.cpp:801
armarx::VariantContainerType::allTypesToString
static std::string allTypesToString(const ContainerTypePtr &type)
Definition: VariantContainer.cpp:232
armarx::StateBase::Impl::__StateInstancesMutex
static HiddenTimedMutex * __StateInstancesMutex
Definition: StateBaseImpl.h:39
FinalState.h
armarx::StateBase::init
bool init(StatechartContextInterface *context, StatechartManager *manager)
Function to initialize this state. Must be called in the highest level of the hierarchy - and only th...
Definition: StateBase.cpp:420