22#include "hardware_models_thread.h"
24#include <config/yaml.h>
25#include <core/threading/mutex_locker.h>
26#include <interfaces/SwitchInterface.h>
27#include <utils/misc/map_skill.h>
28#include <utils/misc/string_conversions.h>
29#include <utils/misc/string_split.h>
30#include <utils/time/wait.h>
58:
Thread(
"HardwareModelsThread",
Thread::OPMODE_WAITFORWAKEUP),
69 std::string cfg_interface_ =
"HardwareModels";
90 envs_[env_name] = clips;
92 clips->batch_evaluate(SRCDIR
"/hardware_models.clp");
95 for (
const auto &c : components_) {
96 if (!
config->
exists(std::string(c +
"/states").c_str())) {
102 std::vector<std::string> states =
config->
get_strings(std::string(c +
"/states").c_str());
105 if (states.empty()) {
110 for (
const auto &state : states) {
112 if (!(
config->
is_list(std::string(c +
"/" + state +
"/edges").c_str()))) {
113 logger->
log_warn(
name(),
"State %s of %s needs edges field", state.c_str(), c.c_str());
117 std::vector<std::string> edges =
119 for (
const auto &edge : edges) {
120 std::string transition =
"";
123 if (
config->
exists(std::string(c +
"/" + state +
"/" + edge +
"/transition"))) {
125 config->
get_string(std::string(c +
"/" + state +
"/" + edge +
"/transition").c_str());
127 if (
config->
exists(std::string(c +
"/" + state +
"/" + edge +
"/probability").c_str())) {
128 clips_add_edge(clips, c, state, edge, transition);
131 }
else if (
config->
exists(std::string(c +
"/" + state +
"/" + edge +
"/transitions"))) {
132 std::vector<std::string> transitions =
134 for (std::string transition : transitions) {
136 std::string(c +
"/" + state +
"/" + edge +
"/probability").c_str())) {
137 clips_add_edge(clips, c, state, edge, transition);
142 "Cant find transition/transitions value in %s",
143 std::string(c +
"/" + state +
"/" + edge).c_str());
149 if (
config->
exists(std::string(c +
"/" + state +
"/terminal").c_str())
151 clips_add_terminal_state(clips, c, state);
154 clips_add_component(clips, c, states[0]);
163 envs_.erase(env_name);
169 const std::string & component,
170 const std::string & state)
172 CLIPS::Template::pointer temp = clips->get_template(
"hm-terminal-state");
174 CLIPS::Fact::pointer fact = CLIPS::Fact::create(**clips, temp);
175 fact->set_slot(
"name", CLIPS::Value(component.c_str(), CLIPS::TYPE_SYMBOL));
176 fact->set_slot(
"state", CLIPS::Value(state.c_str(), CLIPS::TYPE_SYMBOL));
178 CLIPS::Fact::pointer new_fact = clips->assert_fact(fact);
186 "Did not get terminal state template, did you load hardware_models.clp?");
199 const std::string & component,
200 const std::string & init_state)
202 CLIPS::Template::pointer temp = clips->get_template(
"hm-component");
204 CLIPS::Fact::pointer fact = CLIPS::Fact::create(**clips, temp);
205 fact->set_slot(
"name", CLIPS::Value(component.c_str(), CLIPS::TYPE_SYMBOL));
206 fact->set_slot(
"initial-state", CLIPS::Value(init_state.c_str(), CLIPS::TYPE_SYMBOL));
208 CLIPS::Fact::pointer new_fact = clips->assert_fact(fact);
215 logger->
log_warn(
name(),
"Did not get component template, did you load hardware_models.clp?");
231 const std::string & component,
232 const std::string & from,
233 const std::string & to,
234 const std::string & trans)
237 prob =
config->
get_float(std::string(component +
"/" + from +
"/" + to +
"/probability").c_str());
239 CLIPS::Template::pointer temp = clips->get_template(
"hm-edge");
241 CLIPS::Fact::pointer fact = CLIPS::Fact::create(**clips, temp);
242 fact->set_slot(
"component", CLIPS::Value(component.c_str(), CLIPS::TYPE_SYMBOL));
243 fact->set_slot(
"from", CLIPS::Value(from.c_str(), CLIPS::TYPE_SYMBOL));
244 fact->set_slot(
"to", CLIPS::Value(to.c_str(), CLIPS::TYPE_SYMBOL));
245 fact->set_slot(
"transition", CLIPS::Value(trans.c_str(), CLIPS::TYPE_SYMBOL));
246 fact->set_slot(
"probability", prob);
248 CLIPS::Fact::pointer new_fact = clips->assert_fact(fact);
251 logger->
log_warn(
name(),
"Asserting edge from %s to %s failed", from.c_str(), to.c_str());
254 name(),
"Edge from %s to %s via %s", from.c_str(), to.c_str(), trans.c_str());
258 logger->
log_warn(
name(),
"Did not get edge template, did you load hardware_models.clp?");
270HardwareModelsThread::clips_add_transition(
const std::string &component,
271 const std::string &transition)
noexcept
273 for (
const auto &e : envs_) {
276 CLIPS::Template::pointer temp = clips->get_template(
"hm-transition");
278 CLIPS::Fact::pointer fact = CLIPS::Fact::create(**clips, temp);
279 fact->set_slot(
"component", component.c_str());
280 fact->set_slot(
"transition", transition.c_str());
282 CLIPS::Fact::pointer new_fact = clips->assert_fact(fact);
286 "Asserting transition of %s: %s failed",
292 logger->
log_warn(name(),
"Did not get edge template, did you load hardware_models.clp?");
295 logger->
log_error(name(),
"Added transition in env: %s", e.first.c_str());
317 std::string comp = std::string(msg->
component());
318 std::string trans = std::string(msg->
transition());
321 "Component: %s changed state by executing transition: %s",
325 clips_add_transition(comp, trans);
HardwareModelsThread()
Constructor.
virtual void finalize()
Finalize the thread.
virtual void loop()
Loop function to be executed when a new HardwareInterfaceMessage is recieved.
virtual void clips_context_destroyed(const std::string &env_name)
Notification that a CLIPS environment has been destroyed.
virtual void clips_context_init(const std::string &env_name, fawkes::LockPtr< CLIPS::Environment > &clips)
Initializes hardware components from yaml files for a clips environment.
virtual void init()
Initialize the thread.
BlackBoard * blackboard
This is the BlackBoard instance you can use to interact with the BlackBoard.
BlackBoard interface listener.
void bbil_add_message_interface(Interface *interface)
Add an interface to the message received watch list.
virtual Interface * open_for_writing(const char *interface_type, const char *identifier, const char *owner=NULL)=0
Open interface for writing.
virtual void register_listener(BlackBoardInterfaceListener *listener, ListenerRegisterFlag flag=BBIL_FLAG_ALL)
Register BB event listener.
Thread aspect to provide a feature to CLIPS environments.
CLIPS feature maintainer.
Configuration * config
This is the Configuration member used to access the configuration.
virtual bool get_bool(const char *path)=0
Get value from configuration which is of type bool.
virtual float get_float(const char *path)=0
Get value from configuration which is of type float.
virtual std::vector< std::string > get_strings(const char *path)=0
Get list of values from configuration which is of type string.
virtual bool is_list(const char *path)=0
Check if a value is a list.
virtual bool exists(const char *path)=0
Check if a given value exists.
virtual std::string get_string(const char *path)=0
Get value from configuration which is of type string.
StateChangeMessage Fawkes BlackBoard Interface Message.
char * component() const
Get component value.
char * transition() const
Get transition value.
HardwareModelsInterface Fawkes BlackBoard Interface.
void set_busy(const bool new_busy)
Set busy value.
bool msgq_first_is()
Check if first message has desired type.
void msgq_pop()
Erase first message from queue.
Message * msgq_first()
Get the first message from the message queue.
void write()
Write from local copy into BlackBoard memory.
bool msgq_empty()
Check if queue is empty.
void read()
Read from BlackBoard into local copy.
void lock() const
Lock access to the encapsulated object.
void unlock() const
Unlock object mutex.
virtual void log_debug(const char *component, const char *format,...)=0
Log debug message.
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.
Logger * logger
This is the Logger member used to access the logger.
virtual void log_warn(const char *component, const char *format,...)
Log warning message.
virtual void log_error(const char *component, const char *format,...)
Log error message.
Thread class encapsulation of pthreads.
const char * name() const
Get name of thread.
void wakeup()
Wake up thread.
Fawkes library namespace.