22 #include "clips_agent_thread.h" 24 #include <core/threading/mutex_locker.h> 25 #include <interfaces/SwitchInterface.h> 26 #include <utils/misc/string_conversions.h> 27 #include <utils/misc/string_split.h> 39 :
Thread(
"ClipsAgentThread",
Thread::OPMODE_WAITFORWAKEUP),
55 cfg_auto_start_ =
false;
56 cfg_assert_time_each_loop_ =
false;
57 cfg_skill_sim_time_ = 2.0;
58 cfg_skill_sim_ =
false;
59 cfg_steal_skiller_control_ =
true;
66 cfg_assert_time_each_loop_ =
config->
get_bool(
"/clips-agent/assert-time-each-loop");
78 cfg_steal_skiller_control_ =
config->
get_bool(
"/clips-agent/steal-skiller-control");
82 std::vector<std::string> clips_dirs;
85 for (
size_t i = 0; i < clips_dirs.size(); ++i) {
86 if (clips_dirs[i][clips_dirs[i].size() - 1] !=
'/') {
93 clips_dirs.insert(clips_dirs.begin(), std::string(SRCDIR) +
"/clips/");
95 if (!cfg_skill_sim_) {
100 throw Exception(
"Skiller has no writer, aborting");
104 throw Exception(
"Skiller already has a different exclusive controller");
112 clips->evaluate(std::string(
"(path-add-subst \"@BASEDIR@\" \"") + BASEDIR +
"\")");
113 clips->evaluate(std::string(
"(path-add-subst \"@FAWKES_BASEDIR@\" \"") + FAWKES_BASEDIR +
"\")");
114 clips->evaluate(std::string(
"(path-add-subst \"@RESDIR@\" \"") + RESDIR +
"\")");
115 clips->evaluate(std::string(
"(path-add-subst \"@CONFDIR@\" \"") + CONFDIR +
"\")");
117 for (
size_t i = 0; i < clips_dirs.size(); ++i) {
118 clips->evaluate(
"(path-add \"" + clips_dirs[i] +
"\")");
121 clips->add_function(
"skill-call-ext",
122 sigc::slot<void, std::string, std::string>(
123 sigc::mem_fun(*
this, &ClipsAgentThread::clips_skill_call_ext)));
125 clips->evaluate(
"(ff-feature-request \"config\")");
127 bool cfg_req_redefwarn_feature =
true;
129 cfg_req_redefwarn_feature =
config->
get_bool(
"/clips-agent/request-redefine-warning-feature");
132 if (cfg_req_redefwarn_feature) {
134 clips->evaluate(
"(ff-feature-request \"redefine-warning\")");
137 if (!
clips->batch_evaluate(SRCDIR
"/clips/init.clp")) {
139 "Failed to initialize CLIPS environment, " 140 "batch file failed.");
142 throw Exception(
"Failed to initialize CLIPS environment, batch file failed.");
145 clips->assert_fact(
"(agent-init)");
146 clips->refresh_agenda();
149 ctrl_recheck_ =
true;
158 clips->remove_function(
"skill-call-ext");
160 if (!cfg_skill_sim_ && skiller_if_->
has_writer()) {
174 if (!started_ && cfg_auto_start_) {
175 clips->assert_fact(
"(start)");
179 if (!cfg_skill_sim_) {
189 ctrl_recheck_ =
false;
191 ctrl_recheck_ =
true;
200 clips->assert_fact(
"(start)");
207 if (cfg_assert_time_each_loop_) {
208 clips->assert_fact(
"(time (now))");
212 if (!active_skills_.empty()) {
216 std::list<std::string> finished_skills;
217 std::map<std::string, SkillExecInfo>::iterator as;
218 for (as = active_skills_.begin(); as != active_skills_.end(); ++as) {
219 const std::string & as_name = as->first;
220 const SkillExecInfo &as_info = as->second;
222 if (cfg_skill_sim_) {
223 if ((now - as_info.start_time) >= cfg_skill_sim_time_) {
225 clips->assert_fact_f(
"(skill-update (name \"%s\") (status FINAL))", as_name.c_str());
226 finished_skills.push_back(as_name);
228 clips->assert_fact_f(
"(skill-update (name \"%s\") (status RUNNING))", as_name.c_str());
231 if (as_info.skill_string == skiller_if_->
skill_string()) {
232 clips->assert_fact_f(
"(skill-update (name \"%s\") (status %s))",
234 status_string(skiller_if_->
status()));
235 if (skiller_if_->
status() == SkillerInterface::S_FINAL
236 || skiller_if_->
status() == SkillerInterface::S_FAILED) {
237 finished_skills.push_back(as_name);
243 std::list<std::string>::iterator fs;
244 for (fs = finished_skills.begin(); fs != finished_skills.end(); ++fs) {
245 active_skills_.erase(*fs);
249 clips->refresh_agenda();
257 case SkillerInterface::S_FINAL:
return "FINAL";
258 case SkillerInterface::S_FAILED:
return "FAILED";
259 case SkillerInterface::S_RUNNING:
return "RUNNING";
260 default:
return "IDLE";
265 ClipsAgentThread::clips_skill_call_ext(std::string skill_name, std::string skill_string)
267 if (active_skills_.find(skill_name) != active_skills_.end()) {
268 logger->
log_warn(
name(),
"Skill %s called again while already active", skill_name.c_str());
271 if (cfg_skill_sim_) {
285 sei.skill_string = skill_string;
286 active_skills_[skill_name] = sei;
char * skill_string() const
Get skill_string value.
virtual void log_info(const char *component, const char *format,...)=0
Log informational message.
Fawkes library namespace.
virtual bool get_bool(const char *path)=0
Get value from configuration which is of type bool.
A class for handling time.
ReleaseControlMessage Fawkes BlackBoard Interface Message.
SkillStatusEnum
This determines the current status of skill execution.
uint32_t exclusive_controller() const
Get exclusive_controller value.
Thread class encapsulation of pthreads.
Logger * logger
This is the Logger member used to access the logger.
AcquireControlMessage Fawkes BlackBoard Interface Message.
Clock * clock
By means of this member access to the clock is given.
Time now() const
Get the current time.
Thread aspect to use blocked timing.
ClipsAgentThread()
Constructor.
SwitchInterface Fawkes BlackBoard Interface.
Base class for exceptions in Fawkes.
unsigned short serial() const
Get instance serial of interface.
void read()
Read from BlackBoard into local copy.
Mutex * objmutex_ptr() const
Get object mutex.
bool has_writer() const
Check if there is a writer for the interface.
SkillStatusEnum status() const
Get status value.
ExecSkillMessage Fawkes BlackBoard Interface Message.
const char * name() const
Get name of thread.
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.
bool is_enabled() const
Get enabled value.
LockPtr< CLIPS::Environment > clips
CLIPS environment for exclusive usage.
unsigned int msgq_enqueue(Message *message)
Enqueue message at end of queue.
virtual void log_debug(const char *component, const char *format,...)=0
Log debug message.
virtual std::vector< std::string > get_strings(const char *path)=0
Get list of values from configuration which is of type string.
virtual Interface * open_for_reading(const char *interface_type, const char *identifier, const char *owner=NULL)=0
Open interface for reading.
SkillerInterface Fawkes BlackBoard Interface.
Configuration * config
This is the Configuration member used to access the configuration.
virtual void init()
Initialize the thread.
virtual void finalize()
Finalize the thread.
virtual float get_float(const char *path)=0
Get value from configuration which is of type float.
Thread aspect to get access to a CLIPS environment.
BlackBoard * blackboard
This is the BlackBoard instance you can use to interact with the BlackBoard.
virtual void loop()
Code to execute in the thread.
virtual void close(Interface *interface)=0
Close interface.
virtual ~ClipsAgentThread()
Destructor.