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

Skip to content

Commit 5fea315

Browse files
author
Davide Faconti
committed
trying to integrate default values into ports (WIP)
1 parent 67dd2a7 commit 5fea315

File tree

5 files changed

+100
-37
lines changed

5 files changed

+100
-37
lines changed

include/behaviortree_cpp/basic_types.h

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,22 +121,30 @@ StringConverter GetAnyFromStringFunctor<void>()
121121

122122
//------------------------------------------------------------------
123123

124+
template <typename T>
125+
std::string toStr(T value)
126+
{
127+
return std::to_string(value);
128+
}
129+
130+
template<> std::string toStr<BT::NodeStatus>(BT::NodeStatus status);
131+
124132
/**
125133
* @brief toStr converts NodeStatus to string. Optionally colored.
126134
*/
127-
const char* toStr(BT::NodeStatus status, bool colored = false);
135+
std::string toStr(BT::NodeStatus status, bool colored);
128136

129137
std::ostream& operator<<(std::ostream& os, const BT::NodeStatus& status);
130138

131139
/**
132140
* @brief toStr converts NodeType to string.
133141
*/
134-
const char* toStr(BT::NodeType type);
142+
template<> std::string toStr<BT::NodeType>(BT::NodeType type);
135143

136144
std::ostream& operator<<(std::ostream& os, const BT::NodeType& type);
137145

138146

139-
const char* toStr(BT::PortDirection direction);
147+
template<> std::string toStr<BT::PortDirection>(BT::PortDirection direction);
140148

141149
std::ostream& operator<<(std::ostream& os, const BT::PortDirection& type);
142150

@@ -257,9 +265,9 @@ std::pair<std::string,PortInfo> CreatePort(PortDirection direction,
257265
return out;
258266
}
259267

260-
268+
//----------
261269
template <typename T = void> inline
262-
std::pair<std::string,PortInfo> InputPort(StringView name, StringView description = {})
270+
std::pair<std::string,PortInfo> InputPort(StringView name, StringView description = {})
263271
{
264272
return CreatePort<T>(PortDirection::INPUT, name, description );
265273
}
@@ -271,11 +279,35 @@ std::pair<std::string,PortInfo> OutputPort(StringView name, StringView descripti
271279
}
272280

273281
template <typename T = void> inline
274-
std::pair<std::string,PortInfo> BidirectionalPort(StringView name, StringView description = {})
282+
std::pair<std::string,PortInfo> BidirectionalPort(StringView name, StringView description = {})
275283
{
276284
return CreatePort<T>(PortDirection::INOUT, name, description );
277285
}
286+
//----------
287+
template <typename T = void> inline
288+
std::pair<std::string,PortInfo> InputPort(StringView name, const T& default_value, StringView description)
289+
{
290+
auto out = CreatePort<T>(PortDirection::INPUT, name, description );
291+
out.second.setDefaultValue( BT::toStr(default_value) );
292+
return out;
293+
}
278294

295+
template <typename T = void> inline
296+
std::pair<std::string,PortInfo> OutputPort(StringView name, const T& default_value, StringView description)
297+
{
298+
auto out = CreatePort<T>(PortDirection::OUTPUT, name, description );
299+
out.second.setDefaultValue( BT::toStr(default_value) );
300+
return out;
301+
}
302+
303+
template <typename T = void> inline
304+
std::pair<std::string,PortInfo> BidirectionalPort(StringView name, const T& default_value, StringView description)
305+
{
306+
auto out = CreatePort<T>(PortDirection::INOUT, name, description );
307+
out.second.setDefaultValue( BT::toStr(default_value) );
308+
return out;
309+
}
310+
//----------
279311

280312
typedef std::unordered_map<std::string, PortInfo> PortsList;
281313

src/basic_types.cpp

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,29 @@
44

