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  {
41  if (hasObjectNameParameter)
42  {
43  defineOptionalProperty<std::string>("ObjectName", "", "Name of IceGrid well-known object");
44  }
45  defineOptionalProperty<MessageTypeT>("MinimumLoggingLevel", armarx::MessageTypeT::UNDEFINED, "Local logging level only for this component", PropertyDefinitionBase::eModifiable);
46  defineOptionalProperty<bool>("EnableProfiling", false, "enable profiler which is used for logging performance events", PropertyDefinitionBase::eModifiable);
47  }
48 
49 
51  {
54  configName = "";
55  configDomain = "ArmarX";
56  createdByComponentCreate = false;
57  }
58 
59 
61  {
63  return configDomain;
64  }
65 
66 
68  {
70  return configName;
71  }
72 
73 
75  {
77  return configDomain + "." + configName;
78  }
79 
80 
81  void Component::initializeProperties(const std::string& configName, const Ice::PropertiesPtr& properties, const std::string& configDomain)
82  {
84  ARMARX_DEBUG << "Initializing properties.";
85 
86  this->configDomain = configDomain;
87  this->configName = configName;
88 
89  // set default object name to configName
90  setName(this->configName);
91 
92  // set properties
93  setIceProperties(properties);
95  }
96 
97 
98  void
100  {
101  ARMARX_TRACE;
102  ARMARX_DEBUG << "Preparing for onInitComponent().";
103 
104  // Update all referenced properties.
106 
107  // Subscribe to all configured topics.
108  for (const std::string& offered_topic_name : getSubscribedTopicNames())
109  {
110  ARMARX_DEBUG << "Subscribing to topic `" << offered_topic_name << "`.";
111  usingTopic(offered_topic_name);
112  }
113 
114  // Signal dependencies on configured components.
115  for (const std::string& proxy_name : getComponentProxyNames())
116  {
117  ARMARX_DEBUG << "Using component proxy `" << proxy_name << "`.";
118  usingProxy(proxy_name);
119  }
120 
121  // Offer all configured topics.
122  for (const std::string& proxy_name : getTopicProxyNames())
123  {
124  ARMARX_DEBUG << "Offering topic `" << proxy_name << "`.";
125  offeringTopic(proxy_name);
126  }
127  }
128 
129 
130  void
132  {
133  ARMARX_TRACE;
134  ARMARX_DEBUG << "Preparing for onConnectComponent().";
135  // Update all referenced proxy properties.
137  }
138 
139 
140  void Component::addPropertyUser(const PropertyUserPtr& subPropertyUser)
141  {
142  ARMARX_TRACE;
143  additionalPropertyUsers.push_back(subPropertyUser);
144  }
145 
146 
147  std::vector<PropertyUserPtr> Component::getAdditionalPropertyUsers() const
148  {
149  ARMARX_TRACE;
150  return additionalPropertyUsers;
151  }
152 
153 
154  void Component::offeringTopicFromProperty(const std::string& propertyName)
155  {
156  offeringTopic(getProperty<std::string>(propertyName));
157  }
158 
159 
160  void Component::usingTopicFromProperty(const std::string& propertyName, bool orderedPublishing)
161  {
162  ARMARX_TRACE;
163  usingTopic(getProperty<std::string>(propertyName), orderedPublishing);
164  }
165 
166 
167  bool Component::usingProxyFromProperty(const std::string& propertyName, const std::string& endpoints)
168  {
169  ARMARX_TRACE;
170  return usingProxy(getProperty<std::string>(propertyName), endpoints);
171  }
172 
173 
175  {
176  ARMARX_TRACE;
177  this->createdByComponentCreate = true;
178  }
179 
180 
182  {
183  ARMARX_DEBUG << "call postCreatePropertyDefinitions for all plugins...";
184  foreach_plugin([&](const auto & typeidx, const auto & name, const auto & plugin)
185  {
186  if (auto ptr = dynamic_cast<ComponentPlugin*>(plugin))
187  {
188  ARMARX_DEBUG << "plugin '" << name
189  << "' (" << GetTypeString(typeidx)
190  << ") postCreatePropertyDefinitions...";
191  ptr->postCreatePropertyDefinitions(props);
192  ARMARX_DEBUG << "plugin '" << name
193  << "' (" << GetTypeString(typeidx)
194  << ") postCreatePropertyDefinitions...done!";
195  }
196  else
197  {
198  ARMARX_DEBUG << "plugin '" << name
199  << "' (" << GetTypeString(typeidx)
200  << ") is no ComponentPlugin (not calling postCreatePropertyDefinitions)";
201  }
202  }, __LINE__, __FILE__, BOOST_CURRENT_FUNCTION);
203  ARMARX_DEBUG << "call postCreatePropertyDefinitions for all plugins...done!";
204  }
205 
206 
208  {
209  ARMARX_TRACE;
211  }
212 
213 
214  void Component::icePropertiesUpdated(const std::set<std::string>& changedProperties)
215  {
216  ARMARX_TRACE;
217  // check if properties have been initialized
218  if (getConfigDomain().empty() || getConfigName().empty())
219  {
220  return;
221  }
222 
223  // set the logging behavior for this component
224  if (changedProperties.count("MinimumLoggingLevel") && getProperty<MessageTypeT>("MinimumLoggingLevel").isSet())
225  {
227  getProperty<MessageTypeT>("MinimumLoggingLevel").getValue());
228  }
229 
230  // get well-known name of this component instance
231  // (default is the ManagedIceObject name itself)
232  if (changedProperties.count("ObjectName") && getProperty<std::string>("ObjectName").isSet())
233  {
234  setName(getProperty<std::string>("ObjectName").getValue());
235  }
236 
237  // set logging tag, may be overwritten by implementer
238  setTag(getName());
239 
240  // enable Profiling
241  // enable via global ArmarX.Profiling property and disable with specific one?
242  if (changedProperties.count("EnableProfiling"))
243  {
244  enableProfiler(getProperty<bool>("EnableProfiling").getValue());
245  }
246 
247  //only update user properties after the object was initialized
248  if (getState() > eManagedIceObjectInitialized)
249  {
250  componentPropertiesUpdated(changedProperties);
251  ARMARX_DEBUG << "call componentPropertiesUpdated for all plugins...";
252  foreach_plugin([&](const auto & typeidx, const auto & name, const auto & plugin)
253  {
254  if (auto ptr = dynamic_cast<ComponentPlugin*>(plugin))
255  {
256  ARMARX_DEBUG << "plugin '" << name
257  << "' (" << GetTypeString(typeidx)
258  << ") componentPropertiesUpdated...";
259  ptr->componentPropertiesUpdated(changedProperties);
260  ARMARX_DEBUG << "plugin '" << name
261  << "' (" << GetTypeString(typeidx)
262  << ") componentPropertiesUpdated...done!";
263  }
264  else
265  {
266  ARMARX_DEBUG << "plugin '" << name
267  << "' (" << GetTypeString(typeidx.name())
268  << ") is no ComponentPlugin (not calling componentPropertiesUpdated)";
269  }
270  }, __LINE__, __FILE__, BOOST_CURRENT_FUNCTION);
271  ARMARX_DEBUG << "call componentPropertiesUpdated for all plugins...done!";
272  }
273  }
274 
275 
276  void Component::componentPropertiesUpdated(const std::set<std::string>& changedProperties)
277  {
278  ARMARX_TRACE;
279  (void) changedProperties;
280  }
281 
282 }
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:99
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:140
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:276
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:154
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:167
armarx::Component::injectPropertyDefinitions
void injectPropertyDefinitions(PropertyDefinitionsPtr &props) override
Definition: Component.cpp:181
armarx::Component::createPropertyDefinitions
PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: Component.cpp:207
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_TRACE
#define ARMARX_TRACE
Definition: trace.h:69
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:147
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:160
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:60
armarx::GetTypeString
std::string GetTypeString(const std::type_info &tinf, bool withoutNamespaceSpecifier=false)
Definition: GetTypeString.h:36
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:248
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:74
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:81
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:174
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:131
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:57
armarx::Component::Component
Component()
Protected default constructor. Used for virtual inheritance. Use createManagedIceObject() instead.
Definition: Component.cpp:50
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:67
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