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 #include "State.h"
26 #include "StatechartManager.h"
27 #include "ParameterMapping.h"
28 
29 #include "../core/application/Application.h"
30 #include "../statechart/StatechartObjectFactories.h"
31 #include "../observers/ObserverObjectFactories.h"
32 #include "../observers/exceptions/local/InvalidChannelException.h"
33 #include "../observers/variant/DatafieldRef.h"
34 #include <ArmarXCore/core/services/profiler/Profiler.h> // for ProfilerPtr
36 
37 
38 namespace armarx
39 {
42  {
43  defineOptionalProperty<int>("ProfilingDepth", -1, "-1 to profile all hierarchy levels; any positive number to stop profiling after a certain depth in the state hierarchy");
44  }
45 
46 
47 
49  {
52  setTag("StatechartContext");
53  }
54 
56  {
57 
58  }
59 
61  {
63  }
64 
65 
66  void StatechartContext::onInitComponent()
67  {
68  if (!reportingTopic.empty())
69  {
71  }
72  stateReporter.reset(new Profiler::Profiler());
73  stateReporter->setName("StatechartReporter");
74 
75  getProfiler()->setName(getName());
76  usingProxy("ConditionHandler");
77  usingProxy("SystemObserver");
78  eventDistributor = new StatechartEventDistributor;
79  eventDistributor->setStatechartName(getName());
80  eventDistributor->setStatechartManager(statechartManager);
81 
82  getArmarXManager()->addObject(ManagedIceObjectPtr::dynamicCast(eventDistributor), false);
83 
85 
88  }
89 
90  void StatechartContext::onConnectComponent()
91  {
92  ARMARX_DEBUG << "Starting StatechartContext\n" << flush;
93 
94  conditionHandlerPrx = getProxy<ConditionHandlerInterfacePrx>("ConditionHandler");
95  systemObserverPrx = getProxy<SystemObserverInterfacePrx>("SystemObserver");
96  if (!reportingTopic.empty())
97  {
98  stateReporter->setLoggingStrategy(Profiler::LoggingStrategyPtr(new Profiler::IceBufferedLoggingStrategy(Component::getTopic<ProfilerListenerPrx>(reportingTopic))));
99  }
100 
101 
104  startStatechart();
105  }
106 
107  void StatechartContext::onDisconnectComponent()
108  {
109  eventDistributor->clearEventMap();
110  }
111 
112  void StatechartContext::onExitComponent()
113  {
114 
115  statechartManager->shutdown();
117  ARMARX_VERBOSE << "shutting statechart down" << flush;
118  }
119 
120 
121  TimedVariantBaseList
122  StatechartContext::getDataListFromObserver(std::string observerName, const DataFieldIdentifierBaseList& identifierList)
123  {
124  if (observerMap.find(observerName) == observerMap.end())
125  {
126  observerMap[observerName] = getProxy<ObserverInterfacePrx>(observerName);
127  }
128 
129  return observerMap[observerName]->getDataFields(identifierList);
130  }
131 
133  {
134  if (observerMap.find(identifier->observerName) == observerMap.end())
135  {
136  observerMap[identifier->observerName] = getProxy<ObserverInterfacePrx>(identifier->observerName);
137  }
138 
139  return observerMap[identifier->observerName]->getDataField(identifier);
140  }
141 
142  ChannelRefPtr StatechartContext::getChannelRef(const std::string& observerName, const std::string& channelName)
143  {
144  ObserverInterfacePrx obs;
145  auto it = observerMap.find(observerName);
146 
147  if (it == observerMap.end())
148  {
149  try
150  {
151  obs = getProxy<ObserverInterfacePrx>(observerName);
152  ObserverInterfacePrx::checkedCast(obs);
153  }
154  catch (...)
155  {
156  throw exceptions::local::InvalidChannelException(observerName + "." + channelName);
157  }
158 
159  observerMap[observerName] = obs;
160  }
161  else
162  {
163  obs = it->second;
164  }
165 
166  return new ChannelRef(obs, channelName);
167  }
168 
170  {
171  ChannelRefPtr channel = getChannelRef(datafieldIdentifier.getObserverName(), datafieldIdentifier.getChannelName());
172  return new DatafieldRef(channel, datafieldIdentifier.getDataFieldName());
173  }
174 
175  DatafieldRefPtr StatechartContext::getDatafieldRef(ChannelRefPtr channelRef, const std::string& datafieldName)
176  {
177  return new DatafieldRef(channelRef, datafieldName);
178  }
179 
180  bool StatechartContext::setToplevelState(const StatePtr& newToplevelState, StringVariantContainerBaseMap startParameters)
181  {
182  newToplevelState->init(this, statechartManager.get());
183 
184  return statechartManager->setToplevelState(newToplevelState, startParameters);
185  }
186 
188  {
189  autoEnterToplevelState = autoEnter;
190  }
191 
192  void StatechartContext::setReportingTopic(std::string reportingTopic)
193  {
194  this->reportingTopic = reportingTopic;
195  }
196 
198  {
200  }
201 
203  {
204  return selectedStatechartProfile;
205  }
206 
207 
209  {
210  selectedStatechartProfile = value;
211  }
212 
213 }
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:180
armarx::DataFieldIdentifierBaseList
::std::vector< ::armarx::DataFieldIdentifierBasePtr > DataFieldIdentifierBaseList
Definition: StatechartContextInterface.h:43
armarx::StatechartContext::getDataFromObserver
VariantBasePtr getDataFromObserver(const DataFieldIdentifierBasePtr &identifier) override
Definition: StatechartContext.cpp:132
armarx::StatechartContext::onConnectStatechart
virtual void onConnectStatechart()=0
onConnectStatechart is called before armarx::StatechartContext::startStatechart() and after armarx::S...
armarx::StatechartContext::onInitStatechartContext
virtual void onInitStatechartContext()
onInitStatechartonInitStatechartContext can be implemented by subclasses
Definition: StatechartContext.h:109
armarx::StatechartContext::setReportingTopic
void setReportingTopic(std::string reportingTopic)
Definition: StatechartContext.cpp:192
armarx::StatechartContext::systemObserverPrx
SystemObserverInterfacePrx systemObserverPrx
Definition: StatechartContext.h:187
armarx::ManagedIceObject::getArmarXManager
ArmarXManagerPtr getArmarXManager() const
Returns the ArmarX manager used to add and remove components.
Definition: ManagedIceObject.cpp:348
armarx::DataFieldIdentifier::getObserverName
std::string getObserverName() const
Retrieve observer name.
Definition: DataFieldIdentifier.h:101
armarx::StatechartProfilePtr
std::shared_ptr< class StatechartProfile > StatechartProfilePtr
Definition: StatechartContext.h:52
armarx::StatechartContext::conditionHandlerPrx
ConditionHandlerInterfacePrx conditionHandlerPrx
Definition: StatechartContext.h:186
ParameterMapping.h
armarx::ManagedIceObject::getProfiler
Profiler::ProfilerPtr getProfiler() const
getProfiler returns an instance of armarx::Profiler
Definition: ManagedIceObject.cpp:359
armarx::Profiler::LoggingStrategyPtr
std::shared_ptr< LoggingStrategy > LoggingStrategyPtr
Definition: LoggingStrategy.h:42
armarx::StatechartContext::getChannelRef
ChannelRefPtr getChannelRef(const std::string &observerName, const std::string &channelName) override
Definition: StatechartContext.cpp:142
StatechartContext.h
IceLoggingStrategy.h
IceInternal::Handle<::armarx::VariantBase >
armarx::XMLStatechartContext::getSelectedStatechartProfile
const StatechartProfilePtr & getSelectedStatechartProfile() const
Definition: StatechartContext.cpp:202
armarx::DataFieldIdentifier::getChannelName
std::string getChannelName() const
Retrieve channel name.
Definition: DataFieldIdentifier.h:111
armarx::StatechartContext::setToplevelState
bool setToplevelState(const armarx::StatePtr &newToplevelState, StringVariantContainerBaseMap startParameters=StringVariantContainerBaseMap())
setToplevelState initializes newToplevelState with the current StatechartContext and the current Stat...
Definition: StatechartContext.cpp:180
armarx::StatechartContext::startStatechart
void startStatechart()
startStatechart actives both, the toplevel startchart stored in the variable statechart and the State...
Definition: StatechartContext.cpp:197
armarx::ParameterMapping::_setStatechartContext
static void _setStatechartContext(StatechartContext *__context)
Definition: ParameterMapping.cpp:595
armarx::StatechartContext::reportingTopic
std::string reportingTopic
Definition: StatechartContext.h:192
armarx::StatechartContext::eventDistributor
StatechartEventDistributorPtr eventDistributor
The EventListenerInterface instance, that receives events from observers and redirects them to the co...
Definition: StatechartContext.h:184
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::DataFieldIdentifier::getDataFieldName
std::string getDataFieldName() const
Retrieve datafield name.
Definition: DataFieldIdentifier.h:121
State.h
armarx::flush
const LogSender::manipulator flush
Definition: LogSender.h:251
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:177
armarx::StatechartContext::observerMap
std::unordered_map< std::string, ObserverInterfacePrx > observerMap
Definition: StatechartContext.h:179
armarx::VariantType::ChannelRef
const VariantTypeId ChannelRef
Definition: ChannelRef.h:162
armarx::StatechartContextPropertyDefinitions::StatechartContextPropertyDefinitions
StatechartContextPropertyDefinitions(std::string prefix)
Definition: StatechartContext.cpp:40
armarx::StatechartContext::getDatafieldRef
DatafieldRefPtr getDatafieldRef(const DataFieldIdentifier &datafieldIdentifier) override
Definition: StatechartContext.cpp:169
armarx::StatechartContext::onExitStatechart
virtual void onExitStatechart()
onExitStatechart can be implemented by subclasses
Definition: StatechartContext.h:177
armarx::StatechartContext::stateReporter
Profiler::ProfilerPtr stateReporter
Definition: StatechartContext.h:191
armarx::StatechartContext::~StatechartContext
~StatechartContext() override
Definition: StatechartContext.cpp:55
armarx::exceptions::local::InvalidChannelException
Definition: InvalidChannelException.h:33
armarx::VariantType::DatafieldRef
const VariantTypeId DatafieldRef
Definition: DatafieldRef.h:169
armarx::StatechartContext::createPropertyDefinitions
PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: StatechartContext.cpp:60
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:74
armarx::StatechartContext::statechartManager
StatechartManagerPtr statechartManager
Definition: StatechartContext.h:185
StatechartManager.h
Profiler.h
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
armarx::StatechartContext::getDataListFromObserver
TimedVariantBaseList getDataListFromObserver(std::string observerName, const DataFieldIdentifierBaseList &identifierList) override
Definition: StatechartContext.cpp:122
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::XMLStatechartContext::setSelectedStatechartProfile
void setSelectedStatechartProfile(const StatechartProfilePtr &value)
Definition: StatechartContext.cpp:208
armarx::StatechartContext::onInitStatechart
virtual void onInitStatechart()=0
onInitStatechart this method is called when the statechart is started.
armarx::ManagedIceObject::getName
std::string getName() const
Retrieve name of object.
Definition: ManagedIceObject.cpp:107
armarx::StatechartContext::StatechartContext
StatechartContext()
Definition: StatechartContext.cpp:48
armarx::StatechartContext::autoEnterToplevelState
bool autoEnterToplevelState
Definition: StatechartContext.h:190
armarx::Logging::setTag
void setTag(const LogTag &tag)
Definition: Logging.cpp:55
armarx::StatechartContext::onConnectStatechartContext
virtual void onConnectStatechartContext()
onConnectStatechartContext can be implemented by subclasses
Definition: StatechartContext.h:119
armarx::PropertyDefinitionsPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Definition: forward_declarations.h:34
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
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::StatechartManager
Definition: StatechartManager.h:42
armarx::StatechartContextPropertyDefinitions
The StatechartContextPropertyDefinitions class contains properties associated with all statecharts.
Definition: StatechartContext.h:58
armarx::DataFieldIdentifier
DataFieldIdentifier provide the basis to identify data field within a distributed ArmarX scenario.
Definition: DataFieldIdentifier.h:48
armarx::StatechartContext::setAutoEnterToplevelState
void setAutoEnterToplevelState(bool autoEnter)
setAutoStartStatechart controls whether the toplevelstate is automatically entered,...
Definition: StatechartContext.cpp:187