PropertyDefinitionContainer.cpp
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::core
19  * @author Mirko Waechter (waechter at kit dot edu)
20  * @date 2016
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 
27 
28 
29 // STD/STL
30 #include <map>
31 #include <string>
32 
33 #include <SimoxUtility/algorithm/string/string_tools.h>
34 
35 // Ice
36 #include <Ice/Properties.h>
37 
38 // ArmarXCore
47 #include <ArmarXCore/util/check.h>
48 
49 namespace armarx
50 {
51 
52 
54  {
55  setDescription(prefix + " properties");
56 
57  this->prefix = prefix + ".";
58  }
59 
61  {
62  // Delete property definitions.
63  for (auto it = definitions.begin(); it != definitions.end(); ++it)
64  {
65  delete it->second;
66  }
67 
68  // Delete proxy definitions,
69  for (auto it = proxies.begin(); it != proxies.end(); ++it)
70  {
71  delete it->second;
72  }
73  }
74 
75  void
77  {
78  if (properties)
79  {
80  for (auto& [name, definition] : definitions)
81  {
82  definition->writeValueToSetter(getPrefix(), properties);
83  }
84  }
85  }
86 
87  void
89  {
90  if (properties)
91  {
92  for (auto& [name, definition] : proxies)
93  {
94  ARMARX_CHECK(definitions.find(name) != definitions.end())
95  << "`" << name << "` must be in definitions and proxies";
96 
97  // Get proxy name, fetch proxy and write to passed setter.
98  const std::string proxy_name =
99  getDefintion<std::string>(name).getValue(getPrefix(), properties);
100  definition->write_proxy_to_setter(ice_manager, proxy_name);
101  }
102  }
103  }
104 
105  std::map<std::string, ProxyPropertyDefinitionBase*>
107  {
108  return proxies;
109  }
110 
111  std::vector<std::string>
113  {
114  return subscribedTopics;
115  }
116 
119  {
120  return properties;
121  }
122 
123  void
125  {
126  this->properties = properties;
127  }
128 
131  {
132  // check definition existance
133  if (definitions.find(name) == definitions.end())
134  {
135  throw armarx::LocalException(name + " property is not defined.");
136  }
137 
138  PropertyDefinitionBase* def = definitions[name];
139  return def;
140  }
141 
142  std::string
144  {
145  formatter.setPrefix(prefix);
146  std::string definitionStr;
147 
148  DefinitionContainer::iterator it = definitions.begin();
149  Ice::PropertyDict propertiesMap;
150 
151  if (properties)
152  {
153  propertiesMap = properties->getPropertiesForPrefix(getPrefix());
154  }
155  while (it != definitions.end())
156  {
157  definitionStr += it->second->toString(formatter, getValue(it->first));
158  propertiesMap.erase(it->first);
159  it++;
160  }
161 
162  if (properties)
163  {
164  formatter.setPrefix("");
165  }
166 
167  Ice::StringSeq values;
168  for (Ice::PropertyDict::iterator property = propertiesMap.begin();
169  property != propertiesMap.end();
170  property++)
171  {
172  definitionStr += formatter.formatDefinition(
173  property->first, "", "", "", "", "", "", "", values, property->second);
174  }
175 
176  return definitionStr;
177  }
178 
179  std::string
181  {
182  return description;
183  }
184 
185  void
186  PropertyDefinitionContainer::setDescription(const std::string& description)
187  {
188  this->description = description;
189  }
190 
191  void
193  {
194  this->prefix = prefix;
195  }
196 
197  std::string
199  {
200  return prefix;
201  }
202 
203  bool
205  {
206  Ice::PropertyDict selectedProperties = properties->getPropertiesForPrefix(getPrefix());
207  return (selectedProperties.count(name) > 0);
208  }
209 
210  std::string
211  PropertyDefinitionContainer::getValue(const std::string& name)
212  {
213  if (properties)
214  {
215  return properties->getPropertiesForPrefix(getPrefix())[name];
216  }
217  else
218  {
219  return "";
220  }
221  }
222 
223  std::map<std::string, std::string>
224  PropertyDefinitionContainer::getPropertyValues(const std::string& prefix) const
225  {
226  std::map<std::string, std::string> result;
227  for (const auto& elem : definitions)
228  {
229  std::string value;
230  if (properties)
231  {
232  value = properties->getProperty(this->prefix + elem.first);
233  }
234  else
235  {
236  ARMARX_INFO << " properties NULL";
237  }
238  if (value.empty())
239  {
240  PropertyDefinitionBase* def = elem.second;
241  value = def->getDefaultAsString();
242  // PropertyDefinition<std::string>* def = dynamic_cast<PropertyDefinition<std::string>*>(elem.second);
243  // result[elem.first] = Property<std::string>(
244  // *def,
245  // prefix,
246  // properties).getValue();
247  }
248 
249  result[prefix + elem.first] = value;
250  }
251  return result;
252  }
253 
254  std::string
256  long size,
257  std::string delim)
258  {
259  std::stringstream ss;
260  ss << description << "\n";
261  ss << "(Format: Sequence of ";
262  if (size < 0)
263  {
264  ss << "n";
265  }
266  else
267  {
268  ss << size;
269  }
270  ss << " numbers delimited by '" << delim << "'.)";
271 
272  return ss.str();
273  }
274 
275  std::string
276  PropertyDefinitionContainer_ice_class_name(std::string const& full_type_name)
277  {
278  std::vector<std::string> splt = simox::alg::split(full_type_name, ":");
279  std::string class_name = splt.at(splt.size() - 1);
280 
281  if (class_name == "ComponentInterface" and splt.size() >= 2)
282  {
283  /*
284  * The specific interface of a modern class with name like:
285  * package::components::name::ComponentInterface.
286  * The component's name is the last namespace, i.e. the item before the class name.
287  */
288  class_name = splt.at(splt.size() - 2);
289  }
290  else
291  {
292  // Remove common suffix patterns.
293  std::vector<std::string> trims = {"InterfaceListener",
294  "_interface_listener",
295  "Interface",
296  "_interface",
297  "Listener",
298  "_listener"};
299  for (const std::string& suffix : trims)
300  {
301  if (ends_with(class_name, suffix))
302  {
303  class_name.erase(class_name.length() - suffix.length());
304  break;
305  }
306  }
307  }
308 
309  return class_name;
310  }
311 
312 
313 } // namespace armarx
armarx::PropertyDefinitionFormatter::setPrefix
virtual void setPrefix(std::string prefix)
Definition: PropertyDefinitionFormatter.h:72
armarx::PropertyDefinitionContainer::setPrefix
void setPrefix(std::string prefix)
Definition: PropertyDefinitionContainer.cpp:192
armarx::PropertyDefinitionContainer::getDescription
std::string getDescription() const
Returns the detailed description of the property user.
Definition: PropertyDefinitionContainer.cpp:180
armarx::PropertyDefinitionContainer::getProperties
Ice::PropertiesPtr getProperties()
Definition: PropertyDefinitionContainer.cpp:118
armarx::PropertyDefinitionBase
Common interface of any property definition.
Definition: PropertyDefinitionInterface.h:51
armarx::PropertyDefinitionContainer::setProperties
void setProperties(Ice::PropertiesPtr properties)
Definition: PropertyDefinitionContainer.cpp:124
check.h
armarx::PropertyDefinitionContainer::setDescription
void setDescription(const std::string &description)
Sets the detailed description of the property user.
Definition: PropertyDefinitionContainer.cpp:186
armarx::PropertyDefinitionContainer::writeValues
void writeValues()
Definition: PropertyDefinitionContainer.cpp:76
armarx::PropertyDefinitionContainer::prefix
std::string prefix
Prefix of the properties such as namespace, domain, component name, etc.
Definition: PropertyDefinitionContainer.h:345
InvalidPropertyValueException.h
ProsthesisInterface.values
values
Definition: ProsthesisInterface.py:190
armarx::PropertyDefinitionContainer::eigenVectorPropertyDescription
static std::string eigenVectorPropertyDescription(const std::string &description, long size, std::string delim)
Definition: PropertyDefinitionContainer.cpp:255
armarx::PropertyDefinitionContainer::subscribedTopics
std::vector< std::string > subscribedTopics
Definition: PropertyDefinitionContainer.h:339
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
IceInternal::Handle<::Ice::Properties >
armarx::PropertyDefinitionContainer::getValue
std::string getValue(const std::string &name)
Definition: PropertyDefinitionContainer.cpp:211
armarx::PropertyDefinitionContainer::description
std::string description
Property User description.
Definition: PropertyDefinitionContainer.h:350
ValueRangeExceededException.h
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:855
Property.h
armarx::PropertyDefinitionContainer::definitions
DefinitionContainer definitions
Property definitions container.
Definition: PropertyDefinitionContainer.h:335
armarx::PropertyDefinitionBase::getDefaultAsString
virtual std::string getDefaultAsString()=0
armarx::PropertyDefinitionContainer::getProxyDefinitions
std::map< std::string, ProxyPropertyDefinitionBase * > getProxyDefinitions()
Definition: PropertyDefinitionContainer.cpp:106
armarx::PropertyDefinitionContainer::proxies
std::map< std::string, ProxyPropertyDefinitionBase * > proxies
Definition: PropertyDefinitionContainer.h:337
armarx::PropertyDefinitionContainer::properties
Ice::PropertiesPtr properties
Definition: PropertyDefinitionContainer.h:357
armarx::PropertyDefinitionContainer::getDefinitionBase
PropertyDefinitionBase * getDefinitionBase(const std::string &name)
Definition: PropertyDefinitionContainer.cpp:130
armarx::PropertyDefinitionContainer::getPropertyValues
std::map< std::string, std::string > getPropertyValues(const std::string &prefix="") const
Definition: PropertyDefinitionContainer.cpp:224
MissingRequiredPropertyException.h
armarx::PropertyDefinitionContainer::writeProxyValues
void writeProxyValues(IceManagerPtr)
Definition: PropertyDefinitionContainer.cpp:88
armarx::PropertyDefinitionContainer::getSubscribedTopicDefinitions
std::vector< std::string > getSubscribedTopicDefinitions()
Definition: PropertyDefinitionContainer.cpp:112
UnmappedValueException.h
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
PropertyDefinitionContainer.h
IceUtil::Handle< IceManager >
armarx::PropertyDefinitionFormatter::formatDefinition
virtual std::string formatDefinition(std::string name, std::string description, std::string min, std::string max, std::string default_, std::string casesensitivity, std::string requirement, std::string reged, std::vector< std::string > values, std::string value)=0
Definition: PropertyDefinitionFormatter.cpp:33
armarx::ends_with
bool ends_with(const std::string &haystack, const std::string &needle)
Definition: StringHelpers.cpp:53
armarx::PropertyDefinitionContainer::isPropertySet
bool isPropertySet(const std::string &name)
Definition: PropertyDefinitionContainer.cpp:204
armarx::PropertyDefinitionContainer_ice_class_name
std::string PropertyDefinitionContainer_ice_class_name(std::string const &full_type_name)
Definition: PropertyDefinitionContainer.cpp:276
armarx::PropertyDefinitionContainer::getPrefix
std::string getPrefix()
Definition: PropertyDefinitionContainer.cpp:198
Logging.h
armarx::PropertyDefinitionContainer::~PropertyDefinitionContainer
~PropertyDefinitionContainer() override
Definition: PropertyDefinitionContainer.cpp:60
armarx::PropertyDefinitionContainer::PropertyDefinitionContainer
PropertyDefinitionContainer(const std::string &prefix)
Definition: PropertyDefinitionContainer.cpp:53
armarx::PropertyDefinitionFormatter
PropertyDefinitionFormatter is the base class for all formatters of PropertyDefinitions.
Definition: PropertyDefinitionFormatter.h:38
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
Exception.h
PropertyDefinition.h
armarx::PropertyDefinitionContainer::toString
std::string toString(PropertyDefinitionFormatter &formatter)
Definition: PropertyDefinitionContainer.cpp:143
armarx::split
std::vector< std::string > split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelpers.cpp:38