Fawkes API Fawkes Development Version
execution_time_estimator.cpp
1/***************************************************************************
2 * execution_time_estimator.cpp - Aspect for a execution time provider
3 *
4 * Created: Thu 12 Dec 2019 19:03:19 CET 19:03
5 * Copyright 2019 Till Hofmann <hofmann@kbsg.rwth-aachen.de>
6 ****************************************************************************/
7
8/* This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Library General Public License for more details.
17 *
18 * Read the full text in the LICENSE.GPL file in the doc directory.
19 */
20
21#include "execution_time_estimator.h"
22
23#include <core/exception.h>
24#include <core/exceptions/software.h>
25
26#include <algorithm>
27
28namespace fawkes {
29
30/** @class ExecutionTimeEstimatorManager
31 * A manager for execution time providers.
32 * It stores prioritized providers, where the provider with the maximal
33 * priority is considered first.
34 */
35
36/** Get the execution time provider for the given skill string.
37 * @param skill_string The string to get the execution time for
38 * @return a pointer to the provider
39 * @throws IllegalArgumentException if no provider for the given skill exists
40 */
41std::shared_ptr<ExecutionTimeEstimator>
42ExecutionTimeEstimatorManager::get_provider(const std::string &skill_string) const
43{
44 for (const auto &pair : execution_time_estimators_) {
45 const auto &provider = pair.second;
46 if (provider->can_execute(skill_string)) {
47 return provider;
48 }
49 }
50 throw IllegalArgumentException("No provider found for %s", skill_string.c_str());
51}
52
53/** Add an execution time provider.
54 * @param provider The provider to add
55 * @param priority The priority of the new provider
56 */
57void
58ExecutionTimeEstimatorManager::register_provider(std::shared_ptr<ExecutionTimeEstimator> provider,
59 int priority)
60{
61 execution_time_estimators_.insert(std::make_pair(priority, provider));
62}
63
64/** Remove an execution time estimate provider.
65 * @param provider The provider to remove
66 */
67void
68ExecutionTimeEstimatorManager::unregister_provider(std::shared_ptr<ExecutionTimeEstimator> provider)
69{
70#if __GNUC__ >= 9
71 std::erase_if(execution_time_estimators_, [&](auto &pair) { return provider == pair.second; });
72#else
73 for (auto it = execution_time_estimators_.begin(); it != execution_time_estimators_.end();) {
74 if (it->second == provider) {
75 it = execution_time_estimators_.erase(it);
76 } else {
77 it++;
78 }
79 }
80#endif
81}
82
83/** @class ExecutionTimeEstimatorsAspect
84 * An aspect to give access to the execution time estimator manager.
85 * Use this aspect to add an execution time provider.
86 *
87 * @var ExecutionTimeEstimatorsAspect::execution_time_estimator_manager_
88 * The ExecutionTimeEstimatorManager that is used to manage the estimators.
89 * @see ExecutionTimeEstimatorManager
90 */
91
92/** Constructor. */
94: execution_time_estimator_manager_(nullptr)
95{
96 add_aspect("SkillExecutionTimeEstimatorAspect");
97}
98
99/** Initialize the aspect with a provider manager.
100 * @param provider_manager The manager of the execution time providers
101 */
102void
104 ExecutionTimeEstimatorManager *provider_manager)
105{
106 execution_time_estimator_manager_ = provider_manager;
107}
108
109/** Finalize the aspect. */
110void
112{
113}
114
115} // namespace fawkes
void add_aspect(const char *name)
Add an aspect to a thread.
Definition: aspect.cpp:49
A manager for execution time providers.
std::shared_ptr< ExecutionTimeEstimator > get_provider(const std::string &skill_string) const
Get the execution time provider for the given skill string.
void register_provider(std::shared_ptr< ExecutionTimeEstimator > provider, int priority=0)
Add an execution time provider.
void unregister_provider(std::shared_ptr< ExecutionTimeEstimator > provider)
Remove an execution time estimate provider.
ExecutionTimeEstimatorManager * execution_time_estimator_manager_
The ExecutionTimeEstimatorManager that is used to manage the estimators.
void finalize_ExecutionTimeEstimatorsAspect()
Finalize the aspect.
void init_ExecutionTimeEstimatorsAspect(ExecutionTimeEstimatorManager *provider_manager)
Initialize the aspect with a provider manager.
Expected parameter is missing.
Definition: software.h:80
Fawkes library namespace.