Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f139887

Browse files
author
Davide Faconti
committed
issue BehaviorTree#59: add loop in Retry and Repeat
1 parent dafaaf1 commit f139887

File tree

3 files changed

+46
-87
lines changed

3 files changed

+46
-87
lines changed

gtest/gtest_decorator.cpp

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -120,29 +120,16 @@ TEST_F(RetryTest, RetryTestA)
120120
{
121121
action.setBoolean(false);
122122

123-
root.executeTick();
124-
ASSERT_EQ(NodeStatus::RUNNING, root.status());
125-
ASSERT_EQ(1, action.tickCount() );
126-
127-
root.executeTick();
128-
ASSERT_EQ(NodeStatus::RUNNING, root.status());
129-
ASSERT_EQ(2, action.tickCount() );
130-
131123
root.executeTick();
132124
ASSERT_EQ(NodeStatus::FAILURE, root.status());
133125
ASSERT_EQ(3, action.tickCount() );
134126

135-
// try again
136-
action.resetTicks();
137-
root.executeTick();
138-
ASSERT_EQ(NodeStatus::RUNNING, root.status());
139-
ASSERT_EQ(1, action.tickCount() );
140-
141127
action.setBoolean(true);
128+
action.resetTicks();
142129

143130
root.executeTick();
144131
ASSERT_EQ(NodeStatus::SUCCESS, root.status());
145-
ASSERT_EQ(2, action.tickCount() );
132+
ASSERT_EQ(1, action.tickCount() );
146133
}
147134

148135
TEST_F(RepeatTest, RepeatTestA)
@@ -153,42 +140,13 @@ TEST_F(RepeatTest, RepeatTestA)
153140
ASSERT_EQ(NodeStatus::FAILURE, root.status());
154141
ASSERT_EQ(1, action.tickCount() );
155142

156-
root.executeTick();
157-
ASSERT_EQ(NodeStatus::FAILURE, root.status());
158-
ASSERT_EQ(2, action.tickCount() );
159-
160143
//-------------------
161144
action.resetTicks();
162145
action.setBoolean(true);
163146

164-
root.executeTick();
165-
ASSERT_EQ(NodeStatus::RUNNING, root.status());
166-
ASSERT_EQ(1, action.tickCount() );
167-
168-
root.executeTick();
169-
ASSERT_EQ(NodeStatus::RUNNING, root.status());
170-
ASSERT_EQ(2, action.tickCount() );
171-
172147
root.executeTick();
173148
ASSERT_EQ(NodeStatus::SUCCESS, root.status());
174149
ASSERT_EQ(3, action.tickCount() );
175-
176-
//-------------------
177-
action.resetTicks();
178-
action.setBoolean(true);
179-
180-
root.executeTick();
181-
ASSERT_EQ(NodeStatus::RUNNING, root.status());
182-
ASSERT_EQ(1, action.tickCount() );
183-
184-
root.executeTick();
185-
ASSERT_EQ(NodeStatus::RUNNING, root.status());
186-
ASSERT_EQ(2, action.tickCount() );
187-
188-
action.setBoolean(false);
189-
root.executeTick();
190-
ASSERT_EQ(NodeStatus::FAILURE, root.status());
191-
ASSERT_EQ(3, action.tickCount() );
192150
}
193151

194152
// https://github.com/BehaviorTree/BehaviorTree.CPP/issues/57

src/decorators/repeat_node.cpp

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -46,38 +46,39 @@ NodeStatus RepeatNode::tick()
4646
}
4747

4848
setStatus(NodeStatus::RUNNING);
49-
NodeStatus child_state = child_node_->executeTick();
5049

51-
switch (child_state)
50+
while (try_index_ < num_cycles_)
5251
{
53-
case NodeStatus::SUCCESS:
52+
NodeStatus child_state = child_node_->executeTick();
53+
54+
switch (child_state)
5455
{
55-
try_index_++;
56-
if (try_index_ >= num_cycles_)
56+
case NodeStatus::SUCCESS:
5757
{
58-
try_index_ = 0;
59-
return (NodeStatus::SUCCESS);
58+
try_index_++;
6059
}
61-
}
62-
break;
60+
break;
6361

64-
case NodeStatus::FAILURE:
65-
{
66-
try_index_ = 0;
67-
return (NodeStatus::FAILURE);
68-
}
62+
case NodeStatus::FAILURE:
63+
{
64+
try_index_ = 0;
65+
return (NodeStatus::FAILURE);
66+
}
6967

70-
case NodeStatus::RUNNING:
71-
{
72-
return (NodeStatus::RUNNING);
73-
}
68+
case NodeStatus::RUNNING:
69+
{
70+
return NodeStatus::RUNNING;
71+
}
7472

75-
default:
76-
{
77-
throw LogicError("A child node must never return IDLE");
73+
default:
74+
{
75+
throw LogicError("A child node must never return IDLE");
76+
}
7877
}
7978
}
80-
return status();
79+
80+
try_index_ = 0;
81+
return (NodeStatus::SUCCESS);
8182
}
8283

8384
void RepeatNode::halt()

src/decorators/retry_node.cpp

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -51,39 +51,39 @@ NodeStatus RetryNode::tick()
5151
}
5252

5353
setStatus(NodeStatus::RUNNING);
54-
NodeStatus child_state = child_node_->executeTick();
5554

56-
switch (child_state)
55+
while (try_index_ < max_attempts_)
5756
{
58-
case NodeStatus::SUCCESS:
59-
{
60-
try_index_ = 0;
61-
return (NodeStatus::SUCCESS);
62-
}
57+
NodeStatus child_state = child_node_->executeTick();
6358

64-
case NodeStatus::FAILURE:
59+
switch (child_state)
6560
{
66-
try_index_++;
67-
if (try_index_ >= max_attempts_)
61+
case NodeStatus::SUCCESS:
6862
{
6963
try_index_ = 0;
70-
return (NodeStatus::FAILURE);
64+
return (NodeStatus::SUCCESS);
7165
}
72-
}
73-
break;
7466

75-
case NodeStatus::RUNNING:
76-
{
77-
return NodeStatus::RUNNING;
78-
}
67+
case NodeStatus::FAILURE:
68+
{
69+
try_index_++;
70+
}
71+
break;
7972

80-
default:
81-
{
82-
throw LogicError("A child node must never return IDLE");
73+
case NodeStatus::RUNNING:
74+
{
75+
return NodeStatus::RUNNING;
76+
}
77+
78+
default:
79+
{
80+
throw LogicError("A child node must never return IDLE");
81+
}
8382
}
8483
}
8584

86-
return status();
85+
try_index_ = 0;
86+
return (NodeStatus::FAILURE);
8787
}
8888

8989
}

0 commit comments

Comments
 (0)