22 #include "stn-generator_thread.h"
24 #include <utils/misc/string_conversions.h>
26 #include <bsoncxx/builder/basic/document.hpp>
29 #include <mongocxx/client.hpp>
34 using namespace mongocxx;
35 using namespace bsoncxx;
44 :
Thread(
"StnGeneratorThread",
Thread::OPMODE_WAITFORWAKEUP),
53 std::string cfg_prefix =
"plugins/stn-generator/";
55 cfg_output_collection_ =
config->
get_string(cfg_prefix +
"output/collection");
56 cfg_publish_to_robot_memory_ =
config->
get_bool(cfg_prefix +
"output/publish-to-rm");
57 cfg_draw_graph_ =
config->
get_bool(cfg_prefix +
"output/draw-graph");
59 std::string pddl_domain_path =
60 StringConversions::resolve_path(
config->
get_string(cfg_prefix +
"domain-file"));
61 cfg_pddl_problem_path_ =
62 StringConversions::resolve_path(
config->
get_string(cfg_prefix +
"problem-file"));
64 std::ifstream s(pddl_domain_path);
68 std::string pddl_domain;
70 s.seekg(0, std::ios::end);
71 pddl_domain.reserve(s.tellg());
72 s.seekg(0, std::ios::beg);
73 pddl_domain.assign((std::istreambuf_iterator<char>(s)), std::istreambuf_iterator<char>());
76 std::string classic_dom_path =
77 StringConversions::resolve_path(
config->
get_string(cfg_prefix +
"classic-domain-file"));
94 std::ifstream s(cfg_pddl_problem_path_);
96 logger->
log_error(
name(),
"Could not open problem-file at %s", cfg_pddl_problem_path_.c_str());
98 std::string pddl_problem;
99 s.seekg(0, std::ios::end);
100 pddl_problem.reserve(s.tellg());
101 s.seekg(0, std::ios::beg);
102 pddl_problem.assign((std::istreambuf_iterator<char>(s)), std::istreambuf_iterator<char>());
106 for (
auto doc : cursor) {
107 array::view actions = doc[
"actions"].get_array();
108 for (
auto &a : actions) {
111 array::view args_array = a[
"args"].get_array();
112 for (
auto &arg : args_array) {
117 args += arg.get_utf8().value.to_string();
119 std::string action_name = a[
"name"].get_utf8().value.to_string();
125 if (cfg_draw_graph_) {
128 }
catch (std::out_of_range &e) {
134 using namespace bsoncxx::builder;
135 if (cfg_publish_to_robot_memory_) {
137 for (
auto &action : stn_->get_bson()) {
138 basic::document rm_action;
139 rm_action.append(basic::kvp(
"relation",
"proposed-stn-action"));
140 rm_action.append(bsoncxx::builder::concatenate(action.view()));
144 std::this_thread::sleep_for(std::chrono::milliseconds(500));
145 num_published_actions_ += stn_->get_bson().size();
146 basic::document rm_final;
147 rm_final.append(basic::kvp(
"relation",
"stn-sync"));
148 rm_final.append(basic::kvp(
"state",
"synced"));
149 rm_final.append(basic::kvp(
"count", std::to_string(num_published_actions_)));
163 if (interface->uid() == plan_if_->uid()) {
165 if (plan_if_->is_final()) {