23 #include "eclipse_thread.h"
24 #include "externals/fawkes_bb_interface.h"
25 #include "externals/fawkes_logger.h"
27 #include <interfaces/TestInterface.h>
28 #include <core/exception.h>
30 #include <eclipseclass.h>
38 using namespace fawkes;
46 extern "C" int ec_external( dident,
int (*) (), dident );
52 :
Thread(
"ECLiPSe thread", fawkes::
Thread::OPMODE_CONTINUOUS ),
53 m_initialized( false )
67 char* eclipse_dir = NULL;
70 eclipse_dir = strdup(
config->
get_string(
"/readylogagent/eclipse_dir" ).c_str() );
72 ec_set_option_ptr( EC_OPTION_ECLIPSEDIR, (
void*) eclipse_dir );
86 if ( EC_succeed != ec_external( ec_did(
"read_interface", 2 ), p_read_interface, ec_did(
"eclipse", 0 ) ) )
87 {
throw Exception(
"Registering external predicate read_interface/2 failed" ); }
88 if ( EC_succeed != ec_external( ec_did(
"write_interface", 2 ), p_write_interface, ec_did(
"eclipse", 0 ) ) )
89 {
throw Exception(
"Registering external predicate write_interface/2 failed" ); }
90 if ( EC_succeed != ec_external( ec_did(
"send_message", 2 ), p_send_message, ec_did(
"eclipse", 0 ) ) )
91 {
throw Exception(
"Registering external predicate send_message/2 failed" ); }
92 if ( EC_succeed != ec_external( ec_did(
"recv_messages", 2 ), p_recv_messages, ec_did(
"eclipse", 0 ) ) )
93 {
throw Exception(
"Registering external predicate recv_messages/2 failed" ); }
94 if ( EC_succeed != ec_external( ec_did(
"log", 2 ), p_log, ec_did(
"eclipse", 0 ) ) )
95 {
throw Exception(
"Registering external predicate log/2 failed" ); }
104 while ( vit->
next() )
109 if ( s.find(
"::") == string::npos )
110 {
throw Exception(
"Not a valid interface id: %s", s.c_str() ); }
112 string iftype = s.substr( 0, s.find(
"::" ) );
113 string ifname = s.substr( s.find(
"::" ) + 2 );
116 ifname.c_str(), iftype.c_str() );
119 m_reading_ifaces.push_back( iface );
120 register_interface( iface );
125 vit =
config->
search(
"/readylogagent/interfaces/writing" );
126 while ( vit->
next() )
131 if ( s.find(
"::") == string::npos )
132 {
throw Exception(
"Not a valid interface id: %s", s.c_str() ); }
134 string iftype = s.substr( 0, s.find(
"::" ) );
135 string ifname = s.substr( s.find(
"::" ) + 2 );
138 ifname.c_str(), iftype.c_str() );
141 m_writing_ifaces.push_back( iface );
142 register_interface( iface );
148 e.
append(
"Failed to open interfaces" );
153 load_file( ECLIPSE_CODE_DIR
"/utils/logging.ecl" );
156 load_file( ECLIPSE_CODE_DIR
"/interpreter/dummy.ecl" );
169 if ( EC_succeed != EC_resume() )
170 {
throw Exception(
"Error running agent program" ); }
179 if ( !m_initialized ) {
return; }
182 char* atom = strdup( event );
191 for ( vector< Interface* >::iterator i = m_reading_ifaces.begin();
192 i != m_reading_ifaces.end();
196 for ( vector< Interface* >::iterator i = m_writing_ifaces.begin();
197 i != m_writing_ifaces.end();
206 for ( vector< Interface* >::iterator i = m_writing_ifaces.begin();
207 i != m_writing_ifaces.end();
217 EclipseAgentThread::load_file(
const char* filename )
219 if ( !m_initialized ) {
return false; }
221 char* ensure_loaded = strdup(
"ensure_loaded" );
222 post_goal( term( EC_functor( ensure_loaded, 1 ), filename ) );
223 free( ensure_loaded );
225 if ( EC_succeed != ec_resume() )
226 {
throw Exception(
"File %s could not be loaded", filename ); }
238 if ( !m_initialized ) {
return false; }
240 m_registered_interfaces[ string( interface->
id() ) ] = interface;
248 asprintf( &struct_name,
"data_%s", interface->
type() );
250 post_goal( term( EC_functor( (
char *)
"current_struct", 2 ),
251 EC_atom( struct_name ),
254 if ( EC_succeed != ec_resume() )
259 vector< string > fields;
263 { fields.push_back( i.get_name() ); }
265 EC_word args[ fields.size() ];
267 for (
size_t i = 0 ; i < fields.size(); ++i )
269 char* c = strdup( fields.at( i ).c_str() );
270 args[ i ] = EC_atom( c );
274 EC_word new_struct = term( EC_functor( struct_name, (
int) fields.size() ), args );
276 char* local = strdup(
"local" );
277 char* strct = strdup(
"struct" );
278 EC_word struct_def = term( EC_functor( strct, 1 ), new_struct );
279 EC_word struct_def_local = term( EC_functor( local, 1), struct_def );
281 char* call = strdup(
"call" );
283 post_goal( term( EC_functor( call, 1 ), struct_def_local ) );
290 if ( EC_succeed != ec_resume() )
291 {
throw Exception(
"Failed to define structure %s", struct_name ); }
301 for ( std::list<const char *>::iterator type_iter = message_types.begin();
302 type_iter != message_types.end();
307 asprintf( &struct_name,
"data_%s_%s", interface->
type(), *type_iter );
309 post_goal( term( EC_functor( (
char *)
"current_struct", 2 ),
310 EC_atom( struct_name ),
313 if ( EC_succeed != ec_resume() )
320 vector< string > fields;
325 string name = field_iter.get_name();
326 fields.push_back( name );
331 EC_word args[ fields.size() ];
333 for (
size_t i = 0; i < fields.size(); ++i )
335 char* c = strdup( fields.at( i ).c_str() );
336 args[ i ] = EC_atom( c );
340 if ( 0 != fields.size() )
342 EC_word new_struct = term( EC_functor( struct_name, (
int) fields.size() ), args );
343 char* local = strdup(
"local" );
344 char* strct = strdup(
"struct" );
345 EC_word struct_def = term( EC_functor( strct, 1 ), new_struct );
346 EC_word struct_def_local = term( EC_functor( local, 1), struct_def );
348 char* call = strdup(
"call" );
350 post_goal( term( EC_functor( call, 1 ), struct_def_local ) );
357 if ( EC_succeed != ec_resume() )
358 {
throw Exception(
"Failed to define structure %s", struct_name ); }
375 map< string, fawkes::Interface* >::iterator i = m_registered_interfaces.find(
string(
id ) );
377 if ( i == m_registered_interfaces.end() ) {
return NULL; }
398 {
throw Exception(
"No instance of type EclipseThread instantiated" ); }
Interface field iterator.
EclipseAgentThread()
Constructor.
Base class for all messages passed through interfaces in Fawkes BlackBoard.
virtual void log_info(const char *component, const char *format,...)=0
Log informational message.
virtual void finalize()
Finalize the thread.
fawkes::Logger * get_logger()
Get the logger.
virtual ValueIterator * search(const char *path)=0
Iterator with search results.
virtual bool next()=0
Check if there is another element and advance to this if possible.
Thread class encapsulation of pthreads.
virtual Message * create_message(const char *type) const =0
Create message based on type name.
Base class for all Fawkes BlackBoard interfaces.
virtual ~EclipseAgentThread()
Destructor.
Logger * logger
This is the Logger member used to access the logger.
InterfaceFieldIterator fields_end()
Invalid iterator.
virtual Interface * open_for_writing(const char *interface_type, const char *identifier)=0
Open interface for writing.
void post_event(const char *)
Post an event to the ECLiPSe context.
const char * id() const
Get identifier of interface.
static EclipseAgentThread * instance()
Get the EclipseAgentThread instance.
virtual bool is_string() const =0
Check if current value is a string.
Base class for exceptions in Fawkes.
void read_interfaces()
Read all registered interfaces.
virtual std::string get_string() const =0
Get string value.
fawkes::Interface * get_registered_interface(const char *id)
Get the registered interface with the given id.
This thread creates an ECLiPSe context in which the Readylog interpreter and the program are loaded...
InterfaceFieldIterator fields()
Get iterator over all fields of this interface instance.
const char * name() const
Get name of thread.
virtual void init()
Initialize the thread.
InterfaceFieldIterator fields_end()
Invalid iterator.
virtual void log_debug(const char *component, const char *format,...)=0
Log debug message.
virtual void once()
Execute an action exactly once.
Iterator interface to iterate over config values.
virtual Interface * open_for_reading(const char *interface_type, const char *identifier)=0
Open interface for reading.
void write_interfaces()
Write the registered interface that have been opened for writing.
InterfaceFieldIterator fields()
Get iterator over all fields of this interface instance.
Configuration * config
This is the Configuration member used to access the configuration.
const char * type() const
Get type of interface.
std::list< const char * > get_message_types()
Obtain a list of textual representations of the message types available for this interface.
void append(const char *format,...)
Append messages to the message list.
virtual std::string get_string(const char *path)=0
Get value from configuration which is of type string.
BlackBoard * blackboard
This is the BlackBoard instance you can use to interact with the BlackBoard.