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