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
28using namespace armarx;
29
31 QString basename,
32 NodeType nodeType,
33 StateTreeNodePtr parentNode,
34 StatechartGroupPtr parentGroup,
35 bool stateIsPublic)
36{
37 this->display = display;
38 this->basename = basename;
39 this->nodeType = nodeType;
40 this->parentNode = parentNode;
41 this->parentGroup = parentGroup;
42 this->stateIsPublic = stateIsPublic;
43 this->cppExists = false;
45}
46
48{
49 //qDeleteAll(children);
50}
51
52void
57
58int
59StateTreeNode::NodeTypeToSortIndex(NodeType nodeType)
60{
61 switch (nodeType)
62 {
63 case Group:
64 return 0;
65
66 case Folder:
67 return 1;
68
69 case State:
70 return 2;
71
72 default:
73 return 999;
74 }
75}
76
77bool
78StateTreeNode::CompareChildren(const StateTreeNodePtr& a, const StateTreeNodePtr& b)
79{
80 int aTypeIndex = NodeTypeToSortIndex(a->nodeType);
81 int bTypeIndex = NodeTypeToSortIndex(b->nodeType);
82
83 if (aTypeIndex != bTypeIndex)
84 {
85 return aTypeIndex < bTypeIndex;
86 }
87
88 return a->display.toLower().localeAwareCompare(b->display.toLower()) < 0;
89}
90
91void
93{
94 qStableSort(children.begin(), children.end(), CompareChildren);
95}
96
97void
99{
100 children.removeOne(child);
101}
102
105{
106 return children.value(row);
107}
108
109int
111{
112 return children.count();
113}
114
115int
117{
118 return 1;
119}
120
121QString
123{
124 return display;
125}
126
127int
129{
130 StateTreeNodePtr parent = parentNode.lock();
131
132 if (parent)
133 {
134 //return parentNode->children.indexOf(const_cast<StatechartGroupTreeNode*>(this));
135
136 //NOT WORKING:
137 //return parent->children.indexOf(const_cast<StatechartGroupTreeNodePtr>(shared_from_this()));
138 for (int i = 0; i < parent->children.count(); i++)
139 {
140 if (parent->children.at(i).get() == this)
141 {
142 return i;
143 }
144 }
145
146 return -1;
147 }
148
149 return 0;
150}
151
154{
155 return parentNode.lock();
156}
157
160{
161 return parentGroup.lock();
162}
163
166{
167 return nodeType;
168}
169
170QString
172{
173 return basename;
174}
175
176std::filesystem::path
178{
179 return parentGroup.lock()->getBoostGroupPath() / getBoostPathRelativeToGroup();
180}
181
182std::filesystem::path
184{
185 if (!state)
186 {
187 return std::filesystem::path();
188 }
189
191}
192
193std::filesystem::path
195{
196 if (!state)
197 {
198 return std::filesystem::path();
199 }
200
201 std::string filename = std::string(state->getStateName().toUtf8().data()) + ".cpp";
202 auto path =
204 return (path.remove_filename() / filename);
205}
206
207std::filesystem::path
209{
210 if (!state)
211 {
212 return std::filesystem::path();
213 }
214
215 std::string filename = std::string(state->getStateName().toUtf8().data()) + ".h";
216 auto path =
218 return (path.remove_filename() / filename);
219}
220
221std::filesystem::path
223{
224 if (!state)
225 {
226 return std::filesystem::path();
227 }
228
229 std::string filename = std::string(state->getStateName().toUtf8().data()) + ".generated.h";
230 auto path =
232 return (path.remove_filename() / filename);
233}
234
235bool
237{
238 if (!state)
239 {
240 return false;
241 }
242
243 return cppExists;
244}
245
246bool
248{
249 if (!state)
250 {
251 return false;
252 }
253
254 cppExists = std::filesystem::exists(getBoostCppFilePath());
255 return cppExists;
256}
257
258std::filesystem::path
259StateTreeNode::getBoostPath(std::filesystem::path parentPath) const
260{
261 return parentPath / basename.toUtf8().data();
262}
263
264std::filesystem::path
266{
267 std::filesystem::path result = basename.toUtf8().data();
269
270 while (p && !p->isGroup())
271 {
272 result = std::filesystem::path(p->getBasename().toUtf8().data()) / result;
273 p = p->getParent();
274 }
275
276 return result;
277}
278
281{
282 return state;
283}
284
285bool
287{
288 return nodeType == State;
289}
290
291bool
293{
294 return nodeType == Folder || nodeType == Group;
295}
296
297bool
299{
300 return nodeType == Group;
301}
302
303bool
305{
306 return nodeType == Folder;
307}
308
311{
312 StateTreeNodePtr node = shared_from_this();
313
314 if (isState())
315 {
316 node = node->getParent();
317 }
318
319 ARMARX_CHECK_EXPRESSION(node->isFolderOrGroup()) << "State Tree is broken.";
320 return node;
321}
322
325{
326 if (state && state->getStateName() == name)
327 {
328 return shared_from_this();
329 }
330
331 for (int i = 0; i < children.count(); i++)
332 {
333 StateTreeNodePtr match = children.at(i)->findNodeByStateName(name);
334
335 if (match)
336 {
337 return match;
338 }
339 }
340
341 return StateTreeNodePtr();
342}
343
344QVector<StateTreeNodePtr>
346{
347 QVector<StateTreeNodePtr> result;
348
349 for (const StateTreeNodePtr& node : children)
350 {
351 result.push_back(node);
352
353 for (const auto& n : node->getAllSubNodesRecursively())
354 {
355 result.push_back(n);
356 }
357 }
358
359 return result;
360}
361
362QVector<statechartmodel::StatePtr>
364{
365 QVector<statechartmodel::StatePtr> result;
366
367 if (state)
368 {
369 result.push_back(state);
370 GetAllSubstatesRecursively(state, result, localOnly);
371 }
372 else
373 {
374 for (const auto& child : children)
375 {
376 for (const auto& state : child->getAllStatesRecursively(localOnly))
377 {
378 result.push_back(state);
379 }
380 }
381 }
382
383 return result;
384}
385
386void
387StateTreeNode::GetAllSubstatesRecursively(const statechartmodel::StatePtr& state,
388 QVector<statechartmodel::StatePtr>& result,
389 bool localOnly) const
390{
391 for (const auto& substate : state->getSubstates())
392 {
393 const auto& stateClass = substate->getStateClass();
394
395 if (stateClass && (!localOnly || substate->getType() != eRemoteState))
396 {
397 result.push_back(stateClass);
398 GetAllSubstatesRecursively(stateClass, result, localOnly);
399 }
400 }
401}
402
403int
405{
406 if (isState() || isFolder())
407 {
408 return parentNode.lock()->getNestingLevelRelativeToGroup() + 1;
409 }
410
411 return 0;
412}
413
414bool
416{
417 std::string str = name.toUtf8().data();
418 boost::regex nameRegex("[a-zA-Z][a-zA-Z0-9]*");
419 boost::smatch what;
420 return boost::regex_match(str, what, nameRegex);
421}
422
423QList<StateTreeNodePtr>
425{
426 return children;
427}
428
429bool
431{
432 return stateIsPublic;
433}
434
435void
437{
438 stateIsPublic = isPublic;
439}
std::string str(const T &t)
void appendChild(StateTreeNodePtr child)
std::filesystem::path getBoostHFilePath(Path pathType=Path::Absolute) const
QVector< statechartmodel::StatePtr > getAllStatesRecursively(bool localOnly) const
QVector< StateTreeNodePtr > getAllSubNodesRecursively() const
armarx::statechartmodel::StatePtr getState() const
std::filesystem::path getBoostPath(std::filesystem::path parentPath) const
void setPublic(bool isPublic)
std::filesystem::path getBoostGeneratedHFilePath(Path pathType=Path::Absolute) const
std::filesystem::path getAbsoluteBoostPath() const
StateTreeNodePtr child(int row)
StatechartGroupPtr getGroup()
std::filesystem::path getBoostXmlFilePath(Path pathType=Path::Absolute) const
QList< StateTreeNodePtr > getChildren() const
QString getBasename() const
StateTreeNode(QString display, QString basename, NodeType nodeType, StateTreeNodePtr parentNode, StatechartGroupPtr parentGroup, bool stateIsPublic)
std::filesystem::path getBoostPathRelativeToGroup() const
static bool CheckNodeName(QString name)
void removeChild(StateTreeNodePtr child)
StateTreeNodePtr getParent() const
StateTreeNodePtr findNodeByStateName(QString name)
StateTreeNodePtr getClosestParentFolderOrGroupNode()
std::filesystem::path getBoostCppFilePath(Path pathType=Path::Absolute) const
QString getDisplay() const
Class that offers the main functionality needed to create a statechart.
Definition State.h:54
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
double a(double t, double a0, double j)
Definition CtrlUtil.h:45
std::shared_ptr< State > StatePtr
Definition State.h:48
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::shared_ptr< StatechartGroup > StatechartGroupPtr
std::shared_ptr< StateTreeNode > StateTreeNodePtr