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"
30
31namespace 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 =
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
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
106
107
108 public:
110 {
111 this->stateType = eFinalState;
112 this->unbreakable = false;
113 this->greedyInputDictionary = false;
115 }
116
117 //! Overridden function, which always throws an exception, to forbid the usage of this function here
118 template <class derivedClass>
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 {
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
#define DEFINEEVENT(NEWEVENT)
this macro declares a new event-class derived vom Event, to have a compiletime check for typos in eve...
An Event is used to communicate between e.g.
Definition Event.h:51
void defineParameters() override
Virtual function, in which input/local/output parameters can be specified.
Definition FinalState.h:81
EventPtr eventToSend
Event that is automatically sent when this state is entered.
Definition FinalState.h:142
StatePtr addState(std::string stateName)
Overridden function, which always throws an exception, to forbid the usage of this function here.
Definition FinalState.h:120
void onEnter() override
Virtual function, in which the behaviour of state is defined, when it is entered. Can be overridden,...
Definition FinalState.h:98
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
friend class FinalState
Definition FinalState.h:145
This is the standard implementation of FinalStateBase.
Definition FinalState.h:159
static StatePtr createState(const EventPtr &event)
createState creates a finalstate instance with the specified event type.
Definition FinalState.h:170
std::unique_ptr< Impl > impl
Definition StateBase.h:289
StateControllerPtr __getParentState() const
Getter function that automatically casts the parentState member of StateBase into StateControllerPtr.
EventPtr createEvent()
Utility function to create a new Event.
Definition StateUtil.h:63
StateParameterMap getInputParameters() override
Returns a new copy of the inputparameters-dictionary, so that the caller cannot modify them (const wo...
Definition State.cpp:407
StateParameterMap & getOutputParameters() override
getter function to get the map of output parameters
Definition State.cpp:119
FinalState< Failure > FailureState
State that automatically signals "Failure" to the parent state.
Definition FinalState.h:199
FinalState< Success > SuccessState
State that automatically signals "Success" to the parent state.
Definition FinalState.h:188
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< State > StatePtr
Definition State.h:44
IceInternal::Handle< Event > EventPtr
Typedef of EventPtr as IceInternal::Handle<Event> for convenience.
Definition Event.h:40
DEFINEEVENT(EvInit) struct StateRun
ParameterMappingPtr createMapping()
Returns a new and empty instance of ParameterMapping.