XMLRemoteStateOfferer.h
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 ArmarX::
19* @author Mirko Waechter ( mirko.waechter at kit dot edu)
20* @date 2013
21* @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22* GNU General Public License
23*/
24
25#pragma once
26
34
35#include "GroupXmlReader.h"
36
37namespace armarx
38{
41
43 virtual Ice::Object,
44 virtual public AbstractFactoryMethod<XMLStateOffererFactoryBase,
45 StatechartGroupXmlReaderPtr,
46 XMLStateOffererFactoryBasePtr>
47 {
48 };
49
50 template <typename ContextType = StatechartContext>
52 public RemoteStateOfferer<ContextType>,
53 virtual public XMLStateOffererFactoryBase
54 {
55 public:
57
58 virtual void
62
63 virtual void
67
68 virtual void
72
73 std::string
74 getStateOffererName() const override
75 {
76 return statechartGroupName;
77 }
78
79 std::string getStatechartGroupName() const;
80 void setStatechartGroupName(const std::string& value);
81 //void setStatechartGroupReader(StatechartGroupXmlReaderPtr reader) { this->reader = reader;}
82 private:
83 void onInitRemoteStateOfferer() override;
84 void onConnectRemoteStateOfferer() override;
85 void onExitRemoteStateOfferer() override;
86
87 bool loadLib(std::string libPath);
88
89 StateBasePtr getStatePtr(const std::string& stateName) const override;
90 StatePtr loadState(RapidXmlReaderPtr reader);
91 StatePtr addState(StatePtr state);
92 std::map<std::string, DynamicLibraryPtr> libraries;
93 std::string statechartGroupName;
94 std::map<std::string, StatePtr> uuidStateMap;
95 StringXMLNodeMapPtr uuidXMLMap;
97 };
98
99 template <typename ContextType>
101 reader(reader)
102 {
103 if (reader)
104 {
105 statechartGroupName = reader->getGroupName();
106 ManagedIceObject::setName(reader->getGroupName() + "StateOfferer");
107 }
108 else
109 {
110 ARMARX_WARNING << "reader ptr must not be NULL";
111 }
112
113 Logging::setTag("XMLRemoteStateOfferer");
114 }
115
116 template <typename ContextType>
117 void
118 XMLRemoteStateOfferer<ContextType>::onInitRemoteStateOfferer()
119 {
120 uuidXMLMap.reset(new StringXMLNodeMap());
121 statechartGroupName = reader->getGroupName();
122 Ice::StringSeq xmlStateFileList = reader->getStateFilepaths();
123
124 std::map<std::string, StatechartGroupXmlReader::StateVisibility> visibilities;
125
126 for (unsigned int i = 0; i < xmlStateFileList.size(); i++)
127 {
128 std::string& filepath = xmlStateFileList.at(i);
129 ARMARX_VERBOSE << "Loading xml state file : " << filepath;
130
131 if (!ArmarXDataPath::getAbsolutePath(filepath, filepath))
132 {
133 ARMARX_ERROR << "Could not find xml state file: " << filepath;
134 continue;
135 }
136
137 std::string xmlString = RapidXmlReader::ReadFileContents(filepath);
139 auto uuid = reader->getRoot("State").attribute_value("uuid");
140 uuidXMLMap->insert(std::make_pair(uuid, reader));
141 visibilities[uuid] = this->reader->getStateVisibility(filepath);
142 }
143
144 for (StringXMLNodeMap::iterator it = uuidXMLMap->begin(); it != uuidXMLMap->end(); it++)
145 {
146 try
147 {
148 RapidXmlReaderPtr& reader = it->second;
149
150 if (visibilities[it->first] == StatechartGroupXmlReader::ePublic)
151 {
152 loadState(reader);
153 }
154 }
156 {
157 ARMARX_ERROR << "The loading of the xml state failed due to an invalid xml file '"
158 << it->first << "'.\nError hint: " << e.what();
159 }
160 catch (std::exception& e)
161 {
162 ARMARX_ERROR << "The loading of the xml state failed due to an invalid xml file '"
163 << it->first << "'.\nError hint: " << e.what();
164 }
165 }
166
167 onInitXMLRemoteStateOfferer();
168 }
169
170 template <typename ContextType>
171 void
172 XMLRemoteStateOfferer<ContextType>::onConnectRemoteStateOfferer()
173 {
174 onConnectXMLRemoteStateOfferer();
175 }
176
177 template <typename ContextType>
178 void
179 XMLRemoteStateOfferer<ContextType>::onExitRemoteStateOfferer()
180 {
181 onExitXMLRemoteStateOfferer();
182 }
183
184 template <typename ContextType>
186 XMLRemoteStateOfferer<ContextType>::getStatePtr(const std::string& stateName) const
187 {
188 for (const auto& substate : StateBase::subStateList)
189 {
190 if (StateBasePtr::dynamicCast(substate)->stateName == stateName)
191 {
192 return StateBasePtr::dynamicCast(substate);
193 }
194 }
195
196 auto it = uuidStateMap.find(stateName);
197
198 if (it != uuidStateMap.end())
199 {
200 if (it->second)
201 {
202 return it->second;
203 }
204 else
205 {
206 ARMARX_ERROR << "State with id '" << stateName << "' has NULL ptr in map!";
207 }
208 }
209
210 ARMARX_ERROR << "Could not find state with name '" << stateName << "'";
211 // throw LocalException("Could not find state with name '" + stateName + "'");
212 return nullptr;
213 }
214
215 template <typename ContextType>
217 XMLRemoteStateOfferer<ContextType>::loadState(RapidXmlReaderPtr reader)
218 {
219 RapidXmlReaderNode node = reader->getRoot("State");
220 std::string stateName = node.attribute_value("name");
221 std::string uuid = node.attribute_value("uuid");
222
223 IceInternal::Handle<XMLState> xmlStateInstance =
224 IceInternal::Handle<XMLState>::dynamicCast(XMLStateFactoryBase::fromName(
225 stateName,
227 reader,
228 this->reader->getSelectedProfile(),
229 uuidXMLMap,
230 StatechartContext::getIceManager()->getCommunicator())));
231 StatePtr state = StatePtr::dynamicCast(xmlStateInstance);
232
233 if (!state)
234 {
235 state = StatePtr::dynamicCast(NoUserCodeState::CreateInstance(
237 reader,
238 this->reader->getSelectedProfile(),
239 uuidXMLMap,
240 StatechartContext::getIceManager()->getCommunicator())));
241 // xmlStateInstance = IceInternal::Handle<XMLState>::dynamicCast(state);
242 }
243 // xmlStateInstance->setXMLStateData(XMLStateConstructorParams("", reader, this->reader->getSelectedProfile(), uuidXMLMap, StatechartContext::getIceManager()->getCommunicator()));
244
245
246 if (state)
247 {
248 state->stateName = stateName;
249 state->stateClassName = stateName;
250 // uuidStateMap.find()
251 uuidStateMap[uuid] = state;
252 addState(state);
253 }
254
255 return state;
256 }
257
258 template <typename ContextType>
260 XMLRemoteStateOfferer<ContextType>::addState(StatePtr state)
261 {
262 if (!state)
263 {
264 return state;
265 }
266
267 if (State::findSubstateByName(state->getStateName()))
268 {
270 "There exists already a substate with name '" + state->getStateName() +
271 "' in this hierarchy level. In one hierarchy level (aka one substatelist) the "
272 "names must be unique.");
273 }
274
275 if (state->getStateName().empty())
276 {
277 throw exceptions::local::eStatechartLogicError("The statename must not be empty");
278 }
279
280 state->__setParentState(this);
281 this->StateBase::subStateList.push_back(state);
282 this->initState(*state);
283
284 return state;
285 }
286
287 template <typename ContextType>
288 std::string
290 {
291 return statechartGroupName;
292 }
293
294 template <typename ContextType>
295 void
297 {
298 statechartGroupName = value;
299 }
300
301
302} // namespace armarx
A template that can be used as a superclass of a class hierarchy that wants to provide a factory meth...
static SharedPointerType fromName(const std::string &name, XMLStateConstructorParams params)
static bool getAbsolutePath(const std::string &relativeFilename, std::string &storeAbsoluteFilename, const std::vector< std::string > &additionalSearchPaths={}, bool verbose=true)
void setTag(const LogTag &tag)
Definition Logging.cpp:54
IceManagerPtr getIceManager() const
Returns the IceManager.
void setName(std::string name)
Override name of well-known object.
std::string attribute_value(const char *attrName) const
static std::string ReadFileContents(const std::string &path)
static RapidXmlReaderPtr FromXmlString(const std::string &xml)
StateBasePtr findSubstateByName(const std::string &substateName)
Utility function to find a substate of this state by the name.
void setStatechartGroupName(const std::string &value)
XMLRemoteStateOfferer(StatechartGroupXmlReaderPtr reader)
std::string getStateOffererName() const override
Implement this function to specify the RemoteStateOfferer prefix.
This exception is thrown if the macro ARMARX_CHECK_EXPRESSION is used.
#define ARMARX_ERROR
The logging level for unexpected behaviour, that must be fixed.
Definition Logging.h:196
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
#define ARMARX_VERBOSE
The logging level for verbose information.
Definition Logging.h:187
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::shared_ptr< StringXMLNodeMap > StringXMLNodeMapPtr
Definition XMLState.h:42
std::shared_ptr< StatechartGroupXmlReader > StatechartGroupXmlReaderPtr
std::shared_ptr< RapidXmlReader > RapidXmlReaderPtr
IceInternal::Handle< State > StatePtr
Definition State.h:44
IceInternal::Handle< XMLStateOffererFactoryBase > XMLStateOffererFactoryBasePtr
IceInternal::Handle< StateBase > StateBasePtr
Definition StateBase.h:49
std::map< std::string, RapidXmlReaderPtr > StringXMLNodeMap
Definition XMLState.h:41
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
Definition XMLState.cpp:56
Class that holds states, which offer functionality for other states over Ice.