24#include "eclipse_thread.h"
26#include "blackboard_listener_thread.h"
27#include "externals/blackboard.h"
28#include "externals/eclipse_path.h"
29#include "externals/eclipseclp_config.h"
30#include "externals/fawkes_logger.h"
32#include <core/exception.h>
33#include <core/threading/mutex_locker.h>
34#include <interfaces/TestInterface.h>
54extern "C" int ec_external(dident,
int (*)(...), dident);
70 if (EclExternalBlackBoard::instance()) {
72 EclExternalBlackBoard::cleanup_instance();
83 char *eclipse_dir = NULL;
85 eclipse_dir = strdup(
config->
get_string(
"/eclipse-clp/eclipse_dir").c_str());
87 ec_set_option_ptr(EC_OPTION_ECLIPSEDIR, (
void *)eclipse_dir);
96 ec_set_option_ptr(EC_OPTION_DEFAULT_MODULE, (
void *)agent.c_str());
102 if (0 != ec_init()) {
108 m_initialized =
true;
115 for (
size_t i = 0; i < paths.size(); ++i) {
126 if (filepath_path.empty()) {
127 throw Exception(
"Failed to determine path to filepath module");
129 load_file(filepath_path.c_str());
130 char *filepath = ::strdup(
"filepath");
131 post_goal(term(EC_functor(
":", 2),
133 term(EC_functor(
"add_library_path", 1),
134 ::list(EC_word(SRCDIR
"/externals"),
135 ::list(EC_word(SRCDIR
"/utils"),
136 ::list(EC_word(SRCDIR
"/consoletool"),
137 ::list(EC_word(SRCDIR
"/interpreter"), nil())))))));
138 if (EC_succeed != EC_resume())
139 throw Exception(
"Failed to add " SRCDIR
"/externals to library path");
142 if (
config->
get_bool((
"/eclipse-clp/" + agent +
"/use_graph").c_str())) {
144 CONFDIR +
config->
get_string((
"/eclipse-clp/" + agent +
"/rel_graph_path").c_str());
147 post_goal(term(EC_functor(
"load_graph", 1), graph_path.c_str()));
148 if (EC_succeed != EC_resume()) {
149 throw Exception(
"Error loading graph config to agent");
155 if (agent_path.empty()) {
156 throw Exception(
"Failed to determine path to agent module");
158 load_file(agent_path.c_str());
161 if (EC_succeed != ec_external(ec_did(
"log", 2), p_log, ec_did(agent.c_str(), 0))) {
162 throw Exception(
"Registering external predicate log/2 failed");
170 if (EclExternalBlackBoard::instance())
178 ec_result = EC_resume(
"init", ec_yield_reason);
184 if (ec_result == EC_status::EC_yield) {
185 EC_word bb_updates(::nil());
186 if (EC_word(ec_yield_reason) == EC_atom(
"exogenous_update")) {
192 ec_result = EC_resume(bb_updates, ec_yield_reason);
194 if (ec_result == EC_status::EC_succeed)
210 if (!m_initialized) {
215 char *atom = strdup(event);
225EclipseAgentThread::load_file(
const char *filename)
227 if (!m_initialized) {
231 char *ensure_loaded = strdup(
"ensure_loaded");
232 post_goal(term(EC_functor(ensure_loaded, 1), filename));
235 if (EC_succeed != ec_resume()) {
236 throw Exception(
"File %s could not be loaded", filename);
258 throw Exception(
"No instance of type EclipseThread instantiated");
static BlackboardListenerThread * instance()
Get the singleton instance of this thread.
This thread creates an ECLiPSe context in which the ECLiPSe interpreter and the program are loaded.
virtual void finalize()
Finalize the thread.
static EclipseAgentThread * instance()
Get the EclipseAgentThread instance.
void post_event(const char *)
Post an event to the ECLiPSe context.
fawkes::Logger * get_logger()
Get the logger.
virtual void init()
Initialize the thread.
virtual void loop()
Code to execute in the thread.
virtual ~EclipseAgentThread()
Destructor.
virtual void once()
Execute an action exactly once.
EclipseAgentThread()
Constructor.
void add_path(const std::string &path)
Add a new path.
static EclipsePath * instance()
Get the EclipsePath instance.
std::string locate_file(const std::string &filename)
Locate a file by filename.
static void create_initial_object()
Create the initial EclipsePath object.
void print_all_paths()
Debug method to print all path to the command line.
void apply_regexes()
Apply the regexes to all paths.
void add_regex(boost::regex re, const std::string &str)
Add a regex.
BlackBoard * blackboard
This is the BlackBoard instance you can use to interact with the BlackBoard.
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::vector< std::string > get_strings(const char *path)=0
Get list of values from configuration which is of type string.
virtual std::string get_string(const char *path)=0
Get value from configuration which is of type string.
Wrapper class for using the blackboard in the implementation of the external predicates.
static void create_initial_object(BlackBoard *bb, Logger *logger)
Creates the initial EclExternalBlackBoard object.
static void cleanup_instance()
Delete the current EclExternalBlackBoard instance and set it to NULL.
static void create_initial_object(Configuration *config)
Creates the initial EclExternalConfig object.
Base class for exceptions in Fawkes.
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.
Mutex mutual exclusion lock.
Thread class encapsulation of pthreads.
void set_prepfin_conc_loop(bool concurrent=true)
Set concurrent execution of prepare_finalize() and loop().
const char * name() const
Get name of thread.
void exit()
Exit the thread.
Fawkes library namespace.