FinalState.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 #include "../RemoteState.h"
26 #include "../StateBase.h"
27 #include "../StateBaseImpl.h"
28 #include "../StateTemplate.h"
29 #include "../StateUtilFunctions.h"
30 
31 namespace armarx
32 {
33  /**
34  \class FinalStateBase
35  \ingroup StatechartGrp
36  Base class for all Finalstates. Finalstates automatically send in onEnter()
37  an event to the grandparent state that triggers the parent state to be left.<br/>
38  Before sending the event the output-dictionary of the parentstate is filled.
39  The input-dictionary of the FinalState is copied from the output-dictionary
40  of the parent state before StateBase::onEnter().
41  To map from the output of the previous state to the output of the parent state,
42  the mapping has to be specified for the transition between FinalState and
43  the previous state.<br/>
44  @Note onExit() <b>will not</b> be called in this state.
45  @see FinalState
46  */
47  template <class EventType, class StateType>
48  class FinalStateBase : virtual public StateTemplate<StateType>
49  {
50  //! \brief Function to signal the parent state that an end/final-state of this substatemachine is reached.
51  //! This function gets called bei derived types of FinalStateBase.<br/>
52  //! The FinalStateBase's output and the event parameters are mapped into the parent's output with a '*'=>'*' mapping.
53  void
54  signalFinish(const EventPtr event)
55  {
56  //send to parent of parent to induce transition
57  if (StateBase::impl->__parentState)
58  {
59 
60  StringVariantContainerBaseMap setOutputValues =
61  StateUtilFunctions::getSetValues(this->getOutputParameters());
63  ->mapFromOutput("*")
64  ->mapFromEvent("*")
65  ->_addSourceDictionary(eOutput, setOutputValues)
66  ->_addSourceDictionary(eEvent, event->properties)
67  ->_applyMapping(StateBase::impl->__parentState->outputParameters);
68 
69  EventPtr ev =
70  new Event(StateBase::impl->__parentState->stateName, event->eventName);
71  ev->properties = event->properties;
72  this->__getParentState()->__substatesFinished(ev);
73  }
74  else //if no parent of parent exists, statemachine has reached its endpoint
75  {
76  }
77  }
78 
79  protected:
80  void
81  defineParameters() override
82  {
83  StatePtr parent = StatePtr::dynamicCast(StateController::__getParentState());
84  StateUtilFunctions::copyDictionary(parent->getOutputParameters(),
85  this->StateIceBase::inputParameters);
86 
87  // make all input parameters optional, because the output paramters
88  // of the parent state might be set in onEnter of the parent state
89  StateParameterMap::iterator it = this->StateIceBase::inputParameters.begin();
90 
91  for (; it != this->StateIceBase::inputParameters.end(); it++)
92  {
93  it->second->optionalParam = true;
94  }
95  }
96 
97  void
98  onEnter() override
99  {
100 
103 
104  signalFinish(eventToSend);
105  }
106 
107 
108  public:
110  {
111  this->stateType = eFinalState;
112  this->unbreakable = false;
113  this->greedyInputDictionary = false;
114  this->eventToSend = StateUtility::createEvent<EventType>();
115  }
116 
117  //! Overridden function, which always throws an exception, to forbid the usage of this function here
118  template <class derivedClass>
119  StatePtr
120  addState(std::string stateName)
121  {
123  "You cannot add substates to a final state!");
124 
125  return nullptr;
126  }
127 
128  //! Overridden function, which always throws an exception, to forbid the usage of this function here
130  addRemoteState(std::string stateName,
131  std::string proxyName,
132  std::string instanceName = "") override
133  {
135  "You cannot add substates to a final state!");
136 
137  return nullptr;
138  }
139 
140  protected:
141  //! Event that is automatically sent when this state is entered
143 
144  template <class Event>
145  friend class FinalState;
146  };
147 
148  DEFINEEVENT(EvDummy)
149 
150  /** \brief This is the standard implementation of FinalStateBase. It should
151  * be used, if no additional functionality is needed. The template parameter
152  * EventType specifies the event that is automatically send, when this state
153  * is entered.
154  *
155  * @see FinalStateBase
156  */
157  template <class EventType = EvDummy>
158  class FinalState : public FinalStateBase<EventType, FinalState<EventType>>
159  {
160 
161  public:
162  /**
163  * @brief createState creates a finalstate instance with the specified
164  * event type.
165  * This function is needed if the EventType is only known at runtime.
166  * @param event EventType that is automatically send in StateBase::onEnter()
167  * @return new instance to the FinalState
168  */
169  static StatePtr
170  createState(const EventPtr& event)
171  {
173  new FinalState<EvDummy>();
174  state->setStateName(event->eventName);
175  state->eventToSend = event;
176  return state;
177  }
178  };
179 
180  // Definition of the event that the SuccessState sends to the grandparent state
181  DEFINEEVENT(Success)
182  /**
183  *\typedef armarx::SuccessState
184  *\ingroup StatechartGrp
185  *State that automatically signals "Success" to the parent state.
186  * See class FinalStateBase for more information.
187  */
188  using SuccessState = FinalState<Success>;
189 
190  // Definition of the event that the FailureState sends to the grandparent state
191  DEFINEEVENT(Failure)
192 
193  /**
194  * \typedef armarx::FailureState
195  * \ingroup StatechartGrp
196  * State that automatically signals "Failure" to the parent state.
197  * See class FinalStateBase for more information.
198  */
199  using FailureState = FinalState<Failure>;
200 
201 } // namespace armarx
armarx::State::getOutputParameters
StateParameterMap & getOutputParameters() override
getter function to get the map of output parameters
Definition: State.cpp:119
armarx::StateController::__getParentState
StateControllerPtr __getParentState() const
Getter function that automatically casts the parentState member of StateBase into StateControllerPtr.
Definition: StateController.cpp:587
armarx::FinalStateBase::addRemoteState
RemoteStatePtr addRemoteState(std::string stateName, std::string proxyName, std::string instanceName="") override
Overridden function, which always throws an exception, to forbid the usage of this function here.
Definition: FinalState.h:130
armarx::FinalStateBase::onEnter
void onEnter() override
Definition: FinalState.h:98
armarx::FinalStateBase
Definition: FinalState.h:48
armarx::StateTemplate
Definition: State.h:38
armarx::createMapping
ParameterMappingPtr createMapping()
Returns a new and empty instance of ParameterMapping.
Definition: ParameterMapping.cpp:251
armarx::FinalStateBase::eventToSend
EventPtr eventToSend
Event that is automatically sent when this state is entered.
Definition: FinalState.h:142
armarx::exceptions::local::eStatechartLogicError
Definition: Exception.h:30
IceInternal::Handle< Event >
armarx::Event
An Event is used to communicate between e.g. condition handlers and statecharts.
Definition: Event.h:50
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:197
armarx::FinalState::createState
static StatePtr createState(const EventPtr &event)
createState creates a finalstate instance with the specified event type.
Definition: FinalState.h:170
armarx::FinalStateBase::defineParameters
void defineParameters() override
Definition: FinalState.h:81
armarx::FinalStateBase::addState
StatePtr addState(std::string stateName)
Overridden function, which always throws an exception, to forbid the usage of this function here.
Definition: FinalState.h:120
armarx::DEFINEEVENT
DEFINEEVENT(EvInit) struct StateRun
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:407
armarx::FinalStateBase::FinalStateBase
FinalStateBase()
Definition: FinalState.h:109
armarx::StateUtilFunctions::getSetValues
StringVariantContainerBaseMap getSetValues(const StateParameterMap &paramMap)
Definition: StateUtilFunctions.cpp:56
armarx::StateBase::impl
std::unique_ptr< Impl > impl
Definition: StateBase.h:288
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::FinalState
This is the standard implementation of FinalStateBase.
Definition: FinalState.h:158