Fawkes API  Fawkes Development Version
execution_time_estimator.h
1 /***************************************************************************
2  * execution_time_estimator.h - An execution time estimator for skills
3  *
4  * Created: Thu 12 Dec 2019 15:10:06 CET 15:10
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 #pragma once
22 
23 #include <config/config.h>
24 #include <interfaces/SkillerInterface.h>
25 
26 #include <optional>
27 #include <string>
28 #include <unordered_map>
29 #include <vector>
30 
31 namespace fawkes {
32 
34 {
35 public:
36  ExecutionTimeEstimator(Configuration *config, const ::std::string &cfg_prefix);
37  /** A structured representation of a skill. */
38  class Skill
39  {
40  public:
41  Skill(const std::string &skill_string);
42  bool matches(const Skill &skill) const;
43 
44  /** The name of the skill */
45  std::string skill_name = "";
46  /** A map of the skill's argument keys to argument values */
47  std::unordered_map<std::string, std::string> skill_args = {};
48 
49  private:
50  void parse_args(const std::string &args);
51  };
52 
53  /** A configurable property that is skill-specific and may have a default value. */
54  template <typename T>
55  class Property
56  {
57  public:
59  const std::string & path,
60  const std::string & property,
61  const std::optional<T> &default_value = std::nullopt);
62  T get_property(const std::string &key) const;
63  T get_default_value() const;
64 
65  /** Mapping from skill entry id to property value */
66  std::map<std::string, T> property_entries;
67 
68  private:
69  /** Global default value that is used whenever no property entry is found */
70  std::optional<T> default_value;
71  };
72 
73  /** Destructor. */
74  virtual ~ExecutionTimeEstimator() = default;
75 
76  virtual float get_execution_time(const Skill &skill) = 0;
77  virtual bool can_execute(const Skill &skill);
78  virtual std::pair<SkillerInterface::SkillStatusEnum, std::string>
79  execute(const Skill &skill)
80  {
81  return std::make_pair(SkillerInterface::SkillStatusEnum::S_FINAL, "");
82  };
83 
84 protected:
85  std::map<std::string, Skill> get_skills_from_config(const std::string &path) const;
86 
87  virtual bool can_provide_exec_time(const Skill &skill) const = 0;
88 
89  template <typename T>
90  T get_property(const Property<T> &property) const;
91  /** Config to obtain common configurables */
93  /** Config prefix of the estimator */
94  const std::string cfg_prefix_;
95  /** Config estimator-specific speedup factor */
96  const float speed_;
97  /** Points to the whitelist entry that matches the skill to execute. */
98  std::map<std::string, Skill>::const_iterator active_whitelist_entry_;
99  /** Whitelist of skills that the estimator is allowed to process */
100  const std::map<std::string, Skill> whitelist_;
101  /** Blacklist of skills that the estimator must not process */
102  const std::map<std::string, Skill> blacklist_;
103 
104 private:
105 };
106 
107 } // namespace fawkes
Interface for configuration handling.
Definition: config.h:65
A configurable property that is skill-specific and may have a default value.
Property(fawkes::Configuration *config, const std::string &path, const std::string &property, const std::optional< T > &default_value=std::nullopt)
Constructor.
T get_property(const std::string &key) const
Get the property falue for a given skill.
T get_default_value() const
Get the default value if it is set, otherwise throw an exception.
std::map< std::string, T > property_entries
Mapping from skill entry id to property value.
A structured representation of a skill.
bool matches(const Skill &skill) const
Check, whether the skill matches another skill description.
std::string skill_name
The name of the skill.
Skill(const std::string &skill_string)
Constructor.
std::unordered_map< std::string, std::string > skill_args
A map of the skill's argument keys to argument values.
An abstract estimator for the execution time of a skill.
virtual float get_execution_time(const Skill &skill)=0
Get the estimated execution time for the given skill string.
virtual bool can_provide_exec_time(const Skill &skill) const =0
Check if this estimator can give an estimate for a given skill.
Configuration *const config_
Config to obtain common configurables.
virtual std::pair< SkillerInterface::SkillStatusEnum, std::string > execute(const Skill &skill)
Let the estimator know that we are executing this skill, so it can apply possible side effects.
std::map< std::string, Skill >::const_iterator active_whitelist_entry_
Points to the whitelist entry that matches the skill to execute.
const std::map< std::string, Skill > blacklist_
Blacklist of skills that the estimator must not process.
virtual bool can_execute(const Skill &skill)
Check if this estimator is both allowed and able to give an estimate for a given skill.
const std::map< std::string, Skill > whitelist_
Whitelist of skills that the estimator is allowed to process.
T get_property(const Property< T > &property) const
Get the current property value for active_whitelist_entry_.
virtual ~ExecutionTimeEstimator()=default
Destructor.
std::map< std::string, Skill > get_skills_from_config(const std::string &path) const
Load skill descriptions from a yaml config.
const std::string cfg_prefix_
Config prefix of the estimator.
ExecutionTimeEstimator(Configuration *config, const ::std::string &cfg_prefix)
Constructor.
const float speed_
Config estimator-specific speedup factor.
Fawkes library namespace.