-
Notifications
You must be signed in to change notification settings - Fork 714
/
Copy pathset_blackboard_node.h
118 lines (102 loc) · 4.02 KB
/
set_blackboard_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
/* Copyright (C) 2018-2020 Davide Faconti, Eurecat - 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.
*/
#ifndef ACTION_SETBLACKBOARD_NODE_H
#define ACTION_SETBLACKBOARD_NODE_H
#include "behaviortree_cpp/action_node.h"
namespace BT
{
/**
* @brief The SetBlackboard is action used to store a string
* into an entry of the Blackboard specified in "output_key".
*
* Example usage:
*
* <SetBlackboard value="42" output_key="the_answer" />
*
* Will store the string "42" in the entry with key "the_answer".
*
* Alternatively, you can use it to copy one port inside another port:
*
* <SetBlackboard value="{src_port}" output_key="dst_port" />
*
* This will copy the type and content of {src_port} into {dst_port}
*/
class SetBlackboardNode : public SyncActionNode
{
public:
SetBlackboardNode(const std::string& name, const NodeConfig& config)
: SyncActionNode(name, config)
{
setRegistrationID("SetBlackboard");
}
static PortsList providedPorts()
{
return { InputPort("value", "Value to be written int othe output_key"),
BidirectionalPort("output_key", "Name of the blackboard entry where the "
"value should be written") };
}
private:
virtual BT::NodeStatus tick() override
{
std::string output_key;
if(!getInput("output_key", output_key))
{
throw RuntimeError("missing port [output_key]");
}
const std::string value_str = config().input_ports.at("value");
StringView stripped_key;
BT::Any out_value;
std::shared_ptr<Blackboard::Entry> dst_entry =
config().blackboard->getEntry(output_key);
if(isBlackboardPointer(value_str, &stripped_key))
{
const auto input_key = std::string(stripped_key);
std::shared_ptr<Blackboard::Entry> src_entry =
config().blackboard->getEntry(input_key);
if(!src_entry)
{
throw RuntimeError("Can't find the port referred by [value]");
}
if(!dst_entry)
{
config().blackboard->createEntry(output_key, src_entry->info);
dst_entry = config().blackboard->getEntry(output_key);
}
out_value = src_entry->value;
}
else
{
out_value = BT::Any(value_str);
}
if(out_value.empty())
return NodeStatus::FAILURE;
// avoid type issues when port is remapped: current implementation of the set might be a little bit problematic for initialized on the fly values
// this still does not attack math issues
if(dst_entry && dst_entry->info.type() != typeid(std::string) && out_value.isString())
{
try
{
out_value = dst_entry->info.parseString(out_value.cast<std::string>());
}
catch(const std::exception& e)
{
throw LogicError("Can't convert string [", out_value.cast<std::string>(),
"] to type [", BT::demangle(dst_entry->info.type()),
"]: ", e.what());
}
}
config().blackboard->set(output_key, out_value);
return NodeStatus::SUCCESS;
}
};
} // namespace BT
#endif