24 #include "blackboard.h" 27 #include <eclipseclass.h> 40 BlackBoard* EclExternalBlackBoard::m_blackboard = NULL;
44 EclExternalBlackBoard::EclExternalBlackBoard()
46 if (m_instance != NULL) {
47 throw Exception(
"There is already an instance of type " 48 "EclExternalBlackBoard instantiated");
50 m_own_blackboard =
true;
54 EclExternalBlackBoard::EclExternalBlackBoard(
BlackBoard* blackboard)
56 if (m_instance != NULL) {
57 throw Exception(
"There is already an instance of type " 58 "EclExternalBlackBoard instantiated");
60 m_blackboard = blackboard;
61 m_own_blackboard =
false;
67 for (std::map<std::string, Interface *>::iterator iit = m_interfaces.begin();
68 iit != m_interfaces.end(); ++iit) {
69 m_blackboard->
close(iit->second);
71 if (m_own_blackboard) {
100 throw Exception(
"EclExternalBlackBoard::instance(): " 101 "No instance of type EclExternalBlackBoard instantiated");
114 if (m_blackboard && m_own_blackboard) {
126 return m_blackboard ? true :
false;
132 for (std::map<std::string, Interface *>::iterator iit = m_interfaces.begin();
133 iit != m_interfaces.end();
135 { m_blackboard->
close(iit->second); }
136 if (m_own_blackboard) {
148 {
throw Exception(
"No instance of type BlackBoard instantiated"); }
166 bool process_message_args(
Message* msg, EC_word arg_list);
169 p_bb_connect_to_remote_blackboard()
173 fprintf(stderr,
"p_bb_connect_to_remote_blackboard(): already connected\n");
180 if (EC_succeed != EC_arg(1).is_string(&hostname))
182 fprintf(stderr,
"p_bb_connect_to_remote_blackboard(): first argument is not a string\n");
187 if (EC_succeed != EC_arg(2).is_long(&port))
198 fprintf(stderr,
"p_bb_connect_to_remote_blacboard() failed: %s\n", e.
what_no_backtrace());
207 p_bb_disconnect_from_blackboard()
211 fprintf(stderr,
"p_bb_disconnect_from_blackboard(): not connected\n");
226 fprintf(stderr,
"p_bb_is_alive(): not connected\n");
231 {
return EC_succeed; }
247 p_bb_open_interface()
251 fprintf(stderr,
"p_bb_open_interface(): not connected\n");
256 char* interface_type;
259 if (EC_succeed != EC_arg(1).is_atom(&mode))
261 fprintf(stderr,
"p_bb_open_interface(): no mode given\n");
265 if (EC_succeed != EC_arg(2).is_string(&interface_type))
267 fprintf(stderr,
"p_bb_open_interface(): no type given\n");
271 if (EC_succeed != EC_arg (3).is_string(&interface_id))
273 fprintf(stderr,
"p_bb_open_interface(): no id given\n");
277 std::map<std::string, Interface *> &
interfaces =
280 std::string uid = std::string(interface_type) +
"::" + interface_id;
281 if (interfaces.find(uid) == interfaces.end()) {
285 if (0 == strcmp(
"w", mode.name()))
294 interfaces[iface->
uid()] = iface;
301 if (interfaces.find(uid) == interfaces.end()) {
310 p_bb_close_interface()
314 fprintf(stderr,
"p_bb_close_interface(): not connected\n");
321 if (EC_succeed != EC_arg(1).is_string(&uid))
323 fprintf(stderr,
"p_bb_close_interface(): no id given\n");
327 std::map<std::string, Interface *> &
interfaces =
330 if (interfaces.find(uid) != interfaces.end()) {
344 if (EC_succeed != EC_arg(1).is_string(&uid)) {
345 fprintf(stderr,
"p_bb_has_writer(): no uid given\n");
349 std::map<std::string, Interface *> &
interfaces =
352 if (interfaces.find(uid) != interfaces.end()) {
353 return interfaces[uid]->has_writer() ? EC_succeed : EC_fail;
361 p_bb_instance_serial()
365 fprintf(stderr,
"p_bb_instance_serial(): not connected to blackboard\n");
370 if (EC_succeed != EC_arg(1).is_string(&uid))
372 fprintf(stderr,
"p_bb_instance_serial(): no interface uid given\n");
376 std::map<std::string, Interface *> &
interfaces =
379 if (interfaces.find(uid) != interfaces.end()) {
381 if (EC_succeed != EC_arg(2).unify(interfaces[uid]->serial())) {
382 fprintf(stderr,
"p_bb_instance_serial(): could not bind return value\n");
394 p_bb_read_interfaces()
407 p_bb_read_interface()
410 if (EC_succeed != EC_arg(1).is_string(&uid))
412 fprintf(stderr,
"p_read_interface(): no interface UID given\n");
416 std::map<std::string, Interface *> &
interfaces =
419 if (interfaces.find(uid) == interfaces.end()) {
420 fprintf(stderr,
"p_bb_read_interface: interface %s has not been opened\n", uid);
424 interfaces[uid]->read();
430 p_bb_write_interfaces()
436 if (it->second->is_writer()) { it->second->write(); }
443 p_bb_write_interface()
446 if (EC_succeed != EC_arg(1).is_string(&uid))
448 fprintf(stderr,
"p_read_interface(): no interface UID given\n");
452 std::map<std::string, Interface *> &
interfaces =
455 if (interfaces.find(uid) == interfaces.end()) {
456 fprintf(stderr,
"p_bb_read_interface: interface %s has not been opened\n", uid);
460 if (! interfaces[uid]->is_writer()) {
461 fprintf(stderr,
"p_bb_set(): interface %s not a writer\n", uid);
465 interfaces[uid]->write();
470 p_bb_interface_changed()
473 if (EC_succeed != EC_arg(1).is_string(&uid))
475 fprintf(stderr,
"p_interface_changed(): no interface UID given\n");
479 std::map<std::string, Interface *> &
interfaces =
482 if (interfaces.find(uid) == interfaces.end()) {
483 fprintf(stderr,
"p_bb_interface_changed: interface %s has not been opened\n", uid);
487 return interfaces[uid]->changed() ? EC_succeed : EC_fail;
497 if (EC_succeed != EC_arg(1).is_string(&uid))
499 fprintf(stderr,
"p_bb_get(): no interface uid given\n");
503 if (EC_succeed != EC_arg(2).is_string(&field))
505 fprintf(stderr,
"p_bb_get(): no field given\n");
509 std::map<std::string, Interface *> &
interfaces =
512 if (interfaces.find(uid) != interfaces.end()) {
517 if (0 == strcmp(field, fit.
get_name())) {
521 if (EC_succeed != EC_arg(3).unify(EC_atom((
char*)
"true"))) {
522 fprintf(stderr,
"p_bb_get(): could not bind return value\n");
526 if (EC_succeed != EC_arg(3).unify(EC_atom((
char*)
"false"))) {
527 fprintf(stderr,
"p_bb_get(): could not bind return value\n");
534 if (EC_succeed != EC_arg(3).unify(EC_word((
long) fit.
get_int8()))) {
535 fprintf(stderr,
"p_bb_get(): could not bind return value\n");
541 if (EC_succeed != EC_arg(3).unify(EC_word((
long) fit.
get_uint8()))) {
542 fprintf(stderr,
"p_bb_get(): could not bind return value\n");
548 if (EC_succeed != EC_arg(3).unify(EC_word((
long) fit.
get_int16()))) {
549 fprintf(stderr,
"p_bb_get(): could not bind return value\n");
555 if (EC_succeed != EC_arg(3).unify(EC_word((
long) fit.
get_uint16()))) {
556 fprintf(stderr,
"p_bb_get(): could not bind return value\n");
562 if (EC_succeed != EC_arg(3).unify(EC_word((
long) fit.
get_int32()))) {
563 fprintf(stderr,
"p_bb_get: could not bind value\n");
569 if (EC_succeed != EC_arg(3).unify(EC_word((
long) fit.
get_uint32()))) {
570 fprintf(stderr,
"p_bb_get(): could not bind return value\n");
576 if (EC_succeed != EC_arg(3).unify(EC_word((
long) fit.
get_int64()))) {
577 fprintf(stderr,
"p_bb_get(): could not bind return value\n");
583 if (EC_succeed != EC_arg(3).unify(EC_word((
long) fit.
get_uint64()))) {
584 fprintf(stderr,
"p_bb_get(): could not bind return value\n");
594 res = list(EC_word(f_array[i]), res);
595 if (EC_succeed != EC_arg(3).unify(res)) {
596 fprintf(stderr,
"p_bb_get(): could not bind return value\n");
600 if (EC_succeed != EC_arg(3).unify(EC_word((
double) fit.
get_float()))) {
601 fprintf(stderr,
"p_bb_get(): could not bind return value\n");
612 res = list(EC_word(double_array[i]), res);
613 if (EC_succeed != EC_arg(3).unify(res)) {
614 fprintf(stderr,
"p_bb_get(): could not bind return value\n");
618 if (EC_succeed != EC_arg(3).unify(EC_word((
double) fit.
get_double()))) {
619 fprintf(stderr,
"p_bb_get(): could not bind return value\n");
626 if (EC_succeed != EC_arg(3).unify(EC_word(fit.
get_string()))) {
627 fprintf(stderr,
"p_bb_get(): could not bind return value\n");
638 res = list( EC_word( (
long) array[i]), res);
639 if ( EC_succeed != EC_arg( 3 ).unify( res ) )
641 printf(
"p_bb_get(): could not bind return value\n" );
646 if ( EC_succeed != EC_arg( 3 ).unify( EC_word( (
long) fit.
get_byte() ) ) )
648 printf(
"p_bb_get(): could not bind return value\n" );
656 fprintf(stderr,
"p_bb_get(): could not bind return value\n");
662 fprintf(stderr,
"p_bb_get(): could not find type of interface! Type: %s (%d)", fit.
get_typename(), fit.
get_type());
670 fprintf(stderr,
"p_bb_get(): interface %s has no field %s\n", uid, field);
675 fprintf(stderr,
"p_bb_get(): no interface with id %s found\n", uid);
689 if (EC_succeed != EC_arg(1).is_string(&uid)) {
690 fprintf(stderr,
"p_bb_set(): no interface id given\n");
694 if (EC_succeed != EC_arg(2).is_string(&field)) {
695 fprintf(stderr,
"p_bb_set(): no field given\n");
699 std::map<std::string, Interface *> &
interfaces =
702 if (interfaces.find(uid) != interfaces.end()) {
706 fprintf(stderr,
"p_bb_set(): interface %s not a writer\n", uid);
712 if (0 == strcmp(field, fit.
get_name())) {
717 if (EC_succeed != EC_arg(3).is_atom(&val)) {
718 fprintf(stderr,
"p_bb_set(): no value_given\n");
722 if (0 == strcmp(
"true", val.name()))
724 else if (0 == strcmp(
"false", val.name()))
728 fprintf(stderr,
"p_bb_set(): boolean value neither true nor false\n");
737 if (EC_succeed != EC_arg(3).is_long(&val))
739 fprintf(stderr,
"p_bb_set(): no value given\n");
750 if (EC_succeed != EC_arg(3).is_long(&val))
752 fprintf(stderr,
"p_bb_set(): no value given\n");
763 if (EC_succeed != EC_arg(3).is_long(&val))
765 fprintf(stderr,
"p_bb_set(): no value given\n");
776 if (EC_succeed != EC_arg(3).is_long(&val))
778 fprintf(stderr,
"p_bb_set(): no value given\n");
789 if (EC_succeed != EC_arg(3).is_long(&val))
791 fprintf(stderr,
"p_bb_set(): no value given\n");
802 if (EC_succeed != EC_arg(3).is_long(&val))
804 fprintf(stderr,
"p_bb_set(): no value given\n");
815 if (EC_succeed != EC_arg(3).is_long(&val))
817 fprintf(stderr,
"p_bb_set(): no value given\n");
828 if (EC_succeed != EC_arg(3).is_long(&val))
830 fprintf(stderr,
"p_bb_set(): no value given\n");
841 if (EC_succeed != EC_arg(3).is_double(&val))
843 fprintf(stderr,
"p_bb_set(): no value given\n");
854 if (EC_succeed != EC_arg(3).is_string(&val))
856 fprintf(stderr,
"p_bb_set(): no value given\n");
866 fprintf(stderr,
"p_bb_set(): NOT YET IMPLEMENTET\n");
877 fprintf(stderr,
"p_bb_set(): interface %s has no field %s\n", uid, field);
882 fprintf(stderr,
"p_bb_set(): no interface with id %s found\n", uid);
897 if (EC_succeed != EC_arg(1).is_string(&uid)) {
898 fprintf(stderr,
"p_bb_send_message(): no interface id given\n");
902 if (EC_succeed != EC_arg(2).is_string(&message_type))
904 fprintf(stderr,
"p_bb_send_message(): no message type given\n");
908 std::map<std::string, Interface *> &
interfaces =
911 if (interfaces.find(uid) != interfaces.end()) {
915 fprintf(stderr,
"p_bb_send_message(): interface with uid %s is a writer\n", uid);
924 if (EC_succeed == EC_arg(3).is_list(head, tail)) {
925 if (!process_message_args(msg, ::list(head, tail))) {
932 (iface)->msgq_enqueue( msg );
934 EC_arg( 4 ).unify((
int)(msg->
id()));
947 fprintf(stderr,
"p_bb_send_message(): no interface with name %s\n", uid);
960 if (EC_succeed != EC_arg(1).is_string(&uid)) {
961 fprintf(stderr,
"p_bb_recv_messages(): no interface uid given\n");
965 std::map<std::string, Interface *> &
interfaces =
968 if (interfaces.find(uid) != interfaces.end()) {
972 fprintf(stderr,
"p_bb_recv_messages(): interface with uid %s is not a writer\n", uid);
976 EC_word msg_list = nil();
982 EC_word args = nil();
986 switch (fit.get_type()) {
989 { value = EC_atom((
char*)
"true"); }
991 { value = EC_atom((
char*)
"false"); }
996 value = EC_word((
long) fit.get_int8());
1000 value = EC_word((
long) fit.get_uint8());
1004 value = EC_word((
long) fit.get_int16());
1008 value = EC_word((
long) fit.get_uint16());
1012 value = EC_word((
long) fit.get_int32());
1016 value = EC_word((
long) fit.get_uint32());
1020 value = EC_word((
long) fit.get_int64());
1024 value = EC_word((
long) fit.get_uint64());
1028 value = EC_word((
double) fit.get_float());
1032 value = EC_word(fit.get_string());
1037 fprintf(stderr,
"p_bb_recv_messages(): NOT YET IMPLEMENTED\n");
1041 fprintf(stderr,
"p_bb_recv_messages(): unknown field type\n");
1044 EC_word field = ::list(EC_word(fit.get_name()),
1045 ::list(value, nil()));
1046 args = ::list(field, args);
1050 msg_list = ::list(::list(EC_word(msg->
type()),
1051 ::list(args, nil())),
1057 if (EC_succeed != EC_arg(2).unify(msg_list)) {
1058 fprintf(stderr,
"p_bb_recv_messages(): could not bind return value\n");
1063 fprintf(stderr,
"p_bb_recv_messages(): no interface with id %s found\n", uid);
1072 process_message_args(
Message* msg, EC_word arg_list)
1077 for (; EC_succeed == arg_list.is_list(head, tail) ; arg_list = tail)
1085 if (EC_succeed != head.is_list(field, t1) ||
1086 EC_succeed != t1.is_list(value, t2) )
1088 fprintf(stderr,
"p_bb_send_messge(): could not parse argument list\n");
1093 if (EC_succeed != field.is_string(&field_name))
1095 fprintf(stderr,
"p_bb_send_message(): malformed argument list\n");
1100 for (fit = msg->
fields();
1104 if (0 == strcmp(fit.
get_name(), field_name))
1111 if (EC_succeed != value.is_atom(&val))
1113 fprintf(stderr,
"p_bb_send_message(): no value_given (bool)\n");
1117 if (0 == strcmp(
"true", val.name()))
1119 else if (0 == strcmp(
"false", val.name()))
1123 fprintf(stderr,
"p_bb_send_message(): boolean value neither true nor false\n");
1133 if (EC_succeed != value.is_long(&val))
1135 fprintf(stderr,
"p_bb_send_message(): no value given (int8)\n");
1147 if (EC_succeed != value.is_long(&val))
1149 fprintf(stderr,
"p_bb_send_message(): no value given (uint8)\n");
1161 if (EC_succeed != value.is_long(&val))
1163 fprintf(stderr,
"p_bb_send_message(): no value given (int16)\n");
1175 if (EC_succeed != value.is_long(&val))
1177 fprintf(stderr,
"p_bb_send_message(): no value given (uint16)\n");
1189 if (EC_succeed != value.is_long(&val))
1191 fprintf(stderr,
"p_bb_send_message(): no value given (int32)\n");
1203 if (EC_succeed != value.is_long(&val))
1205 fprintf(stderr,
"p_bb_send_message(): no value given (uint32)\n");
1217 if (EC_succeed != value.is_long(&val))
1219 fprintf(stderr,
"p_bb_send_message(): no value given (int64)\n");
1231 if (EC_succeed != value.is_long(&val))
1233 fprintf(stderr,
"p_bb_send_message(): no value given (uint64)\n");
1245 if (EC_succeed != value.is_double(&val))
1247 fprintf(stderr,
"p_bb_send_message(): no value given (float)\n");
1259 if (EC_succeed != value.is_string(&val))
1261 fprintf(stderr,
"p_bb_send_message(): no value given (string)\n");
1272 fprintf(stderr,
"p_bb_send_message(): NOT YET IMPLEMENTET\n");
1285 fprintf(stderr,
"p_bb_send_message(): message has no field with name %s\n",
Interface field iterator.
void set_int64(int64_t i, unsigned int index=0)
Set value of current field as integer.
Base class for all messages passed through interfaces in Fawkes BlackBoard.
unsigned int id() const
Get message ID.
uint8_t * get_bytes() const
Get value of current field as byte array.
uint16_t get_uint16(unsigned int index=0) const
Get value of current field as unsigned integer.
void set_float(float f, unsigned int index=0)
Set value of current field as float.
bool msgq_empty()
Check if queue is empty.
const char * get_typename() const
Get type of current field as string.
void unref()
Decrement reference count and conditionally delete this instance.
double get_double(unsigned int index=0) const
Get value of current field as double.
void set_bool(bool b, unsigned int index=0)
Set value of current field as bool.
void set_int16(int16_t i, unsigned int index=0)
Set value of current field as integer.
Fawkes library namespace.
bool get_bool(unsigned int index=0) const
Get value of current field as bool.
8 bit unsigned integer field
void set_uint16(uint16_t i, unsigned int index=0)
Set value of current field as unsigned integer.
float * get_floats() const
Get value of current field as float array.
16 bit unsigned integer field
void set_int8(int8_t i, unsigned int index=0)
Set value of current field as integer.
interface_fieldtype_t get_type() const
Get type of current field.
std::map< std::string, Interface * > & interfaces()
Obtain the list of opened interfaces.
void set_uint8(uint8_t i, unsigned int index=0)
Set value of current field as unsigned integer.
byte field, alias for uint8
static BlackBoard * blackboard_instance()
Access the BlackBoard instance.
const char * get_value_string(const char *array_sep=", ")
Get value of current field as string.
float get_float(unsigned int index=0) const
Get value of current field as float.
virtual Message * create_message(const char *type) const =0
Create message based on type name.
Base class for all Fawkes BlackBoard interfaces.
InterfaceFieldIterator fields_end()
Invalid iterator.
static void cleanup_instance()
Delete the current EclExternalBlackBoard instance and set it to NULL.
uint8_t get_byte(unsigned int index=0) const
Get value of current field as byte.
void set_int32(int32_t i, unsigned int index=0)
Set value of current field as integer.
static void create_initial_object(BlackBoard *bb)
Creates the initial EclExternalBlackBoard object.
static EclExternalBlackBoard * instance()
Get the EclExternalBlackBoard instance.
int16_t get_int16(unsigned int index=0) const
Get value of current field as integer.
void msgq_pop()
Erase first message from queue.
int8_t get_int8(unsigned int index=0) const
Get value of current field as integer.
Base class for exceptions in Fawkes.
Message * msgq_first()
Get the first message from the message queue.
void disconnect()
Disconnect remote blackboard connection.
const char * get_name() const
Get name of current field.
uint8_t get_uint8(unsigned int index=0) const
Get value of current field as unsigned integer.
void ref()
Increment reference count.
double * get_doubles() const
Get value of current field as double array.
Wrapper class for using the blackboard in the implementation of the external predicates.
~EclExternalBlackBoard()
Destructor.
const char * uid() const
Get unique identifier of interface.
virtual const char * what_no_backtrace() const
Get primary string (does not implicitly print the back trace).
void set_string(const char *s)
Set value of current field as string.
uint64_t get_uint64(unsigned int index=0) const
Get value of current field as unsigned integer.
64 bit unsigned integer field
const char * get_string() const
Get value of current field as string.
InterfaceFieldIterator fields()
Get iterator over all fields of this interface instance.
bool connected()
Query connection status.
void set_uint32(uint32_t i, unsigned int index=0)
Set value of current field as unsigned integer.
uint32_t get_uint32(unsigned int index=0) const
Get value of current field as unsigned integer.
size_t get_length() const
Get length of current field.
bool is_writer() const
Check if this is a writing instance.
void print_trace()
Prints trace to stderr.
InterfaceFieldIterator fields_end()
Invalid iterator.
void connect(const char *host, long port)
Open remote blackboard connection.
virtual Interface * open_for_reading(const char *interface_type, const char *identifier, const char *owner=NULL)=0
Open interface for reading.
void set_uint64(uint64_t i, unsigned int index=0)
Set value of current field as unsigned integer.
The BlackBoard abstract class.
InterfaceFieldIterator fields()
Get iterator over all fields of this interface instance.
int32_t get_int32(unsigned int index=0) const
Get value of current field as integer.
virtual Interface * open_for_writing(const char *interface_type, const char *identifier, const char *owner=NULL)=0
Open interface for writing.
const char * type() const
Get message type.
32 bit unsigned integer field
field with interface specific enum type
virtual void close(Interface *interface)=0
Close interface.
int64_t get_int64(unsigned int index=0) const
Get value of current field as integer.