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
27#include <set>
28#include <string>
29#include <vector>
30
38#include <ArmarXCore/interface/core/ManagedIceObjectDefinitions.h>
40
41namespace armarx
42{
43
45 bool hasObjectNameParameter) :
47 {
49 if (hasObjectNameParameter)
50 {
52 "ObjectName", "", "Name of IceGrid well-known object");
53 }
54 defineOptionalProperty<MessageTypeT>("MinimumLoggingLevel",
56 "Local logging level only for this component",
58 defineOptionalProperty<bool>("EnableProfiling",
59 false,
60 "enable profiler which is used for logging performance events",
62 }
63
65 {
68 configName = "";
69 configDomain = "ArmarX";
70 createdByComponentCreate = false;
71 }
72
73 std::string
75 {
77 return configDomain;
78 }
79
80 std::string
82 {
84 return configName;
85 }
86
87 std::string
89 {
91 return configDomain + "." + configName;
92 }
93
94 void
95 Component::initializeProperties(const std::string& configName,
96 const Ice::PropertiesPtr& properties,
97 const std::string& configDomain)
98 {
100 ARMARX_DEBUG << "Initializing properties.";
101
102 this->configDomain = configDomain;
103 this->configName = configName;
104
105 // set default object name to configName
106 setName(this->configName);
107
108 // set properties
109 setIceProperties(properties);
111 }
112
113 void
115 {
117 ARMARX_DEBUG << "Preparing for onInitComponent().";
118
119 MessageTypeT const currentLoggingLevel = getEffectiveLoggingLevel();
120 if (currentLoggingLevel == MessageTypeT::DEBUG or
121 currentLoggingLevel == MessageTypeT::VERBOSE)
122 {
124 << "Logging level of this component is very low (Debug or Verbose). For debugging "
125 "and development purposes, this warning can be ignored and only serves as a "
126 "reminder. A low logging level can have a performance impact, so reset the "
127 "logging level to at least Info after it is not used anymore, and especially "
128 "for stable or production workspaces.";
129 }
130
131 // Update all referenced properties.
133
134 // Subscribe to all configured topics.
135 for (const std::string& offered_topic_name : getSubscribedTopicNames())
136 {
137 ARMARX_DEBUG << "Subscribing to topic `" << offered_topic_name << "`.";
138 usingTopic(offered_topic_name);
139 }
140
141 // Signal dependencies on configured components.
142 for (const std::string& proxy_name : getComponentProxyNames())
143 {
144 ARMARX_DEBUG << "Using component proxy `" << proxy_name << "`.";
145 usingProxy(proxy_name);
146 }
147
148 // Offer all configured topics.
149 for (const std::string& proxy_name : getTopicProxyNames())
150 {
151 ARMARX_DEBUG << "Offering topic `" << proxy_name << "`.";
152 offeringTopic(proxy_name);
153 }
154 }
155
156 void
158 {
160 ARMARX_DEBUG << "Preparing for onConnectComponent().";
161 // Update all referenced proxy properties.
163 }
164
165 void
167 {
169 additionalPropertyUsers.push_back(subPropertyUser);
170 }
171
172 std::vector<PropertyUserPtr>
174 {
176 return additionalPropertyUsers;
177 }
178
179 void
180 Component::offeringTopicFromProperty(const std::string& propertyName)
181 {
183 }
184
185 void
186 Component::usingTopicFromProperty(const std::string& propertyName, bool orderedPublishing)
187 {
189 usingTopic(getProperty<std::string>(propertyName), orderedPublishing);
190 }
191
192 bool
193 Component::usingProxyFromProperty(const std::string& propertyName, const std::string& endpoints)
194 {
196 return usingProxy(getProperty<std::string>(propertyName), endpoints);
197 }
198
199 void
201 {
203 this->createdByComponentCreate = true;
204 }
205
206 void
208 {
209 ARMARX_DEBUG << "call postCreatePropertyDefinitions for all plugins...";
210 foreach_plugin(
211 [&](const auto& typeidx, const auto& name, const auto& plugin)
212 {
213 if (auto ptr = dynamic_cast<ComponentPlugin*>(plugin))
214 {
215 ARMARX_DEBUG << "plugin '" << name << "' (" << GetTypeString(typeidx)
216 << ") postCreatePropertyDefinitions...";
217 ptr->postCreatePropertyDefinitions(props);
218 ARMARX_DEBUG << "plugin '" << name << "' (" << GetTypeString(typeidx)
219 << ") postCreatePropertyDefinitions...done!";
220 }
221 else
222 {
224 << "plugin '" << name << "' (" << GetTypeString(typeidx)
225 << ") is no ComponentPlugin (not calling postCreatePropertyDefinitions)";
226 }
227 },
228 __LINE__,
229 __FILE__,
230 __PRETTY_FUNCTION__);
231 ARMARX_DEBUG << "call postCreatePropertyDefinitions for all plugins...done!";
232 }
233
240
241 void
242 Component::icePropertiesUpdated(const std::set<std::string>& changedProperties)
243 {
245 // check if properties have been initialized
246 if (getConfigDomain().empty() || getConfigName().empty())
247 {
248 return;
249 }
250
251 // set the logging behavior for this component
252 if (changedProperties.count("MinimumLoggingLevel") &&
253 getProperty<MessageTypeT>("MinimumLoggingLevel").isSet())
254 {
256 getProperty<MessageTypeT>("MinimumLoggingLevel").getValue());
257 }
258
259 // get well-known name of this component instance
260 // (default is the ManagedIceObject name itself)
261 if (changedProperties.count("ObjectName") && getProperty<std::string>("ObjectName").isSet())
262 {
264 }
265
266 // set logging tag, may be overwritten by implementer
267 setTag(getName());
268
269 // enable Profiling
270 // enable via global ArmarX.Profiling property and disable with specific one?
271 if (changedProperties.count("EnableProfiling"))
272 {
273 enableProfiler(getProperty<bool>("EnableProfiling").getValue());
274 }
275
276 //only update user properties after the object was initialized
277 if (getState() > eManagedIceObjectInitialized)
278 {
279 componentPropertiesUpdated(changedProperties);
280 ARMARX_DEBUG << "call componentPropertiesUpdated for all plugins...";
281 foreach_plugin(
282 [&](const auto& typeidx, const auto& name, const auto& plugin)
283 {
284 if (auto ptr = dynamic_cast<ComponentPlugin*>(plugin))
285 {
286 ARMARX_DEBUG << "plugin '" << name << "' (" << GetTypeString(typeidx)
287 << ") componentPropertiesUpdated...";
288 ptr->componentPropertiesUpdated(changedProperties);
289 ARMARX_DEBUG << "plugin '" << name << "' (" << GetTypeString(typeidx)
290 << ") componentPropertiesUpdated...done!";
291 }
292 else
293 {
295 << "plugin '" << name << "' (" << GetTypeString(typeidx.name())
296 << ") is no ComponentPlugin (not calling componentPropertiesUpdated)";
297 }
298 },
299 __LINE__,
300 __FILE__,
301 __PRETTY_FUNCTION__);
302 ARMARX_DEBUG << "call componentPropertiesUpdated for all plugins...done!";
303 }
304 }
305
306 void
307 Component::componentPropertiesUpdated(const std::set<std::string>& changedProperties)
308 {
310 (void)changedProperties;
311 }
312
313} // namespace armarx
Default component property definition container.
Definition Component.h:70
ComponentPropertyDefinitions(std::string prefix, bool hasObjectNameParameter=true)
Definition Component.cpp:44
Component()
Protected default constructor. Used for virtual inheritance. Use createManagedIceObject() instead.
Definition Component.cpp:64
std::string getConfigName()
Retrieve config name for this component as set in constructor.
Definition Component.cpp:81
void forceComponentCreatedByComponentCreateFunc()
forces the flag to be set to true that the object instance was created by the Component::create funct...
virtual void componentPropertiesUpdated(const std::set< std::string > &changedProperties)
Implement this function if you would like to react to changes in the properties.
std::vector< PropertyUserPtr > getAdditionalPropertyUsers() const
std::string getConfigDomain()
Retrieve config domain for this component as set in constructor.
Definition Component.cpp:74
bool usingProxyFromProperty(const std::string &propertyName, const std::string &endpoints="")
Use a proxy whose name is specified by the given property.
void offeringTopicFromProperty(const std::string &propertyName)
Offer a topic whose name is specified by the given property.
virtual void preOnInitComponent() override
virtual void preOnConnectComponent() override
void initializeProperties(const std::string &configName, Ice::PropertiesPtr const &properties, const std::string &configDomain)
initializes the properties of this component.
Definition Component.cpp:95
void usingTopicFromProperty(const std::string &propertyName, bool orderedPublishing=false)
Use a topic whose name is specified by the given property.
virtual void icePropertiesInitialized()
Definition Component.h:308
void addPropertyUser(const PropertyUserPtr &subPropertyUser)
Add additional property users here that should show up in the application help text.
PropertyDefinitionsPtr createPropertyDefinitions() override
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition Component.cpp:88
Property< PropertyType > getProperty(const std::string &name)
void injectPropertyDefinitions(PropertyDefinitionsPtr &props) override
void setTag(const LogTag &tag)
Definition Logging.cpp:54
void setLocalMinimumLoggingLevel(MessageTypeT level)
With setLocalMinimumLoggingLevel the minimum verbosity-level of log-messages can be set.
Definition Logging.cpp:66
MessageTypeT getEffectiveLoggingLevel() const
Definition Logging.cpp:130
void offeringTopic(const std::string &name)
Registers a topic for retrival after initialization.
bool usingProxy(const std::string &name, const std::string &endpoints="")
Registers a proxy for retrieval after initialization and adds it to the dependency list.
void usingTopic(const std::string &name, bool orderedPublishing=false)
Registers a proxy for subscription after initialization.
std::string getName() const
Retrieve name of object.
IceManagerPtr getIceManager() const
Returns the IceManager.
int getState() const
Retrieve current state of the ManagedIceObject.
void setName(std::string name)
Override name of well-known object.
void enableProfiler(bool enable)
setProfiler allows setting ManagedIceObject::profiler to a new instance (if the new instance is actua...
std::string prefix
Prefix of the properties such as namespace, domain, component name, etc.
PropertyDefinition< PropertyType > & defineOptionalProperty(const std::string &name, PropertyType defaultValue, const std::string &description="", PropertyDefinitionBase::PropertyConstness constness=PropertyDefinitionBase::eConstant)
std::vector< std::string > getComponentProxyNames()
void updateProxies(IceManagerPtr)
virtual void setIceProperties(Ice::PropertiesPtr properties)
Sets the Ice properties.
std::vector< std::string > getSubscribedTopicNames()
std::vector< std::string > getTopicProxyNames()
#define ARMARX_DEBUG
The logging level for output that is only interesting while debugging.
Definition Logging.h:182
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:191
Ice::PropertiesPtr createProperties()
::IceInternal::Handle<::Ice::Properties > PropertiesPtr
T getValue(nlohmann::json &userConfig, nlohmann::json &defaultConfig, const std::string &entryName)
Definition utils.h:80
client::plugins::ComponentPlugin ComponentPlugin
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::string GetTypeString(const std::type_info &tinf, bool withoutNamespaceSpecifier=false)
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
IceUtil::Handle< class PropertyUser > PropertyUserPtr
PropertyUser smart pointer type.
MessageTypeT
Definition LogSender.h:46
#define ARMARX_TRACE
Definition trace.h:75