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