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