55
namespace BT
66
{
7-
const char* toStr(NodeStatus status, bool colored)
7+
8+
template <>
9+
std::string toStr<NodeStatus>(NodeStatus status)
10+
{
11+
switch (status)
12+
{
13+
case NodeStatus::SUCCESS:
14+
return "SUCCESS";
15+
case NodeStatus::FAILURE:
16+
return "FAILURE";
17+
case NodeStatus::RUNNING:
18+
return "RUNNING";
19+
case NodeStatus::IDLE:
20+
return "IDLE";
21+
}
22+
return "";
23+
}
24+
25+
std::string toStr(NodeStatus status, bool colored)
826
{
927
if (!colored)
1028
{
11-
switch (status)
12-
{
13-
case NodeStatus::SUCCESS:
14-
return "SUCCESS";
15-
case NodeStatus::FAILURE:
16-
return "FAILURE";
17-
case NodeStatus::RUNNING:
18-
return "RUNNING";
19-
case NodeStatus::IDLE:
20-
return "IDLE";
21-
}
29+
return toStr(status);
2230
}
2331
else
2432
{
@@ -45,7 +53,22 @@ const char* toStr(NodeStatus status, bool colored)
4553
return "Undefined";
4654
}
4755

48-
const char* toStr(NodeType type)
56+
57+
58+
template <>
59+
std::string toStr<PortDirection>(PortDirection direction)
60+
{
61+
switch(direction)
62+
{
63+
case PortDirection::INPUT: return "Input";
64+
case PortDirection::OUTPUT: return "Output";
65+
case PortDirection::INOUT: return "InOut";
66+
}
67+
return "InOut";
68+
}
69+
70+
71+
template<> std::string toStr<NodeType>(NodeType type)
4972
{
5073
switch (type)
5174
{
@@ -64,12 +87,14 @@ const char* toStr(NodeType type)
6487
}
6588
}
6689

90+
6791
template <>
6892
std::string convertFromString<std::string>(StringView str)
6993
{
7094
return std::string( str.data(), str.size() );
7195
}
7296

97+
7398
template <>
7499
const char* convertFromString<const char*>(StringView str)
75100
{
@@ -269,16 +294,6 @@ const std::string &PortInfo::defaultValue() const
269294
return default_value_;
270295
}
271296

272-
const char *toStr(PortDirection type)
273-
{
274-
switch(type)
275-
{
276-
case PortDirection::INPUT: return "Input";
277-
case PortDirection::OUTPUT: return "Output";
278-
case PortDirection::INOUT: return "InOut";
279-
}
280-
return "InOut";
281-
}
282297

283298

284299
} // end namespace

src/loggers/bt_cout_logger.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ void StdCoutLogger::callback(Duration timestamp, const TreeNode& node, NodeStatu
2626
constexpr const size_t ws_count = 25;
2727

2828
double since_epoch = duration<double>(timestamp).count();
29-
printf("[%.3f]: %s%s %s -> %s", since_epoch, node.name().c_str(),
30-
&whitespaces[std::min(ws_count, node.name().size())], toStr(prev_status, true),
31-
toStr(status, true));
29+
printf("[%.3f]: %s%s %s -> %s",
30+
since_epoch, node.name().c_str(),
31+
&whitespaces[std::min(ws_count, node.name().size())],
32+
toStr(prev_status, true).c_str(),
33+
toStr(status, true).c_str() );
3234
std::cout << std::endl;
3335
}
3436

src/loggers/bt_minitrace_logger.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,20 @@ void MinitraceLogger::callback(Duration /*timestamp*/,
3535

3636
const bool statusCompleted = (status == NodeStatus::SUCCESS || status == NodeStatus::FAILURE);
3737

38-
const char* category = toStr(node.type());
38+
const std::string& category = toStr(node.type());
3939
const char* name = node.name().c_str();
4040

4141
if (prev_status == NodeStatus::IDLE && statusCompleted)
4242
{
43-
MTR_INSTANT(category, name);
43+
MTR_INSTANT(category.c_str(), name);
4444
}
4545
else if (status == NodeStatus::RUNNING)
4646
{
47-
MTR_BEGIN(category, name);
47+
MTR_BEGIN(category.c_str(), name);
4848
}
4949
else if (prev_status == NodeStatus::RUNNING && statusCompleted)
5050
{
51-
MTR_END(category, name);
51+
MTR_END(category.c_str(), name);
5252
}
5353
}
5454

src/xml_parsing.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,20 @@ TreeNode::Ptr XMLParser::Pimpl::createNodeFromXML(const XMLElement *element,
549549
}
550550
}
551551
}
552+
// use default value if available for empty ports. Only inputs
553+
for (const auto& port_it: manifest.ports)
554+
{
555+
const std::string& port_name = port_it.first;
556+
const PortInfo& port_info = port_it.second;
557+
558+
auto direction = port_info.direction();
559+
if( direction != PortDirection::INPUT &&
560+
config.input_ports.count(port_name) == 0 &&
561+
port_info.defaultValue().empty() == false)
562+
{
563+
config.input_ports.insert( { port_name, port_info.defaultValue() } );
564+
}
565+
}
552566
child_node = factory.instantiateTreeNode(instance_name, ID, config);
553567
}
554568
else if( tree_roots.count(ID) != 0) {
@@ -648,7 +662,7 @@ std::string writeTreeNodesModelXML(const BehaviorTreeFactory& factory)
648662
{
649663
continue;
650664
}
651-
XMLElement* element = doc.NewElement(toStr(model.type));
665+
XMLElement* element = doc.NewElement( toStr(model.type).c_str() );
652666
element->SetAttribute("ID", model.registration_ID.c_str());
653667

654668
for (auto& port : model.ports)

0 commit comments

Comments
 (0)