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 
32 using namespace armarx;
33 
35  rootProfile(new StatechartProfile(StatechartProfiles::GetRootName(), StatechartProfilePtr()))
36 {
37 }
38 
39 void
41 {
42  RapidXmlReaderNode node = reader->getRoot("Profiles");
43  rootProfile->readFromXml(node);
44 }
45 
47 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 << ": "
72  << profilesFile.string();
73  }
74  }
75 
76  return profiles;
77 }
78 
79 std::vector<StatechartProfilePtr>
81 {
82  std::vector<StatechartProfilePtr> profiles;
83  getAllLeaves(rootProfile, profiles);
84  return profiles;
85 }
86 
87 std::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 
116 void
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 
139 void
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 
179 bool
181 {
182  return children.size() == 0;
183 }
184 
185 bool
187 {
188  return !parent.lock();
189 }
190 
191 std::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 
213 std::string
215 {
216  return name;
217 }
218 
219 std::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 
258 const std::vector<StatechartProfilePtr>&
260 {
261  return children;
262 }
263 
265 StatechartProfile::getChildByName(std::string name) const
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 
284 int
286 {
287  return isRoot() ? 1 : getParent()->getNesting() + 1;
288 }
289 
290 std::string
292 {
293  return isRoot() ? "" : getName();
294 }
295 
296 std::string
298 {
299  return additionalProperties;
300 }
armarx::RapidXmlReaderPtr
std::shared_ptr< RapidXmlReader > RapidXmlReaderPtr
Definition: RapidXmlReader.h:67
armarx::RapidXmlReader::FromFile
static RapidXmlReaderPtr FromFile(const std::string &path)
Definition: RapidXmlReader.h:573
armarx::StatechartProfile::getAdditionalProperties
std::string getAdditionalProperties() const
Definition: StatechartProfiles.cpp:297
armarx::StatechartProfiles::getProfileByName
StatechartProfilePtr getProfileByName(std::string name)
Definition: StatechartProfiles.cpp:100
armarx::StatechartProfile
Definition: StatechartProfiles.h:42
armarx::CMakePackageFinder
The CMakePackageFinder class provides an interface to the CMake Package finder capabilities.
Definition: CMakePackageFinder.h:52
armarx::StatechartProfile::getStatechartGroupPrefix
std::string getStatechartGroupPrefix() const
Definition: StatechartProfiles.cpp:291
armarx::StatechartProfilePtr
std::shared_ptr< class StatechartProfile > StatechartProfilePtr
Definition: StatechartContext.h:51
armarx::StatechartProfiles::getRootProfile
StatechartProfilePtr getRootProfile() const
Definition: StatechartProfiles.cpp:111
armarx::StatechartProfile::findByNameRecursive
StatechartProfilePtr findByNameRecursive(std::string name) const
Definition: StatechartProfiles.cpp:238
armarx::StatechartProfiles::getAllLeafNames
std::set< std::string > getAllLeafNames() const
Definition: StatechartProfiles.cpp:88
project
std::string project
Definition: VisualizationRobot.cpp:85
armarx::RapidXmlReaderNode::has_node
bool has_node(const char *nodeName) const
Definition: RapidXmlReader.h:236
armarx::StatechartProfilesPtr
std::shared_ptr< StatechartProfiles > StatechartProfilesPtr
Definition: StatechartProfiles.h:36
ARMARX_DEBUG_S
#define ARMARX_DEBUG_S
Definition: Logging.h:205
armarx::StatechartProfile::StatechartProfile
StatechartProfile(const std::string &name, StatechartProfilePtr parent)
Definition: StatechartProfiles.cpp:133
armarx::StatechartProfiles
Definition: StatechartProfiles.h:74
armarx::StatechartProfile::getChildren
const std::vector< StatechartProfilePtr > & getChildren() const
Definition: StatechartProfiles.cpp:259
armarx::StatechartProfile::isLeaf
bool isLeaf() const
Definition: StatechartProfiles.cpp:180
ARMARX_ERROR_S
#define ARMARX_ERROR_S
Definition: Logging.h:216
armarx::StatechartProfile::getNesting
int getNesting() const
Definition: StatechartProfiles.cpp:285
StatechartProfiles.h
armarx::StatechartProfiles::getAllLeaves
std::vector< StatechartProfilePtr > getAllLeaves() const
Definition: StatechartProfiles.cpp:80
armarx::RapidXmlReaderNode
Definition: RapidXmlReader.h:69
armarx::StatechartProfile::isRoot
bool isRoot() const
Definition: StatechartProfiles.cpp:186
armarx::StatechartProfile::getParent
StatechartProfilePtr getParent() const
Definition: StatechartProfiles.cpp:279
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:220
armarx::StatechartProfile::getAllPackages
std::vector< std::string > getAllPackages() const
Definition: StatechartProfiles.cpp:192
armarx::StatechartProfile::getName
std::string getName() const
Definition: StatechartProfiles.cpp:214
armarx::RapidXmlReaderNode::nodes
std::vector< RapidXmlReaderNode > nodes(const char *name=nullptr) const
Definition: RapidXmlReader.h:181
ARMARX_INFO_S
#define ARMARX_INFO_S
Definition: Logging.h:202
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:140
armarx::RapidXmlReaderNode::first_node_value
std::string first_node_value(const char *nodeName=nullptr) const
Definition: RapidXmlReader.h:421
armarx::StatechartProfiles::readStatechartProfiles
void readStatechartProfiles(RapidXmlReaderPtr reader)
Definition: StatechartProfiles.cpp:40
armarx::StatechartProfile::getChildByName
StatechartProfilePtr getChildByName(std::string name) const
Definition: StatechartProfiles.cpp:265
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::RapidXmlReaderNode::attribute_value_or_default
std::string attribute_value_or_default(const char *attrName, const std::string &defaultValue) const
Definition: RapidXmlReader.h:262