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()) {
mongocxx::cursor query(bsoncxx::document::view query, const std::string &collection_name="", mongocxx::options::find query_options=mongocxx::options::find())
Query information from the robot memory.
int insert(bsoncxx::document::view, const std::string &collection="")
Inserts a document into the robot memory.
virtual void finalize()
Finalize the thread.
virtual void loop()
Code to execute in the thread.
StnGeneratorThread()
Constructor.
virtual void bb_interface_data_changed(fawkes::Interface *interface)
BlackBoard data changed notification.
virtual void init()
Initialize the thread.
BlackBoard * blackboard
This is the BlackBoard instance you can use to interact with the BlackBoard.
BlackBoard interface listener.
void bbil_add_data_interface(Interface *interface)
Add an interface to the data modification watch list.
virtual Interface * open_for_reading(const char *interface_type, const char *identifier, const char *owner=NULL)=0
Open interface for reading.
virtual void register_listener(BlackBoardInterfaceListener *listener, ListenerRegisterFlag flag=BBIL_FLAG_ALL)
Register BB event listener.
Configuration * config
This is the Configuration member used to access the configuration.
virtual bool get_bool(const char *path)=0
Get value from configuration which is of type bool.
virtual std::string get_string(const char *path)=0
Get value from configuration which is of type string.
Base class for all Fawkes BlackBoard interfaces.
virtual void log_debug(const char *component, const char *format,...)=0
Log debug message.
virtual void log_warn(const char *component, const char *format,...)=0
Log warning message.
virtual void log_error(const char *component, const char *format,...)=0
Log error message.
virtual void log_info(const char *component, const char *format,...)=0
Log informational message.
Logger * logger
This is the Logger member used to access the logger.
RobotMemory * robot_memory
RobotMemory object for storing and querying information.
Thread class encapsulation of pthreads.
const char * name() const
Get name of thread.
void wakeup()
Wake up thread.
A Simple Temporal Network.
void add_plan_action(const std::string &name, const std::string ¶ms)
Add a (grounded action).
void set_pddl_domain(const std::string &pddl_domain_string)
Set the domain of the STN to the given PDDL domain.
void read_initial_state(const std::string &pddl_problem_string)
Read the initial state from the given PDDL problem.
void generate()
Regenerate the STN.
void drawGraph()
Render a graph representation of the STN.
Fawkes library namespace.