10 Logging::setTag(
"armarx::skills::" + description.skillId.toString());
15 Skill::installConditionWithCallback(std::function<
bool()>&& f, std::function<
void()>&& cb)
17 std::scoped_lock l(conditionCallbacksMutex);
18 conditionCallbacks.push_back({f, cb});
21 std::optional<TerminatedSkillStatusUpdate>
27 std::optional<TerminatedSkillStatusUpdate>
30 auto executionId = callSubskillAsync(proxy, parameters);
31 auto ret = proxy.
join(executionId);
37 throwIfSkillShouldTerminate();
44 std::unique_lock l(subskillsMutex);
46 std::string executorHistory = this->executorName +
"->" + getSkillId().toString();
48 this->subskills.push_back(eid);
52 std::optional<TerminatedSkillStatusUpdate>
53 Skill::callSubskill(
const SkillID& skillId)
55 return callSubskill(
SkillProxy(manager, skillId));
58 std::optional<TerminatedSkillStatusUpdate>
61 return callSubskill(
SkillProxy(manager, skillId), parameters);
64 std::optional<TerminatedSkillStatusUpdate>
65 Skill::callSubskill(
const SkillID& skillId,
76 parametersFunction(parameters);
78 return callSubskill(proxy, parameters);
84 std::scoped_lock l(this->parametersMutex);
85 if (this->parameters ==
nullptr)
93 this->parameters->mergeAndReplaceCopy(d);
101 if (running or exiting or finished)
106 std::scoped_lock l(this->parametersMutex);
107 this->parameters = d;
111 Skill::getParameters()
const
113 return this->parameters;
120 this->initializing =
true;
121 this->constructing =
false;
122 this->preparing =
false;
123 this->running =
false;
124 this->exiting =
false;
125 this->finished =
false;
128 installConditionWithCallback(
132 [&]() { notifyTimeoutReached(); });
134 conditionCheckingThread = std::thread(
138 while (initializing or preparing or
142 std::scoped_lock l(conditionCallbacksMutex);
143 for (
auto& p : conditionCallbacks)
154 const auto sleepDuration = metronome.waitForNextTick();
155 if (not sleepDuration.isPositive())
158 <<
"ConditionCheckingThread: execution took too long ("
159 << -sleepDuration <<
" vs "
160 << conditionCheckingThreadFrequency.toCycleDuration()
165 return {.
status = TerminatedSkillStatus::Succeeded};
171 this->preparing =
true;
172 this->initializing =
false;
173 this->constructing =
false;
174 this->running =
false;
175 this->exiting =
false;
176 this->finished =
false;
178 if (shouldSkillTerminate())
180 return {.status = ActiveOrTerminatedSkillStatus::Aborted};
184 if (not description.parametersType)
186 return {.status = ActiveOrTerminatedSkillStatus::Succeeded};
188 if (this->parameters && this->parameters->fullfillsType(description.parametersType))
191 return {.status = ActiveOrTerminatedSkillStatus::Succeeded};
201 this->running =
true;
202 this->initializing =
false;
203 this->constructing =
false;
204 this->preparing =
false;
205 this->exiting =
false;
206 this->finished =
false;
207 return {.status = TerminatedSkillStatus::Succeeded};
214 this->exiting =
true;
215 this->running =
false;
216 this->initializing =
false;
217 this->constructing =
false;
218 this->preparing =
false;
219 this->finished =
false;
221 if (conditionCheckingThread.joinable())
223 conditionCheckingThread.join();
227 this->finished =
true;
228 this->exiting =
false;
229 return {.status = TerminatedSkillStatus::Succeeded};
235 std::scoped_lock l(parametersMutex);
236 auto _res = this->_init();
237 auto res = this->init();
242 Skill::prepareSkill()
244 std::scoped_lock l(parametersMutex);
245 auto _res = this->_prepare();
246 auto res = this->prepare();
253 std::scoped_lock l(parametersMutex);
254 auto _res = this->_main();
255 auto res = this->
main();
263 std::scoped_lock l(parametersMutex);
264 auto res = this->exit();
265 auto _res = this->_exit();
270 Skill::throwIfSkillShouldTerminate(
const std::function<
void()>& do_before,
271 const std::string& abortedMessage)
273 if (shouldSkillTerminate())
276 throwIfSkillShouldTerminate(abortedMessage);
281 Skill::throwIfSkillShouldTerminate(
const std::string& abortedMessage)
286 std::string(
"The skill '" + getSkillId().
toString() +
"' was asked to stop.");
287 message += abortedMessage.empty() ?
"" :
" Additional message: " + abortedMessage;
296 std::string(
"The skill '" + getSkillId().
toString() +
"' reached timeout.");
297 message += abortedMessage.empty() ?
"" :
" Additional message: " + abortedMessage;
308 .
status = TerminatedSkillStatus::Succeeded,
314 Skill::MakeFailedResult()
317 .
status = TerminatedSkillStatus::Failed,
323 Skill::MakeAbortedResult()
326 .
status = TerminatedSkillStatus::Aborted,
332 Skill::notifySkillToStop()
334 std::scoped_lock l(subskillsMutex);
341 Skill::notifyTimeoutReached()
343 std::scoped_lock l(subskillsMutex);
344 timeoutReached =
true;
350 Skill::shouldSkillTerminate()
const
352 return stopped || timeoutReached;
357 Skill::_onTimeoutReached()
363 for (
const auto& execId : subskills)
365 manager->abortSkillAsync(execId.toManagerIce());
370 Skill::_onStopRequested()
376 for (
const auto& execId : subskills)
378 manager->abortSkillAsync(execId.toManagerIce());
383 Skill::onTimeoutReached()
388 Skill::onStopRequested()
397 return {.
status = TerminatedSkillStatus::Succeeded};
405 return {.
status = ActiveOrTerminatedSkillStatus::Succeeded};
413 return {.
status = TerminatedSkillStatus::Succeeded};
421 <<
"'. Please overwrite this method.";
422 return {.status = TerminatedSkillStatus::Succeeded, .data =
nullptr};