-
Notifications
You must be signed in to change notification settings - Fork 6
/
ExplainableBT.h
371 lines (314 loc) · 15 KB
/
ExplainableBT.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// This file contains Ports and ExplainableBT classes.
// ExplainableBT class contains all the proposed algorithms in the paper.
// The only input needed is a behavior tree. Please refer to
// https://www.behaviortree.dev/ to get familiar with it.
// Please use our fork https://github.com/uml-robotics/BehaviorTree.CPP because
// it contains modifications that make the algorithms easy to reason about.
// You should also be familiar with ROS.
#include <ros/ros.h>
#include <behaviortree_cpp_v3/bt_factory.h>
#include "main_service/Explain.h"
#include "BehaviorTracker.h"
#include <boost/algorithm/string/predicate.hpp> // starts_with
class Ports {
public:
Ports(const std::unordered_map<std::string, std::string>& ports) : ports(ports) {}
std::set<std::string> get_keyed_value_set() {
std::set<std::string> set;
for (const auto &kv : ports) {
std::string value = kv.second;
if (is_keyed(value)) {
set.emplace(value);
}
}
return set;
}
bool has_keyed_value(const std::string &port) {
std::set<std::string> set;
for (const auto &kv : ports) {
std::string value = kv.second;
if (is_keyed(value)) {
if (value == port) {
return true;
}
}
}
return false;
}
private:
static bool is_keyed(const std::string &value) {
return boost::starts_with(value, "{");
}
const std::unordered_map<std::string, std::string>& ports;
};
class ExplainableBT {
public:
explicit ExplainableBT(const BT::Tree & tree) : tree(tree), behavior_tracker(tree) {
printTreeRecursively(tree.root_node);
}
BT::NodeStatus execute() {
return tree.root_node->executeTick();
}
bool explain_callback(main_service::Explain::Request &req, main_service::Explain::Response &res) {
const std::string q = req.what; // question
std::string a; // answer
ROS_INFO_STREAM("Q: " << q);
BT::TreeNode* n = behavior_tracker.get_running_node();
if (q == "What are you doing?") {
a = "I " + n->short_description() + ".";
}
else if (q == "Why are you doing this?") {
std::string goal = behavior_tracker.get_running_node_different_control_parent()->name();
a = "I " + (n->short_description()) + " in order to " + goal + ".";
}
else if (q == "What is your subgoal?") {
BT::TreeNode* tree_parent = behavior_tracker.get_tree_parent();
if (tree_parent == nullptr)
a = "Sorry. I don't have a subgoal.";
else {
std::string subgoal = tree_parent->name();
a = "My subgoal is to " + subgoal + ".";
}
}
else if (q == "How do you achieve your subgoal?" || q== "What are the steps for your subgoal?") {
BT::TreeNode* tree_parent = behavior_tracker.get_tree_parent();
if (tree_parent == nullptr)
a = "Sorry. I don't have a subgoal.";
else {
std::string goal = tree_parent->name();
std::vector<BT::TreeNode *> steps = find_steps(tree_parent);
a = "To achieve the subgoal \"" + goal + "\", I need to do " + std::to_string(steps.size()) +
" steps. ";
for (int i = 0; i < steps.size(); ++i) {
a += std::to_string(i + 1) + ". " + steps.at(i)->name() + ". ";
}
}
}
else if (q == "What is your goal?") {
std::string goal = behavior_tracker.get_overall_goal_node()->name();
a = "My goal is to " + goal + ".";
}
else if (q == "How do you achieve your goal?" || q == "What are the steps for your goal") {
std::string goal = behavior_tracker.get_overall_goal_node()->name();
std::vector<BT::TreeNode*> steps = find_steps(tree.root_node);
a = "To achieve the goal \"" + goal + "\", I need to do " + std::to_string(steps.size()) + " steps. ";
for (int i = 0; i < steps.size(); ++i) {
a += std::to_string(i+1) + ". " + steps.at(i)->name() + ". ";
}
}
else if (q == "What went wrong?") {
BT::TreeNode *running_node = behavior_tracker.get_running_node();
bool is_wrong = false;
bool is_fell_back = false;
BT::FallbackNode* fallback_node = nullptr;
BT::TreeNode *p = running_node->getParent();
while (p != nullptr && p->type() != BT::NodeType::SUBTREE) {
ROS_INFO_STREAM(p->short_description());
bool is_fallback_node = (dynamic_cast<BT::FallbackNode*>(p) != nullptr);
if (is_fallback_node) {
//
// Fallback node
//
fallback_node = dynamic_cast<BT::FallbackNode*>(p);
ROS_INFO_STREAM("Fallback node found: " << fallback_node->short_description());
if (fallback_node->child(0)->status() == BT::NodeStatus::FAILURE) {
is_wrong = true;
is_fell_back = true;
a = "I could not " + fallback_node->short_description() + " because ";
// find the failed child
const BT::TreeNode* failed_child;
BT::applyRecursiveVisitorSelectively(fallback_node, [&failed_child](const BT::TreeNode* node) -> bool {
if (node->has_failed() && (node->type() == BT::NodeType::CONDITION || node->type() == BT::NodeType::ACTION)) {
failed_child = node;
return true;
}
return false;
});
if (failed_child->getParent() != nullptr) {
if (failed_child->getParent()->short_description() != fallback_node->short_description()) {
a += "I was unable to " + failed_child->getParent()->short_description() + " as ";
}
}
a += failed_child->short_description() + " failed.";
break;
}
}
bool is_retry_node = (dynamic_cast<BT::RetryNode*>(p) != nullptr);
if (is_retry_node) {
auto retry_node = dynamic_cast<BT::RetryNode *>(p);
ROS_INFO_STREAM("Retry node found: " << retry_node->short_description());
if (retry_node->is_retrying()) {
is_wrong = true;
// check if have non-null parent
BT::TreeNode *rp = retry_node->getParent();
while (rp == nullptr) {
rp = rp->getParent();
}
if (rp != nullptr) {
a = "I am retrying for attempt " + std::to_string(retry_node->n_th_retry()) + " to " + rp->short_description() + ". ";
// find the failed child
const BT::TreeNode* failed_child;
BT::applyRecursiveVisitorSelectively(retry_node, [&failed_child](const BT::TreeNode* node) -> bool {
if (node->has_failed() && (node->type() == BT::NodeType::CONDITION || node->type() == BT::NodeType::ACTION)) {
failed_child = node;
return true;
}
return false;
});
auto fp = failed_child->getParent();
while (fp->name().empty()) {
fp = fp->getParent();
}
a += "I could not " + fp->short_description() + " because " + failed_child->short_description() + " failed.";;
break;
}
}
}
p = p->getParent();
}
if (is_fell_back) {
// find if there is a parent Retry node retrying. If so, say that
p = fallback_node->getParent();
while (p != nullptr && p->type() != BT::NodeType::SUBTREE) {
bool is_retry_node = (dynamic_cast<BT::RetryNode*>(p) != nullptr);
if (is_retry_node) {
auto retry_node = dynamic_cast<BT::RetryNode *>(p);
ROS_INFO_STREAM("Retry node found: " << retry_node->short_description());
if (retry_node->is_retrying()) {
// check if have non-null parent
BT::TreeNode *rp = retry_node->getParent();
while (rp == nullptr) {
rp = rp->getParent();
}
if (rp != nullptr) {
a += " I am retrying for attempt " + std::to_string(retry_node->n_th_retry()) + " to " + rp->short_description() + ".";
}
}
}
p = p->getParent();
}
}
if ( ! is_wrong)
a = "Nothing went wrong.";
}
else if (boost::starts_with(q, "Can you")) {
std::string asked = q.substr(8, q.size() - 8 - 1);
ROS_INFO_STREAM(asked);
a = asked;
// get tree parent
BT::TreeNode* tree_parent = behavior_tracker.get_tree_parent();
if (tree_parent == nullptr)
tree_parent = tree.root_node;
ROS_INFO_STREAM("tree parent: " + tree_parent->short_description());
// build supported nodes
std::vector<BT::TreeNode*> supported_nodes;
auto visitor = [&supported_nodes](BT::TreeNode* node) {
bool is_sequence_node = (dynamic_cast<BT::SequenceNode*>(node) != nullptr);
if ((is_sequence_node || node->type() == BT::NodeType::SUBTREE) && (! node->name().empty())) {
// if a node with the same name is added, don't add this node
bool has_same_name_node_added = false;
for (auto sn : supported_nodes) {
if (sn->name() == node->name()) {
has_same_name_node_added = true;
break;
}
}
if ( ! has_same_name_node_added) {
ROS_INFO_STREAM("V " << node->short_description());
supported_nodes.emplace_back(node);
}
}
};
applyRecursiveVisitor(tree_parent, visitor);
// find the node, reply if not supported
BT::TreeNode* supported_node;
bool is_supported = false;
for (auto sn : supported_nodes) {
if (sn->name() == asked) {
is_supported = true;
supported_node = sn;
break;
}
}
if ( ! is_supported) {
a = "Sorry. I cannot redo \"" + asked + "\".";
}
else {
ROS_INFO_STREAM("\"" + asked + "\" is supported");
auto ns = find_self_contained_behavior_node(supported_node);
auto goal = behavior_tracker.get_overall_goal_node();
auto subgoal = behavior_tracker.get_tree_parent();
ROS_INFO_STREAM("Checking " << goal->UID() << " vs. " << subgoal->UID());
if (goal->UID() == subgoal->UID()) {
ROS_INFO_STREAM("must have a subtree...");
} else {
auto goal_sequence = dynamic_cast<BT::SequenceNode *>(goal);
std::cout << "Before: " << goal_sequence->childrenCount();
goal_sequence->insertChildAfter(ns, subgoal);
std::cout << "After: " << goal_sequence->childrenCount();
BT::printTreeRecursively(goal);
a = "Yes. I will " + asked + " after " + subgoal->name() + ".";
}
}
}
else {
a = "Sorry. I don't understand that \"" + req.what + "\"";
}
ROS_INFO_STREAM("A: " << a);
res.reply = a;
return true;
}
private:
BT::TreeNode* find_self_contained_behavior_node(BT::TreeNode* supported_node) {
// build unique, keyed input ports
std::set<std::string> unique_keyed_input_ports;
applyRecursiveVisitor(supported_node, [&unique_keyed_input_ports](BT::TreeNode *node) {
if (node->type() == BT::NodeType::ACTION) { // action node only
auto set = Ports(node->config().input_ports).get_keyed_value_set();
unique_keyed_input_ports.insert(set.begin(), set.end());
}
});
// find an ancestor node providing all dynamic input ports
auto ns = supported_node;
for (auto &dynamic_input : unique_keyed_input_ports) {
ROS_INFO_STREAM("looking for " << dynamic_input);
bool found = false;
while ( ! found) {
applyRecursiveVisitor(ns, [&found, &dynamic_input](BT::TreeNode *node) {
if (found) {
return;
}
if (node->type() == BT::NodeType::ACTION) { // action node only
if (Ports(node->config().output_ports).has_keyed_value(dynamic_input)) {
ROS_INFO_STREAM("found " << dynamic_input << "from node: " << node->short_description());
found = true;
}
}
});
if (!found) { // go up a level
ns = ns->getParent();
ROS_INFO_STREAM("went up to: " << ns->short_description());
}
}
}
return ns;
}
std::vector<BT::TreeNode*> find_steps(BT::TreeNode* parent_node) {
std::vector<BT::TreeNode*> steps;
auto visitor = [this, &parent_node, &steps](BT::TreeNode* node) -> bool {
if ( node->name().empty() || node->name() == parent_node->name() || node->type() == BT::NodeType::DECORATOR) {
ROS_INFO_STREAM("X " << node->short_description());
return false;
}
else {
ROS_INFO_STREAM("V " << node->short_description());
steps.emplace_back(node);
return true;
}
};
applyRecursiveVisitorSelectively(parent_node, visitor);
return steps;
}
const BT::Tree &tree;
BehaviorTracker behavior_tracker;
};