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