StateTreeNode.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 "StateTreeNode.h"
25 
26 #include <boost/regex.hpp>
27 
28 using namespace armarx;
29 
30 StateTreeNode::StateTreeNode(QString display, QString basename, NodeType nodeType, StateTreeNodePtr parentNode, StatechartGroupPtr parentGroup, bool stateIsPublic)
31 {
32  this->display = display;
33  this->basename = basename;
34  this->nodeType = nodeType;
35  this->parentNode = parentNode;
36  this->parentGroup = parentGroup;
37  this->stateIsPublic = stateIsPublic;
38  this->cppExists = false;
40 }
41 
43 {
44  //qDeleteAll(children);
45 }
46 
48 {
49  children.append(child);
50 }
51 
52 int StateTreeNode::NodeTypeToSortIndex(NodeType nodeType)
53 {
54  switch (nodeType)
55  {
56  case Group:
57  return 0;
58 
59  case Folder:
60  return 1;
61 
62  case State:
63  return 2;
64 
65  default:
66  return 999;
67  }
68 }
69 
70 bool StateTreeNode::CompareChildren(const StateTreeNodePtr& a, const StateTreeNodePtr& b)
71 {
72  int aTypeIndex = NodeTypeToSortIndex(a->nodeType);
73  int bTypeIndex = NodeTypeToSortIndex(b->nodeType);
74 
75  if (aTypeIndex != bTypeIndex)
76  {
77  return aTypeIndex < bTypeIndex;
78  }
79 
80  return a->display.toLower().localeAwareCompare(b->display.toLower()) < 0;
81 }
82 
84 {
85  qStableSort(children.begin(), children.end(), CompareChildren);
86 }
87 
89 {
90  children.removeOne(child);
91 }
92 
94 {
95  return children.value(row);
96 }
97 
99 {
100  return children.count();
101 }
102 
104 {
105  return 1;
106 }
107 
109 {
110  return display;
111 }
112 
114 {
115  StateTreeNodePtr parent = parentNode.lock();
116 
117  if (parent)
118  {
119  //return parentNode->children.indexOf(const_cast<StatechartGroupTreeNode*>(this));
120 
121  //NOT WORKING:
122  //return parent->children.indexOf(const_cast<StatechartGroupTreeNodePtr>(shared_from_this()));
123  for (int i = 0; i < parent->children.count(); i++)
124  {
125  if (parent->children.at(i).get() == this)
126  {
127  return i;
128  }
129  }
130 
131  return -1;
132  }
133 
134  return 0;
135 }
136 
138 {
139  return parentNode.lock();
140 }
141 
143 {
144  return parentGroup.lock();
145 }
146 
148 {
149  return nodeType;
150 }
152 {
153  return basename;
154 }
155 
156 std::filesystem::path StateTreeNode::getAbsoluteBoostPath() const
157 {
158  return parentGroup.lock()->getBoostGroupPath() / getBoostPathRelativeToGroup();
159 }
160 
161 std::filesystem::path StateTreeNode::getBoostXmlFilePath(StateTreeNode::Path pathType) const
162 {
163  if (!state)
164  {
165  return std::filesystem::path();
166  }
167 
169 }
170 
171 std::filesystem::path StateTreeNode::getBoostCppFilePath(StateTreeNode::Path pathType) const
172 {
173  if (!state)
174  {
175  return std::filesystem::path();
176  }
177 
178  std::string filename = std::string(state->getStateName().toUtf8().data()) + ".cpp";
179  auto path = (pathType == Path::Absolute) ? getAbsoluteBoostPath() : getBoostPathRelativeToGroup();
180  return (path.remove_filename() / filename);
181 }
182 
183 std::filesystem::path StateTreeNode::getBoostHFilePath(StateTreeNode::Path pathType) const
184 {
185  if (!state)
186  {
187  return std::filesystem::path();
188  }
189 
190  std::string filename = std::string(state->getStateName().toUtf8().data()) + ".h";
191  auto path = (pathType == Path::Absolute) ? getAbsoluteBoostPath() : getBoostPathRelativeToGroup();
192  return (path.remove_filename() / filename);
193 }
194 
196 {
197  if (!state)
198  {
199  return std::filesystem::path();
200  }
201 
202  std::string filename = std::string(state->getStateName().toUtf8().data()) + ".generated.h";
203  auto path = (pathType == Path::Absolute) ? getAbsoluteBoostPath() : getBoostPathRelativeToGroup();
204  return (path.remove_filename() / filename);
205 }
206 
208 {
209  if (!state)
210  {
211  return false;
212  }
213 
214  return cppExists;
215 }
216 
218 {
219  if (!state)
220  {
221  return false;
222  }
223 
224  cppExists = std::filesystem::exists(getBoostCppFilePath());
225  return cppExists;
226 }
227 
228 std::filesystem::path StateTreeNode::getBoostPath(std::filesystem::path parentPath) const
229 {
230  return parentPath / basename.toUtf8().data();
231 }
232 
233 std::filesystem::path StateTreeNode::getBoostPathRelativeToGroup() const
234 {
235  std::filesystem::path result = basename.toUtf8().data();
237 
238  while (p && !p->isGroup())
239  {
240  result = std::filesystem::path(p->getBasename().toUtf8().data()) / result;
241  p = p->getParent();
242  }
243 
244  return result;
245 }
247 {
248  return state;
249 }
250 
251 
253 {
254  return nodeType == State;
255 }
256 
258 {
259  return nodeType == Folder || nodeType == Group;
260 }
261 
263 {
264  return nodeType == Group;
265 }
266 
268 {
269  return nodeType == Folder;
270 }
271 
273 {
274  StateTreeNodePtr node = shared_from_this();
275 
276  if (isState())
277  {
278  node = node->getParent();
279  }
280 
281  ARMARX_CHECK_EXPRESSION(node->isFolderOrGroup()) << "State Tree is broken.";
282  return node;
283 }
284 
286 {
287  if (state && state->getStateName() == name)
288  {
289  return shared_from_this();
290  }
291 
292  for (int i = 0; i < children.count(); i++)
293  {
294  StateTreeNodePtr match = children.at(i)->findNodeByStateName(name);
295 
296  if (match)
297  {
298  return match;
299  }
300  }
301 
302  return StateTreeNodePtr();
303 }
304 
305 QVector<StateTreeNodePtr> StateTreeNode::getAllSubNodesRecursively() const
306 {
307  QVector<StateTreeNodePtr> result;
308 
309  for (const StateTreeNodePtr& node : children)
310  {
311  result.push_back(node);
312 
313  for (const auto& n : node->getAllSubNodesRecursively())
314  {
315  result.push_back(n);
316  }
317  }
318 
319  return result;
320 }
321 
322 QVector<statechartmodel::StatePtr> StateTreeNode::getAllStatesRecursively(bool localOnly) const
323 {
324  QVector<statechartmodel::StatePtr> result;
325 
326  if (state)
327  {
328  result.push_back(state);
329  GetAllSubstatesRecursively(state, result, localOnly);
330  }
331  else
332  {
333  for (const auto& child : children)
334  {
335  for (const auto& state : child->getAllStatesRecursively(localOnly))
336  {
337  result.push_back(state);
338  }
339  }
340  }
341 
342  return result;
343 }
344 
345 void StateTreeNode::GetAllSubstatesRecursively(const statechartmodel::StatePtr& state, QVector<statechartmodel::StatePtr>& result, bool localOnly) const
346 {
347  for (const auto& substate : state->getSubstates())
348  {
349  const auto& stateClass = substate->getStateClass();
350 
351  if (stateClass && (!localOnly || substate->getType() != eRemoteState))
352  {
353  result.push_back(stateClass);
354  GetAllSubstatesRecursively(stateClass, result, localOnly);
355  }
356  }
357 }
358 
360 {
361  if (isState() || isFolder())
362  {
363  return parentNode.lock()->getNestingLevelRelativeToGroup() + 1;
364  }
365 
366  return 0;
367 }
368 
370 {
371  std::string str = name.toUtf8().data();
372  boost::regex nameRegex("[a-zA-Z][a-zA-Z0-9]*");
373  boost::smatch what;
374  return boost::regex_match(str, what, nameRegex);
375 }
376 
377 
378 
379 QList<StateTreeNodePtr> StateTreeNode::getChildren() const
380 {
381  return children;
382 }
383 
385 {
386  return stateIsPublic;
387 }
388 
389 void StateTreeNode::setPublic(bool isPublic)
390 {
391  stateIsPublic = isPublic;
392 }
393 
394 
395 
armarx::StateTreeNode::getAllStatesRecursively
QVector< statechartmodel::StatePtr > getAllStatesRecursively(bool localOnly) const
Definition: StateTreeNode.cpp:322
armarx::StateTreeNode::isFolderOrGroup
bool isFolderOrGroup() const
Definition: StateTreeNode.cpp:257
armarx::StateTreeNode::row
int row() const
Definition: StateTreeNode.cpp:113
str
std::string str(const T &t)
Definition: UserAssistedSegmenterGuiWidgetController.cpp:42
armarx::StateTreeNode::isState
bool isState() const
Definition: StateTreeNode.cpp:252
armarx::StateTreeNodePtr
std::shared_ptr< StateTreeNode > StateTreeNodePtr
Definition: StatechartGroupDefs.h:31
armarx::StateTreeNode::getBoostXmlFilePath
std::filesystem::path getBoostXmlFilePath(Path pathType=Path::Absolute) const
Definition: StateTreeNode.cpp:161
armarx::StateTreeNode::columnCount
int columnCount() const
Definition: StateTreeNode.cpp:103
armarx::StateTreeNode::isPublic
bool isPublic()
Definition: StateTreeNode.cpp:384
armarx::StateTreeNode::findNodeByStateName
StateTreeNodePtr findNodeByStateName(QString name)
Definition: StateTreeNode.cpp:285
armarx::StateTreeNode::getGroup
StatechartGroupPtr getGroup()
Definition: StateTreeNode.cpp:142
armarx::StateTreeNode::Group
@ Group
Definition: StateTreeNode.h:45
armarx::StateTreeNode::getBasename
QString getBasename() const
Definition: StateTreeNode.cpp:151
display
Use of this software is granted under one of the following two to be chosen freely by the user Boost Software License Version Marcin Kalicinski Permission is hereby free of to any person or organization obtaining a copy of the software and accompanying documentation covered by this display
Definition: license.txt:11
armarx::StateTreeNode::getBoostPathRelativeToGroup
std::filesystem::path getBoostPathRelativeToGroup() const
Definition: StateTreeNode.cpp:233
armarx::StateTreeNode::checkCppExists
bool checkCppExists()
Definition: StateTreeNode.cpp:217
armarx::StateTreeNode::appendChild
void appendChild(StateTreeNodePtr child)
Definition: StateTreeNode.cpp:47
armarx::StateTreeNode::getBoostPath
std::filesystem::path getBoostPath(std::filesystem::path parentPath) const
Definition: StateTreeNode.cpp:228
armarx::StateTreeNode::sortChildren
void sortChildren()
Definition: StateTreeNode.cpp:83
armarx::StateTreeNode::getAbsoluteBoostPath
std::filesystem::path getAbsoluteBoostPath() const
Definition: StateTreeNode.cpp:156
armarx::StateTreeNode::getState
armarx::statechartmodel::StatePtr getState() const
Definition: StateTreeNode.cpp:246
armarx::ctrlutil::a
double a(double t, double a0, double j)
Definition: CtrlUtil.h:45
armarx::StateTreeNode::~StateTreeNode
~StateTreeNode()
Definition: StateTreeNode.cpp:42
armarx::StateTreeNode::State
@ State
Definition: StateTreeNode.h:45
armarx::StateTreeNode::getBoostHFilePath
std::filesystem::path getBoostHFilePath(Path pathType=Path::Absolute) const
Definition: StateTreeNode.cpp:183
filename
std::string filename
Definition: VisualizationRobot.cpp:84
armarx::StateTreeNode::getClosestParentFolderOrGroupNode
StateTreeNodePtr getClosestParentFolderOrGroupNode()
Definition: StateTreeNode.cpp:272
armarx::StateTreeNode::getBoostGeneratedHFilePath
std::filesystem::path getBoostGeneratedHFilePath(Path pathType=Path::Absolute) const
Definition: StateTreeNode.cpp:195
armarx::StateTreeNode::setPublic
void setPublic(bool isPublic)
Definition: StateTreeNode.cpp:389
armarx::StateTreeNode::child
StateTreeNodePtr child(int row)
Definition: StateTreeNode.cpp:93
armarx::StateTreeNode::CheckNodeName
static bool CheckNodeName(QString name)
Definition: StateTreeNode.cpp:369
armarx::StateTreeNode::getCppExists
bool getCppExists() const
Definition: StateTreeNode.cpp:207
armarx::StateTreeNode::removeChild
void removeChild(StateTreeNodePtr child)
Definition: StateTreeNode.cpp:88
armarx::StateTreeNode::isGroup
bool isGroup() const
Definition: StateTreeNode.cpp:262
armarx::StatechartGroupPtr
std::shared_ptr< StatechartGroup > StatechartGroupPtr
Definition: StatechartGroupDefs.h:34
armarx::StateTreeNode::StateTreeNode
StateTreeNode(QString display, QString basename, NodeType nodeType, StateTreeNodePtr parentNode, StatechartGroupPtr parentGroup, bool stateIsPublic)
Definition: StateTreeNode.cpp:30
armarx::StateTreeNode::getAllSubNodesRecursively
QVector< StateTreeNodePtr > getAllSubNodesRecursively() const
Definition: StateTreeNode.cpp:305
armarx::StateTreeNode::getChildren
QList< StateTreeNodePtr > getChildren() const
Definition: StateTreeNode.cpp:379
armarx::StateTreeNode::getDisplay
QString getDisplay() const
Definition: StateTreeNode.cpp:108
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
armarx::StateTreeNode::NodeType
NodeType
Definition: StateTreeNode.h:45
armarx::StateTreeNode::getParent
StateTreeNodePtr getParent() const
Definition: StateTreeNode.cpp:137
armarx::statechartmodel::StatePtr
std::shared_ptr< State > StatePtr
Definition: State.h:46
armarx::StateTreeNode::Folder
@ Folder
Definition: StateTreeNode.h:45
armarx::StateTreeNode::childCount
int childCount() const
Definition: StateTreeNode.cpp:98
armarx::StateTreeNode::Path
Path
Definition: StateTreeNode.h:46
armarx::StateTreeNode::getNodeType
NodeType getNodeType()
Definition: StateTreeNode.cpp:147
armarx::StateTreeNode::getNestingLevelRelativeToGroup
int getNestingLevelRelativeToGroup()
Definition: StateTreeNode.cpp:359
armarx::StateTreeNode::Path::Absolute
@ Absolute
StateTreeNode.h
armarx::StateTreeNode::getBoostCppFilePath
std::filesystem::path getBoostCppFilePath(Path pathType=Path::Absolute) const
Definition: StateTreeNode.cpp:171
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::StateTreeNode::isFolder
bool isFolder() const
Definition: StateTreeNode.cpp:267