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 
38 namespace armarx
39 {
40  class ParameterMapping;
42  class StatechartContext;
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 
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:
177  ~ParameterMapping() override
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 
257 namespace std
258 {
259 
260  ARMARXCORE_IMPORT_EXPORT ostream& operator<<(ostream& stream,
261  const armarx::ParameterMapping& mapping);
262 
263 
264 }
armarx::eNoValidMapping::~eNoValidMapping
~eNoValidMapping() noexcept override
Definition: ParameterMapping.h:248
armarx::ParameterMapping
This class maps parameters from several source dictionaries to one input dictionary....
Definition: ParameterMapping.h:61
armarx::ParameterMapping::mapFromParent
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...
Definition: ParameterMapping.cpp:596
armarx::ParameterMapping::mapFromDataField
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 ...
Definition: ParameterMapping.cpp:628
armarx::FinalStateBase
Definition: FinalState.h:48
armarx::ParameterMapping::_hasMappingEntry
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.
Definition: ParameterMapping.cpp:345
armarx::StateController
Definition: StateController.h:55
armarx::ParameterMapping::_addSourceDictionary
ParameterMappingPtr _addSourceDictionary(MappingSource mappingSrc, const StringVariantContainerBaseMap &sourceDict, const Ice::Current &c=Ice::emptyCurrent)
Definition: ParameterMapping.cpp:545
armarx::ParameterMapping::createMapping
static ParameterMappingPtr createMapping()
Creates a new instance of a ParameterMapping. Since the constructors are private, this method must be...
Definition: ParameterMapping.cpp:573
armarx::ParameterMappingPtr
IceInternal::Handle< ParameterMapping > ParameterMappingPtr
Definition: ParameterMapping.h:41
armarx::ParameterMapping::_addMissingSources
static void _addMissingSources(PriorityMap &priorityMap)
Definition: ParameterMapping.cpp:486
armarx::ParameterMapping::operator=
ParameterMapping & operator=(const ParameterMapping &rhs)
Definition: ParameterMapping.cpp:272
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:46
armarx::ParameterMapping::clone
virtual ParameterMappingPtr clone() const
Returns a new instance of ParameterMapping with the contents of this instance.
Definition: ParameterMapping.cpp:264
armarx::createMapping
ParameterMappingPtr createMapping()
Returns a new and empty instance of ParameterMapping.
Definition: ParameterMapping.cpp:251
armarx::ParameterMapping::ice_clone
::Ice::ObjectPtr ice_clone() const override
Returns a new instance of ParameterMapping with the contents of this instance.
Definition: ParameterMapping.cpp:257
armarx::ParameterMapping::setSourcePriority
ParameterMappingPtr setSourcePriority(int priorityLevel, MappingSource mappingSrc, const Ice::Current &c=Ice::emptyCurrent)
Adds a priority for a specific source dictionary to the mapping.
Definition: ParameterMapping.cpp:529
armarx::ParameterMapping::_applyMapping
void _applyMapping(StateParameterMap &targetDictionary)
This function applies a given mapping to the given inputdictionary.
Definition: ParameterMapping.cpp:48
IceInternal::Handle
Definition: forward_declarations.h:8
armarx::ParameterMapping::mapFromOutput
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 ...
Definition: ParameterMapping.cpp:580
armarx::ParameterMapping::_setStatechartContext
static void _setStatechartContext(StatechartContext *__context)
Definition: ParameterMapping.cpp:645
armarx::ParameterMapping::_findSourceEntry
StringVariantContainerBaseMap::const_iterator _findSourceEntry(const std::string sourceKey, const StringVariantContainerBaseMap &sourceDict, int destWildcardIndex, const Ice::StringSeq &fieldsDest)
Definition: ParameterMapping.cpp:410
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:855
armarx::ParameterMapping::StringToMappingSource
static MappingSource StringToMappingSource(const std::string &mappingSourceString)
Definition: ParameterMapping.cpp:316
armarx::ParameterMapping::_greedyMapping
void _greedyMapping(StateParameterMap &targetDictionary, StringVariantContainerBaseMap &sourceDictionary)
Definition: ParameterMapping.cpp:155
armarx::ParameterMapping::setTargetDictToGreedy
ParameterMappingPtr setTargetDictToGreedy(bool on=true)
Sets the behaviour of the mapping into the target dictionary to greedy.
Definition: ParameterMapping.cpp:638
armarx::statechartmodel::StateParameterMap
QMap< QString, StateParameterPtr > StateParameterMap
Definition: StateParameter.h:46
armarx::StatechartContext
This class contains a statechart and provides the interfaces to distributed components.
Definition: StatechartContext.h:85
armarx::ParameterMapping::mapFromEvent
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...
Definition: ParameterMapping.cpp:612
armarx::ParameterMapping::_getFields
static Ice::StringSeq _getFields(std::string source, char seperator='.')
Takes a string and seperates the string by the seperator-char.
Definition: ParameterMapping.cpp:470
armarx::ParameterMapping::_fillFromValues
void _fillFromValues(StateParameterMap &targetDictionary)
Definition: ParameterMapping.cpp:230
VariantListParameter.h
boost::source
Vertex source(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:661
Parameter.h
std::operator<<
ARMARXCORE_IMPORT_EXPORT ostream & operator<<(ostream &stream, const armarx::RunningTaskIceBase &task)
armarx::StateBase
Definition: StateBase.h:59
armarx::ParameterMapping::MappingSourceToString
static std::string MappingSourceToString(MappingSource mappingSource)
Definition: ParameterMapping.cpp:291
VariantParameter.h
armarx::ParameterMapping::_fillFromDataField
void _fillFromDataField(StateParameterMap &targetDictionary)
Definition: ParameterMapping.cpp:172
armarx::Logging
Base Class for all Logging classes.
Definition: Logging.h:239
std
Definition: Application.h:66
armarx::eNoValidMapping
Definition: ParameterMapping.h:239
armarx::ParameterMapping::~ParameterMapping
~ParameterMapping() override
Definition: ParameterMapping.h:177
armarx::eNoValidMapping::eNoValidMapping
eNoValidMapping(std::string missingKey)
Definition: ParameterMapping.h:241
armarx::aron::type::ObjectPtr
std::shared_ptr< Object > ObjectPtr
Definition: Object.h:36
ARMARXCORE_IMPORT_EXPORT
#define ARMARXCORE_IMPORT_EXPORT
Definition: ImportExport.h:38
Logging.h
Exception.h
Variant.h
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::ParameterMapping::addMappingEntry
void addMappingEntry(MappingSource mappingSource, const std::string &sourceKey, const std::string &targetKey, VariantContainerBasePtr value=nullptr)
Definition: ParameterMapping.cpp:555