1 #include <sarafun_tree/parse_tree.h>
9 if (!node.count(
"name")) {
10 std::string
id = node[
"id"];
11 std::string error_message(
"The BT JSON file nodes must all have the 'name' "
12 "member! Offending node has id: ");
14 error_message = error_message + id;
16 throw std::logic_error(error_message);
18 if (!node.count(
"id") || !node.count(
"type")) {
19 std::string name = node[
"name"];
20 std::string error_message(
"The BT JSON file nodes must all have the 'id', "
21 "'type' members! Offending node has name: ");
23 error_message = error_message + name;
25 throw std::logic_error(error_message);
34 if (!tree_j_.count(
"root") || !tree_j_.count(
"nodes")) {
35 throw std::logic_error(
36 std::string(
"The tree file must declare 'root' and 'nodes' members "
37 "at the base level!"));
39 std::string root_id = tree_j_[
"root"];
41 json root_node = tree_j_[
"nodes"][root_id];
45 }
catch (
const std::invalid_argument &e) {
46 ROS_ERROR(
"Error parsing BT json file: %s. Current id: %s", e.what(),
48 }
catch (
const std::logic_error &e) {
49 ROS_ERROR(
"Error with BT Node: %s.", e.what());
50 }
catch (
const std::exception &e) {
51 ROS_ERROR(
"Unknown exception: %s.", e.what());
59 std::string type = node[
"type"];
60 std::string
id = node[
"id"];
64 BT::TreeNode *bt_node;
66 if (type == std::string(
"Selector")) {
67 bt_node =
new BT::SelectorNode(
id);
68 }
else if (type == std::string(
"SelectorStar")) {
69 bt_node =
new BT::SelectorStarNode(
id);
70 }
else if (type == std::string(
"Sequence")) {
71 bt_node =
new BT::SequenceNode(
id);
72 }
else if (type == std::string(
"SequenceStar")) {
73 bt_node =
new BT::SequenceStarNode(
id);
74 }
else if (type == std::string(
"Action")) {
76 std::string name = node[
"name"];
77 bt_node =
new BT::ROSAction(name);
78 }
else if (type == std::string(
"Condition")) {
80 std::string name = node[
"name"];
81 bt_node =
new BT::ROSCondition(name);
83 std::string error_message(
"BT input file includes an unknown node type: ");
84 error_message = error_message + type;
85 throw std::logic_error(error_message);
89 std::vector<std::string> children = node[
"children"];
91 for (std::vector<std::string>::iterator i = children.begin();
92 i != children.end(); i++) {
void verifyNode(json node)
BT::TreeNode * parseTree()
Parser(std::string filepath)