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

Skip to content

Commit 18f43ce

Browse files
author
Davide Faconti
committed
unit test added and problems fixed with onInit()
1 parent e2d1024 commit 18f43ce

File tree

8 files changed

+100
-9
lines changed

8 files changed

+100
-9
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ set(BT_TESTS
166166
gtest/gtest_fallback.cpp
167167
gtest/gtest_factory.cpp
168168
gtest/gtest_decorator.cpp
169+
gtest/gtest_blackboard.cpp
169170
)
170171

171172
if(ament_cmake_FOUND AND BUILD_TESTING)

gtest/gtest_blackboard.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/* Copyright (C) 2018 Davide Faconti - All Rights Reserved
2+
*
3+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
4+
* to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
5+
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7+
*
8+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
10+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11+
*/
12+
13+
#include <gtest/gtest.h>
14+
#include "action_test_node.h"
15+
#include "condition_test_node.h"
16+
#include "behaviortree_cpp/behavior_tree.h"
17+
#include "behaviortree_cpp/blackboard/blackboard_local.h"
18+
19+
using namespace BT;
20+
21+
class InitTestNode: public ActionNodeBase
22+
{
23+
public:
24+
InitTestNode(bool try_to_access_bb, const std::string& name):
25+
ActionNodeBase(name),
26+
_value(0)
27+
{
28+
if( try_to_access_bb )
29+
{
30+
// this should throw
31+
blackboard()->set(KEY(), 33);
32+
}
33+
}
34+
35+
void onInit() {
36+
blackboard()->get(KEY(), _value);
37+
}
38+
void halt() {}
39+
40+
NodeStatus tick()
41+
{
42+
_value *= 2;
43+
blackboard()->set(KEY(), _value);
44+
return NodeStatus::SUCCESS;
45+
}
46+
47+
static const char* KEY() { return "my_entry"; }
48+
49+
private:
50+
int _value;
51+
};
52+
53+
54+
55+
56+
/****************TESTS START HERE***************************/
57+
58+
TEST(BlackboardTest, CheckOInit)
59+
{
60+
auto bb = Blackboard::create<BlackboardLocal>();
61+
const auto KEY = InitTestNode::KEY();
62+
63+
EXPECT_THROW( InitTestNode(true,"init_test"), std::logic_error );
64+
65+
InitTestNode node(false,"init_test");
66+
node.setBlackboard(bb);
67+
68+
bb->set(KEY, 11 );
69+
70+
// this should read and write "my_entry", respectively in onInit() and tick()
71+
node.executeTick();
72+
73+
ASSERT_EQ( bb->get<int>(KEY), 22 );
74+
75+
// check that onInit is executed only once
76+
bb->set(KEY, 1 );
77+
78+
// since this value is read in OnInit(), the node will not notice the change in bb
79+
node.setStatus( NodeStatus::IDLE );
80+
node.executeTick();
81+
ASSERT_EQ( bb->get<int>(KEY), 44 );
82+
}

gtest/include/action_test_node.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class SyncActionTest : public ActionNodeBase
3333
int tick_count_;
3434
};
3535

36-
class AsyncActionTest : public ActionNode
36+
class AsyncActionTest : public AsyncActionNode
3737
{
3838
public:
3939
AsyncActionTest(const std::string& name);

include/behaviortree_cpp/action_node.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@
1919

2020
namespace BT
2121
{
22+
/** IMPORTANT: to avoid unexpected behaviors when Sequence (not SequenceStar) is used
23+
* an Action that returned SUCCESS or FAILURE will not be ticked again unless
24+
* setStatus(IDLE) is called first (reset the Action).
25+
*
26+
* Usually the parent node takes care of this for you.
27+
*/
28+
29+
2230
class ActionNodeBase : public LeafNode
2331
{
2432
public:

include/behaviortree_cpp/tree_node.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ class TreeNode
137137

138138
friend class BehaviorTreeFactory;
139139

140-
void initialize();
140+
void initializeOnce();
141141

142142
private:
143143

src/action_node.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ ActionNodeBase::ActionNodeBase(const std::string& name, const NodeParameters& pa
2323

2424
NodeStatus ActionNodeBase::executeTick()
2525
{
26-
initialize();
26+
initializeOnce();
2727
NodeStatus prev_status = status();
2828

2929
if (prev_status == NodeStatus::IDLE || prev_status == NodeStatus::RUNNING)
@@ -94,7 +94,7 @@ void AsyncActionNode::waitForTick()
9494

9595
NodeStatus AsyncActionNode::executeTick()
9696
{
97-
initialize();
97+
initializeOnce();
9898
//send signal to other thread.
9999
// The other thread is in charge for changing the status
100100
if (status() == NodeStatus::IDLE)
@@ -145,7 +145,7 @@ void CoroActionNode::setStatusRunningAndYield()
145145

146146
NodeStatus CoroActionNode::executeTick()
147147
{
148-
initialize();
148+
initializeOnce();
149149
if (status() == NodeStatus::IDLE)
150150
{
151151
_p->coro = coroutine::create( [this]() { setStatus(tick()); } );

src/bt_factory.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ std::unique_ptr<TreeNode> BehaviorTreeFactory::instantiateTreeNode(
140140
std::unique_ptr<TreeNode> node = it->second(name, params);
141141
node->setRegistrationName(ID);
142142
node->setBlackboard(blackboard);
143-
node->initialize();
143+
node->initializeOnce();
144144

145145
return node;
146146
}

src/tree_node.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ TreeNode::TreeNode(const std::string& name, const NodeParameters& parameters)
3434

3535
NodeStatus TreeNode::executeTick()
3636
{
37-
37+
initializeOnce();
3838
const NodeStatus status = tick();
3939
setStatus(status);
4040
return status;
@@ -115,12 +115,12 @@ void TreeNode::setRegistrationName(const std::string& registration_name)
115115
registration_name_ = registration_name;
116116
}
117117

118-
void TreeNode::initialize()
118+
void TreeNode::initializeOnce()
119119
{
120120
if( not_initialized_ )
121121
{
122-
onInit();
123122
not_initialized_ = false;
123+
onInit();
124124
}
125125
}
126126

0 commit comments

Comments
 (0)