21 #include "execution_thread.h"
23 #include "exog_manager.h"
24 #include "gologpp_fawkes_backend.h"
26 #include <golog++/model/procedural.h>
27 #include <golog++/parser/parser.h>
28 #include <golog++/semantics/readylog/execution.h>
35 using namespace gologpp;
37 const std::string cfg_prefix(
"/plugins/gologpp");
44 GologppThread::GologppThread() : Thread(
"gologpp_agent", Thread::OPMODE_WAITFORWAKEUP)
46 set_prepfin_conc_loop(
true);
52 std::filesystem::path spec{config->
get_string(cfg_prefix +
"/spec")};
53 auto prog_file = find_prog_file(spec);
54 std::string spec_cfg_prefix{cfg_prefix +
"/specs/" + spec.string()};
55 logger->
log_debug(name(),
"spec config: %s", spec_cfg_prefix.c_str());
59 gologpp::global_scope().clear();
64 global_scope().register_type_raw(
65 new ListType(*global_scope().lookup_type(BoolType::static_name())));
66 global_scope().register_type_raw(
67 new ListType(*global_scope().lookup_type(NumberType::static_name())));
68 global_scope().register_type_raw(
69 new ListType(*global_scope().lookup_type(SymbolType::static_name())));
71 logger->
log_info(name(),
"Parsing %s...", prog_file.c_str());
72 main_prog_ = gologpp::parser::parse_file(prog_file);
73 logger->
log_info(name(),
"... parsing done");
75 logger->
log_info(name(),
"Initializing ReadyLog context...");
77 exog_mgr_ =
new ExogManager(
this, config, spec_cfg_prefix, blackboard, logger);
79 gologpp::eclipse_opts options;
81 options.guitrace = options.trace;
82 gologpp::ReadylogContext::init(
83 options, std::make_unique<GologppFawkesBackend>(config, spec_cfg_prefix, logger, blackboard));
85 logger->
log_info(name(),
"... initialization done");
92 std::lock_guard<std::mutex> l{run_mutex_};
93 gologpp::ReadylogContext::instance().run(
94 gologpp::Block{
new gologpp::Scope{gologpp::global_scope()}, {main_prog_.release()}});
95 logger->
log_info(name(),
"golog++ main program has ended");
96 }
catch (gologpp::UserError &e) {
97 logger->
log_error(name(),
"User Error: %s", e.what());
98 }
catch (gologpp::EclipseError &e) {
99 logger->
log_error(name(),
"Eclipse Error: %s", e.what());
104 GologppThread::prepare_finalize_user()
106 gologpp::ReadylogContext::instance().terminate();
107 std::lock_guard<std::mutex> l{run_mutex_};
112 GologppThread::finalize()
116 gologpp::global_scope().clear();
117 gologpp::ReadylogContext::shutdown();
124 gologpp::ExecutionContext &
125 GologppThread::gologpp_context()
127 return gologpp::ReadylogContext::instance();
130 std::filesystem::path
131 GologppThread::find_prog_file(
const std::filesystem::path &spec)
const
133 const std::unordered_map<std::string, std::string> mapping = {{
"@BASEDIR@", BASEDIR},
134 {
"@SRCDIR@", SRCDIR}};
135 auto gologpp_cfg_dirs = config->
get_strings(cfg_prefix +
"/gologpp-dirs");
136 std::vector<std::filesystem::path> gologpp_dirs;
137 for (
auto &dir : gologpp_cfg_dirs) {
138 for (
auto &entry : mapping) {
139 size_t start_pos = 0;
140 while ((start_pos = dir.find(entry.first, start_pos)) != std::string::npos) {
141 dir.replace(start_pos, entry.first.length(), entry.second);
142 start_pos += entry.second.length();
145 gologpp_dirs.push_back(std::filesystem::path{dir});
146 logger->
log_debug(name(),
"Golog++ dir: %s", dir.c_str());
148 std::filesystem::path prog_file;
149 std::filesystem::path spec_file{spec};
151 for (
auto &dir : gologpp_dirs) {
152 prog_file = dir / spec / spec_file;
153 if (std::filesystem::exists(prog_file)) {
157 throw Exception(
"Could not find Golog++ spec dir for '%s'", spec.c_str());
virtual std::vector< std::string > get_strings(const char *path)=0
Get list of values from configuration which is of type string.
virtual bool get_bool_or_default(const char *path, const bool &default_val)
Get value from configuration which is of type bool, or the given default if the path does not exist.
virtual std::string get_string(const char *path)=0
Get value from configuration which is of type string.
virtual void log_info(const char *component, const char *format,...)
Log informational message.
virtual void log_debug(const char *component, const char *format,...)
Log debug message.
virtual void log_error(const char *component, const char *format,...)
Log error message.
Watch/observe blackboard interfaces according to the mappings specified for exogenous actions in the ...
Fawkes library namespace.