22 #include "clips_agent_thread.h" 24 #include <utils/misc/string_conversions.h> 25 #include <utils/misc/string_split.h> 26 #include <interfaces/SwitchInterface.h> 27 #include <core/threading/mutex_locker.h> 39 :
Thread(
"ClipsAgentThread",
Thread::OPMODE_WAITFORWAKEUP),
57 cfg_auto_start_ =
false;
58 cfg_assert_time_each_loop_ =
false;
59 cfg_skill_sim_time_ = 2.0;
60 cfg_skill_sim_ =
false;
61 cfg_steal_skiller_control_ =
true;
67 cfg_assert_time_each_loop_ =
config->
get_bool(
"/clips-agent/assert-time-each-loop");
77 cfg_steal_skiller_control_ =
config->
get_bool(
"/clips-agent/steal-skiller-control");
81 std::vector<std::string> clips_dirs;
84 for (
size_t i = 0; i < clips_dirs.size(); ++i) {
85 if (clips_dirs[i][clips_dirs[i].size()-1] !=
'/') {
91 clips_dirs.insert(clips_dirs.begin(), std::string(SRCDIR) +
"/clips/");
93 if (! cfg_skill_sim_) {
98 throw Exception(
"Skiller has no writer, aborting");
102 throw Exception(
"Skiller already has a different exclusive controller");
110 clips->evaluate(std::string(
"(path-add-subst \"@BASEDIR@\" \"") + BASEDIR +
"\")");
111 clips->evaluate(std::string(
"(path-add-subst \"@FAWKES_BASEDIR@\" \"") +
112 FAWKES_BASEDIR +
"\")");
113 clips->evaluate(std::string(
"(path-add-subst \"@RESDIR@\" \"") + RESDIR +
"\")");
114 clips->evaluate(std::string(
"(path-add-subst \"@CONFDIR@\" \"") + CONFDIR +
"\")");
116 for (
size_t i = 0; i < clips_dirs.size(); ++i) {
117 clips->evaluate(
"(path-add \"" + clips_dirs[i] +
"\")");
120 clips->add_function(
"skill-call-ext", sigc::slot<void, std::string, std::string>(sigc::mem_fun( *
this, &ClipsAgentThread::clips_skill_call_ext)));
122 clips->evaluate(
"(ff-feature-request \"config\")");
124 bool cfg_req_redefwarn_feature =
true;
126 cfg_req_redefwarn_feature =
127 config->
get_bool(
"/clips-agent/request-redefine-warning-feature");
129 if (cfg_req_redefwarn_feature) {
131 clips->evaluate(
"(ff-feature-request \"redefine-warning\")");
134 if (!
clips->batch_evaluate(SRCDIR
"/clips/init.clp")) {
136 "batch file failed.");
138 throw Exception(
"Failed to initialize CLIPS environment, batch file failed.");
141 clips->assert_fact(
"(agent-init)");
142 clips->refresh_agenda();
145 ctrl_recheck_ =
true;
155 clips->remove_function(
"skill-call-ext");
157 if ( ! cfg_skill_sim_ && skiller_if_->
has_writer()) {
173 if (! started_ && cfg_auto_start_) {
174 clips->assert_fact(
"(start)");
178 if (! cfg_skill_sim_) {
188 ctrl_recheck_ =
false;
190 ctrl_recheck_ =
true;
199 clips->assert_fact(
"(start)");
206 if (cfg_assert_time_each_loop_) {
207 clips->assert_fact(
"(time (now))");
212 if (! active_skills_.empty()) {
213 if (! cfg_skill_sim_) skiller_if_->
read();
215 std::list<std::string> finished_skills;
216 std::map<std::string, SkillExecInfo>::iterator as;
217 for (as = active_skills_.begin(); as != active_skills_.end(); ++as) {
218 const std::string &as_name = as->first;
219 const SkillExecInfo &as_info = as->second;
221 if (cfg_skill_sim_) {
222 if ((now - as_info.start_time) >= cfg_skill_sim_time_) {
224 clips->assert_fact_f(
"(skill-update (name \"%s\") (status FINAL))",
226 finished_skills.push_back(as_name);
228 clips->assert_fact_f(
"(skill-update (name \"%s\") (status RUNNING))",
232 if (as_info.skill_string == skiller_if_->
skill_string()) {
233 clips->assert_fact_f(
"(skill-update (name \"%s\") (status %s))", as_name.c_str(),
234 status_string(skiller_if_->
status()));
236 if (skiller_if_->
status() == SkillerInterface::S_FINAL ||
237 skiller_if_->
status() == SkillerInterface::S_FAILED)
239 finished_skills.push_back(as_name);
244 std::list<std::string>::iterator fs;
245 for (fs = finished_skills.begin(); fs != finished_skills.end(); ++fs) {
246 active_skills_.erase(*fs);
250 clips->refresh_agenda();
259 case SkillerInterface::S_FINAL:
return "FINAL";
260 case SkillerInterface::S_FAILED:
return "FAILED";
261 case SkillerInterface::S_RUNNING:
return "RUNNING";
262 default:
return "IDLE";
268 ClipsAgentThread::clips_skill_call_ext(std::string skill_name, std::string skill_string)
270 if (active_skills_.find(skill_name) != active_skills_.end()) {
275 if (cfg_skill_sim_) {
289 sei.skill_string = skill_string;
290 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.