Skip to content

Commit 94c202c

Browse files
committed
Imported upstream version '3.8.6' of 'upstream'
1 parent f43ad9c commit 94c202c

File tree

11 files changed

+265
-57
lines changed

11 files changed

+265
-57
lines changed

include/behaviortree_cpp_v3/blackboard.h

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,24 +48,7 @@ class Blackboard
4848
*
4949
* @return the pointer or nullptr if it fails.
5050
*/
51-
const Any* getAny(const std::string& key) const
52-
{
53-
std::unique_lock<std::mutex> lock(mutex_);
54-
auto it = storage_.find(key);
55-
56-
if(it == storage_.end())
57-
{
58-
// Try with autoremapping. This should work recursively
59-
if(autoremapping_)
60-
{
61-
if(auto parent = parent_bb_.lock()) {
62-
return parent->getAny(key);
63-
}
64-
}
65-
return nullptr;
66-
}
67-
return &(it->second->value);
68-
}
51+
const Any* getAny(const std::string& key) const;
6952

7053
Any* getAny(const std::string& key)
7154
{
@@ -122,8 +105,15 @@ class Blackboard
122105
else
123106
{
124107
Any new_value(value);
108+
std::shared_ptr<Blackboard::Entry> entry;
125109
lock.unlock();
126-
entry = createEntryImpl(key, PortInfo(PortDirection::INOUT, new_value.type(), {}));
110+
if(std::is_constructible<std::string, T>::value)
111+
{
112+
entry = createEntryImpl(key, PortInfo(PortDirection::INOUT));
113+
}
114+
else {
115+
entry = createEntryImpl(key, PortInfo(PortDirection::INOUT, new_value.type(), {}));
116+
}
127117
entry->value = new_value;
128118
return;
129119
}

package.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0"?>
22
<package format="3">
33
<name>behaviortree_cpp_v3</name>
4-
<version>3.8.5</version>
4+
<version>3.8.6</version>
55
<description>
66
This package provides the Behavior Trees core library.
77
</description>

src/blackboard.cpp

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,35 @@ void Blackboard::enableAutoRemapping(bool remapping)
77
autoremapping_ = remapping;
88
}
99

