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/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 
46 using namespace boost::placeholders;
47 
49 {
50 public:
52  virtual ~ComputablesManager();
53 
54  bool check_and_compute(const bsoncxx::document::view &query, std::string collection);
55  void remove_computable(Computable *computable);
56  void cleanup_computed_docs();
57 
58  /**
59  * Registers a Computable which provides information in the robot memory that is computed on demand.
60  * @param query_to_compute Query describing what the function computes. Yor computable is called when an new query matches query_to_compute.
61  * @param collection db.collection to fill with computed information
62  * @param compute_func Callback function that computes the information and retruns a list of computed documents
63  * @param obj Pointer to class the callback is a function of (usaually this)
64  * @param caching_time How long should computed results for a query be cached and be used for identical queries in that time?
65  * @param priority Computable priority ordering the evaluation
66  * @return Computable Object pointer used for removing it
67  */
68  template <typename T>
69  Computable *
70  register_computable(bsoncxx::document::value &&query_to_compute,
71  const std::string & collection,
72  std::list<bsoncxx::document::value> (
73  T::*compute_func)(const bsoncxx::document::view &, const std::string &),
74  T * obj,
75  double caching_time = 0.0,
76  int priority = 0)
77  {
78  Computable *comp = new Computable(
79  query_to_compute, collection, boost::bind(compute_func, obj, _1, _2), caching_time, priority);
80  //sort it into the right position
81  std::list<Computable *>::iterator pos = computables.begin();
82  while (pos != computables.end() && priority < (*pos)->get_priority())
83  pos++;
84  computables.insert(pos, comp);
85  return comp;
86  }
87 
88 private:
90 
91 private:
92  std::string name = "RobotMemory ComputablesManager";
93  fawkes::Configuration *config_;
94  RobotMemory * robot_memory_;
95 
96  std::list<Computable *> computables;
97  std::string matching_test_collection_;
98  //cached querries as ((collection, querry), cached_until)
99  std::map<std::tuple<std::string, std::string>, long long> cached_querries_;
100 #ifdef USE_TIMETRACKER
101  fawkes::TimeTracker *tt_;
102  unsigned int tt_loopcount_;
103  unsigned int ttc_cleanup_;
104  unsigned int ttc_cleanup_inner_loop_;
105  unsigned int ttc_cleanup_remove_query_;
106 #endif
107 };
108 
109 #endif /* FAWKES_SRC_PLUGINS_ROBOT_MEMORY_COMPUTABLES_COMPUTABLES_MANAGER_H_ */
Class holding information for a single computable this class also enhances computed documents by addi...
Definition: computable.h:32
This class manages registering computables and can check if any computables are invoced by a query.
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.
Access to the robot memory based on mongodb.
Definition: robot_memory.h:47
Interface for configuration handling.
Definition: config.h:68
Time tracking utility.
Definition: tracker.h:37
Fawkes library namespace.