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 <map>
26 #include <string>
27 #include <exception>
28 
29 #include <ArmarXCore/interface/observers/VariantBase.h>
30 #include <ArmarXCore/interface/statechart/StatechartIce.h>
31 
37 
39 
40 namespace armarx
41 {
42  class ParameterMapping;
44  class StatechartContext;
45 
46 
47  /**
48  \class ParameterMapping
49  \ingroup StatechartGrp
50  \brief This class maps parameters from several source dictionaries to one input dictionary.
51  The mapping depends on an instance of ParameterMappingIceBase, in which the mapping is specified.<br/>
52 
53  An example mapping looks like this:<br/>
54  sourceKey: State1.timeout<br/>
55  targetKey: State2.globaltimeout<br/>
56  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/>
57 
58  Also wildcards are possible.<br/>
59  sourceKey: State1.*<br/>
60  targetKey: State2.*<br/>
61  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/>
62  */
63 
65  virtual public ParameterMappingIceBase,
66  virtual public Logging
67  {
68 
69  public:
70  /*!
71  * \brief Creates a new instance of a ParameterMapping. Since the
72  * constructors are private, this method must be used, to create a
73  * new ParameterMapping.
74  * \return Pointer to the new instance of a ParameterMapping.
75  */
77 
78  //! Returns a new instance of ParameterMapping with the contents of this instance.
79  ::Ice::ObjectPtr ice_clone() const override;
80  //! Returns a new instance of ParameterMapping with the contents of this instance.
81  virtual ParameterMappingPtr clone() const;
82 
84 
85  static std::string MappingSourceToString(MappingSource mappingSource);
86  static MappingSource StringToMappingSource(const std::string& mappingSourceString);
87 
88  /*!
89  * \brief Adds a priority for a specific source dictionary to the mapping.
90  *
91  * The priorities determine which source dictionary is chosen, if two
92  * entries of different source dictionaries map onto the same target
93  * parameter.
94  * Each priority level can only be used once. If the priority level
95  * already exists, an eLogicError exception is thrown.
96  * \throw eLogicError
97  * \param priorityLevel Any int value possible. High value -> Higher Priority
98  * \param mappingSrc The mapping source for which this priority level should apply.
99  * \return Shared pointer to this for fluent interface.
100  */
101  ParameterMappingPtr setSourcePriority(int priorityLevel, MappingSource mappingSrc, const Ice::Current& c = Ice::emptyCurrent);
102 
103  /*!
104  * \brief Adds an entry to the ParameterMapping, that maps the sourceKey's value
105  * from the <b>output</b> parameters of the last state to the targetKey's value
106  * of the target dictionary.
107  * \param sourceKey The key in the source dictionary.
108  * \param targetKey The key in the target dictionary.
109  * \return Shared pointer to this for fluent interface.
110  */
111  ParameterMappingPtr mapFromOutput(const std::string& sourceKey, const std::string& targetKey, const Ice::Current& c = Ice::emptyCurrent);
112  ParameterMappingPtr mapFromOutput(const std::string& bothKeys, 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, const std::string& targetKey, const Ice::Current& c = Ice::emptyCurrent);
123  ParameterMappingPtr mapFromParent(const std::string& bothKeys, const Ice::Current& c = Ice::emptyCurrent);
124 
125  /*!
126  * \brief Adds an entry to the ParameterMapping, that maps the sourceKey's value
127  * from the <b>event</b> parameters of the transition to the targetKey's value
128  * of the target dictionary.
129  * \param sourceKey The key in the source dictionary.
130  * \param targetKey The key in the target dictionary.
131  * \return Shared pointer to this for fluent interface.
132  */
133  ParameterMappingPtr mapFromEvent(const std::string& eventKey, const std::string& targetKey, const Ice::Current& c = Ice::emptyCurrent);
134  ParameterMappingPtr mapFromEvent(const std::string& bothKeys, const Ice::Current& c = Ice::emptyCurrent);
135 
136  /*!
137  * \brief Adds an entry to the ParameterMapping, that maps the value of the
138  * datafield entry to the targetKey's value of the target dictionary.
139  * \param sourceKey The key in the source dictionary.
140  * \param targetKey The key in the target dictionary.
141  * \return Shared pointer to this for fluent interface.
142  */
143  ParameterMappingPtr mapFromDataField(const DataFieldIdentifierBasePtr& dataFieldIdentifier, const std::string& targetKey, const Ice::Current& c = Ice::emptyCurrent);
144 
145  void addMappingEntry(MappingSource mappingSource, const std::string& sourceKey, const std::string& targetKey, VariantContainerBasePtr value = nullptr);
146 
147  /*!
148  * \brief Sets the behaviour of the mapping into the target dictionary
149  * to greedy.
150  *
151  * Greedy means, that all parameters of all source dictionaries that
152  * have the same key value will be mapped into the target dictionary,
153  * whether or not they are specified in the mapping.
154  * \param on If true, the mapping is greedy.
155  * \return Shared pointer to this for fluent interface.
156  */
158 
159  friend class StateBase;
160  friend class StateController;
161  template <class EventType, class StateType> friend class FinalStateBase;
162  friend class StatechartContext;
163 
164  protected:
165  ~ParameterMapping() override {} // protected so that nobody can delete this instance except _decRef of Ice::Shared
166  static void _setStatechartContext(StatechartContext* __context);
167 
168  //! Checks wether the mapping has an entry like keyDestination that
169  //! maps onto a parameter of mapSource
170  StringVariantContainerBaseMap::const_iterator _hasMappingEntry(const std::string& keyDestination,
171  const StringVariantContainerBaseMap& sourceDict, MappingSource allowedMappingSource);
172  StringVariantContainerBaseMap::const_iterator _findSourceEntry(const std::string sourceKey,
173  const StringVariantContainerBaseMap& sourceDict, int destWildcardIndex, const Ice::StringSeq& fieldsDest);
174 
175  //! Takes a string and seperates the string by the seperator-char. Inserts all strings
176  //! between the seperator-chars into a vector of strings (without the seperator-char).<br/>
177  //! E.g. 'State.angle' transforms the 'State' and 'angle'
178  static Ice::StringSeq _getFields(std::string source, char seperator = '.');
179  //
180  static void _addMissingSources(PriorityMap& priorityMap);
181 
182  /** \brief This function applies a given mapping to the given inputdictionary.
183  All source-dictionary pointers can be set to NULL if the dictionary should not be considered.
184  The function applyStandardMapping() is executed everytime a mapping is applied with this function.
185  Afterwards the specified (if any) mapping is applied and overwrites the standard mapping.
186  */
187  void _applyMapping(StateParameterMap& targetDictionary);
188  void _greedyMapping(StateParameterMap& targetDictionary, StringVariantContainerBaseMap& sourceDictionary);
189  void _fillFromDataField(StateParameterMap& targetDictionary);
190  void _fillFromValues(StateParameterMap& targetDictionary);
191 
192  ParameterMappingPtr _addSourceDictionary(MappingSource mappingSrc, const StringVariantContainerBaseMap& sourceDict, const Ice::Current& c = Ice::emptyCurrent);
193 
194  private:
195  bool __greedyInput;
196  static StatechartContext* __context;
197  ParameterMapping() : __greedyInput(false) {} //Protected so that nobody can create non-shared-pointer instances.
198  ParameterMapping(const ParameterMapping& source); //Protected so that nobody can create non-shared-pointer instances.
199 
200  void __fillFromMappingSource(MappingSource mappingSource, StateParameterMap& targetDictionary);
201  //! copies the
202  void __copyDataFromSourceDict(StringVariantContainerBaseMap::const_iterator& itSourceEntry, StateParameterMap::iterator itTargetEntry);
203  };
204 
205  //! \brief Returns a new and empty instance of ParameterMapping.
207 
208  struct eNoValidMapping : LocalException
209  {
210  eNoValidMapping(std::string missingKey): LocalException(std::string("armarx::eNoValidMapping: The given mapping is invalid! The key '" + missingKey + "' could not be mapped.")) {}
211  ~eNoValidMapping() noexcept override {}
212  };
215 }
216 
217 namespace std
218 {
219 
220  ARMARXCORE_IMPORT_EXPORT ostream& operator<< (ostream& stream, const armarx::ParameterMapping& mapping);
221 
222 
223 }
armarx::eNoValidMapping::~eNoValidMapping
~eNoValidMapping() noexcept override
Definition: ParameterMapping.h:211
armarx::ParameterMapping
This class maps parameters from several source dictionaries to one input dictionary....
Definition: ParameterMapping.h:64
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:558
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:582
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:324
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:518
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:539
armarx::ParameterMappingPtr
IceInternal::Handle< ParameterMapping > ParameterMappingPtr
Definition: ParameterMapping.h:43
armarx::ParameterMapping::_addMissingSources
static void _addMissingSources(PriorityMap &priorityMap)
Definition: ParameterMapping.cpp:461
armarx::ParameterMapping::operator=
ParameterMapping & operator=(const ParameterMapping &rhs)
Definition: ParameterMapping.cpp:255
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::ParameterMapping::clone
virtual ParameterMappingPtr clone() const
Returns a new instance of ParameterMapping with the contents of this instance.
Definition: ParameterMapping.cpp:248
armarx::createMapping
ParameterMappingPtr createMapping()
Returns a new and empty instance of ParameterMapping.
Definition: ParameterMapping.cpp:235
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:242
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:503
armarx::ParameterMapping::_applyMapping
void _applyMapping(StateParameterMap &targetDictionary)
This function applies a given mapping to the given inputdictionary.
Definition: ParameterMapping.cpp:50
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:546
armarx::ParameterMapping::_setStatechartContext
static void _setStatechartContext(StatechartContext *__context)
Definition: ParameterMapping.cpp:595
armarx::ParameterMapping::_findSourceEntry
StringVariantContainerBaseMap::const_iterator _findSourceEntry(const std::string sourceKey, const StringVariantContainerBaseMap &sourceDict, int destWildcardIndex, const Ice::StringSeq &fieldsDest)
Definition: ParameterMapping.cpp:388
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::ParameterMapping::StringToMappingSource
static MappingSource StringToMappingSource(const std::string &mappingSourceString)
Definition: ParameterMapping.cpp:295
armarx::ParameterMapping::_greedyMapping
void _greedyMapping(StateParameterMap &targetDictionary, StringVariantContainerBaseMap &sourceDictionary)
Definition: ParameterMapping.cpp:151
armarx::ParameterMapping::setTargetDictToGreedy
ParameterMappingPtr setTargetDictToGreedy(bool on=true)
Sets the behaviour of the mapping into the target dictionary to greedy.
Definition: ParameterMapping.cpp:589
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:89
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:570
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:446
armarx::ParameterMapping::_fillFromValues
void _fillFromValues(StateParameterMap &targetDictionary)
Definition: ParameterMapping.cpp:216
VariantListParameter.h
boost::source
Vertex source(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:681
Parameter.h
std::operator<<
ARMARXCORE_IMPORT_EXPORT ostream & operator<<(ostream &stream, const armarx::RunningTaskIceBase &task)
armarx::StateBase
Definition: StateBase.h:61
armarx::ParameterMapping::MappingSourceToString
static std::string MappingSourceToString(MappingSource mappingSource)
Definition: ParameterMapping.cpp:271
VariantParameter.h
armarx::ParameterMapping::_fillFromDataField
void _fillFromDataField(StateParameterMap &targetDictionary)
Definition: ParameterMapping.cpp:164
armarx::Logging
Base Class for all Logging classes.
Definition: Logging.h:232
std
Definition: Application.h:66
armarx::eNoValidMapping
Definition: ParameterMapping.h:208
armarx::ParameterMapping::~ParameterMapping
~ParameterMapping() override
Definition: ParameterMapping.h:165
armarx::eNoValidMapping::eNoValidMapping
eNoValidMapping(std::string missingKey)
Definition: ParameterMapping.h:210
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:28
armarx::ParameterMapping::addMappingEntry
void addMappingEntry(MappingSource mappingSource, const std::string &sourceKey, const std::string &targetKey, VariantContainerBasePtr value=nullptr)
Definition: ParameterMapping.cpp:525