ParameterMapping.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 <exception>
26#include <map>
27#include <string>
28
30#include <ArmarXCore/interface/observers/VariantBase.h>
31#include <ArmarXCore/interface/statechart/StatechartIce.h>
37
38namespace armarx
39{
40 class ParameterMapping;
43
44 /**
45 \class ParameterMapping
46 \ingroup StatechartGrp
47 \brief This class maps parameters from several source dictionaries to one input dictionary.
48 The mapping depends on an instance of ParameterMappingIceBase, in which the mapping is specified.<br/>
49
50 An example mapping looks like this:<br/>
51 sourceKey: State1.timeout<br/>
52 targetKey: State2.globaltimeout<br/>
53 This would map the value of the outputparameter of a state named "State1.timeout" on the value of the inputparameter of a state named "State2.globaltimeout".<br/>
54
55 Also wildcards are possible.<br/>
56 sourceKey: State1.*<br/>
57 targetKey: State2.*<br/>
58 This mapping would try to map all parameters of a source on the inputparameters of a target state. But only those that are matching exactly on the wildcard level.<br/>
59 */
60
61 class ParameterMapping : virtual public ParameterMappingIceBase, virtual public Logging
62 {
63
64 public:
65 /*!
66 * \brief Creates a new instance of a ParameterMapping. Since the
67 * constructors are private, this method must be used, to create a
68 * new ParameterMapping.
69 * \return Pointer to the new instance of a ParameterMapping.
70 */
72
73 //! Returns a new instance of ParameterMapping with the contents of this instance.
74 ::Ice::ObjectPtr ice_clone() const override;
75 //! Returns a new instance of ParameterMapping with the contents of this instance.
76 virtual ParameterMappingPtr clone() const;
77
78 ParameterMapping& operator=(const ParameterMapping& rhs);
79
80 static std::string MappingSourceToString(MappingSource mappingSource);
81 static MappingSource StringToMappingSource(const std::string& mappingSourceString);
82
83 /*!
84 * \brief Adds a priority for a specific source dictionary to the mapping.
85 *
86 * The priorities determine which source dictionary is chosen, if two
87 * entries of different source dictionaries map onto the same target
88 * parameter.
89 * Each priority level can only be used once. If the priority level
90 * already exists, an eLogicError exception is thrown.
91 * \throw eLogicError
92 * \param priorityLevel Any int value possible. High value -> Higher Priority
93 * \param mappingSrc The mapping source for which this priority level should apply.
94 * \return Shared pointer to this for fluent interface.
95 */
96 ParameterMappingPtr setSourcePriority(int priorityLevel,
97 MappingSource mappingSrc,
98 const Ice::Current& c = Ice::emptyCurrent);
99
100 /*!
101 * \brief Adds an entry to the ParameterMapping, that maps the sourceKey's value
102 * from the <b>output</b> parameters of the last state to the targetKey's value
103 * of the target dictionary.
104 * \param sourceKey The key in the source dictionary.
105 * \param targetKey The key in the target dictionary.
106 * \return Shared pointer to this for fluent interface.
107 */
108 ParameterMappingPtr mapFromOutput(const std::string& sourceKey,
109 const std::string& targetKey,
110 const Ice::Current& c = Ice::emptyCurrent);
111 ParameterMappingPtr mapFromOutput(const std::string& bothKeys,
112 const Ice::Current& c = Ice::emptyCurrent);
113
114 /*!
115 * \brief Adds an entry to the ParameterMapping, that maps the sourceKey's value
116 * from the <b>parent's input</b> parameters of the current state to the
117 * targetKey's value of the target dictionary.
118 * \param sourceKey The key in the source dictionary.
119 * \param targetKey The key in the target dictionary.
120 * \return Shared pointer to this for fluent interface.
121 */
122 ParameterMappingPtr mapFromParent(const std::string& parentKey,
123 const std::string& targetKey,
124 const Ice::Current& c = Ice::emptyCurrent);
125 ParameterMappingPtr mapFromParent(const std::string& bothKeys,
126 const Ice::Current& c = Ice::emptyCurrent);
127
128 /*!
129 * \brief Adds an entry to the ParameterMapping, that maps the sourceKey's value
130 * from the <b>event</b> parameters of the transition to the targetKey's value
131 * of the target dictionary.
132 * \param sourceKey The key in the source dictionary.
133 * \param targetKey The key in the target dictionary.
134 * \return Shared pointer to this for fluent interface.
135 */
136 ParameterMappingPtr mapFromEvent(const std::string& eventKey,
137 const std::string& targetKey,
138 const Ice::Current& c = Ice::emptyCurrent);
139 ParameterMappingPtr mapFromEvent(const std::string& bothKeys,
140 const Ice::Current& c = Ice::emptyCurrent);
141
142 /*!
143 * \brief Adds an entry to the ParameterMapping, that maps the value of the
144 * datafield entry to the targetKey's value of the target dictionary.
145 * \param sourceKey The key in the source dictionary.
146 * \param targetKey The key in the target dictionary.
147 * \return Shared pointer to this for fluent interface.
148 */
150 const std::string& targetKey,
151 const Ice::Current& c = Ice::emptyCurrent);
152
153 void addMappingEntry(MappingSource mappingSource,
154 const std::string& sourceKey,
155 const std::string& targetKey,
156 VariantContainerBasePtr value = nullptr);
157
158 /*!
159 * \brief Sets the behaviour of the mapping into the target dictionary
160 * to greedy.
161 *
162 * Greedy means, that all parameters of all source dictionaries that
163 * have the same key value will be mapped into the target dictionary,
164 * whether or not they are specified in the mapping.
165 * \param on If true, the mapping is greedy.
166 * \return Shared pointer to this for fluent interface.
167 */
169
170 friend class StateBase;
171 friend class StateController;
172 template <class EventType, class StateType>
173 friend class FinalStateBase;
174 friend class StatechartContext;
175
176 protected:
178 {
179 } // protected so that nobody can delete this instance except _decRef of Ice::Shared
180
181 static void _setStatechartContext(StatechartContext* __context);
182
183 //! Checks wether the mapping has an entry like keyDestination that
184 //! maps onto a parameter of mapSource
185 StringVariantContainerBaseMap::const_iterator
186 _hasMappingEntry(const std::string& keyDestination,
187 const StringVariantContainerBaseMap& sourceDict,
188 MappingSource allowedMappingSource);
189 StringVariantContainerBaseMap::const_iterator
190 _findSourceEntry(const std::string sourceKey,
191 const StringVariantContainerBaseMap& sourceDict,
192 int destWildcardIndex,
193 const Ice::StringSeq& fieldsDest);
194
195 //! Takes a string and seperates the string by the seperator-char. Inserts all strings
196 //! between the seperator-chars into a vector of strings (without the seperator-char).<br/>
197 //! E.g. 'State.angle' transforms the 'State' and 'angle'
198 static Ice::StringSeq _getFields(std::string source, char seperator = '.');
199 //
200 static void _addMissingSources(PriorityMap& priorityMap);
201
202 /** \brief This function applies a given mapping to the given inputdictionary.
203 All source-dictionary pointers can be set to NULL if the dictionary should not be considered.
204 The function applyStandardMapping() is executed everytime a mapping is applied with this function.
205 Afterwards the specified (if any) mapping is applied and overwrites the standard mapping.
206 */
207 void _applyMapping(StateParameterMap& targetDictionary);
208 void _greedyMapping(StateParameterMap& targetDictionary,
209 StringVariantContainerBaseMap& sourceDictionary);
210 void _fillFromDataField(StateParameterMap& targetDictionary);
211 void _fillFromValues(StateParameterMap& targetDictionary);
212
213 ParameterMappingPtr _addSourceDictionary(MappingSource mappingSrc,
214 const StringVariantContainerBaseMap& sourceDict,
215 const Ice::Current& c = Ice::emptyCurrent);
216
217 private:
218 bool __greedyInput;
219 static StatechartContext* __context;
220
221 ParameterMapping() : __greedyInput(false)
222 {
223 } //Protected so that nobody can create non-shared-pointer instances.
224
225 ParameterMapping(
226 const ParameterMapping&
227 source); //Protected so that nobody can create non-shared-pointer instances.
228
229 void __fillFromMappingSource(MappingSource mappingSource,
230 StateParameterMap& targetDictionary);
231 //! copies the
232 void __copyDataFromSourceDict(StringVariantContainerBaseMap::const_iterator& itSourceEntry,
233 StateParameterMap::iterator itTargetEntry);
234 };
235
236 //! \brief Returns a new and empty instance of ParameterMapping.
238
239 struct eNoValidMapping : LocalException
240 {
241 eNoValidMapping(std::string missingKey) :
242 LocalException(
243 std::string("armarx::eNoValidMapping: The given mapping is invalid! The key '" +
244 missingKey + "' could not be mapped."))
245 {
246 }
247
248 ~eNoValidMapping() noexcept override
249 {
250 }
251 };
252
255} // namespace armarx
256
257namespace std
258{
259
260 ARMARXCORE_IMPORT_EXPORT ostream& operator<<(ostream& stream,
261 const armarx::ParameterMapping& mapping);
262
263
264}
#define ARMARXCORE_IMPORT_EXPORT
constexpr T c
This class maps parameters from several source dictionaries to one input dictionary.
void _fillFromValues(StateParameterMap &targetDictionary)
void _greedyMapping(StateParameterMap &targetDictionary, StringVariantContainerBaseMap &sourceDictionary)
ParameterMappingPtr mapFromParent(const std::string &parentKey, const std::string &targetKey, const Ice::Current &c=Ice::emptyCurrent)
Adds an entry to the ParameterMapping, that maps the sourceKey's value from the parent's input parame...
static void _setStatechartContext(StatechartContext *__context)
ParameterMappingPtr setTargetDictToGreedy(bool on=true)
Sets the behaviour of the mapping into the target dictionary to greedy.
virtual ParameterMappingPtr clone() const
Returns a new instance of ParameterMapping with the contents of this instance.
void _applyMapping(StateParameterMap &targetDictionary)
This function applies a given mapping to the given inputdictionary.
void _fillFromDataField(StateParameterMap &targetDictionary)
ParameterMapping & operator=(const ParameterMapping &rhs)
ParameterMappingPtr mapFromOutput(const std::string &sourceKey, const std::string &targetKey, const Ice::Current &c=Ice::emptyCurrent)
Adds an entry to the ParameterMapping, that maps the sourceKey's value from the output parameters of ...
static void _addMissingSources(PriorityMap &priorityMap)
ParameterMappingPtr setSourcePriority(int priorityLevel, MappingSource mappingSrc, const Ice::Current &c=Ice::emptyCurrent)
Adds a priority for a specific source dictionary to the mapping.
StringVariantContainerBaseMap::const_iterator _findSourceEntry(const std::string sourceKey, const StringVariantContainerBaseMap &sourceDict, int destWildcardIndex, const Ice::StringSeq &fieldsDest)
StringVariantContainerBaseMap::const_iterator _hasMappingEntry(const std::string &keyDestination, const StringVariantContainerBaseMap &sourceDict, MappingSource allowedMappingSource)
Checks wether the mapping has an entry like keyDestination that maps onto a parameter of mapSource.
void addMappingEntry(MappingSource mappingSource, const std::string &sourceKey, const std::string &targetKey, VariantContainerBasePtr value=nullptr)
static Ice::StringSeq _getFields(std::string source, char seperator='.')
Takes a string and seperates the string by the seperator-char.
static std::string MappingSourceToString(MappingSource mappingSource)
ParameterMappingPtr mapFromEvent(const std::string &eventKey, const std::string &targetKey, const Ice::Current &c=Ice::emptyCurrent)
Adds an entry to the ParameterMapping, that maps the sourceKey's value from the event parameters of t...
::Ice::ObjectPtr ice_clone() const override
Returns a new instance of ParameterMapping with the contents of this instance.
ParameterMappingPtr _addSourceDictionary(MappingSource mappingSrc, const StringVariantContainerBaseMap &sourceDict, const Ice::Current &c=Ice::emptyCurrent)
ParameterMappingPtr mapFromDataField(const DataFieldIdentifierBasePtr &dataFieldIdentifier, const std::string &targetKey, const Ice::Current &c=Ice::emptyCurrent)
Adds an entry to the ParameterMapping, that maps the value of the datafield entry to the targetKey's ...
static ParameterMappingPtr createMapping()
Creates a new instance of a ParameterMapping. Since the constructors are private, this method must be...
static MappingSource StringToMappingSource(const std::string &mappingSourceString)
This class contains a statechart and provides the interfaces to distributed components.
This file offers overloads of toIce() and fromIce() functions for STL container types.
ParameterMapping PM
IceInternal::Handle< DataFieldIdentifierBase > DataFieldIdentifierBasePtr
ParameterMappingPtr PMPtr
IceInternal::Handle< ParameterMapping > ParameterMappingPtr
ParameterMappingPtr createMapping()
Returns a new and empty instance of ParameterMapping.
ARMARXCORE_IMPORT_EXPORT ostream & operator<<(ostream &stream, const armarx::RunningTaskIceBase &task)
eNoValidMapping(std::string missingKey)
~eNoValidMapping() noexcept override