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?");
270 HardwareModelsThread::clips_add_transition(
const std::string &component,
271 const std::string &transition)
throw()
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);