23 #include "feature_blackboard.h" 25 #include <blackboard/blackboard.h> 26 #include <blackboard/exceptions.h> 27 #include <core/threading/mutex_locker.h> 28 #include <interface/interface_info.h> 29 #include <logging/logger.h> 30 #include <utils/misc/string_conversions.h> 31 #include <utils/misc/string_split.h> 32 #include <utils/time/time.h> 56 blackboard_(blackboard),
57 cfg_retract_early_(retract_early)
64 for (
auto &iface_map : interfaces_) {
65 for (
auto &iface_list : iface_map.second.reading) {
66 for (
auto iface : iface_list.second) {
67 blackboard_->
close(iface);
70 for (
auto &iface_list : iface_map.second.writing) {
71 for (
auto iface : iface_list.second) {
72 blackboard_->
close(iface);
84 envs_[env_name] = clips;
85 clips->evaluate(
"(path-load \"blackboard.clp\")");
87 "blackboard-enable-time-read",
88 sigc::slot<void>(sigc::bind<0>(
89 sigc::mem_fun(*
this, &BlackboardCLIPSFeature::clips_blackboard_enable_time_read), env_name)));
92 sigc::slot<void, std::string, std::string>(sigc::bind<0>(
93 sigc::mem_fun(*
this, &BlackboardCLIPSFeature::clips_blackboard_open_interface_reading),
96 "blackboard-open-reading",
97 sigc::slot<void, std::string, std::string>(sigc::bind<0>(
98 sigc::mem_fun(*
this, &BlackboardCLIPSFeature::clips_blackboard_open_interface_reading),
101 "blackboard-open-writing",
102 sigc::slot<void, std::string, std::string>(sigc::bind<0>(
103 sigc::mem_fun(*
this, &BlackboardCLIPSFeature::clips_blackboard_open_interface_writing),
107 sigc::slot<void, std::string, std::string>(
108 sigc::bind<0>(sigc::mem_fun(*
this, &BlackboardCLIPSFeature::clips_blackboard_close_interface),
110 clips->add_function(
"blackboard-preload",
111 sigc::slot<void, std::string>(sigc::bind<0>(
112 sigc::mem_fun(*
this, &BlackboardCLIPSFeature::clips_blackboard_preload),
114 clips->add_function(
"blackboard-read",
115 sigc::slot<void>(sigc::bind<0>(
116 sigc::mem_fun(*
this, &BlackboardCLIPSFeature::clips_blackboard_read),
118 clips->add_function(
"blackboard-write",
119 sigc::slot<void, std::string>(sigc::bind<0>(
120 sigc::mem_fun(*
this, &BlackboardCLIPSFeature::clips_blackboard_write),
122 clips->add_function(
"blackboard-get-info",
123 sigc::slot<void>(sigc::bind<0>(
124 sigc::mem_fun(*
this, &BlackboardCLIPSFeature::clips_blackboard_get_info),
126 clips->add_function(
"blackboard-set",
127 sigc::slot<void, std::string, std::string, CLIPS::Value>(sigc::bind<0>(
128 sigc::mem_fun(*
this, &BlackboardCLIPSFeature::clips_blackboard_set),
131 "blackboard-set-multifield",
132 sigc::slot<void, std::string, std::string, CLIPS::Values>(
133 sigc::bind<0>(sigc::mem_fun(*
this, &BlackboardCLIPSFeature::clips_blackboard_set_multifield),
135 clips->add_function(
"blackboard-create-msg",
136 sigc::slot<CLIPS::Value, std::string, std::string>(sigc::bind<0>(
137 sigc::mem_fun(*
this, &BlackboardCLIPSFeature::clips_blackboard_create_msg),
140 "blackboard-list-msg-fields",
141 sigc::slot<CLIPS::Values, void *>(
142 sigc::bind<0>(sigc::mem_fun(*
this, &BlackboardCLIPSFeature::clips_blackboard_list_msg_fields),
145 "blackboard-set-msg-field",
146 sigc::slot<void, void *, std::string, CLIPS::Value>(
147 sigc::bind<0>(sigc::mem_fun(*
this, &BlackboardCLIPSFeature::clips_blackboard_set_msg_field),
149 clips->add_function(
"blackboard-set-msg-multifield",
150 sigc::slot<void, void *, std::string, CLIPS::Values>(sigc::bind<0>(
152 &BlackboardCLIPSFeature::clips_blackboard_set_msg_multifield),
154 clips->add_function(
"blackboard-send-msg",
155 sigc::slot<CLIPS::Value, void *>(sigc::bind<0>(
156 sigc::mem_fun(*
this, &BlackboardCLIPSFeature::clips_blackboard_send_msg),
163 if (interfaces_.find(env_name) != interfaces_.end()) {
164 for (
auto &iface_map : interfaces_[env_name].reading) {
165 for (
auto iface : iface_map.second) {
166 logger_->
log_debug((
"BBCLIPS|" + env_name).c_str(),
167 "Closing reading interface %s",
169 blackboard_->
close(iface);
172 for (
auto &iface_map : interfaces_[env_name].writing) {
173 for (
auto iface : iface_map.second) {
174 logger_->
log_debug((
"BBCLIPS|" + env_name).c_str(),
175 "Closing writing interface %s",
177 blackboard_->
close(iface);
180 interfaces_.erase(env_name);
182 envs_.erase(env_name);
186 BlackboardCLIPSFeature::clips_blackboard_enable_time_read(
const std::string &env_name)
188 if (envs_.find(env_name) == envs_.end()) {
189 logger_->
log_warn((
"BBCLIPS|" + env_name).c_str(),
190 "Cannot enable reading for environment %s " 196 std::string bb_read_defrule =
"(defrule blackboard-read\n" 197 " (declare (salience 1000))\n" 200 " (blackboard-read)\n" 204 envs_[env_name]->build(bb_read_defrule);
208 BlackboardCLIPSFeature::clips_assert_interface_type(
const std::string &env_name,
209 const std::string &log_name,
211 const std::string &type)
213 std::string deftemplate =
"(deftemplate " + type +
"\n" +
" (slot id (type STRING))\n" 214 +
" (multislot time (type INTEGER) (cardinality 2 2))\n";
218 for (f = iface->
fields(); f != f_end; ++f) {
223 deftemplate += std::string() +
" (" + ((f.
get_length() > 1) ?
"multi" :
"") +
"slot " 224 + f.
get_name() +
" (type SYMBOL) (allowed-values TRUE FALSE))\n";
236 deftemplate += std::string() +
" (" + ((f.
get_length() > 1) ?
"multi" :
"") +
"slot " 237 + f.
get_name() +
" (type INTEGER))\n";
242 deftemplate += std::string() +
" (" + ((f.
get_length() > 1) ?
"multi" :
"") +
"slot " 243 + f.
get_name() +
" (type FLOAT))\n";
247 deftemplate += std::string() +
" (slot " + f.
get_name() +
" (type STRING))\n";
251 deftemplate += std::string() +
" (" + ((f.
get_length() > 1) ?
"multi" :
"") +
"slot " 252 + f.
get_name() +
" (type SYMBOL))\n";
262 if (cfg_retract_early_) {
263 retract =
"(defrule " + type +
"-cleanup\n" +
" (declare (salience -10000))\n" +
" ?f <- (" 270 retract =
"(deffunction " + type
271 +
"-cleanup-late (?id)\n" 272 " (delayed-do-for-all-facts ((?f " 279 logstr =
"Deffunction";
282 if (envs_[env_name]->build(deftemplate) && envs_[env_name]->build(retract)) {
283 logger_->
log_debug(log_name.c_str(),
"Deftemplate:\n%s", deftemplate.c_str());
284 logger_->
log_debug(log_name.c_str(),
"%s:\n%s", logstr.c_str(), retract.c_str());
288 "Defining blackboard type for %s in %s failed",
296 BlackboardCLIPSFeature::clips_blackboard_preload(
const std::string &env_name,
297 const std::string &type)
299 std::string name =
"BBCLIPS|" + env_name;
301 if (envs_.find(env_name) == envs_.end()) {
303 "Environment %s has not been registered " 304 "for blackboard feature",
309 if (interfaces_[env_name].reading.find(type) == interfaces_[env_name].reading.end()
310 && interfaces_[env_name].writing.find(type) == interfaces_[env_name].writing.end()) {
314 iface = blackboard_->
open_for_reading(type.c_str(),
"clips_blackboard_preload___");
315 clips_assert_interface_type(env_name, name, iface, type);
316 blackboard_->
close(iface);
317 interfaces_[env_name].reading.insert(std::make_pair(type, std::list<fawkes::Interface *>()));
320 "Failed to preload interface type %s, " 330 BlackboardCLIPSFeature::clips_blackboard_open_interface(
const std::string &env_name,
331 const std::string &type,
332 const std::string &
id,
335 std::string name =
"BBCLIPS|" + env_name;
336 std::string owner =
"CLIPS:" + env_name;
338 if (envs_.find(env_name) == envs_.end()) {
340 "Environment %s has not been registered " 341 "for blackboard feature",
349 InterfaceMap &iface_map = writing ? interfaces_[env_name].writing : interfaces_[env_name].reading;
351 if (iface_map.find(type) == iface_map.end()) {
355 iface = blackboard_->
open_for_writing(type.c_str(),
id.c_str(), owner.c_str());
357 iface = blackboard_->
open_for_reading(type.c_str(),
id.c_str(), owner.c_str());
361 "Failed to open interface %s:%s, exception follows",
368 if (!clips_assert_interface_type(env_name, name, iface, type)) {
369 blackboard_->
close(iface);
372 "Added interface %s for %s",
374 iface->
is_writer() ?
"writing" :
"reading");
375 iface_map.insert(std::make_pair(type, std::list<fawkes::Interface *>(1, iface)));
377 clips->assert_fact_f(
"(blackboard-interface (id \"%s\") (type \"%s\") (uid \"%s\") " 378 " (hash \"%s\") (serial %u) (writing %s))",
384 writing ?
"TRUE" :
"FALSE");
387 auto &iface_list = iface_map[type];
388 if (std::none_of(iface_list.begin(),
390 [&type, &id](
const Interface *i) ->
bool {
391 return (type == i->type()) && (
id == i->id());
395 iface = blackboard_->
open_for_writing(type.c_str(),
id.c_str(), owner.c_str());
397 iface = blackboard_->
open_for_reading(type.c_str(),
id.c_str(), owner.c_str());
399 iface_map[type].push_back(iface);
401 "Added interface %s for %s",
403 iface->
is_writer() ?
"writing" :
"reading");
405 clips->assert_fact_f(
"(blackboard-interface (id \"%s\") (type \"%s\") (uid \"%s\") " 406 " (hash \"%s\") (serial %u) (writing %s))",
412 writing ?
"TRUE" :
"FALSE");
415 "Failed to open interface %s:%s, exception follows",
426 BlackboardCLIPSFeature::clips_blackboard_open_interface_reading(
const std::string &env_name,
427 const std::string &type,
428 const std::string &
id)
430 clips_blackboard_open_interface(env_name, type,
id,
false);
434 BlackboardCLIPSFeature::clips_blackboard_open_interface_writing(
const std::string &env_name,
435 const std::string &type,
436 const std::string &
id)
438 clips_blackboard_open_interface(env_name, type,
id,
true);
442 BlackboardCLIPSFeature::clips_blackboard_close_interface(
const std::string &env_name,
443 const std::string &type,
444 const std::string &
id)
446 std::string name =
"BBCLIPS|" + env_name;
448 if (envs_.find(env_name) == envs_.end()) {
450 "Environment %s has not been registered " 451 "for blackboard feature",
456 if (interfaces_[env_name].reading.find(type) != interfaces_[env_name].reading.end()) {
457 auto &l = interfaces_[env_name].reading[type];
459 find_if(l.begin(), l.end(), [&id](
const Interface *iface) {
return id == iface->
id(); });
460 if (iface_it != l.end()) {
461 blackboard_->
close(*iface_it);
467 if (interfaces_[env_name].writing.find(type) != interfaces_[env_name].writing.end()) {
468 auto &l = interfaces_[env_name].writing[type];
470 find_if(l.begin(), l.end(), [&id](
const Interface *iface) {
return id == iface->
id(); });
471 if (iface_it != l.end()) {
472 blackboard_->
close(*iface_it);
481 BlackboardCLIPSFeature::clips_blackboard_read(
const std::string &env_name)
484 if (interfaces_.find(env_name) == interfaces_.end())
486 if (envs_.find(env_name) == envs_.end()) {
488 logger_->
log_warn((
"BBCLIPS|" + env_name).c_str(),
489 "Environment %s not registered," 490 " cannot read interfaces",
496 CLIPS::Environment &env = **(envs_[env_name]);
497 for (
auto &iface_map : interfaces_[env_name].reading) {
498 for (
auto i : iface_map.second) {
501 if (!cfg_retract_early_) {
502 std::string fun = std::string(
"(") + i->type() +
"-cleanup-late \"" + i->id() +
"\")";
505 const Time *t = i->timestamp();
507 std::string fact = std::string(
"(") + i->type() +
" (id \"" + i->id() +
"\")" +
" (time " 508 + StringConversions::to_string(t->
get_sec()) +
" " 509 + StringConversions::to_string(t->
get_usec()) +
")";
512 for (f = i->fields(); f != f_end; ++f) {
515 value = f.
get_bool() ?
"TRUE" :
"FALSE";
517 value = f.get_value_string();
518 std::string::size_type pos = 0;
519 while ((pos = value.find(
"\"", pos)) != std::string::npos) {
520 value.replace(pos, 1,
"\\\"");
523 value = std::string(
"\"") + value +
"\"";
525 value = f.get_value_string();
526 std::string::size_type pos;
527 while ((pos = value.find(
",")) != std::string::npos) {
528 value = value.erase(pos, 1);
532 std::string::size_type pos;
533 while ((pos = value.find(
"-inf")) != std::string::npos) {
534 value = value.replace(pos, 4, std::to_string(std::numeric_limits<double>::min()));
536 while ((pos = value.find(
"inf")) != std::string::npos) {
537 value = value.replace(pos, 3, std::to_string(std::numeric_limits<double>::max()));
539 while ((pos = value.find(
"-nan")) != std::string::npos) {
541 value.replace(pos, 4, std::to_string(std::numeric_limits<double>::min() + 1));
543 while ((pos = value.find(
"nan")) != std::string::npos) {
545 value.replace(pos, 3, std::to_string(std::numeric_limits<double>::max() - 1));
549 fact += std::string(
" (") + f.get_name() +
" " + value +
")";
552 env.assert_fact(fact);
559 BlackboardCLIPSFeature::clips_blackboard_write(
const std::string &env_name,
const std::string &uid)
562 if (interfaces_.find(env_name) == interfaces_.end())
564 if (envs_.find(env_name) == envs_.end()) {
566 logger_->
log_warn((
"BBCLIPS|" + env_name).c_str(),
567 "Environment %s not registered," 568 " cannot write interface %s",
573 std::string type, id;
574 Interface::parse_uid(uid.c_str(), type, id);
575 if (interfaces_[env_name].writing.find(type) != interfaces_[env_name].writing.end()) {
576 auto i = std::find_if(interfaces_[env_name].writing[type].begin(),
577 interfaces_[env_name].writing[type].end(),
578 [&uid](
const Interface *iface) ->
bool {
return uid == iface->
uid(); });
579 if (i != interfaces_[env_name].writing[type].end()) {
582 logger_->
log_warn((
"BBCLIPS|" + env_name).c_str(),
583 "Interface %s not opened for writing," 584 " in environment %s",
590 logger_->
log_warn((
"BBCLIPS|" + env_name).c_str(),
591 "No interface of type %s opened for," 592 " writing in environment %s",
600 BlackboardCLIPSFeature::clips_blackboard_get_info(
const std::string &env_name)
602 if (envs_.find(env_name) == envs_.end()) {
604 logger_->
log_warn((
"BBCLIPS|" + env_name).c_str(),
605 "Environment %s not registered," 606 " cannot read interfaces",
616 for (
auto ii : *iil) {
617 const Time * timestamp = ii.timestamp();
618 std::list<std::string> quoted_readers;
619 std::list<std::string> readers = ii.readers();
620 std::for_each(readers.begin(), readers.end(), ["ed_readers](
const std::string &r) {
621 quoted_readers.push_back(std::string(
"\"") + r +
"\"");
623 std::string quoted_readers_s =
str_join(quoted_readers,
' ');
624 clips->assert_fact_f(
"(blackboard-interface-info (id \"%s\") (type \"%s\") " 625 "(hash \"%s\") (has-writer %s) (num-readers %u) " 626 "(writer \"%s\") (readers %s) (timestamp %u %u))",
629 ii.hash_printable().c_str(),
630 ii.has_writer() ?
"TRUE" :
"FALSE",
633 quoted_readers_s.c_str(),
642 BlackboardCLIPSFeature::clips_blackboard_set(
const std::string &env_name,
643 const std::string &uid,
644 const std::string &field,
648 if (interfaces_.find(env_name) == interfaces_.end())
650 if (envs_.find(env_name) == envs_.end()) {
652 logger_->
log_warn((
"BBCLIPS|" + env_name).c_str(),
653 "Environment %s not registered," 654 " cannot set %s on interface %s",
660 std::string type, id;
661 Interface::parse_uid(uid.c_str(), type, id);
662 if (interfaces_[env_name].writing.find(type) != interfaces_[env_name].writing.end()) {
663 auto i = std::find_if(interfaces_[env_name].writing[type].begin(),
664 interfaces_[env_name].writing[type].end(),
665 [&uid](
const Interface *iface) ->
bool {
return uid == iface->
uid(); });
666 if (i != interfaces_[env_name].writing[type].end()) {
667 set_field((*i)->fields(), (*i)->fields_end(), env_name, field, value);
669 logger_->
log_error((
"BBCLIPS|" + env_name).c_str(),
670 "Interface %s not opened for writing," 671 " in environment %s",
677 logger_->
log_error((
"BBCLIPS|" + env_name).c_str(),
678 "No interface of type %s opened for," 679 " writing in environment %s",
687 BlackboardCLIPSFeature::clips_blackboard_set_multifield(
const std::string &env_name,
688 const std::string &uid,
689 const std::string &field,
690 CLIPS::Values values)
693 if (interfaces_.find(env_name) == interfaces_.end())
695 if (envs_.find(env_name) == envs_.end()) {
697 logger_->
log_warn((
"BBCLIPS|" + env_name).c_str(),
698 "Environment %s not registered," 699 " cannot set %s on interface %s",
705 std::string type, id;
706 Interface::parse_uid(uid.c_str(), type, id);
707 if (interfaces_[env_name].writing.find(type) != interfaces_[env_name].writing.end()) {
708 auto i = std::find_if(interfaces_[env_name].writing[type].begin(),
709 interfaces_[env_name].writing[type].end(),
710 [&uid](
const Interface *iface) ->
bool {
return uid == iface->
uid(); });
711 if (i != interfaces_[env_name].writing[type].end()) {
712 set_multifield((*i)->fields(), (*i)->fields_end(), env_name, field, values);
714 logger_->
log_error((
"BBCLIPS|" + env_name).c_str(),
715 "Interface %s not opened for writing," 716 " in environment %s",
722 logger_->
log_error((
"BBCLIPS|" + env_name).c_str(),
723 "No interface of type %s opened for," 724 " writing in environment %s",
732 BlackboardCLIPSFeature::clips_blackboard_create_msg(
const std::string &env_name,
733 const std::string &uid,
734 const std::string &msg_type)
737 if (interfaces_.find(env_name) == interfaces_.end()) {
738 return CLIPS::Value(
new std::shared_ptr<Message>());
740 if (envs_.find(env_name) == envs_.end()) {
742 logger_->
log_warn((
"BBCLIPS|" + env_name).c_str(),
743 "Environment %s not registered," 744 " cannot read interfaces",
746 return CLIPS::Value(
new std::shared_ptr<Message>());
750 std::string if_type, id;
751 Interface::parse_uid(uid.c_str(), if_type, id);
754 if (interfaces_[env_name].reading.find(if_type) == interfaces_[env_name].reading.end()) {
756 (
"BBCLIPS|" + env_name).c_str(),
757 "Can't create message for interface %s, because there is no opened interface with this type",
759 return CLIPS::Value(
new std::shared_ptr<Message>());
761 auto i = std::find_if(interfaces_[env_name].reading[if_type].begin(),
762 interfaces_[env_name].reading[if_type].end(),
763 [&uid](
const Interface *iface) ->
bool {
return uid == iface->
uid(); });
764 if (i == interfaces_[env_name].reading[if_type].end()) {
766 (
"BBCLIPS|" + env_name).c_str(),
767 "Can't create message for interface %s, because there is no opened interface with that uid",
769 return CLIPS::Value(
new std::shared_ptr<Message>());
773 std::list<const char *> available_types = (*i)->get_message_types();
774 bool type_exists =
false;
775 for (std::list<const char *>::iterator it = available_types.begin();
776 it != available_types.end() && !type_exists;
778 if (std::string(*it).compare(msg_type) == 0) {
783 logger_->
log_warn((
"BBCLIPS|" + env_name).c_str(),
784 "Can't create message for interface %s, because there is no message type %s",
787 return CLIPS::Value(
new std::shared_ptr<Message>());
791 Message *m = (*i)->create_message(msg_type.c_str());
794 interface_of_msg_[m] = (*i);
797 return CLIPS::Value(
new std::shared_ptr<Message>(m));
801 BlackboardCLIPSFeature::clips_blackboard_list_msg_fields(
const std::string &env_name,
void *msgptr)
803 std::shared_ptr<Message> *m =
static_cast<std::shared_ptr<Message> *
>(msgptr);
805 logger_->
log_warn((
"BBCLIPS|" + env_name).c_str(),
806 "Can't list message fields, the pointer is wrong.");
807 return CLIPS::Values();
810 const int field_count = (*m)->num_fields();
811 CLIPS::Values field_names(field_count);
814 field_names[i].set(it.get_name(),
true);
815 logger_->
log_info((
"BBCLIPS|" + env_name).c_str(),
"Message has field %s", it.get_name());
822 BlackboardCLIPSFeature::clips_blackboard_set_msg_field(
const std::string &env_name,
824 const std::string &field_name,
827 std::shared_ptr<Message> *m =
static_cast<std::shared_ptr<Message> *
>(msgptr);
829 logger_->
log_warn((
"BBCLIPS|" + env_name).c_str(),
830 "Can't set message field, the pointer is wrong.");
834 bool set_success = set_field((*m)->fields(), (*m)->fields_end(), env_name, field_name, value);
836 logger_->
log_warn((
"BBCLIPS|" + env_name).c_str(),
"Can't set message field.");
841 BlackboardCLIPSFeature::clips_blackboard_set_msg_multifield(
const std::string &env_name,
843 const std::string &field_name,
844 CLIPS::Values values)
846 std::shared_ptr<Message> *m =
static_cast<std::shared_ptr<Message> *
>(msgptr);
848 logger_->
log_warn((
"BBCLIPS|" + env_name).c_str(),
849 "Can't set message field, the pointer is wrong.");
854 set_multifield((*m)->fields(), (*m)->fields_end(), env_name, field_name, values);
856 logger_->
log_warn((
"BBCLIPS|" + env_name).c_str(),
"Can't set message field.");
861 BlackboardCLIPSFeature::clips_blackboard_send_msg(
const std::string &env_name,
void *msgptr)
863 std::shared_ptr<Message> *m =
static_cast<std::shared_ptr<Message> *
>(msgptr);
865 logger_->
log_warn((
"BBCLIPS|" + env_name).c_str(),
866 "Can't set message field, the pointer is wrong.");
867 return CLIPS::Value(0);
869 if (!interface_of_msg_[m->get()]) {
870 logger_->
log_warn((
"BBCLIPS|" + env_name).c_str(),
"Can't send message, was it already sent?");
871 return CLIPS::Value(0);
877 unsigned int message_id = 0;
881 interface_of_msg_[m->get()]->msgq_enqueue(m->get());
882 message_id = m->get()->
id();
887 logger_->
log_warn((
"BBCLIPS|" + env_name).c_str(),
888 "Failed to send message: %s",
893 interface_of_msg_.erase(m->get());
898 return CLIPS::Value(message_id);
909 const std::string & env_name,
910 const std::string & field,
911 CLIPS::Values values)
915 for (fit = fit_begin; fit != fit_end; ++fit) {
918 if (values.size() < min_length) {
919 min_length = values.size();
922 for (
size_t i = 0; i < min_length; i++) {
923 bool success = set_field(fit, fit_end, env_name, field, values[i], i);
932 if (fit == fit_end) {
933 logger_->
log_error((
"BBCLIPS|" + env_name).c_str(),
"Can't find field %s", field.c_str());
947 const std::string & env_name,
948 const std::string & field,
953 for (fit = fit_begin; fit != fit_end; ++fit) {
957 if (value.type() != CLIPS::TYPE_SYMBOL && value.type() != CLIPS::TYPE_STRING) {
958 logger_->
log_error((
"BBCLIPS|" + env_name).c_str(),
959 "Cannot set field %s: invalid value (not a symbol)",
963 std::string val_s = value.as_string();
964 if (value ==
"TRUE") {
966 }
else if (value ==
"FALSE") {
969 logger_->
log_error((
"BBCLIPS|" + env_name).c_str(),
970 "Cannot set field %s: invalid value %s (not a bool)",
979 if (value.type() != CLIPS::TYPE_INTEGER) {
980 logger_->
log_error((
"BBCLIPS|" + env_name).c_str(),
981 "Cannot set field %s: invalid value (not an integer)",
985 long long int val = value.as_integer();
991 if (value.type() != CLIPS::TYPE_INTEGER) {
992 logger_->
log_error((
"BBCLIPS|" + env_name).c_str(),
993 "Cannot set field %s: invalid value (not an integer)",
997 long long int val = value.as_integer();
1003 if (value.type() != CLIPS::TYPE_INTEGER) {
1004 logger_->
log_error((
"BBCLIPS|" + env_name).c_str(),
1005 "Cannot set field %s: invalid value (not an integer)",
1009 long long int val = value.as_integer();
1015 if (value.type() != CLIPS::TYPE_INTEGER) {
1016 logger_->
log_error((
"BBCLIPS|" + env_name).c_str(),
1017 "Cannot set field %s: invalid value (not an integer)",
1021 long long int val = value.as_integer();
1027 if (value.type() != CLIPS::TYPE_INTEGER) {
1028 logger_->
log_error((
"BBCLIPS|" + env_name).c_str(),
1029 "Cannot set field %s: invalid value (not an integer)",
1033 long long int val = value.as_integer();
1039 if (value.type() != CLIPS::TYPE_INTEGER) {
1040 logger_->
log_error((
"BBCLIPS|" + env_name).c_str(),
1041 "Cannot set field %s: invalid value (not an integer)",
1045 long long int val = value.as_integer();
1051 if (value.type() != CLIPS::TYPE_INTEGER) {
1052 logger_->
log_error((
"BBCLIPS|" + env_name).c_str(),
1053 "Cannot set field %s: invalid value (not an integer)",
1057 long long int val = value.as_integer();
1063 if (value.type() != CLIPS::TYPE_INTEGER) {
1064 logger_->
log_error((
"BBCLIPS|" + env_name).c_str(),
1065 "Cannot set field %s: invalid value (not an integer)",
1069 long long int val = value.as_integer();
1075 if (value.type() != CLIPS::TYPE_FLOAT && value.type() != CLIPS::TYPE_INTEGER) {
1076 logger_->
log_error((
"BBCLIPS|" + env_name).c_str(),
1077 "Cannot set field %s: invalid value " 1078 "(neither float nor integer)",
1082 if (value.type() == CLIPS::TYPE_FLOAT) {
1083 double val = value.as_float();
1086 long long int val = value.as_integer();
1093 if (value.type() != CLIPS::TYPE_FLOAT && value.type() != CLIPS::TYPE_INTEGER) {
1094 logger_->
log_error((
"BBCLIPS|" + env_name).c_str(),
1095 "Cannot set field %s: invalid value " 1096 "(neither double nor integer)",
1100 if (value.type() == CLIPS::TYPE_FLOAT) {
1101 double val = value.as_float();
1104 long long int val = value.as_integer();
1111 if (value.type() != CLIPS::TYPE_SYMBOL && value.type() != CLIPS::TYPE_STRING) {
1112 logger_->
log_error((
"BBCLIPS|" + env_name).c_str(),
1113 "Cannot set field %s: invalid value " 1114 "(neither symbol nor string)",
1118 std::string val = value.as_string();
1121 logger_->
log_error((
"BBCLIPS|" + env_name).c_str(),
1122 "Cannot set field %s[%d]: " 1123 "there are no string arrays in interfaces",
1131 if (value.type() != CLIPS::TYPE_SYMBOL) {
1132 logger_->
log_error((
"BBCLIPS|" + env_name).c_str(),
1133 "Cannot set field %s: invalid value " 1138 std::string val = value.as_string();
1141 logger_->
log_error((
"BBCLIPS|" + env_name).c_str(),
1142 "Failed to set enum field %s to %s, exception follows",
1144 value.as_string().c_str());
1145 logger_->
log_error((
"BBCLIPS|" + env_name).c_str(), e);
1152 logger_->
log_error((
"BBCLIPS|" + env_name).c_str(),
1153 "Setting of field type %s for %s not supported",
1163 if (fit == fit_end) {
1164 logger_->
log_error((
"BBCLIPS|" + env_name).c_str(),
"Can't find field %s", field.c_str());
Interface field iterator.
void set_int64(int64_t i, unsigned int index=0)
Set value of current field as integer.
virtual void clips_context_init(const std::string &env_name, fawkes::LockPtr< CLIPS::Environment > &clips)
Initialize a CLIPS context to use the provided feature.
Base class for all messages passed through interfaces in Fawkes BlackBoard.
unsigned int id() const
Get message ID.
void set_float(float f, unsigned int index=0)
Set value of current field as float.
const char * get_typename() const
Get type of current field as string.
virtual void log_info(const char *component, const char *format,...)=0
Log informational message.
void unref()
Decrement reference count and conditionally delete this instance.
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.
virtual ~BlackboardCLIPSFeature()
Destructor.
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.
16 bit unsigned integer field
std::string str_join(const InputIterator &first, const InputIterator &last, char delim='/')
Join list of strings string using given delimiter.
const char * id() const
Get identifier of interface.
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.
void set_uint8(uint8_t i, unsigned int index=0)
Set value of current field as unsigned integer.
A class for handling time.
byte field, alias for uint8
Base class for all Fawkes BlackBoard interfaces.
void set_int32(int32_t i, unsigned int index=0)
Set value of current field as integer.
Thrown if no writer interface is alive.
void set_double(double f, unsigned int index=0)
Set value of current field as double.
Interface information list.
const char * type() const
Get type of interface.
Base class for exceptions in Fawkes.
unsigned short serial() const
Get instance serial of interface.
Mutex * objmutex_ptr() const
Get object mutex.
const char * get_name() const
Get name of current field.
void ref()
Increment reference count.
CLIPS feature maintainer.
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.
64 bit unsigned integer field
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.
void set_uint32(uint32_t i, unsigned int index=0)
Set value of current field as unsigned integer.
BlackboardCLIPSFeature(fawkes::Logger *logger, fawkes::BlackBoard *blackboard, bool retract_early)
Constructor.
size_t get_length() const
Get length of current field.
bool is_writer() const
Check if this is a writing instance.
long get_sec() const
Get seconds.
InterfaceFieldIterator fields_end()
Invalid iterator.
long get_usec() const
Get microseconds.
virtual void log_debug(const char *component, const char *format,...)=0
Log debug message.
virtual InterfaceInfoList * list_all()=0
Get list of all currently existing interfaces.
virtual Interface * open_for_reading(const char *interface_type, const char *identifier, const char *owner=NULL)=0
Open interface for reading.
void set_enum_string(const char *e, unsigned int index=0)
Set value of current field as enum (from an integer).
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.
virtual Interface * open_for_writing(const char *interface_type, const char *identifier, const char *owner=NULL)=0
Open interface for writing.
32 bit unsigned integer field
field with interface specific enum type
const char * hash_printable() const
Get printable interface hash.
virtual void clips_context_destroyed(const std::string &env_name)
Notification that a CLIPS environment has been destroyed.
virtual void close(Interface *interface)=0
Close interface.