StatechartGroupMapping.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
26
27#include <set>
28
29#include <QDir>
30#include <QString>
31
34
35namespace armarx
36{
37
38 std::optional<StatechartGroupMapping>
40 {
42 const QString packageName = QFileInfo(packageDir).fileName();
43 const QString mappingFilePath = packageDir + QDir::separator() + "data" +
44 QDir::separator() + packageName + QDir::separator() +
45 "StatechartGroupMapping-" + packageName + ".xml";
46
47 if (!QFileInfo(mappingFilePath).exists())
48 {
49 return std::nullopt;
50 }
51
52 auto readerPtr = RapidXmlReader::FromFile(mappingFilePath.toUtf8().data());
53 RapidXmlReaderNode rootNode = readerPtr->getRoot();
54
55 for (RapidXmlReaderNode groupNode : rootNode.nodes())
56 {
57 if (groupNode.name() != "Group")
58 {
59 continue;
60 }
61
62 const auto groupName = QString::fromUtf8(groupNode.attribute_value("name").c_str());
63 const auto newGroupName =
64 QString::fromUtf8(groupNode.attribute_value("new-name").c_str());
65 const auto package =
66 QString::fromUtf8(groupNode.attribute_value("source-package").c_str());
67
68 StatechartGroupMapping::GroupMapping groupMapping{groupName, newGroupName, package, {}};
69
70 for (RapidXmlReaderNode stateNode : groupNode.nodes())
71 {
72 if (stateNode.name() != "Mapping")
73 {
74 continue;
75 }
76
77 const auto stateName =
78 QString::fromUtf8(stateNode.attribute_value("state").c_str());
79 const auto fromUuid = QString::fromUtf8(stateNode.attribute_value("from").c_str());
80 const auto toUuid = QString::fromUtf8(stateNode.attribute_value("to").c_str());
81 groupMapping.stateMappings.insert({stateName, fromUuid, toUuid});
82 }
83
84 result.groupMappings.insert(groupMapping);
85 }
86
87 return result;
88 }
89
90 std::optional<QString>
91 StatechartGroupMapping::queryMappedGroupName(const QString& sourceGroupName) const
92 {
93 for (const auto& groupMapping : groupMappings)
94 {
95 if (groupMapping.groupName == sourceGroupName)
96 {
97 return groupMapping.newGroupName;
98 }
99 }
100
101 return std::nullopt;
102 }
103
104 std::optional<QString>
105 StatechartGroupMapping::queryMappedUuid(const QString& sourceUuid) const
106 {
107 for (const auto& groupMapping : groupMappings)
108 {
109 for (const auto& stateMapping : groupMapping.stateMappings)
110 {
111 if (stateMapping.fromUuid == sourceUuid)
112 {
113 return stateMapping.toUuid;
114 }
115 }
116 }
117
118 return std::nullopt;
119 }
120
121 void
122 StatechartGroupMapping::writeToFile(const QString& packageDir) const
123 {
124 const QString packageName = QFileInfo(packageDir).fileName();
125 const QString packageDataDir =
126 packageDir + QDir::separator() + "data" + QDir::separator() + packageName;
127 QDir(packageDataDir).mkpath(".");
128 const QString mappingFilePath =
129 packageDataDir + QDir::separator() + "StatechartGroupMapping-" + packageName + ".xml";
130
131 RapidXmlWriter writer;
132 auto rootNode = writer.createRootNode("StatechartGroupMapping");
133
134 for (const auto& g : groupMappings)
135 {
136 auto groupNode = rootNode.append_node("Group");
137 groupNode.append_attribute("name", g.groupName.toUtf8().data());
138 groupNode.append_attribute("new-name", g.newGroupName.toUtf8().data());
139 groupNode.append_attribute("source-package", g.groupPackage.toUtf8().data());
140
141 for (const auto& s : g.stateMappings)
142 {
143 auto stateNode = groupNode.append_node("Mapping");
144 stateNode.append_attribute("state", s.stateName.toUtf8().data());
145 stateNode.append_attribute("from", s.fromUuid.toUtf8().data());
146 stateNode.append_attribute("to", s.toUuid.toUtf8().data());
147 }
148 }
149
150 writer.saveToFile(mappingFilePath.toUtf8().data(), true);
151 }
152
153} // namespace armarx
std::vector< RapidXmlReaderNode > nodes(const char *name=nullptr) const
static RapidXmlReaderPtr FromFile(const std::string &path)
void saveToFile(const std::string &path, bool indent)
RapidXmlWriterNode createRootNode(const std::string &name)
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::optional< QString > queryMappedUuid(const QString &sourceUuid) const
std::set< GroupMapping > groupMappings
void writeToFile(const QString &packageDir) const
std::optional< QString > queryMappedGroupName(const QString &sourceGroupName) const
static std::optional< StatechartGroupMapping > ReadStatechartGroupMappingFile(const QString &packageDir)