Component.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 Kai Welke (welke 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 
25 #include "Component.h"
26 
30 #include <ArmarXCore/interface/core/ManagedIceObjectDefinitions.h>
31 
32 namespace armarx
33 {
34 
36  bool hasObjectNameParameter) :
38  {
40  if (hasObjectNameParameter)
41  {
42  defineOptionalProperty<std::string>(
43  "ObjectName", "", "Name of IceGrid well-known object");
44  }
45  defineOptionalProperty<MessageTypeT>("MinimumLoggingLevel",
47  "Local logging level only for this component",
49  defineOptionalProperty<bool>("EnableProfiling",
50  false,
51  "enable profiler which is used for logging performance events",
53  }
54 
56  {
59  configName = "";
60  configDomain = "ArmarX";
61  createdByComponentCreate = false;
62  }
63 
64  std::string
66  {
68  return configDomain;
69  }
70 
71  std::string
73  {
75  return configName;
76  }
77 
78  std::string
80  {
82  return configDomain + "." + configName;
83  }
84 
85  void
86  Component::initializeProperties(const std::string& configName,
87  const Ice::PropertiesPtr& properties,
88  const std::string& configDomain)
89  {
91  ARMARX_DEBUG << "Initializing properties.";
92 
93  this->configDomain = configDomain;
94  this->configName = configName;
95 
96  // set default object name to configName
97  setName(this->configName);
98 
99  // set properties
100  setIceProperties(properties);
102  }
103 
104  void
106  {
107  ARMARX_TRACE;
108  ARMARX_DEBUG << "Preparing for onInitComponent().";
109 
110  // Update all referenced properties.
112 
113  // Subscribe to all configured topics.
114  for (const std::string& offered_topic_name : getSubscribedTopicNames())
115  {
116  ARMARX_DEBUG << "Subscribing to topic `" << offered_topic_name << "`.";
117  usingTopic(offered_topic_name);
118  }
119 
120  // Signal dependencies on configured components.
121  for (const std::string& proxy_name : getComponentProxyNames())
122  {
123  ARMARX_DEBUG << "Using component proxy `" << proxy_name << "`.";
124  usingProxy(proxy_name);
125  }
126 
127  // Offer all configured topics.
128  for (const std::string& proxy_name : getTopicProxyNames())
129  {
130  ARMARX_DEBUG << "Offering topic `" << proxy_name << "`.";
131  offeringTopic(proxy_name);
132  }
133  }
134 
135  void
137  {
138  ARMARX_TRACE;
139  ARMARX_DEBUG << "Preparing for onConnectComponent().";
140  // Update all referenced proxy properties.
142  }
143 
144  void
146  {
147  ARMARX_TRACE;
148  additionalPropertyUsers.push_back(subPropertyUser);
149  }
150 
151  std::vector<PropertyUserPtr>
153  {
154  ARMARX_TRACE;
155  return additionalPropertyUsers;
156  }
157 
158  void
159  Component::offeringTopicFromProperty(const std::string& propertyName)
160  {
161  offeringTopic(getProperty<std::string>(propertyName));
162  }
163 
164  void
165  Component::usingTopicFromProperty(const std::string& propertyName, bool orderedPublishing)
166  {
167  ARMARX_TRACE;
168  usingTopic(getProperty<std::string>(propertyName), orderedPublishing);
169  }
170 
171  bool
172  Component::usingProxyFromProperty(const std::string& propertyName, const std::string& endpoints)
173  {
174  ARMARX_TRACE;
175  return usingProxy(getProperty<std::string>(propertyName), endpoints);
176  }
177 
178  void
180  {
181  ARMARX_TRACE;
182  this->createdByComponentCreate = true;
183  }
184 
185  void
187  {
188  ARMARX_DEBUG << "call postCreatePropertyDefinitions for all plugins...";
189  foreach_plugin(
190  [&](const auto& typeidx, const auto& name, const auto& plugin)
191  {
192  if (auto ptr = dynamic_cast<ComponentPlugin*>(plugin))
193  {
194  ARMARX_DEBUG << "plugin '" << name << "' (" << GetTypeString(typeidx)
195  << ") postCreatePropertyDefinitions...";
196  ptr->postCreatePropertyDefinitions(props);
197  ARMARX_DEBUG << "plugin '" << name << "' (" << GetTypeString(typeidx)
198  << ") postCreatePropertyDefinitions...done!";
199  }
200  else
201  {
203  << "plugin '" << name << "' (" << GetTypeString(typeidx)
204  << ") is no ComponentPlugin (not calling postCreatePropertyDefinitions)";
205  }
206  },
207  __LINE__,
208  __FILE__,
209  BOOST_CURRENT_FUNCTION);
210  ARMARX_DEBUG << "call postCreatePropertyDefinitions for all plugins...done!";
211  }
212 
215  {
216  ARMARX_TRACE;
218  }
219 
220  void
221  Component::icePropertiesUpdated(const std::set<std::string>& changedProperties)
222  {
223  ARMARX_TRACE;
224  // check if properties have been initialized
225  if (getConfigDomain().empty() || getConfigName().empty())
226  {
227  return;
228  }
229 
230  // set the logging behavior for this component
231  if (changedProperties.count("MinimumLoggingLevel") &&
232  getProperty<MessageTypeT>("MinimumLoggingLevel").isSet())
233  {
235  getProperty<MessageTypeT>("MinimumLoggingLevel").getValue());
236  }
237 
238  // get well-known name of this component instance
239  // (default is the ManagedIceObject name itself)
240  if (changedProperties.count("ObjectName") && getProperty<std::string>("ObjectName").isSet())
241  {
242  setName(getProperty<std::string>("ObjectName").getValue());
243  }
244 
245  // set logging tag, may be overwritten by implementer
246  setTag(getName());
247 
248  // enable Profiling
249  // enable via global ArmarX.Profiling property and disable with specific one?
250  if (changedProperties.count("EnableProfiling"))
251  {
252  enableProfiler(getProperty<bool>("EnableProfiling").getValue());
253  }
254 
255  //only update user properties after the object was initialized
256  if (getState() > eManagedIceObjectInitialized)
257  {
258  componentPropertiesUpdated(changedProperties);
259  ARMARX_DEBUG << "call componentPropertiesUpdated for all plugins...";
260  foreach_plugin(
261  [&](const auto& typeidx, const auto& name, const auto& plugin)
262  {
263  if (auto ptr = dynamic_cast<ComponentPlugin*>(plugin))
264  {
265  ARMARX_DEBUG << "plugin '" << name << "' (" << GetTypeString(typeidx)
266  << ") componentPropertiesUpdated...";
267  ptr->componentPropertiesUpdated(changedProperties);
268  ARMARX_DEBUG << "plugin '" << name << "' (" << GetTypeString(typeidx)
269  << ") componentPropertiesUpdated...done!";
270  }
271  else
272  {
274  << "plugin '" << name << "' (" << GetTypeString(typeidx.name())
275  << ") is no ComponentPlugin (not calling componentPropertiesUpdated)";
276  }
277  },
278  __LINE__,
279  __FILE__,
280  BOOST_CURRENT_FUNCTION);
281  ARMARX_DEBUG << "call componentPropertiesUpdated for all plugins...done!";
282  }
283  }
284 
285  void
286  Component::componentPropertiesUpdated(const std::set<std::string>& changedProperties)
287  {
288  ARMARX_TRACE;
289  (void)changedProperties;
290  }
291 
292 } // namespace armarx
armarx::ManagedIceObject::setName
void setName(std::string name)
Override name of well-known object.
Definition: ManagedIceObject.cpp:445
armarx::ManagedIceObject::getIceManager
IceManagerPtr getIceManager() const
Returns the IceManager.
Definition: ManagedIceObject.cpp:366
armarx::Component::preOnInitComponent
virtual void preOnInitComponent() override
Definition: Component.cpp:105
armarx::Component::addPropertyUser
void addPropertyUser(const PropertyUserPtr &subPropertyUser)
Add additional property users here that should show up in the application help text.
Definition: Component.cpp:145
armarx::PropertyUser::updateProxies
void updateProxies(IceManagerPtr)
Definition: PropertyUser.cpp:157
armarx::MessageTypeT::UNDEFINED
@ UNDEFINED
armarx::Component::componentPropertiesUpdated
virtual void componentPropertiesUpdated(const std::set< std::string > &changedProperties)
Implement this function if you would like to react to changes in the properties.
Definition: Component.cpp:286
armarx::ManagedIceObject::getState
int getState() const
Retrieve current state of the ManagedIceObject.
Definition: ManagedIceObject.cpp:769
armarx::Component::offeringTopicFromProperty
void offeringTopicFromProperty(const std::string &propertyName)
Offer a topic whose name is specified by the given property.
Definition: Component.cpp:159
armarx::Component::usingProxyFromProperty
bool usingProxyFromProperty(const std::string &propertyName, const std::string &endpoints="")
Use a proxy whose name is specified by the given property.
Definition: Component.cpp:172
armarx::Component::injectPropertyDefinitions
void injectPropertyDefinitions(PropertyDefinitionsPtr &props) override
Definition: Component.cpp:186
armarx::Component::createPropertyDefinitions
PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: Component.cpp:214
cxxopts::empty
bool empty(const std::string &s)
Definition: cxxopts.hpp:234
IceInternal::Handle<::Ice::Properties >
armarx::Component::icePropertiesInitialized
virtual void icePropertiesInitialized()
Definition: Component.h:308
armarx::PropertyUser::updateProperties
void updateProperties()
Definition: PropertyUser.cpp:151
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:77
armarx::PropertyUser::getComponentProxyNames
std::vector< std::string > getComponentProxyNames()
Definition: PropertyUser.cpp:163
armarx::PropertyUser::getTopicProxyNames
std::vector< std::string > getTopicProxyNames()
Definition: PropertyUser.cpp:177
Property.h
Ice::createProperties
Ice::PropertiesPtr createProperties()
armarx::ComponentPlugin
Definition: ComponentPlugin.h:37
PropertyUser.h
armarx::Component::getAdditionalPropertyUsers
std::vector< PropertyUserPtr > getAdditionalPropertyUsers() const
Definition: Component.cpp:152
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:184
armarx::Component::usingTopicFromProperty
void usingTopicFromProperty(const std::string &propertyName, bool orderedPublishing=false)
Use a topic whose name is specified by the given property.
Definition: Component.cpp:165
armarx::Logging::setLocalMinimumLoggingLevel
void setLocalMinimumLoggingLevel(MessageTypeT level)
With setLocalMinimumLoggingLevel the minimum verbosity-level of log-messages can be set.
Definition: Logging.cpp:66
armarx::PropertyDefinitionContainer
PropertyDefinitionContainer.
Definition: PropertyDefinitionContainer.h:53
armarx::Component::getConfigDomain
std::string getConfigDomain()
Retrieve config domain for this component as set in constructor.
Definition: Component.cpp:65
armarx::GetTypeString
std::string GetTypeString(const std::type_info &tinf, bool withoutNamespaceSpecifier=false)
Definition: GetTypeString.h:38
Component.h
armarx::control::common::getValue
T getValue(nlohmann::json &userConfig, nlohmann::json &defaultConfig, const std::string &entryName)
Definition: utils.h:71
armarx::ManagedIceObject::usingTopic
void usingTopic(const std::string &name, bool orderedPublishing=false)
Registers a proxy for subscription after initialization.
Definition: ManagedIceObject.cpp:254
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:79
armarx::ComponentPropertyDefinitions
Default component property definition container.
Definition: Component.h:69
armarx::ManagedIceObject::offeringTopic
void offeringTopic(const std::string &name)
Registers a topic for retrival after initialization.
Definition: ManagedIceObject.cpp:300
IceUtil::Handle
Definition: forward_declarations.h:30
armarx::Component::initializeProperties
void initializeProperties(const std::string &configName, Ice::PropertiesPtr const &properties, const std::string &configDomain)
initializes the properties of this component.
Definition: Component.cpp:86
armarx::Component::forceComponentCreatedByComponentCreateFunc
void forceComponentCreatedByComponentCreateFunc()
forces the flag to be set to true that the object instance was created by the Component::create funct...
Definition: Component.cpp:179
armarx::ManagedIceObject::enableProfiler
void enableProfiler(bool enable)
setProfiler allows setting ManagedIceObject::profiler to a new instance (if the new instance is actua...
Definition: ManagedIceObject.cpp:378
armarx::ComponentPropertyDefinitions::ComponentPropertyDefinitions
ComponentPropertyDefinitions(std::string prefix, bool hasObjectNameParameter=true)
Definition: Component.cpp:35
armarx::Component::preOnConnectComponent
virtual void preOnConnectComponent() override
Definition: Component.cpp:136
armarx::ManagedIceObject::getName
std::string getName() const
Retrieve name of object.
Definition: ManagedIceObject.cpp:108
ComponentPlugin.h
armarx::Logging::setTag
void setTag(const LogTag &tag)
Definition: Logging.cpp:54
armarx::PropertyDefinitionBase::eModifiable
@ eModifiable
Definition: PropertyDefinitionInterface.h:57
armarx::Component::Component
Component()
Protected default constructor. Used for virtual inheritance. Use createManagedIceObject() instead.
Definition: Component.cpp:55
armarx::PropertyUser::setIceProperties
virtual void setIceProperties(Ice::PropertiesPtr properties)
Sets the Ice properties.
Definition: PropertyUser.cpp:87
armarx::ManagedIceObject::usingProxy
bool usingProxy(const std::string &name, const std::string &endpoints="")
Registers a proxy for retrieval after initialization and adds it to the dependency list.
Definition: ManagedIceObject.cpp:154
armarx::Component::getConfigName
std::string getConfigName()
Retrieve config name for this component as set in constructor.
Definition: Component.cpp:72
armarx::PropertyUser::getSubscribedTopicNames
std::vector< std::string > getSubscribedTopicNames()
Definition: PropertyUser.cpp:191
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27