10+
const Any *Blackboard::getAny(const std::string &key) const
11+
{
12+
std::unique_lock<std::mutex> lock(mutex_);
13+
auto it = storage_.find(key);
14+
15+
if(it == storage_.end())
16+
{
17+
// Try with autoremapping. This should work recursively
18+
auto remapping_it = internal_to_external_.find(key);
19+
if (remapping_it != internal_to_external_.end())
20+
{
21+
const auto& remapped_key = remapping_it->second;
22+
if (auto parent = parent_bb_.lock())
23+
{
24+
return parent->getAny(remapped_key);
25+
}
26+
}
27+
28+
else if(autoremapping_)
29+
{
30+
if(auto parent = parent_bb_.lock()) {
31+
return parent->getAny(key);
32+
}
33+
}
34+
return nullptr;
35+
}
36+
return &(it->second->value);
37+
}
38+
1039
const PortInfo* Blackboard::portInfo(const std::string& key)
1140
{
1241
std::unique_lock<std::mutex> lock(mutex_);
@@ -75,12 +104,14 @@ Blackboard::createEntryImpl(const std::string &key, const PortInfo& info)
75104
auto storage_it = storage_.find(key);
76105
if(storage_it != storage_.end())
77106
{
78-
const auto old_type = storage_it->second->port_info.type();
79-
if (old_type && info.type() && old_type != info.type())
107+
const auto& prev_info = storage_it->second->port_info;
108+
if (prev_info.type() != info.type() &&
109+
prev_info.isStronglyTyped() &&
110+
info.isStronglyTyped())
80111
{
81112
throw LogicError("Blackboard: once declared, the type of a port "
82113
"shall not change. Previously declared type [",
83-
BT::demangle(old_type), "] != new type [",
114+
BT::demangle(prev_info.type()), "] != new type [",
84115
BT::demangle(info.type()), "]");
85116
}
86117
return storage_it->second;

src/controls/reactive_fallback.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ NodeStatus ReactiveFallback::tick()
2626
switch (child_status)
2727
{
2828
case NodeStatus::RUNNING: {
29-
for (size_t i = index + 1; i < childrenCount(); i++)
29+
30+
// reset the previous children, to make sure that they are in IDLE state
31+
// the next time we tick them
32+
for (size_t i = 0; i < index; i++)
3033
{
3134
haltChild(i);
3235
}

src/controls/reactive_sequence.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ namespace BT
1717
NodeStatus ReactiveSequence::tick()
1818
{
1919
size_t success_count = 0;
20-
size_t running_count = 0;
2120

2221
for (size_t index = 0; index < childrenCount(); index++)
2322
{
@@ -27,9 +26,9 @@ NodeStatus ReactiveSequence::tick()
2726
switch (child_status)
2827
{
2928
case NodeStatus::RUNNING: {
30-
running_count++;
31-
32-
for (size_t i = index + 1; i < childrenCount(); i++)
29+
// reset the previous children, to make sure that they are in IDLE state
30+
// the next time we tick them
31+
for (size_t i = 0; i < index; i++)
3332
{
3433
haltChild(i);
3534
}
@@ -51,6 +50,7 @@ NodeStatus ReactiveSequence::tick()
5150
} // end switch
5251
} //end for
5352

53+
5454
if (success_count == childrenCount())
5555
{
5656
resetChildren();

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ set(BT_TESTS
1515
gtest_blackboard.cpp
1616
gtest_blackboard_precondition.cpp
1717
gtest_ports.cpp
18+
gtest_reactive.cpp
1819
navigation_test.cpp
1920
gtest_subtree.cpp
2021
gtest_switch.cpp

tests/gtest_fallback.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ TEST_F(ReactiveFallbackTest, Condition1ToTrue)
149149
BT::NodeStatus state = root.executeTick();
150150

151151
ASSERT_EQ(NodeStatus::RUNNING, state);
152-
ASSERT_EQ(NodeStatus::FAILURE, condition_1.status());
153-
ASSERT_EQ(NodeStatus::FAILURE, condition_2.status());
152+
ASSERT_EQ(NodeStatus::IDLE, condition_1.status());
153+
ASSERT_EQ(NodeStatus::IDLE, condition_2.status());
154154
ASSERT_EQ(NodeStatus::RUNNING, action_1.status());
155155

156156
condition_1.setExpectedResult(NodeStatus::SUCCESS);
@@ -171,8 +171,8 @@ TEST_F(ReactiveFallbackTest, Condition2ToTrue)
171171
BT::NodeStatus state = root.executeTick();
172172

173173
ASSERT_EQ(NodeStatus::RUNNING, state);
174-
ASSERT_EQ(NodeStatus::FAILURE, condition_1.status());
175-
ASSERT_EQ(NodeStatus::FAILURE, condition_2.status());
174+
ASSERT_EQ(NodeStatus::IDLE, condition_1.status());
175+
ASSERT_EQ(NodeStatus::IDLE, condition_2.status());
176176
ASSERT_EQ(NodeStatus::RUNNING, action_1.status());
177177

178178
condition_2.setExpectedResult(NodeStatus::SUCCESS);

tests/gtest_reactive.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include <gtest/gtest.h>
2+
#include "behaviortree_cpp_v3/bt_factory.h"
3+
#include "test_helper.hpp"
4+
#include "behaviortree_cpp_v3/loggers/bt_cout_logger.h"
5+
6+
using BT::NodeStatus;
7+
using std::chrono::milliseconds;
8+
9+
class SleepNode : public BT::StatefulActionNode
10+
{
11+
public:
12+
13+
SleepNode(const std::string& name, const BT::NodeConfiguration& config):
14+
StatefulActionNode(name, config) {}
15+
16+
NodeStatus onStart() override {
17+
count_ = 0;
18+
return NodeStatus::RUNNING;
19+
}
20+
21+
NodeStatus onRunning() override {
22+
return ++count_ < 10 ? NodeStatus::RUNNING : NodeStatus::SUCCESS;
23+
}
24+
25+
void onHalted() override {}
26+
27+
static BT::PortsList providedPorts(){
28+
return {};
29+
}
30+
31+
private:
32+
int count_ = 0;
33+
};
34+
35+
36+
TEST(Reactive, TestLogging)
37+
{
38+
using namespace BT;
39+
40+
static const char* reactive_xml_text = R"(
41+
<root>
42+
<BehaviorTree ID="Main">
43+
<ReactiveSequence>
44+
<TestA name="testA"/>
45+
<AlwaysSuccess name="success"/>
46+
<Sleep/>
47+
</ReactiveSequence>
48+
</BehaviorTree>
49+
</root>
50+
)";
51+
52+
BehaviorTreeFactory factory;
53+
54+
factory.registerNodeType<SleepNode>("Sleep");
55+
56+
std::array<int, 1> counters;
57+
RegisterTestTick(factory, "Test", counters);
58+
59+
auto tree = factory.createTreeFromText(reactive_xml_text);
60+
StdCoutLogger logger(tree);
61+
62+
auto ret = tree.tickRootWhileRunning();
63+
ASSERT_EQ(ret, NodeStatus::SUCCESS);
64+
65+
int num_ticks = counters[0];
66+
ASSERT_GE(num_ticks, 10);
67+
}
68+
69+

tests/gtest_sequence.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ TEST_F(ComplexSequenceTest, ComplexSequenceConditionsTrue)
226226
BT::NodeStatus state = root.executeTick();
227227

228228
ASSERT_EQ(NodeStatus::RUNNING, state);
229-
ASSERT_EQ(NodeStatus::SUCCESS, seq_conditions.status());
229+
ASSERT_EQ(NodeStatus::IDLE, seq_conditions.status());
230230
ASSERT_EQ(NodeStatus::IDLE, condition_1.status());
231231
ASSERT_EQ(NodeStatus::IDLE, condition_1.status());
232232
ASSERT_EQ(NodeStatus::RUNNING, action_1.status());

0 commit comments

Comments
 (0)