-
Notifications
You must be signed in to change notification settings - Fork 710
/
Copy pathtree_node.h
604 lines (516 loc) · 19 KB
/
tree_node.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
/* Copyright (C) 2015-2018 Michele Colledanchise - All Rights Reserved
* Copyright (C) 2018-2023 Davide Faconti - All Rights Reserved
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* 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:
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* 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,
* 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.
*/
#pragma once
#include <exception>
#include <map>
#include <utility>
#include "behaviortree_cpp/utils/signal.h"
#include "behaviortree_cpp/basic_types.h"
#include "behaviortree_cpp/blackboard.h"
#include "behaviortree_cpp/utils/strcat.hpp"
#include "behaviortree_cpp/utils/wakeup_signal.hpp"
#include "behaviortree_cpp/scripting/script_parser.hpp"
#ifdef _MSC_VER
#pragma warning(disable : 4127)
#endif
namespace BT
{
/// This information is used mostly by the XMLParser.
struct TreeNodeManifest
{
NodeType type;
std::string registration_ID;
PortsList ports;
KeyValueVector metadata;
};
using PortsRemapping = std::unordered_map<std::string, std::string>;
enum class PreCond
{
// order of the enums also tell us the execution order
FAILURE_IF = 0,
SUCCESS_IF,
ONLY_IF,
SKIP_IF,
WHILE_TRUE,
COUNT_
};
enum class PostCond
{
// order of the enums also tell us the execution order
ON_HALTED = 0,
ON_FAILURE,
ON_SUCCESS,
ALWAYS,
COUNT_
};
template <>
[[nodiscard]] std::string toStr<BT::PostCond>(const BT::PostCond& status);
template <>
[[nodiscard]] std::string toStr<BT::PreCond>(const BT::PreCond& status);
using ScriptingEnumsRegistry = std::unordered_map<std::string, int>;
struct NodeConfig
{
NodeConfig()
{}
// Pointer to the blackboard used by this node
Blackboard::Ptr blackboard;
// List of enums available for scripting
std::shared_ptr<ScriptingEnumsRegistry> enums;
// input ports
PortsRemapping input_ports;
// output ports
PortsRemapping output_ports;
const TreeNodeManifest* manifest = nullptr;
// Numberic unique identifier
uint16_t uid = 0;
// Unique human-readable name, that encapsulate the subtree
// hierarchy, for instance, given 2 nested trees, it should be:
//
// main_tree/nested_tree/my_action
std::string path;
std::map<PreCond, std::string> pre_conditions;
std::map<PostCond, std::string> post_conditions;
};
// back compatibility
using NodeConfiguration = NodeConfig;
template <typename T>
inline constexpr bool hasNodeNameCtor()
{
return std::is_constructible<T, const std::string&>::value;
}
template <typename T, typename... ExtraArgs>
inline constexpr bool hasNodeFullCtor()
{
return std::is_constructible<T, const std::string&, const NodeConfig&,
ExtraArgs...>::value;
}
/// Abstract base class for Behavior Tree Nodes
class TreeNode
{
public:
typedef std::shared_ptr<TreeNode> Ptr;
/**
* @brief TreeNode main constructor.
*
* @param name name of the instance, not the type.
* @param config information about input/output ports. See NodeConfig
*
* Note: If your custom node has ports, the derived class must implement:
*
* static PortsList providedPorts();
*/
TreeNode(std::string name, NodeConfig config);
TreeNode(const TreeNode& other) = delete;
TreeNode& operator=(const TreeNode& other) = delete;
TreeNode(TreeNode&& other) noexcept;
TreeNode& operator=(TreeNode&& other) noexcept;
virtual ~TreeNode();
/// The method that should be used to invoke tick() and setStatus();
virtual BT::NodeStatus executeTick();
void haltNode();
[[nodiscard]] bool isHalted() const;
[[nodiscard]] NodeStatus status() const;
/// Name of the instance, not the type
[[nodiscard]] const std::string& name() const;
/// Blocking function that will sleep until the setStatus() is called with
/// either RUNNING, FAILURE or SUCCESS.
[[nodiscard]] BT::NodeStatus waitValidStatus();
virtual NodeType type() const = 0;
using StatusChangeSignal = Signal<TimePoint, const TreeNode&, NodeStatus, NodeStatus>;
using StatusChangeSubscriber = StatusChangeSignal::Subscriber;
using StatusChangeCallback = StatusChangeSignal::CallableFunction;
using PreTickCallback = std::function<NodeStatus(TreeNode&)>;
using PostTickCallback = std::function<NodeStatus(TreeNode&, NodeStatus)>;
/**
* @brief subscribeToStatusChange is used to attach a callback to a status change.
* When StatusChangeSubscriber goes out of scope (it is a shared_ptr) the callback
* is unsubscribed automatically.
*
* @param callback The callback to be execute when status change.
*
* @return the subscriber handle.
*/
[[nodiscard]] StatusChangeSubscriber
subscribeToStatusChange(StatusChangeCallback callback);
/** This method attaches to the TreeNode a callback with signature:
*
* Optional<NodeStatus> myCallback(TreeNode& node)
*
* This callback is executed BEFORE the tick() and, if it returns a valid Optional<NodeStatus>,
* the actual tick() will NOT be executed and this result will be returned instead.
*
* This is useful to inject a "dummy" implementation of the TreeNode at run-time
*/
void setPreTickFunction(PreTickCallback callback);
/**
* This method attaches to the TreeNode a callback with signature:
*
* Optional<NodeStatus> myCallback(TreeNode& node, NodeStatus new_status)
*
* This callback is executed AFTER the tick() and, if it returns a valid Optional<NodeStatus>,
* the value returned by the actual tick() is overriden with this one.
*/
void setPostTickFunction(PostTickCallback callback);
/// The unique identifier of this instance of treeNode.
/// It is assigneld by the factory
[[nodiscard]] uint16_t UID() const;
/// Human readable identifier, that includes the hierarchy of Subtrees
/// See tutorial 10 as an example.
[[nodiscard]] const std::string& fullPath() const;
/// registrationName is the ID used by BehaviorTreeFactory to create an instance.
[[nodiscard]] const std::string& registrationName() const;
/// Configuration passed at construction time. Can never change after the
/// creation of the TreeNode instance.
[[nodiscard]] const NodeConfig& config() const;
/** Read an input port, which, in practice, is an entry in the blackboard.
* If the blackboard contains a std::string and T is not a string,
* convertFromString<T>() is used automatically to parse the text.
*
* @param key the name of the port.
* @param destination reference to the object where the value should be stored
* @return false if an error occurs.
*/
template <typename T>
Result getInput(const std::string& key, T& destination) const;
/**
* @brief getInputStamped is similar to getInput(dey, destination),
* but it returne also the Timestamp object, that can be used to check if
* a value was updated and when.
*
* @param key the name of the port.
* @param destination reference to the object where the value should be stored
*/
template <typename T>
[[nodiscard]] Expected<Timestamp> getInputStamped(const std::string& key,
T& destination) const;
/** Same as bool getInput(const std::string& key, T& destination)
* but using optional.
*
* @param key the name of the port.
*/
template <typename T>
[[nodiscard]] Expected<T> getInput(const std::string& key) const
{
T out;
auto res = getInput(key, out);
return (res) ? Expected<T>(out) : nonstd::make_unexpected(res.error());
}
/** Same as bool getInputStamped(const std::string& key, T& destination)
* but return StampedValue<T>
*
* @param key the name of the port.
*/
template <typename T>
[[nodiscard]] Expected<StampedValue<T>> getInputStamped(const std::string& key) const
{
StampedValue<T> out;
if(auto res = getInputStamped(key, out.value))
{
out.stamp = *res;
return out;
}
else
{
return nonstd::make_unexpected(res.error());
}
}
/**
* @brief setOutput modifies the content of an Output port
* @param key the name of the port.
* @param value new value
* @return valid Result, if succesful.
*/
template <typename T>
Result setOutput(const std::string& key, const T& value);
/**
* @brief getLockedPortContent should be used when:
*
* - your port contains an object with reference semantic (usually a smart pointer)
* - you want to modify the object we are pointing to.
* - you are concerned about thread-safety.
*
* For example, if your port has type std::shared_ptr<Foo>,
* the code below is NOT thread safe:
*
* auto foo_ptr = getInput<std::shared_ptr<Foo>>("port_name");
* // modifying the content of foo_ptr is NOT thread-safe
*
* What you must do, instead, to guaranty thread-safety, is:
*
* if(auto any_ref = getLockedPortContent("port_name")) {
* Any* any = any_ref.get();
* auto foo_ptr = any->cast<std::shared_ptr<Foo>>();
* // modifying the content of foo_ptr inside this scope IS thread-safe
* }
*
* It is important to destroy the object AnyPtrLocked, to release the lock.
*
* NOTE: this method doesn't work, if the port contains a static string, instead
* of a blackboard pointer.
*
* @param key the identifier of the port.
* @return empty AnyPtrLocked if the blackboard entry doesn't exist or the content
* of the port was a static string.
*/
[[nodiscard]] AnyPtrLocked getLockedPortContent(const std::string& key);
// function provided mostly for debugging purpose to see the raw value
// in the port (no remapping and no conversion to a type)
[[nodiscard]] StringView getRawPortValue(const std::string& key) const;
/// Check a string and return true if it matches the pattern: {...}
[[nodiscard]] static bool isBlackboardPointer(StringView str,
StringView* stripped_pointer = nullptr);
[[nodiscard]] static StringView stripBlackboardPointer(StringView str);
[[nodiscard]] static Expected<StringView> getRemappedKey(StringView port_name,
StringView remapped_port);
/// Notify that the tree should be ticked again()
void emitWakeUpSignal();
[[nodiscard]] bool requiresWakeUp() const;
/** Used to inject config into a node, even if it doesn't have the proper
* constructor
*/
template <class DerivedT, typename... ExtraArgs>
static std::unique_ptr<TreeNode> Instantiate(const std::string& name,
const NodeConfig& config,
ExtraArgs... args)
{
static_assert(hasNodeFullCtor<DerivedT, ExtraArgs...>() ||
hasNodeNameCtor<DerivedT>());
if constexpr(hasNodeFullCtor<DerivedT, ExtraArgs...>())
{
return std::make_unique<DerivedT>(name, config, args...);
}
else if constexpr(hasNodeNameCtor<DerivedT>())
{
auto node_ptr = new DerivedT(name, args...);
node_ptr->config() = config;
return std::unique_ptr<DerivedT>(node_ptr);
}
}
protected:
friend class BehaviorTreeFactory;
friend class DecoratorNode;
friend class ControlNode;
friend class Tree;
[[nodiscard]] NodeConfig& config();
/// Method to be implemented by the user
virtual BT::NodeStatus tick() = 0;
/// Set the status to IDLE
void resetStatus();
// Only BehaviorTreeFactory should call this
void setRegistrationID(StringView ID);
void setWakeUpInstance(std::shared_ptr<WakeUpSignal> instance);
void modifyPortsRemapping(const PortsRemapping& new_remapping);
/**
* @brief setStatus changes the status of the node.
* it will throw if you try to change the status to IDLE, because
* your parent node should do that, not the user!
*/
void setStatus(NodeStatus new_status);
using PreScripts = std::array<ScriptFunction, size_t(PreCond::COUNT_)>;
using PostScripts = std::array<ScriptFunction, size_t(PostCond::COUNT_)>;
PreScripts& preConditionsScripts();
PostScripts& postConditionsScripts();
template <typename T>
T parseString(const std::string& str) const;
private:
struct PImpl;
std::unique_ptr<PImpl> _p;
Expected<NodeStatus> checkPreConditions();
void checkPostConditions(NodeStatus status);
/// The method used to interrupt the execution of a RUNNING node.
/// Only Async nodes that may return RUNNING should implement it.
virtual void halt() = 0;
};
//-------------------------------------------------------
template <typename T>
T TreeNode::parseString(const std::string& str) const
{
if constexpr(std::is_enum_v<T> && !std::is_same_v<T, NodeStatus>)
{
auto it = config().enums->find(str);
// conversion available
if(it != config().enums->end())
{
return static_cast<T>(it->second);
}
else
{
// hopefully str contains a number that can be parsed. May throw
return static_cast<T>(convertFromString<int>(str));
}
}
return convertFromString<T>(str);
}
template <typename T>
inline Expected<Timestamp> TreeNode::getInputStamped(const std::string& key,
T& destination) const
{
std::string port_value_str;
auto input_port_it = config().input_ports.find(key);
if(input_port_it != config().input_ports.end())
{
port_value_str = input_port_it->second;
}
else
{
// maybe it is declared with a default value in the manifest
auto port_manifest_it = config().manifest->ports.find(key);
if(port_manifest_it == config().manifest->ports.end())
{
return nonstd::make_unexpected(StrCat("getInput() of node '", fullPath(),
"' failed because the manifest doesn't "
"contain the key: [",
key, "]"));
}
const auto& port_info = port_manifest_it->second;
// there is a default value
if(port_info.defaultValue().empty())
{
return nonstd::make_unexpected(StrCat("getInput() of node '", fullPath(),
"' failed because nor the manifest or the "
"XML contain the key: [",
key, "]"));
}
if(port_info.defaultValue().isString())
{
port_value_str = port_info.defaultValue().cast<std::string>();
}
else
{
destination = port_info.defaultValue().cast<T>();
return Timestamp{};
}
}
auto blackboard_ptr = getRemappedKey(key, port_value_str);
try
{
// pure string, not a blackboard key
if(!blackboard_ptr)
{
try
{
destination = parseString<T>(port_value_str);
}
catch(std::exception& ex)
{
return nonstd::make_unexpected(StrCat("getInput(): ", ex.what()));
}
return Timestamp{};
}
const auto& blackboard_key = blackboard_ptr.value();
if(!config().blackboard)
{
return nonstd::make_unexpected("getInput(): trying to access "
"an invalid Blackboard");
}
if(auto entry = config().blackboard->getEntry(std::string(blackboard_key)))
{
std::unique_lock lk(entry->entry_mutex);
auto& any_value = entry->value;
// support getInput<Any>()
if constexpr(std::is_same_v<T, Any>)
{
destination = any_value;
return Timestamp{ entry->sequence_id, entry->stamp };
}
if(!entry->value.empty())
{
if(!std::is_same_v<T, std::string> && any_value.isString())
{
destination = parseString<T>(any_value.cast<std::string>());
}
else
{
destination = any_value.cast<T>();
}
return Timestamp{ entry->sequence_id, entry->stamp };
}
}
return nonstd::make_unexpected(StrCat("getInput() failed because it was unable to "
"find the key [",
key, "] remapped to [", blackboard_key, "]"));
}
catch(std::exception& err)
{
return nonstd::make_unexpected(err.what());
}
}
template <typename T>
inline Result TreeNode::getInput(const std::string& key, T& destination) const
{
auto res = getInputStamped(key, destination);
if(!res)
{
return nonstd::make_unexpected(res.error());
}
return {};
}
template <typename T>
inline Result TreeNode::setOutput(const std::string& key, const T& value)
{
if(!config().blackboard)
{
return nonstd::make_unexpected("setOutput() failed: trying to access a "
"Blackboard(BB) entry, but BB is invalid");
}
auto remap_it = config().output_ports.find(key);
if(remap_it == config().output_ports.end())
{
return nonstd::make_unexpected(StrCat("setOutput() failed: "
"NodeConfig::output_ports "
"does not contain the key: [",
key, "]"));
}
StringView remapped_key = remap_it->second;
if(remapped_key == "{=}" || remapped_key == "=")
{
config().blackboard->set(static_cast<std::string>(key), value);
return {};
}
if(!isBlackboardPointer(remapped_key))
{
return nonstd::make_unexpected("setOutput requires a blackboard pointer. Use {}");
}
if constexpr(std::is_same_v<BT::Any, T>)
{
if(config().manifest->ports.at(key).type() != typeid(BT::Any))
{
throw LogicError("setOutput<Any> is not allowed, unless the port "
"was declared using OutputPort<Any>");
}
}
remapped_key = stripBlackboardPointer(remapped_key);
config().blackboard->set(static_cast<std::string>(remapped_key), value);
return {};
}
// Utility function to fill the list of ports using T::providedPorts();
template <typename T>
inline void assignDefaultRemapping(NodeConfig& config)
{
for(const auto& it : getProvidedPorts<T>())
{
const auto& port_name = it.first;
const auto direction = it.second.direction();
if(direction != PortDirection::OUTPUT)
{
// PortDirection::{INPUT,INOUT}
config.input_ports[port_name] = "{=}";
}
if(direction != PortDirection::INPUT)
{
// PortDirection::{OUTPUT,INOUT}
config.output_ports[port_name] = "{=}";
}
}
}
} // namespace BT