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