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