State.h
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 #pragma once
25 
26 
27 #include "StateUtil.h"
28 
32 
33 #include <type_traits>
34 
35 
36 namespace armarx
37 {
38  template <class StateType>
40 
41  class RemoteState;
43 
44  class State;
46 
54  class State :
55  virtual public StateUtility
56  {
57 
58  protected:
59  State();
60  State(const State& source);
61  State& operator=(const State& source);
62  ~State() override;
63  public:
70  bool init(StatechartContextInterface* context, StatechartManager* manager);
71 
72 
73 
74 
76  unsigned int getId() const;
77 
83  std::string getGlobalIdString() const;
84 
85 
86 
93  StateBasePtr getInitState() const;
94 
95  bool isUnbreakable() const;
96 
103 
113  template<typename T>
114  typename std::enable_if_t<std::is_base_of_v<VariantDataClass, T>, IceInternal::Handle<T> >
115  getOutput(const std::string& key) const;
116 
126  template<typename T>
127  typename std::enable_if_t < !(std::is_base_of_v<VariantDataClass, T> || std::is_base_of_v<VariantContainerBase, T>), T >
128  getOutput(const std::string& key) const;
129 
130 
131  public:
132 
134 
142  template <class derivedState>
143  IceInternal::Handle<derivedState> addState(std::string const& stateName = "");
144 
159  virtual RemoteStatePtr addRemoteState(std::string stateName, std::string proxyName, std::string instanceName = "");
160 
178  virtual RemoteStatePtr addDynamicRemoteState(std::string instanceName);
179 
180 
181  TransitionIceBase& addTransition(EventPtr event, StateIceBasePtr sourceState,
182  StateIceBasePtr destinationState,
183  ParameterMappingIceBasePtr mappingToNextStatesInput = nullptr,
184  ParameterMappingIceBasePtr mappingToParentStatesLocal = nullptr,
185  ParameterMappingIceBasePtr mappingToParentStatesOutput = nullptr);
186 
196  template <class EventType>
197  TransitionIceBase& addTransition(StateIceBasePtr sourceState,
198  StateIceBasePtr destinationState,
199  ParameterMappingIceBasePtr mappingToNextStatesInput = nullptr,
200  ParameterMappingIceBasePtr mappingToParentStatesLocal = nullptr,
201  ParameterMappingIceBasePtr mappingToParentStatesOutput = nullptr);
202 
208  template <class EventType>
209  TransitionIceBase& addTransitionFromAllStates(
210  StateIceBasePtr destinationState,
211  ParameterMappingIceBasePtr mappingToNextStatesInput = nullptr,
212  ParameterMappingIceBasePtr mappingToParentStatesLocal = nullptr,
213  ParameterMappingIceBasePtr mappingToParentStatesOutput = nullptr);
214 
215  void inheritInputFromSubstate(std::string stateName);
216 
225  bool addToInput(const std::string& key, const ContainerType& type, bool optional, VariantContainerBasePtr defaultValue = VariantContainerBasePtr());
226 
234  bool addToInput(const std::string& key, VariantTypeId type, bool optional, VariantPtr defaultValue = VariantPtr());
235 
243  bool addToLocal(const std::string& key, VariantTypeId type, VariantPtr defaultValue = VariantPtr());
244 
252  bool addToLocal(const std::string& key, const ContainerType& type, VariantContainerBasePtr defaultValue = VariantContainerBasePtr());
253 
261  bool addToOutput(const std::string& key, VariantTypeId type, bool optional);
262 
270  bool addToOutput(const std::string& key, const ContainerType& type, bool optional);
271 
272 
273 
274 
275 
276 
277 
279  StateBasePtr clone() const override;
281  StateBasePtr createEmptyCopy() const override;
282 
283  //**************************
284  // Getters
285  //**************************
286 
287 
296 
308  template<typename T>
309  typename std::enable_if_t<std::is_base_of_v<VariantDataClass, T>, IceInternal::Handle<T> >
310  getInput(const std::string& key) const;
311 
323  template<typename T>
324  typename std::enable_if_t < !(std::is_base_of_v<VariantDataClass, T> || std::is_base_of_v<VariantContainerBase, T>), T >
325  getInput(const std::string& key) const;
326 
337  VariantPtr getInput(const std::string& key) const;
338 
339 
346 
356  template<typename T>
357  typename std::enable_if_t<std::is_base_of_v<VariantDataClass, T>, IceInternal::Handle<T> >
358  getLocal(const std::string& key) const;
359 
369  template<typename T>
370  typename std::enable_if_t < !(std::is_base_of_v<VariantDataClass, T> || std::is_base_of_v<VariantContainerBase, T>), T >
371  getLocal(const std::string& key) const;
372 
373 
374 
375 
376  VariantContainerBasePtr getLocalContainer(std::string const& key);
377 
378 
379 
385  bool isInputParameterSet(const std::string& key) const;
386  bool isLocalParameterSet(const std::string& key) const;
387  bool isOutputParameterSet(const std::string& key) const;
388 
389  //**************************
390  // Setters
391  //**************************
398  void setInput(std::string const& key, const Variant& value);
399 
406  void setInput(std::string const& key, const VariantContainerBase& valueList);
407 
408 
419  void setLocal(std::string const& key, const Variant& value);
420 
431  void setLocal(std::string const& key, const VariantContainerBase& valueList);
432 
433 
443  void setOutput(std::string const& key, const Variant& value);
444 
454  void setOutput(std::string const& key, const VariantContainerBase& valueList);
455 
456 
477  void setUnbreakable(bool setUnbreakable = true);
478 
488  void setUseRunFunction(bool useRunFuntion);
489 
494  void cancelSubstates();
495 
496  void setStateName(const std::string& newName);
497 
498  void setStateClassNameFromTypeName(const std::string& typeName);
499 
500  public:
501  // /*! @brief Function to jump directly into a state. Should only be called for statemachine-debugging.
502 
503  // @param stateName Name of the target state
504  // @param inputDictionary Dictionary for the state specified by @c stateName. Content is copied to the @c inputDictionary of @c stateName.
505  // */
506  // bool JumpToState(std::string stateName, StringVariantMap inputDictionary);
507 
508  StatePtr operator [](std::string const& stateName);
509 
510  template <class EventType, class StateType> friend class FinalStateBase;
511  template <typename ContextType> friend class RemoteStateOfferer;
512  friend class RemoteStateWrapper;
513  template <class StateType> friend class StateTemplate;
514 
515  private:
516  struct Impl;
517  std::unique_ptr<Impl> simpl;
518  };
519 
520 
524 
525  template <> StatePtr State::addState<State>(std::string const& stateName);
526 
527  template <class derivedState>
529  {
530  // if the compiler points to the next line, something with the template parameter has been done wrong. see the message-string in the macro for further information
531  static_assert(std::is_same_v<typename derivedState::Type, derivedState>,
532  "The template parameter 'derivedState' must be derived from StateTemplate<derivedState> or the class 'State' itself! It must not inherit from a class that inherits from StateTemplate!");
533 
534 
535  __checkPhase(eSubstatesDefinitions, __PRETTY_FUNCTION__);
536 
537  IceInternal::Handle<derivedState> pNewState = derivedState::createInstance(stateName);
538  // stateName = pNewState->getStateName();
539 
540  addState(pNewState);
541 
542  return pNewState;
543  }
544 
545 
546  template <class EventType>
547  TransitionIceBase& State::addTransition(StateIceBasePtr sourceState,
548  StateIceBasePtr destinationState,
549  ParameterMappingIceBasePtr mappingToNextStatesInput,
550  ParameterMappingIceBasePtr mappingToParentStatesLocal,
551  ParameterMappingIceBasePtr mappingToParentStatesOutput)
552  {
553  return addTransition(createEvent<EventType>(), sourceState, destinationState, mappingToNextStatesInput, mappingToParentStatesLocal, mappingToParentStatesOutput);
554  }
555 
556 
557  template <class EventType>
559  StateIceBasePtr destinationState,
560  ParameterMappingIceBasePtr mappingToNextStatesInput,
561  ParameterMappingIceBasePtr mappingToParentStatesLocal,
562  ParameterMappingIceBasePtr mappingToParentStatesOutput)
563  {
564  __checkPhase(eSubstatesDefinitions, __PRETTY_FUNCTION__);
565  TransitionIceBase t;
566 
567  assert(destinationState._ptr);
568  t.evt = new EventType(EVENTTOALL);
569  t.destinationState = destinationState;
570 
571  if (mappingToNextStatesInput)
572  {
573  t.mappingToNextStatesInput = PMPtr::dynamicCast(mappingToNextStatesInput)->clone();
574  }
575 
576  if (mappingToParentStatesLocal)
577  {
578  t.mappingToParentStatesLocal = PMPtr::dynamicCast(mappingToParentStatesLocal)->clone();
579  }
580 
581  if (mappingToParentStatesOutput)
582  {
583  t.mappingToParentStatesOutput = PMPtr::dynamicCast(mappingToParentStatesOutput)->clone();
584  }
585 
586  t.fromAll = true;
587 
589  throw exceptions::local::eStatechartLogicError("Cannot insert a general transition on event '" + t.evt->eventName
590  + "' from any state to '" + destinationState->stateName + "' into state '" + getStateName() + "'! There already exists a transition to that state on that event.");
591 
592  transitions.push_back(t);
593  return *transitions.rbegin();
594  }
595 
596  template<typename T>
597  typename std::enable_if_t<std::is_base_of_v<VariantDataClass, T>, IceInternal::Handle<T> >
598  State::getInput(const std::string& key) const
599  {
600  __checkPhaseMin(eDefined, __PRETTY_FUNCTION__);
601  VariantContainerBasePtr varContainer;
602  getParameterContainer(inputParameters, key, varContainer);
603  if (varContainer && !SingleVariantPtr::dynamicCast(varContainer)) // we are only interested in real containers, not singlevariant containers
604  {
605  return IceInternal::Handle<T>::dynamicCast(varContainer);
606  }
607  else
608  {
609  VariantPtr var;
610  getParameter(inputParameters, key, var);
611  return IceInternal::Handle<T>::dynamicCast(var->get<T>());
612  }
613  }
614 
615 
616  template<typename T>
617  typename std::enable_if_t < !(std::is_base_of_v<VariantDataClass, T> || std::is_base_of_v<VariantContainerBase, T>), T >
618  State::getInput(const std::string& key) const
619  {
620  __checkPhaseMin(eDefined, __PRETTY_FUNCTION__);
621  VariantPtr var;
622  getParameter(inputParameters, key, var);
623  return var->get<T>();
624  }
625 
626 
627  template<typename T>
628  typename std::enable_if_t<std::is_base_of_v<VariantDataClass, T>, IceInternal::Handle<T> >
629  State::getLocal(const std::string& key) const
630  {
631  __checkPhaseMin(eDefined, __PRETTY_FUNCTION__);
632  VariantContainerBasePtr varContainer;
633  getParameterContainer(localParameters, key, varContainer);
634  if (varContainer && !SingleVariantPtr::dynamicCast(varContainer)) // we are only interested in real containers, not singlevariant containers
635  {
636  return IceInternal::Handle<T>::dynamicCast(varContainer);
637  }
638  else
639  {
640  VariantPtr var;
641  getParameter(localParameters, key, var);
642  return IceInternal::Handle<T>::dynamicCast(var->get<T>());
643  }
644  }
645 
646 
647  template<typename T>
648  typename std::enable_if_t < !(std::is_base_of_v<VariantDataClass, T> || std::is_base_of_v<VariantContainerBase, T>), T >
649  State::getLocal(const std::string& key) const
650  {
651  __checkPhaseMin(eDefined, __PRETTY_FUNCTION__);
652  VariantPtr var;
653  getParameter(localParameters, key, var);
654  return var->get<T>();
655  }
656 
657 
658  template<typename T>
659  typename std::enable_if_t<std::is_base_of_v<VariantDataClass, T>, IceInternal::Handle<T> >
660  State::getOutput(const std::string& key) const
661  {
662  __checkPhaseMin(eDefined, __PRETTY_FUNCTION__);
663  VariantContainerBasePtr varContainer;
664  getParameterContainer(outputParameters, key, varContainer);
665  if (varContainer && !SingleVariantPtr::dynamicCast(varContainer)) // we are only interested in real containers, not singlevariant containers
666  {
667  return IceInternal::Handle<T>::dynamicCast(varContainer);
668  }
669  else
670  {
671  VariantPtr var;
672  getParameter(outputParameters, key, var);
673  return IceInternal::Handle<T>::dynamicCast(var->get<T>());
674  }
675  }
676 
677 
678  template<typename T>
679  typename std::enable_if_t < !(std::is_base_of_v<VariantDataClass, T> || std::is_base_of_v<VariantContainerBase, T>), T >
680  State::getOutput(const std::string& key) const
681  {
682  __checkPhaseMin(eDefined, __PRETTY_FUNCTION__);
683  VariantPtr var;
684  getParameter(outputParameters, key, var);
685  return var->get<T>();
686  }
687 
688 
689 
690 }
armarx::State::isOutputParameterSet
bool isOutputParameterSet(const std::string &key) const
Definition: State.cpp:499
armarx::StateBase::getParameterContainer
void getParameterContainer(const StateParameterMap &paramMap, const std::string &key, VariantContainerBasePtr &valueContainer) const
Definition: StateBase.cpp:714
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:224
armarx::State::getOutputParameters
StateParameterMap & getOutputParameters() override
getter function to get the map of output parameters
Definition: State.cpp:111
armarx::State::Impl
Definition: State.cpp:49
SingleTypeVariantList.h
armarx::FinalStateBase
Definition: FinalState.h:48
armarx::RemoteStateWrapper
Definition: RemoteStateWrapper.h:50
armarx::State::isUnbreakable
bool isUnbreakable() const
Definition: State.cpp:106
armarx::StateTemplate
Definition: State.h:39
armarx::State::setOutput
void setOutput(std::string const &key, const Variant &value)
setOuput() sets an output parameter of this state.
Definition: State.cpp:434
armarx::State::setUseRunFunction
void setUseRunFunction(bool useRunFuntion)
setUseRunFunction can be used to configurate whether the thread with the async.
Definition: State.cpp:344
armarx::State::getInput
std::enable_if_t< std::is_base_of_v< VariantDataClass, T >, IceInternal::Handle< T > > getInput(const std::string &key) const
getInput can be used to access a specific input parameter.
Definition: State.h:598
armarx::StateBase::__checkPhaseMin
void __checkPhaseMin(StatePhase allowedType, const char *functionName) const
Definition: StateBase.cpp:225
armarx::ParameterMappingPtr
IceInternal::Handle< ParameterMapping > ParameterMappingPtr
Definition: ParameterMapping.h:43
armarx::State::~State
~State() override
Definition: State.cpp:82
armarx::StateBase::getStateName
std::string getStateName() const
getStateName
Definition: StateBase.cpp:523
armarx::StatechartContextInterface
Definition: StatechartContextInterface.h:46
armarx::State::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: State.cpp:84
armarx::State::setLocal
void setLocal(std::string const &key, const Variant &value)
setLocal() sets a local parameter.
Definition: State.cpp:417
armarx::exceptions::local::eStatechartLogicError
Definition: Exception.h:30
armarx::StateBase::eDefined
@ eDefined
Definition: StateBase.h:271
armarx::State::setStateName
void setStateName(const std::string &newName)
Definition: State.cpp:355
armarx::State::isInputParameterSet
bool isInputParameterSet(const std::string &key) const
Checks whether a given input parameter is set or not.
Definition: State.cpp:489
armarx::State::addState
StateBasePtr addState(StateBasePtr pNewState)
Definition: State.cpp:135
armarx::State::getGlobalIdString
std::string getGlobalIdString() const
Getter for the global state identifier string.
Definition: State.cpp:96
armarx::State::createEmptyCopy
StateBasePtr createEmptyCopy() const override
Generates a new copy of this state with the same overidden functions. stateName, substates,...
Definition: State.cpp:457
armarx::StateController::__checkExistenceOfTransition
bool __checkExistenceOfTransition(const TransitionIceBase &transition)
Definition: StateController.cpp:631
IceInternal::Handle
Definition: forward_declarations.h:8
armarx::State::cancelSubstates
void cancelSubstates()
Use this function in the onEnter() function, if you want to avoid that the substates (i....
Definition: State.cpp:349
StringValueMap.h
armarx::VariantPtr
IceInternal::Handle< Variant > VariantPtr
Definition: Variant.h:42
armarx::State::setInitState
StateBasePtr setInitState(StateBasePtr initState, ParameterMappingPtr initialStateMapping=ParameterMappingPtr())
Sets the initial substate of this state.
Definition: State.cpp:319
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::RemoteStateOfferer
Class that holds states, which offer functionality for other states over Ice.
Definition: RemoteStateOfferer.h:182
armarx::State::getLocal
std::enable_if_t< std::is_base_of_v< VariantDataClass, T >, IceInternal::Handle< T > > getLocal(const std::string &key) const
getLocal can be used to access a specific input parameter.
Definition: State.h:629
armarx::State::addToLocal
bool addToLocal(const std::string &key, VariantTypeId type, VariantPtr defaultValue=VariantPtr())
Adds a key,type-pair to the local parameters.
Definition: State.cpp:307
armarx::statechartmodel::StateParameterMap
QMap< QString, StateParameterPtr > StateParameterMap
Definition: StateParameter.h:46
armarx::State::getLocalParameters
StateParameterMap & getLocalParameters()
Getter for the local parameter map.
Definition: State.cpp:385
armarx::State::setUnbreakable
void setUnbreakable(bool setUnbreakable=true)
With this function the state can be made unbreakable.
Definition: State.cpp:338
armarx::State::addTransition
TransitionIceBase & addTransition(EventPtr event, StateIceBasePtr sourceState, StateIceBasePtr destinationState, ParameterMappingIceBasePtr mappingToNextStatesInput=nullptr, ParameterMappingIceBasePtr mappingToParentStatesLocal=nullptr, ParameterMappingIceBasePtr mappingToParentStatesOutput=nullptr)
Definition: State.cpp:232
armarx::State::State
State()
Definition: State.cpp:56
armarx::RemoteState
This Statetype is used to create a state instance that represents a state that is located in another ...
Definition: RemoteState.h:62
armarx::State::addToInput
bool addToInput(const std::string &key, const ContainerType &type, bool optional, VariantContainerBasePtr defaultValue=VariantContainerBasePtr())
Adds a key,type-pair to the input parameters.
Definition: State.cpp:280
armarx::State::getOutput
std::enable_if_t< std::is_base_of_v< VariantDataClass, T >, IceInternal::Handle< T > > getOutput(const std::string &key) const
getOutput can be used to access a specific input parameter.
Definition: State.h:660
armarx::State::clone
StateBasePtr clone() const override
Generates a new copy of this state with the same statename, substates, transitions,...
Definition: State.cpp:450
armarx::State::getLocalContainer
VariantContainerBasePtr getLocalContainer(std::string const &key)
Definition: State.cpp:479
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::State
Definition: State.h:54
armarx::State::inheritInputFromSubstate
void inheritInputFromSubstate(std::string stateName)
Definition: State.cpp:266
armarx::StateUtility
Definition: StateUtil.h:47
armarx::StateBase::eSubstatesDefinitions
@ eSubstatesDefinitions
Definition: StateBase.h:269
armarx::State::addTransitionFromAllStates
TransitionIceBase & addTransitionFromAllStates(StateIceBasePtr destinationState, ParameterMappingIceBasePtr mappingToNextStatesInput=nullptr, ParameterMappingIceBasePtr mappingToParentStatesLocal=nullptr, ParameterMappingIceBasePtr mappingToParentStatesOutput=nullptr)
Function to add a new transition from all substates to destinationState.
Definition: State.h:558
armarx::State::addDynamicRemoteState
virtual RemoteStatePtr addDynamicRemoteState(std::string instanceName)
Function to add a new dynamic remote substate to this state.
Definition: State.cpp:200
armarx::State::getId
unsigned int getId() const
Returns an id to this state, that is guaranteed to be unique in this process.
Definition: State.cpp:91
armarx::State::setStateClassNameFromTypeName
void setStateClassNameFromTypeName(const std::string &typeName)
Definition: State.cpp:360
StateUtil.h
armarx::State::addRemoteState
virtual RemoteStatePtr addRemoteState(std::string stateName, std::string proxyName, std::string instanceName="")
Function to add a new remote substate to this state.
Definition: State.cpp:151
armarx::State::addToOutput
bool addToOutput(const std::string &key, VariantTypeId type, bool optional)
Adds a key,type-pair to the output parameters.
Definition: State.cpp:287
T
float T
Definition: UnscentedKalmanFilterTest.cpp:35
armarx::State::getInitState
StateBasePtr getInitState() const
Getter for the initial state. The initial state is automatically entered, when this state is entered.
Definition: State.cpp:101
armarx::State::operator=
State & operator=(const State &source)
Definition: State.cpp:74
armarx::State::getInputParameters
StateParameterMap getInputParameters() override
Returns a new copy of the inputparameters-dictionary, so that the caller cannot modify them (const wo...
Definition: State.cpp:366
Variant.h
armarx::State::setInput
void setInput(std::string const &key, const Variant &value)
setInput() sets an input parameter.
Definition: State.cpp:390
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::State::isLocalParameterSet
bool isLocalParameterSet(const std::string &key) const
Definition: State.cpp:494
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::StatechartManager
Definition: StatechartManager.h:42
armarx::StateBase::getParameter
void getParameter(const StateParameterMap &paramMap, const std::string &key, VariantPtr &value) const
Definition: StateBase.cpp:801
EVENTTOALL
#define EVENTTOALL
Definition: StateBase.h:42