1 #include <tree_generator/TreeFromKF.hpp>
3 using json = nlohmann::json;
4 namespace tree_generator {
8 nh_ = ros::NodeHandle(
"~");
17 for (
int i = 0; i < label.size(); i++)
33 if (!
nh_.getParam(ros::this_node::getName() +
"/" + label +
"/file", filename))
35 ROS_ERROR(
"Got label %s, which is not defined in the tree generator", label.c_str());
39 std::string path = ros::package::getPath(
"tree_generator") +
"/data/subtrees/" + filename;
44 ROS_INFO(
"Successfully loaded the tree for label %s", label.c_str());
49 ROS_ERROR(
"The filename for the label %s defined in the parameter server does not exist in /data/subtrees!", label.c_str());
57 throw std::logic_error(
"Called the createSubTree method without a loaded label");
64 if (!subtree_.count(
"nodes") || !subtree_.count(
"root")) {
65 throw std::logic_error(
66 std::string(
"The subtree file must declare the 'nodes' and root members "
67 "at the base level!"));
70 for (
auto i = subtree_[
"nodes"].begin(); i != subtree_[
"nodes"].end(); i++) {
71 subtree_[
"nodes"][i.key()] =
modifyId((json) subtree_[
"nodes"][i.key()], indices);
79 if (!node.count(
"id") || !node.count(
"type"))
81 throw std::logic_error(std::string(
"modifyId called on a json object without the 'id' and 'type' members!"));
84 std::string type = node[
"type"];
85 std::string
id = node[
"id"];
87 if(type ==
"Selector") {
89 node[
"id"] =
id + std::to_string(indices[
SEL]);
90 }
else if (type ==
"SelectorStar") {
92 node[
"id"] =
id + std::to_string(indices[
SELSTAR]);
93 }
else if (type ==
"Sequence") {
95 node[
"id"] =
id + std::to_string(indices[
SEQ]);
96 }
else if (type ==
"SequenceStar") {
98 node[
"id"] =
id + std::to_string(indices[
SEQSTAR]);
99 }
else if (type ==
"Action") {
101 node[
"id"] =
id + std::to_string(indices[
ACTION]);
102 }
else if (type ==
"Condition") {
104 node[
"id"] =
id + std::to_string(indices[
CONDITION]);
106 std::string error_message(
"Tried to modify id of an unknown node type: ");
107 error_message = error_message + type;
108 throw std::logic_error(error_message);
121 json tree, root_sequence;
122 std::vector<std::string> children_id_list;
123 std::map<std::string, json> children;
124 std::vector<json> children_list;
127 tree[
"root"] =
"root_sequence";
129 root_sequence[
"id"] =
"root_sequence";
130 root_sequence[
"type"] =
"SequenceStar";
131 root_sequence[
"name"] =
"SequenceStar";
133 for (
int i = 0; i < keyframes_list.size(); i++)
137 id = subtree[
"root"];
138 children_id_list.push_back(
id);
142 root_sequence[
"children"] = children_id_list;
143 children[
"root_sequence"] = root_sequence;
144 tree[
"nodes"] = children;
146 std::cout << tree << std::endl;
153 for(
auto i = tree[
"nodes"].begin(); i != tree[
"nodes"].end(); i++)
155 children_map[i.key()] = i.value();
void replaceWithUnderscore(std::string &label)
SubTreeFromKF subtree_parser_
bool loadLabel(std::string label)
json createSubTree(std::vector< int > &indices)
json createTree(const std::vector< sarafun_msgs::KeyframeMsg > &keyframes_list)
json modifyId(json node, std::vector< int > &indices)
std::vector< int > indices_
void addChildren(const json &tree, std::map< std::string, json > &children_map)