StatechartProfiles.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 "StatechartProfiles.h"
25
26#include <filesystem>
27
30
31
32using namespace armarx;
33
38
39void
41{
42 RapidXmlReaderNode node = reader->getRoot("Profiles");
43 rootProfile->readFromXml(node);
44}
45
47StatechartProfiles::ReadProfileFiles(const std::vector<std::string>& packages)
48{
50
51 for (std::string project : packages)
52 {
53 std::filesystem::path profilesFile(CMakePackageFinder(project).getDataDir().c_str());
54 profilesFile /= project;
55 profilesFile /= "StatechartProfiles-" + project + ".xml";
56
57 if (std::filesystem::exists(profilesFile))
58 {
59 try
60 {
61 RapidXmlReaderPtr xmlReader = RapidXmlReader::FromFile(profilesFile.string());
62 profiles->readStatechartProfiles(xmlReader);
63 }
64 catch (std::exception& e)
65 {
66 ARMARX_ERROR_S << "Reading " << profilesFile.string() << " failed: " << e.what();
67 }
68 }
69 else
70 {
71 ARMARX_DEBUG_S << "StatechartProfiles File not found for project " << project << ": "
72 << profilesFile.string();
73 }
74 }
75
76 return profiles;
77}
78
79std::vector<StatechartProfilePtr>
81{
82 std::vector<StatechartProfilePtr> profiles;
83 getAllLeaves(rootProfile, profiles);
84 return profiles;
85}
86
87std::set<std::string>
89{
90 std::set<std::string> result;
91 auto leaves = getAllLeaves();
92 for (StatechartProfilePtr& leaf : leaves)
93 {
94 result.insert(leaf->getName());
95 }
96 return result;
97}
98
101{
102 if (name == "")
103 {
104 return rootProfile;
105 }
106
107 return rootProfile->findByNameRecursive(name);
108}
109
112{
113 return rootProfile;
114}
115
116void
118 std::vector<StatechartProfilePtr>& profiles) const
119{
120 if (currentProfile->children.size() == 0)
121 {
122 profiles.push_back(currentProfile);
123 }
124 else
125 {
126 for (StatechartProfilePtr p : currentProfile->children)
127 {
128 getAllLeaves(p, profiles);
129 }
130 }
131}
132
134{
135 this->name = name;
136 this->parent = parent;
137}
138
139void
141{
142 for (RapidXmlReaderNode packageNode : node.nodes("Package"))
143 {
144 std::string packageName = packageNode.attribute_value("name");
145
146 if (std::find(packages.begin(), packages.end(), packageName) == packages.end())
147 {
148 packages.push_back(packageName);
149 }
150 }
151 if (node.has_node("Properties"))
152 {
153 additionalProperties += "#Properties from Statechart profile file: " +
154 node.attribute_value_or_default("name", "NOT-FOUND") + "\n" +
155 node.first_node_value("Properties") + "\n\n";
156 }
157
158
159 for (RapidXmlReaderNode profileNode : node.nodes("Profile"))
160 {
161 std::string name = profileNode.attribute_value("name");
163 name); // Check if profile has already been read previously. Merge profiles if necessary.
164
165 if (profile)
166 {
167 ARMARX_INFO_S << "Merging Profile " << name;
168 }
169 else
170 {
171 profile.reset(
172 new StatechartProfile(profileNode.attribute_value("name"), shared_from_this()));
173 children.push_back(profile);
174 }
175 profile->readFromXml(profileNode);
176 }
177}
178
179bool
181{
182 return children.size() == 0;
183}
184
185bool
187{
188 return !parent.lock();
189}
190
191std::vector<std::string>
193{
194 std::vector<std::string> packages;
195 StatechartProfilePtr parent = this->parent.lock();
196
197 if (parent)
198 {
199 packages = parent->getAllPackages();
200 }
201
202 for (std::string package : this->packages)
203 {
204 if (std::find(packages.begin(), packages.end(), package) == packages.end())
205 {
206 packages.push_back(package);
207 }
208 }
209
210 return packages;
211}
212
213std::string
215{
216 return name;
217}
218
219std::string
221{
222 StatechartProfilePtr parent = this->parent.lock();
223
224 if (!parent)
225 {
226 return name; // root node returns "ROOT"
227 }
228
229 if (parent->isRoot())
230 {
231 return name; // avoid adding "::" in the front
232 }
233
234 return parent->getFullName() + "::" + name;
235}
236
239{
240 if (this->name == name)
241 {
242 return const_cast<StatechartProfile*>(this)->shared_from_this();
243 }
244
245 for (StatechartProfilePtr p : children)
246 {
247 StatechartProfilePtr match = p->findByNameRecursive(name);
248
249 if (match)
250 {
251 return match;
252 }
253 }
254
255 return StatechartProfilePtr();
256}
257
258const std::vector<StatechartProfilePtr>&
260{
261 return children;
262}
263
266{
267 for (StatechartProfilePtr p : children)
268 {
269 if (p->name == name)
270 {
271 return p;
272 }
273 }
274
275 return StatechartProfilePtr();
276}
277
280{
281 return parent.lock();
282}
283
284int
286{
287 return isRoot() ? 1 : getParent()->getNesting() + 1;
288}
289
290std::string
292{
293 return isRoot() ? "" : getName();
294}
295
296std::string
298{
299 return additionalProperties;
300}
The CMakePackageFinder class provides an interface to the CMake Package finder capabilities.
std::vector< RapidXmlReaderNode > nodes(const char *name=nullptr) const
std::string first_node_value(const char *nodeName=nullptr) const
bool has_node(const char *nodeName) const
std::string attribute_value_or_default(const char *attrName, const std::string &defaultValue) const
static RapidXmlReaderPtr FromFile(const std::string &path)
std::string getStatechartGroupPrefix() const
const std::vector< StatechartProfilePtr > & getChildren() const
std::string getFullName() const
Returns name of profile with all parents in the form of Root::Armar3Base::Armar3Simualation if Armar3...
StatechartProfilePtr getParent() const
StatechartProfile(const std::string &name, StatechartProfilePtr parent)
StatechartProfilePtr getChildByName(std::string name) const
std::vector< std::string > getAllPackages() const
void readFromXml(RapidXmlReaderNode node)
StatechartProfilePtr findByNameRecursive(std::string name) const
std::string getAdditionalProperties() const
StatechartProfilePtr getRootProfile() const
StatechartProfilePtr getProfileByName(std::string name)
static StatechartProfilesPtr ReadProfileFiles(const std::vector< std::string > &packages)
std::vector< StatechartProfilePtr > getAllLeaves() const
static std::string GetRootName()
std::set< std::string > getAllLeafNames() const
void readStatechartProfiles(RapidXmlReaderPtr reader)
#define ARMARX_DEBUG_S
The logging level for output that is only interesting while debugging.
Definition Logging.h:205
#define ARMARX_ERROR_S
The logging level for unexpected behaviour, that must be fixed.
Definition Logging.h:216
#define ARMARX_INFO_S
Definition Logging.h:202
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::shared_ptr< StatechartProfiles > StatechartProfilesPtr
std::shared_ptr< RapidXmlReader > RapidXmlReaderPtr
std::shared_ptr< class StatechartProfile > StatechartProfilePtr