StatechartContext.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::Statechart
19* @author Mirko Waechter( mirko.waechter 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#include "StatechartContext.h"
25
27#include <ArmarXCore/core/services/profiler/Profiler.h> // for ProfilerPtr
28
34#include "ParameterMapping.h"
35#include "State.h"
36#include "StatechartManager.h"
37
38namespace armarx
39{
42 {
43 defineOptionalProperty<int>("ProfilingDepth",
44 -1,
45 "-1 to profile all hierarchy levels; any positive number to "
46 "stop profiling after a certain depth in the state hierarchy");
47 }
48
55
59
66
67 void
68 StatechartContext::onInitComponent()
69 {
70 if (!reportingTopic.empty())
71 {
73 }
74 stateReporter.reset(new Profiler::Profiler());
75 stateReporter->setName("StatechartReporter");
76
77 getProfiler()->setName(getName());
78 usingProxy("ConditionHandler");
79 usingProxy("SystemObserver");
80 eventDistributor = new StatechartEventDistributor;
81 eventDistributor->setStatechartName(getName());
82 eventDistributor->setStatechartManager(statechartManager);
83
84 getArmarXManager()->addObject(ManagedIceObjectPtr::dynamicCast(eventDistributor), false);
85
87
90 }
91
92 void
93 StatechartContext::onConnectComponent()
94 {
95 ARMARX_DEBUG << "Starting StatechartContext\n" << flush;
96
99 if (!reportingTopic.empty())
100 {
101 stateReporter->setLoggingStrategy(
102 Profiler::LoggingStrategyPtr(new Profiler::IceBufferedLoggingStrategy(
104 }
105
106
110 }
111
112 void
113 StatechartContext::onDisconnectComponent()
114 {
115 eventDistributor->clearEventMap();
116 }
117
118 void
119 StatechartContext::onExitComponent()
120 {
121
122 statechartManager->shutdown();
124 ARMARX_VERBOSE << "shutting statechart down" << flush;
125 }
126
127 TimedVariantBaseList
129 const DataFieldIdentifierBaseList& identifierList)
130 {
131 if (observerMap.find(observerName) == observerMap.end())
132 {
133 observerMap[observerName] = getProxy<ObserverInterfacePrx>(observerName);
134 }
135
136 return observerMap[observerName]->getDataFields(identifierList);
137 }
138
141 {
142 if (observerMap.find(identifier->observerName) == observerMap.end())
143 {
144 observerMap[identifier->observerName] =
145 getProxy<ObserverInterfacePrx>(identifier->observerName);
146 }
147
148 return observerMap[identifier->observerName]->getDataField(identifier);
149 }
150
152 StatechartContext::getChannelRef(const std::string& observerName,
153 const std::string& channelName)
154 {
155 ObserverInterfacePrx obs;
156 auto it = observerMap.find(observerName);
157
158 if (it == observerMap.end())
159 {
160 try
161 {
162 obs = getProxy<ObserverInterfacePrx>(observerName);
163 ObserverInterfacePrx::checkedCast(obs);
164 }
165 catch (...)
166 {
167 throw exceptions::local::InvalidChannelException(observerName + "." + channelName);
168 }
169
170 observerMap[observerName] = obs;
171 }
172 else
173 {
174 obs = it->second;
175 }
176
177 return new ChannelRef(obs, channelName);
178 }
179
182 {
183 ChannelRefPtr channel = getChannelRef(datafieldIdentifier.getObserverName(),
184 datafieldIdentifier.getChannelName());
185 return new DatafieldRef(channel, datafieldIdentifier.getDataFieldName());
186 }
187
189 StatechartContext::getDatafieldRef(ChannelRefPtr channelRef, const std::string& datafieldName)
190 {
191 return new DatafieldRef(channelRef, datafieldName);
192 }
193
194 bool
196 StringVariantContainerBaseMap startParameters)
197 {
198 newToplevelState->init(this, statechartManager.get());
199
200 return statechartManager->setToplevelState(newToplevelState, startParameters);
201 }
202
203 void
205 {
206 autoEnterToplevelState = autoEnter;
207 }
208
209 void
211 {
212 this->reportingTopic = reportingTopic;
213 }
214
215 void
220
223 {
224 return selectedStatechartProfile;
225 }
226
227 void
229 {
230 selectedStatechartProfile = value;
231 }
232
233} // namespace armarx
The ChannelRef class is a reference to a channel on an Observer.
Definition ChannelRef.h:51
ComponentPropertyDefinitions(std::string prefix, bool hasObjectNameParameter=true)
Definition Component.cpp:46
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition Component.cpp:90
DataFieldIdentifier provide the basis to identify data field within a distributed ArmarX scenario.
std::string getObserverName() const
Retrieve observer name.
std::string getChannelName() const
Retrieve channel name.
std::string getDataFieldName() const
Retrieve datafield name.
The DatafieldRef class is similar to the ChannelRef, but points to a specific Datafield instead of to...
void setTag(const LogTag &tag)
Definition Logging.cpp:54
void offeringTopic(const std::string &name)
Registers a topic for retrival after initialization.
Profiler::ProfilerPtr getProfiler() const
getProfiler returns an instance of armarx::Profiler
TopicProxyType getTopic(const std::string &name)
Returns a proxy of the specified topic.
bool usingProxy(const std::string &name, const std::string &endpoints="")
Registers a proxy for retrieval after initialization and adds it to the dependency list.
std::string getName() const
Retrieve name of object.
Ice::ObjectPrx getProxy(long timeoutMs=0, bool waitForScheduler=true) const
Returns the proxy of this object (optionally it waits for the proxy)
ArmarXManagerPtr getArmarXManager() const
Returns the ArmarX manager used to add and remove components.
static void _setStatechartContext(StatechartContext *__context)
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)
The StatechartContextPropertyDefinitions class contains properties associated with all statecharts.
TimedVariantBaseList getDataListFromObserver(std::string observerName, const DataFieldIdentifierBaseList &identifierList) override
void startStatechart()
startStatechart actives both, the toplevel startchart stored in the variable statechart and the State...
VariantBasePtr getDataFromObserver(const DataFieldIdentifierBasePtr &identifier) override
void setAutoEnterToplevelState(bool autoEnter)
setAutoStartStatechart controls whether the toplevelstate is automatically entered,...
DatafieldRefPtr getDatafieldRef(const DataFieldIdentifier &datafieldIdentifier) override
std::unordered_map< std::string, ObserverInterfacePrx > observerMap
StatechartEventDistributorPtr eventDistributor
The EventListenerInterface instance, that receives events from observers and redirects them to the co...
bool setToplevelState(const armarx::StatePtr &newToplevelState, StringVariantContainerBaseMap startParameters=StringVariantContainerBaseMap())
setToplevelState initializes newToplevelState with the current StatechartContext and the current Stat...
virtual void onExitStatechart()
onExitStatechart can be implemented by subclasses
SystemObserverInterfacePrx systemObserverPrx
StatechartManagerPtr statechartManager
ConditionHandlerInterfacePrx conditionHandlerPrx
virtual void onInitStatechart()=0
onInitStatechart this method is called when the statechart is started.
PropertyDefinitionsPtr createPropertyDefinitions() override
virtual void onConnectStatechart()=0
onConnectStatechart is called before armarx::StatechartContext::startStatechart() and after armarx::S...
virtual void onInitStatechartContext()
onInitStatechartonInitStatechartContext can be implemented by subclasses
Profiler::ProfilerPtr stateReporter
ChannelRefPtr getChannelRef(const std::string &observerName, const std::string &channelName) override
void setReportingTopic(std::string reportingTopic)
virtual void onConnectStatechartContext()
onConnectStatechartContext can be implemented by subclasses
const StatechartProfilePtr & getSelectedStatechartProfile() const
void setSelectedStatechartProfile(const StatechartProfilePtr &value)
#define ARMARX_DEBUG
The logging level for output that is only interesting while debugging.
Definition Logging.h:184
#define ARMARX_VERBOSE
The logging level for verbose information.
Definition Logging.h:187
std::shared_ptr< LoggingStrategy > LoggingStrategyPtr
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceInternal::Handle< DataFieldIdentifierBase > DataFieldIdentifierBasePtr
IceInternal::Handle< State > StatePtr
Definition State.h:44
IceInternal::Handle< ChannelRef > ChannelRefPtr
Definition ChannelRef.h:40
::std::vector<::armarx::DataFieldIdentifierBasePtr > DataFieldIdentifierBaseList
IceInternal::Handle< DatafieldRef > DatafieldRefPtr
Definition Observer.h:43
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
std::shared_ptr< class StatechartProfile > StatechartProfilePtr
const LogSender::manipulator flush
Definition LogSender.h:251
::IceInternal::Handle<::armarx::VariantBase > VariantBasePtr