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 
28 
29 #include <filesystem>
30 
31 
32 using namespace armarx;
33 
35  : rootProfile(new StatechartProfile(StatechartProfiles::GetRootName(), StatechartProfilePtr()))
36 {
37 }
38 
40 {
41  RapidXmlReaderNode node = reader->getRoot("Profiles");
42  rootProfile->readFromXml(node);
43 }
44 
45 
46 
47 StatechartProfilesPtr StatechartProfiles::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 << ": " << profilesFile.string();
72  }
73  }
74 
75  return profiles;
76 }
77 
78 std::vector<StatechartProfilePtr> StatechartProfiles::getAllLeaves() const
79 {
80  std::vector<StatechartProfilePtr> profiles;
81  getAllLeaves(rootProfile, profiles);
82  return profiles;
83 }
84 
85 std::set<std::string> StatechartProfiles::getAllLeafNames() const
86 {
87  std::set<std::string> result;
88  auto leaves = getAllLeaves();
89  for (StatechartProfilePtr& leaf : leaves)
90  {
91  result.insert(leaf->getName());
92  }
93  return result;
94 }
95 
97 {
98  if (name == "")
99  {
100  return rootProfile;
101  }
102 
103  return rootProfile->findByNameRecursive(name);
104 }
105 
107 {
108  return rootProfile;
109 }
110 
111 void StatechartProfiles::getAllLeaves(StatechartProfilePtr currentProfile, std::vector<StatechartProfilePtr>& profiles) const
112 {
113  if (currentProfile->children.size() == 0)
114  {
115  profiles.push_back(currentProfile);
116  }
117  else
118  {
119  for (StatechartProfilePtr p : currentProfile->children)
120  {
121  getAllLeaves(p, profiles);
122  }
123  }
124 }
125 
126 
128 {
129  this->name = name;
130  this->parent = parent;
131 }
132 
134 {
135  for (RapidXmlReaderNode packageNode : node.nodes("Package"))
136  {
137  std::string packageName = packageNode.attribute_value("name");
138 
139  if (std::find(packages.begin(), packages.end(), packageName) == packages.end())
140  {
141  packages.push_back(packageName);
142  }
143  }
144  if (node.has_node("Properties"))
145  {
146  additionalProperties += "#Properties from Statechart profile file: " + node.attribute_value_or_default("name", "NOT-FOUND") + "\n" + node.first_node_value("Properties") + "\n\n";
147  }
148 
149 
150  for (RapidXmlReaderNode profileNode : node.nodes("Profile"))
151  {
152  std::string name = profileNode.attribute_value("name");
153  StatechartProfilePtr profile = getChildByName(name); // Check if profile has already been read previously. Merge profiles if necessary.
154 
155  if (profile)
156  {
157  ARMARX_INFO_S << "Merging Profile " << name;
158  }
159  else
160  {
161  profile.reset(new StatechartProfile(profileNode.attribute_value("name"), shared_from_this()));
162  children.push_back(profile);
163  }
164  profile->readFromXml(profileNode);
165  }
166 }
167 
169 {
170  return children.size() == 0;
171 }
172 
174 {
175  return !parent.lock();
176 }
177 
178 std::vector<std::string> StatechartProfile::getAllPackages() const
179 {
180  std::vector<std::string> packages;
181  StatechartProfilePtr parent = this->parent.lock();
182 
183  if (parent)
184  {
185  packages = parent->getAllPackages();
186  }
187 
188  for (std::string package : this->packages)
189  {
190  if (std::find(packages.begin(), packages.end(), package) == packages.end())
191  {
192  packages.push_back(package);
193  }
194  }
195 
196  return packages;
197 }
198 
199 std::string StatechartProfile::getName() const
200 {
201  return name;
202 }
203 
205 {
206  StatechartProfilePtr parent = this->parent.lock();
207 
208  if (!parent)
209  {
210  return name; // root node returns "ROOT"
211  }
212 
213  if (parent->isRoot())
214  {
215  return name; // avoid adding "::" in the front
216  }
217 
218  return parent->getFullName() + "::" + name;
219 }
220 
222 {
223  if (this->name == name)
224  {
225  return const_cast<StatechartProfile*>(this)->shared_from_this();
226  }
227 
228  for (StatechartProfilePtr p : children)
229  {
230  StatechartProfilePtr match = p->findByNameRecursive(name);
231 
232  if (match)
233  {
234  return match;
235  }
236  }
237 
238  return StatechartProfilePtr();
239 }
240 
241 const std::vector<StatechartProfilePtr>& StatechartProfile::getChildren() const
242 {
243  return children;
244 }
245 
247 {
248  for (StatechartProfilePtr p : children)
249  {
250  if (p->name == name)
251  {
252  return p;
253  }
254  }
255 
256  return StatechartProfilePtr();
257 }
258 
260 {
261  return parent.lock();
262 }
263 
265 {
266  return isRoot() ? 1 : getParent()->getNesting() + 1;
267 }
268 
270 {
271  return isRoot() ? "" : getName();
272 }
273 
275 {
276  return additionalProperties;
277 }
278 
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::StatechartProfile::getAdditionalProperties
std::string getAdditionalProperties() const
Definition: StatechartProfiles.cpp:274
armarx::StatechartProfiles::getProfileByName
StatechartProfilePtr getProfileByName(std::string name)
Definition: StatechartProfiles.cpp:96
armarx::StatechartProfile
Definition: StatechartProfiles.h:41
armarx::CMakePackageFinder
The CMakePackageFinder class provides an interface to the CMake Package finder capabilities.
Definition: CMakePackageFinder.h:53
armarx::StatechartProfile::getStatechartGroupPrefix
std::string getStatechartGroupPrefix() const
Definition: StatechartProfiles.cpp:269
armarx::StatechartProfilePtr
std::shared_ptr< class StatechartProfile > StatechartProfilePtr
Definition: StatechartContext.h:52
armarx::StatechartProfiles::getRootProfile
StatechartProfilePtr getRootProfile() const
Definition: StatechartProfiles.cpp:106
armarx::StatechartProfile::findByNameRecursive
StatechartProfilePtr findByNameRecursive(std::string name) const
Definition: StatechartProfiles.cpp:221
armarx::StatechartProfiles::getAllLeafNames
std::set< std::string > getAllLeafNames() const
Definition: StatechartProfiles.cpp:85
project
std::string project
Definition: VisualizationRobot.cpp:83
armarx::RapidXmlReaderNode::has_node
bool has_node(const char *nodeName) const
Definition: RapidXmlReader.h:203
armarx::StatechartProfilesPtr
std::shared_ptr< StatechartProfiles > StatechartProfilesPtr
Definition: StatechartProfiles.h:35
ARMARX_DEBUG_S
#define ARMARX_DEBUG_S
Definition: Logging.h:198
armarx::StatechartProfile::StatechartProfile
StatechartProfile(const std::string &name, StatechartProfilePtr parent)
Definition: StatechartProfiles.cpp:127
armarx::StatechartProfiles
Definition: StatechartProfiles.h:75
armarx::StatechartProfile::getChildren
const std::vector< StatechartProfilePtr > & getChildren() const
Definition: StatechartProfiles.cpp:241
armarx::StatechartProfile::isLeaf
bool isLeaf() const
Definition: StatechartProfiles.cpp:168
ARMARX_ERROR_S
#define ARMARX_ERROR_S
Definition: Logging.h:209
armarx::StatechartProfile::getNesting
int getNesting() const
Definition: StatechartProfiles.cpp:264
StatechartProfiles.h
armarx::StatechartProfiles::getAllLeaves
std::vector< StatechartProfilePtr > getAllLeaves() const
Definition: StatechartProfiles.cpp:78
armarx::RapidXmlReaderNode
Definition: RapidXmlReader.h:68
armarx::StatechartProfile::isRoot
bool isRoot() const
Definition: StatechartProfiles.cpp:173
armarx::StatechartProfile::getParent
StatechartProfilePtr getParent() const
Definition: StatechartProfiles.cpp:259
CMakePackageFinder.h
armarx::StatechartProfiles::StatechartProfiles
StatechartProfiles()
Definition: StatechartProfiles.cpp:34
armarx::StatechartProfile::getFullName
std::string getFullName() const
Returns name of profile with all parents in the form of Root::Armar3Base::Armar3Simualation if Armar3...
Definition: StatechartProfiles.cpp:204
armarx::StatechartProfile::getAllPackages
std::vector< std::string > getAllPackages() const
Definition: StatechartProfiles.cpp:178
armarx::StatechartProfile::getName
std::string getName() const
Definition: StatechartProfiles.cpp:199
armarx::RapidXmlReaderNode::nodes
std::vector< RapidXmlReaderNode > nodes(const char *name=nullptr) const
Definition: RapidXmlReader.h:158
ARMARX_INFO_S
#define ARMARX_INFO_S
Definition: Logging.h:195
armarx::StatechartProfiles::ReadProfileFiles
static StatechartProfilesPtr ReadProfileFiles(const std::vector< std::string > &packages)
Definition: StatechartProfiles.cpp:47
Logging.h
armarx::StatechartProfile::readFromXml
void readFromXml(RapidXmlReaderNode node)
Definition: StatechartProfiles.cpp:133
armarx::RapidXmlReaderNode::first_node_value
std::string first_node_value(const char *nodeName=nullptr) const
Definition: RapidXmlReader.h:361
armarx::StatechartProfiles::readStatechartProfiles
void readStatechartProfiles(RapidXmlReaderPtr reader)
Definition: StatechartProfiles.cpp:39
armarx::StatechartProfile::getChildByName
StatechartProfilePtr getChildByName(std::string name) const
Definition: StatechartProfiles.cpp:246
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::RapidXmlReaderNode::attribute_value_or_default
std::string attribute_value_or_default(const char *attrName, const std::string &defaultValue) const
Definition: RapidXmlReader.h:225