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 
27 #include "GroupXmlReader.h"
28 
35 
37 
38 
39 namespace armarx
40 {
41  struct XMLStateOffererFactoryBase;
44  virtual Ice::Object,
45  virtual public AbstractFactoryMethod<XMLStateOffererFactoryBase, StatechartGroupXmlReaderPtr, XMLStateOffererFactoryBasePtr >
46  {
47 
48  };
49 
50  template <typename ContextType = StatechartContext>
52  public RemoteStateOfferer<ContextType>,
53  virtual public XMLStateOffererFactoryBase
54  {
55  public:
56 
58 
59  virtual void onInitXMLRemoteStateOfferer() {}
61  virtual void onExitXMLRemoteStateOfferer() {}
62 
63  std::string getStateOffererName() const override
64  {
65  return statechartGroupName;
66  }
67 
68  std::string getStatechartGroupName() const;
69  void setStatechartGroupName(const std::string& value);
70  //void setStatechartGroupReader(StatechartGroupXmlReaderPtr reader) { this->reader = reader;}
71  private:
72  void onInitRemoteStateOfferer() override;
73  void onConnectRemoteStateOfferer() override;
74  void onExitRemoteStateOfferer() override;
75 
76  bool loadLib(std::string libPath);
77 
78  StateBasePtr getStatePtr(const std::string& stateName) const override;
79  StatePtr loadState(RapidXmlReaderPtr reader);
80  StatePtr addState(StatePtr state);
81  std::map<std::string, DynamicLibraryPtr> libraries;
82  std::string statechartGroupName;
83  std::map<std::string, StatePtr> uuidStateMap;
84  StringXMLNodeMapPtr uuidXMLMap;
86 
87 
88  };
89 
90  template <typename ContextType>
92  reader(reader)
93  {
94  if (reader)
95  {
96  statechartGroupName = reader->getGroupName();
97  ManagedIceObject::setName(reader->getGroupName() + "StateOfferer");
98  }
99  else
100  {
101  ARMARX_WARNING << "reader ptr must not be NULL";
102  }
103 
104  Logging::setTag("XMLRemoteStateOfferer");
105  }
106 
107  template <typename ContextType>
109  {
110  uuidXMLMap.reset(new StringXMLNodeMap());
111  statechartGroupName = reader->getGroupName();
112  Ice::StringSeq xmlStateFileList = reader->getStateFilepaths();
113 
114  std::map<std::string, StatechartGroupXmlReader::StateVisibility> visibilities;
115 
116  for (unsigned int i = 0; i < xmlStateFileList.size(); i++)
117  {
118  std::string& filepath = xmlStateFileList.at(i);
119  ARMARX_VERBOSE << "Loading xml state file : " << filepath;
120 
121  if (!ArmarXDataPath::getAbsolutePath(filepath, filepath))
122  {
123  ARMARX_ERROR << "Could not find xml state file: " << filepath;
124  continue;
125  }
126 
127  std::string xmlString = RapidXmlReader::ReadFileContents(filepath);
128  RapidXmlReaderPtr reader = RapidXmlReader::FromXmlString(xmlString);
129  auto uuid = reader->getRoot("State").attribute_value("uuid");
130  uuidXMLMap->insert(std::make_pair(uuid, reader));
131  visibilities[uuid] = this->reader->getStateVisibility(filepath);
132  }
133 
134  for (StringXMLNodeMap::iterator it = uuidXMLMap->begin(); it != uuidXMLMap->end(); it++)
135  {
136  try
137  {
138  RapidXmlReaderPtr& reader = it->second;
139 
140  if (visibilities[it->first] == StatechartGroupXmlReader::ePublic)
141  {
142  loadState(reader);
143  }
144  }
145  catch (exceptions::local::ExpressionException& e)
146  {
147  ARMARX_ERROR << "The loading of the xml state failed due to an invalid xml file '" << it->first << "'.\nError hint: " << e.what();
148  }
149  catch (std::exception& e)
150  {
151  ARMARX_ERROR << "The loading of the xml state failed due to an invalid xml file '" << it->first << "'.\nError hint: " << e.what();
152  }
153 
154  }
155 
156  onInitXMLRemoteStateOfferer();
157  }
158 
159  template <typename ContextType>
160  void XMLRemoteStateOfferer<ContextType>::onConnectRemoteStateOfferer()
161  {
162  onConnectXMLRemoteStateOfferer();
163  }
164 
165  template <typename ContextType>
166  void XMLRemoteStateOfferer<ContextType>::onExitRemoteStateOfferer()
167  {
168  onExitXMLRemoteStateOfferer();
169  }
170 
171  template <typename ContextType>
172  StateBasePtr XMLRemoteStateOfferer<ContextType>::getStatePtr(const std::string& stateName) const
173  {
174  for (const auto& substate : StateBase::subStateList)
175  {
176  if (StateBasePtr::dynamicCast(substate)->stateName == stateName)
177  {
178  return StateBasePtr::dynamicCast(substate);
179  }
180  }
181 
182  auto it = uuidStateMap.find(stateName);
183 
184  if (it != uuidStateMap.end())
185  {
186  if (it->second)
187  {
188  return it->second;
189  }
190  else
191  {
192  ARMARX_ERROR << "State with id '" << stateName << "' has NULL ptr in map!";
193  }
194  }
195 
196  ARMARX_ERROR << "Could not find state with name '" << stateName << "'";
197  // throw LocalException("Could not find state with name '" + stateName + "'");
198  return nullptr;
199  }
200 
201 
202  template <typename ContextType>
203  StatePtr XMLRemoteStateOfferer<ContextType>::loadState(RapidXmlReaderPtr reader)
204  {
205  RapidXmlReaderNode node = reader->getRoot("State");
206  std::string stateName = node.attribute_value("name");
207  std::string uuid = node.attribute_value("uuid");
208 
209  IceInternal::Handle<XMLState> xmlStateInstance = IceInternal::Handle<XMLState>::dynamicCast(XMLStateFactoryBase::fromName(stateName, XMLStateConstructorParams("", reader, this->reader->getSelectedProfile(), uuidXMLMap, StatechartContext::getIceManager()->getCommunicator())));
210  StatePtr state = StatePtr::dynamicCast(xmlStateInstance);
211 
212  if (!state)
213  {
214  state = StatePtr::dynamicCast(NoUserCodeState::CreateInstance(XMLStateConstructorParams("", reader, this->reader->getSelectedProfile(), uuidXMLMap, StatechartContext::getIceManager()->getCommunicator())));
215  // xmlStateInstance = IceInternal::Handle<XMLState>::dynamicCast(state);
216 
217  }
218  // xmlStateInstance->setXMLStateData(XMLStateConstructorParams("", reader, this->reader->getSelectedProfile(), uuidXMLMap, StatechartContext::getIceManager()->getCommunicator()));
219 
220 
221  if (state)
222  {
223  state->stateName = stateName;
224  state->stateClassName = stateName;
225  // uuidStateMap.find()
226  uuidStateMap[uuid] = state;
227  addState(state);
228  }
229 
230  return state;
231 
232  }
233 
234  template <typename ContextType>
235  StatePtr XMLRemoteStateOfferer<ContextType>::addState(StatePtr state)
236  {
237  if (!state)
238  {
239  return state;
240  }
241 
242  if (State::findSubstateByName(state->getStateName()))
243  {
244  throw exceptions::local::eStatechartLogicError("There exists already a substate with name '" + state->getStateName() + "' in this hierarchy level. In one hierarchy level (aka one substatelist) the names must be unique.");
245  }
246 
247  if (state->getStateName().empty())
248  {
249  throw exceptions::local::eStatechartLogicError("The statename must not be empty");
250  }
251 
252  state->__setParentState(this);
253  this->StateBase::subStateList.push_back(state);
254  this->initState(*state);
255 
256  return state;
257  }
258  template <typename ContextType>
260  {
261  return statechartGroupName;
262  }
263 
264  template <typename ContextType>
266  {
267  statechartGroupName = value;
268  }
269 
270 
271 }
272 
armarx::RapidXmlReaderPtr
std::shared_ptr< RapidXmlReader > RapidXmlReaderPtr
Definition: RapidXmlReader.h:66
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:180
armarx::AbstractFactoryMethod
A template that can be used as a superclass of a class hierarchy that wants to provide a factory meth...
Definition: AbstractFactoryMethod.h:46
armarx::XMLRemoteStateOfferer::getStatechartGroupName
std::string getStatechartGroupName() const
Definition: XMLRemoteStateOfferer.h:259
DynamicLibrary.h
XMLState.h
GroupXmlReader.h
armarx::XMLRemoteStateOfferer::onExitXMLRemoteStateOfferer
virtual void onExitXMLRemoteStateOfferer()
Definition: XMLRemoteStateOfferer.h:61
armarx::StringXMLNodeMapPtr
std::shared_ptr< StringXMLNodeMap > StringXMLNodeMapPtr
Definition: XMLState.h:43
IceInternal::Handle< XMLStateOffererFactoryBase >
armarx::StatechartGroupXmlReaderPtr
std::shared_ptr< StatechartGroupXmlReader > StatechartGroupXmlReaderPtr
Definition: GroupXmlReader.h:89
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::RemoteStateOfferer
Class that holds states, which offer functionality for other states over Ice.
Definition: RemoteStateOfferer.h:182
armarx::StateBase::reset
void reset()
Function to reset the state: clear name, clear substatesList, clear transition etc.
Definition: StateBase.cpp:887
armarx::XMLRemoteStateOfferer
Definition: XMLRemoteStateOfferer.h:51
UnknownTypeException.h
StatechartProfiles.h
armarx::XMLRemoteStateOfferer::onConnectXMLRemoteStateOfferer
virtual void onConnectXMLRemoteStateOfferer()
Definition: XMLRemoteStateOfferer.h:60
armarx::StringXMLNodeMap
std::map< std::string, RapidXmlReaderPtr > StringXMLNodeMap
Definition: XMLState.h:42
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:189
armarx::XMLRemoteStateOfferer::XMLRemoteStateOfferer
XMLRemoteStateOfferer(StatechartGroupXmlReaderPtr reader)
Definition: XMLRemoteStateOfferer.h:91
armarx::XMLRemoteStateOfferer::setStatechartGroupName
void setStatechartGroupName(const std::string &value)
Definition: XMLRemoteStateOfferer.h:265
ExpressionException.h
armarx::XMLRemoteStateOfferer::onInitXMLRemoteStateOfferer
virtual void onInitXMLRemoteStateOfferer()
Definition: XMLRemoteStateOfferer.h:59
CMakePackageFinder.h
armarx::XMLRemoteStateOfferer::getStateOffererName
std::string getStateOffererName() const override
Implement this function to specify the RemoteStateOfferer prefix.
Definition: XMLRemoteStateOfferer.h:63
armarx::XMLStateOffererFactoryBase
Definition: XMLRemoteStateOfferer.h:43
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::StatePtr
IceInternal::Handle< State > StatePtr
Definition: State.h:45
RemoteStateOfferer.h
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::StateBasePtr
IceInternal::Handle< StateBase > StateBasePtr
Definition: StateBase.h:50