Fawkes API  Fawkes Development Version
computables_manager.h
1 /***************************************************************************
2  * computables_manager.h - Class managing registered computables and
3  * checking if any computables are invoced by a query
4  *
5  * Created: 6:37:44 PM 2016
6  * Copyright 2016 Frederik Zwilling
7  ****************************************************************************/
8 
9 /* This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Library General Public License for more details.
18  *
19  * Read the full text in the LICENSE.GPL file in the doc directory.
20  */
21 
22 #ifndef FAWKES_SRC_PLUGINS_ROBOT_MEMORY_COMPUTABLES_COMPUTABLES_MANAGER_H_
23 #define FAWKES_SRC_PLUGINS_ROBOT_MEMORY_COMPUTABLES_COMPUTABLES_MANAGER_H_
24 
25 #include "computable.h"
26 
27 #include <aspect/clock.h>
28 #include <aspect/configurable.h>
29 #include <aspect/logging.h>
30 
31 #include <boost/bind.hpp>
32 #include <map>
33 #include <mongocxx/client.hpp>
34 #include <tuple>
35 #include <utility>
36 
37 //forward declaration
38 class RobotMemory;
39 
40 namespace fawkes {
41 #ifdef USE_TIMETRACKER
42 class TimeTracker;
43 #endif
44 } // namespace fawkes
45 
47 {
48 public:
50  virtual ~ComputablesManager();
51 
52  bool check_and_compute(const bsoncxx::document::view &query, std::string collection);
53  void remove_computable(Computable *computable);
54  void cleanup_computed_docs();
55 
56  /**
57  * Registers a Computable which provides information in the robot memory that is computed on demand.
58  * @param query_to_compute Query describing what the function computes. Yor computable is called when an new query matches query_to_compute.
59  * @param collection db.collection to fill with computed information
60  * @param compute_func Callback function that computes the information and retruns a list of computed documents
61  * @param obj Pointer to class the callback is a function of (usaually this)
62  * @param caching_time How long should computed results for a query be cached and be used for identical queries in that time?
63  * @param priority Computable priority ordering the evaluation
64  * @return Computable Object pointer used for removing it
65  */
66  template <typename T>
68  register_computable(bsoncxx::document::value &&query_to_compute,
69  const std::string & collection,
70  std::list<bsoncxx::document::value> (
71  T::*compute_func)(const bsoncxx::document::view &, const std::string &),
72  T * obj,
73  double caching_time = 0.0,
74  int priority = 0)
75  {
76  Computable *comp = new Computable(
77  query_to_compute, collection, boost::bind(compute_func, obj, _1, _2), caching_time, priority);
78  //sort it into the right position
79  std::list<Computable *>::iterator pos = computables.begin();
80  while (pos != computables.end() && priority < (*pos)->get_priority())
81  pos++;
82  computables.insert(pos, comp);
83  return comp;
84  }
85 
86 private:
88 
89 private:
90  std::string name = "RobotMemory ComputablesManager";
91  fawkes::Configuration *config_;
92  RobotMemory * robot_memory_;
93 
94  std::list<Computable *> computables;
95  std::string matching_test_collection_;
96  //cached querries as ((collection, querry), cached_until)
97  std::map<std::tuple<std::string, std::string>, long long> cached_querries_;
98 #ifdef USE_TIMETRACKER
100  unsigned int tt_loopcount_;
101  unsigned int ttc_cleanup_;
102  unsigned int ttc_cleanup_inner_loop_;
103  unsigned int ttc_cleanup_remove_query_;
104 #endif
105 };
106 
107 #endif /* FAWKES_SRC_PLUGINS_ROBOT_MEMORY_COMPUTABLES_COMPUTABLES_MANAGER_H_ */
ComputablesManager::ComputablesManager
ComputablesManager(fawkes::Configuration *config, RobotMemory *robot_memory)
Constructor for class managing computables with refereces to plugin objects.
Definition: computables_manager.cpp:48
ComputablesManager::register_computable
Computable * register_computable(bsoncxx::document::value &&query_to_compute, const std::string &collection, std::list< bsoncxx::document::value >(T::*compute_func)(const bsoncxx::document::view &, const std::string &), T *obj, double caching_time=0.0, int priority=0)
Registers a Computable which provides information in the robot memory that is computed on demand.
Definition: computables_manager.h:67
ComputablesManager
Definition: computables_manager.h:45
RobotMemory
Definition: robot_memory.h:45
fawkes::Configuration
Definition: config.h:68
fawkes
ComputablesManager::check_and_compute
bool check_and_compute(const bsoncxx::document::view &query, std::string collection)
Checks if computable knowledge is queried and calls the compute functions in this case.
Definition: computables_manager.cpp:113
ComputablesManager::cleanup_computed_docs
void cleanup_computed_docs()
Clean up all collections containing documents computed on demand.
Definition: computables_manager.cpp:164
fawkes::TimeTracker
Definition: tracker.h:40
Computable
Definition: computable.h:30
ComputablesManager::remove_computable
void remove_computable(Computable *computable)
Remove previously registered computable.
Definition: computables_manager.cpp:94