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
38using namespace armarx;
39using namespace rapidxml;
40
42 selectedProfile(selectedProfile)
43{
44}
45
46void
47StatechartGroupXmlReader::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
62void
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
94std::filesystem::path
96{
97 return groupDefinitionFile;
98}
99
100void
101StatechartGroupXmlReader::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
123std::string
125{
126 ARMARX_CHECK_NOT_EMPTY(packageName);
127 return packageName;
128}
129
130bool
132{
133 return generateContext;
134}
135
137StatechartGroupXmlReader::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
149int
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
172std::vector<std::string>
174{
175 return proxies;
176}
177
180{
181 return selectedProfile;
182}
183
184std::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
215std::string
217{
218 return description;
219}
#define ARMARX_CHECK_NOT_EMPTY(c)
static std::string cleanPath(const std::string &filepathStr)
std::vector< RapidXmlReaderNode > nodes(const char *name=nullptr) const
std::string first_node_value_or_default(const char *name, const std::string &defaultValue) const
std::string attribute_value(const char *attrName) const
bool attribute_as_optional_bool(const char *name, const std::string &trueValue, const std::string &falseValue, bool defaultValue) const
static RapidXmlReaderPtr FromFile(const std::string &path)
std::vector< std::string > getProxies() const
void readXml(const std::filesystem::path &groupDefinitionFile)
StateVisibility getStateVisibility(const std::string &filepath) const
std::string getConfigurationFileContent() const
StatechartGroupXmlReader(const StatechartProfilePtr &selectedProfile)
std::filesystem::path getGroupDefinitionFilePath() const
int getStateNestingLevel(std::string filepath) const
StatechartProfilePtr getSelectedProfile() const
#define ARMARX_DEBUG_S
The logging level for output that is only interesting while debugging.
Definition Logging.h:205
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::shared_ptr< RapidXmlReader > RapidXmlReaderPtr
std::shared_ptr< class StatechartProfile > StatechartProfilePtr