GroupXmlReader.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
19  * @author
20  * @date
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 #include "GroupXmlReader.h"
25 
26 #include <filesystem>
27 #include <fstream>
28 #include <stdexcept>
29 #include <streambuf>
30 #include <string>
31 
32 #include <SimoxUtility/algorithm/string/string_tools.h>
33 
36 
37 
38 using namespace armarx;
39 using namespace rapidxml;
40 
42  selectedProfile(selectedProfile)
43 {
44 }
45 
46 void
47 StatechartGroupXmlReader::readXml(const std::filesystem::path& groupDefinitionFile)
48 {
49  RapidXmlReaderPtr wrapper = RapidXmlReader::FromFile(groupDefinitionFile.string());
50  this->groupDefinitionFile = groupDefinitionFile;
51  basePath = groupDefinitionFile;
52  basePath = basePath.remove_filename();
53  //ARMARX_IMPORTANT_S << "basePath: " << basePath.string();
54  readXml(wrapper);
55 }
56 
57 /*void StatechartGroupXmlReader::readXml(const std::string &groupDefinitionXMLString)
58 {
59  readXml(RapidXmlReader::FromXmlString(groupDefinitionXMLString));
60 }*/
61 
62 void
64 {
65 
66  RapidXmlReaderNode xmlGroupNode = reader->getRoot("StatechartGroup");
67  groupName = xmlGroupNode.attribute_value("name");
68  description = xmlGroupNode.first_node_value_or_default("Description", "");
69  packageName = xmlGroupNode.attribute_value("package");
70  generateContext =
71  xmlGroupNode.attribute_as_optional_bool("generateContext", "true", "false", false);
72 
73  std::set<std::string> proxySet;
74  proxies.clear();
75  for (RapidXmlReaderNode proxyNode : xmlGroupNode.nodes("Proxies", "Proxy"))
76  {
77  proxySet.insert(proxyNode.attribute_value("value"));
78  }
79  proxySet.insert("ArmarXCoreInterfaces.systemObserver");
80  proxySet.insert("ArmarXCoreInterfaces.conditionHandler");
81  proxies.insert(proxies.end(), proxySet.begin(), proxySet.end());
82 
83  profileConfigurations.clear();
84  for (RapidXmlReaderNode configNode : xmlGroupNode.nodes("Configurations", "Configuration"))
85  {
86  auto profileName = configNode.attribute_value("profileName");
87  auto configuration = configNode.value();
88  profileConfigurations[profileName] = configuration;
89  }
90 
91  ReadChildren(xmlGroupNode, basePath, 0);
92 }
93 
94 std::filesystem::path
96 {
97  return groupDefinitionFile;
98 }
99 
100 void
101 StatechartGroupXmlReader::ReadChildren(RapidXmlReaderNode xmlNode,
102  const std::filesystem::path& path,
103  int nesting)
104 {
105  for (RapidXmlReaderNode xmlFolderNode : xmlNode.nodes("Folder"))
106  {
107  std::filesystem::path basename(xmlFolderNode.attribute_value("basename"));
108 
109  ReadChildren(xmlFolderNode, path / basename, nesting + 1);
110  }
111 
112  for (RapidXmlReaderNode xmlStateNode : xmlNode.nodes("State"))
113  {
114  std::filesystem::path filename(xmlStateNode.attribute_value("filename").c_str());
115  std::string fullpath = (path / filename).string();
116  allstateFilePaths.push_back(fullpath);
117  std::string visibility = xmlStateNode.attribute_value_or_default("visibility", "");
118  stateVisibilityMap[fullpath] = visibility == "public" ? ePublic : ePrivate;
119  stateNestingMap[fullpath] = nesting;
120  }
121 }
122 
123 std::string
125 {
126  ARMARX_CHECK_NOT_EMPTY(packageName);
127  return packageName;
128 }
129 
130 bool
132 {
133  return generateContext;
134 }
135 
137 StatechartGroupXmlReader::getStateVisibility(const std::string& filepath) const
138 {
139  auto it = stateVisibilityMap.find(filepath);
140 
141  if (it == stateVisibilityMap.end())
142  {
143  throw LocalException("State ") << filepath << " not found.";
144  }
145 
146  return it->second;
147 }
148 
149 int
151 {
152  filepath = ArmarXDataPath::cleanPath(filepath);
153  auto it = stateNestingMap.find(filepath);
154 
155  if (it == stateNestingMap.end())
156  {
157  std::stringstream ss;
158 
159  for (std::pair<std::string, int> pair : stateNestingMap)
160  {
161  ss << "\n" << pair.first;
162  }
163 
164  throw LocalException("State File ")
165  << filepath << " not found in nesting map. States in map (" << stateNestingMap.size()
166  << "):" << ss.str();
167  }
168 
169  return it->second;
170 }
171 
172 std::vector<std::string>
174 {
175  return proxies;
176 }
177 
180 {
181  return selectedProfile;
182 }
183 
184 std::string
186 {
187  std::string result;
188  auto profile = selectedProfile;
189  while (profile)
190  {
191  std::string configs;
192  if (profileConfigurations.count(profile->getName()) > 0)
193  {
194  configs = profileConfigurations.at(profile->getName());
195  }
196  if (!configs.empty())
197  {
198  result = "# Profile: " + profile->getName() + "\n" + configs + "\n\n" + result;
199  }
200  profile = profile->getParent();
201  }
202  profile = selectedProfile;
203  std::string profileProperties;
204  while (profile)
205  {
206  profileProperties = profile->getAdditionalProperties() + profileProperties;
207  profile = profile->getParent();
208  }
209  profileProperties = simox::alg::replace_all(profileProperties, "{#GroupName#}", groupName);
210  result = profileProperties + result;
211  ARMARX_DEBUG_S << "ConfigurationFileContent: " << result;
212  return result;
213 }
214 
215 std::string
217 {
218  return description;
219 }
armarx::RapidXmlReaderPtr
std::shared_ptr< RapidXmlReader > RapidXmlReaderPtr
Definition: RapidXmlReader.h:67
armarx::RapidXmlReader::FromFile
static RapidXmlReaderPtr FromFile(const std::string &path)
Definition: RapidXmlReader.h:573
armarx::StatechartGroupXmlReader::getConfigurationFileContent
std::string getConfigurationFileContent() const
Definition: GroupXmlReader.cpp:185
armarx::StatechartGroupXmlReader::getStateNestingLevel
int getStateNestingLevel(std::string filepath) const
Definition: GroupXmlReader.cpp:150
armarx::StatechartGroupXmlReader::ePublic
@ ePublic
Definition: GroupXmlReader.h:43
armarx::ArmarXDataPath::cleanPath
static std::string cleanPath(const std::string &filepathStr)
Definition: ArmarXDataPath.cpp:328
FileIOException.h
armarx::RapidXmlReaderNode::attribute_as_optional_bool
bool attribute_as_optional_bool(const char *name, const std::string &trueValue, const std::string &falseValue, bool defaultValue) const
Definition: RapidXmlReader.h:299
armarx::StatechartGroupXmlReader::StateVisibility
StateVisibility
Definition: GroupXmlReader.h:40
armarx::RapidXmlReaderNode::attribute_value
std::string attribute_value(const char *attrName) const
Definition: RapidXmlReader.h:230
armarx::StatechartProfilePtr
std::shared_ptr< class StatechartProfile > StatechartProfilePtr
Definition: StatechartContext.h:51
armarx::StatechartGroupXmlReader::ePrivate
@ ePrivate
Definition: GroupXmlReader.h:42
armarx::StatechartGroupXmlReader::StatechartGroupXmlReader
StatechartGroupXmlReader(const StatechartProfilePtr &selectedProfile)
Definition: GroupXmlReader.cpp:41
ARMARX_CHECK_NOT_EMPTY
#define ARMARX_CHECK_NOT_EMPTY(c)
Definition: ExpressionException.h:224
GroupXmlReader.h
armarx::StatechartGroupXmlReader::readXml
void readXml(const std::filesystem::path &groupDefinitionFile)
Definition: GroupXmlReader.cpp:47
armarx::StatechartGroupXmlReader::contextGenerationEnabled
bool contextGenerationEnabled() const
Definition: GroupXmlReader.cpp:131
ARMARX_DEBUG_S
#define ARMARX_DEBUG_S
Definition: Logging.h:205
rapidxml
Definition: rapidxml.hpp:62
armarx::StatechartGroupXmlReader::getDescription
std::string getDescription() const
Definition: GroupXmlReader.cpp:216
armarx::StatechartGroupXmlReader::getStateVisibility
StateVisibility getStateVisibility(const std::string &filepath) const
Definition: GroupXmlReader.cpp:137
filename
std::string filename
Definition: VisualizationRobot.cpp:86
armarx::RapidXmlReaderNode
Definition: RapidXmlReader.h:69
ExpressionException.h
armarx::StatechartGroupXmlReader::getSelectedProfile
StatechartProfilePtr getSelectedProfile() const
Definition: GroupXmlReader.cpp:179
armarx::RapidXmlReaderNode::first_node_value_or_default
std::string first_node_value_or_default(const char *name, const std::string &defaultValue) const
Definition: RapidXmlReader.h:436
armarx::StatechartGroupXmlReader::getProxies
std::vector< std::string > getProxies() const
Definition: GroupXmlReader.cpp:173
armarx::StatechartGroupXmlReader::getGroupDefinitionFilePath
std::filesystem::path getGroupDefinitionFilePath() const
Definition: GroupXmlReader.cpp:95
armarx::RapidXmlReaderNode::nodes
std::vector< RapidXmlReaderNode > nodes(const char *name=nullptr) const
Definition: RapidXmlReader.h:181
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::StatechartGroupXmlReader::getPackageName
std::string getPackageName() const
Definition: GroupXmlReader.cpp:124