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 #include <mongo/client/dbclient.h>
31 
32 #include <boost/bind.hpp>
33 #include <map>
34 #include <tuple>
35 #include <utility>
36 
37 //forward declaration
38 class RobotMemory;
39 
41 {
42 public:
44  virtual ~ComputablesManager();
45 
46  bool check_and_compute(mongo::Query query, std::string collection);
47  void remove_computable(Computable *computable);
48  void cleanup_computed_docs();
49 
50  /**
51  * Registers a Computable which provides information in the robot memory that is computed on demand.
52  * @param query_to_compute Query describing what the function computes. Yor computable is called when an new query matches query_to_compute.
53  * @param collection db.collection to fill with computed information
54  * @param compute_func Callback function that computes the information and retruns a list of computed documents
55  * @param obj Pointer to class the callback is a function of (usaually this)
56  * @param caching_time How long should computed results for a query be cached and be used for identical queries in that time?
57  * @param priority Computable priority ordering the evaluation
58  * @return Computable Object pointer used for removing it
59  */
60  template <typename T>
61  Computable *
62  register_computable(const mongo::Query &query_to_compute,
63  const std::string & collection,
64  std::list<mongo::BSONObj> (T::*compute_func)(const mongo::BSONObj &,
65  const std::string &),
66  T * obj,
67  double caching_time = 0.0,
68  int priority = 0)
69  {
70  Computable *comp = new Computable(
71  query_to_compute, collection, boost::bind(compute_func, obj, _1, _2), caching_time, priority);
72  //sort it into the right position
73  std::list<Computable *>::iterator pos = computables.begin();
74  while (pos != computables.end() && priority < (*pos)->get_priority())
75  pos++;
76  computables.insert(pos, comp);
77  return comp;
78  }
79 
80 private:
81  std::string name = "RobotMemory ComputablesManager";
82  fawkes::Configuration *config_;
83  RobotMemory * robot_memory_;
84 
85  std::list<Computable *> computables;
86  std::string matching_test_collection_;
87  //cached querries as ((collection, querry), cached_until)
88  std::map<std::tuple<std::string, std::string>, long long> cached_querries_;
89 };
90 
91 #endif /* FAWKES_SRC_PLUGINS_ROBOT_MEMORY_COMPUTABLES_COMPUTABLES_MANAGER_H_ */
This class manages registering computables and can check if any computables are invoced by a query.
void cleanup_computed_docs()
Clean up all collections containing documents computed on demand.
Class holding information for a single computable this class also enhances computed documents by addi...
Definition: computable.h:29
ComputablesManager(fawkes::Configuration *config, RobotMemory *robot_memory)
Constructor for class managing computables with refereces to plugin objects.
void remove_computable(Computable *computable)
Remove previously registered computable.
Computable * register_computable(const mongo::Query &query_to_compute, const std::string &collection, std::list< mongo::BSONObj >(T::*compute_func)(const mongo::BSONObj &, 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:48
Interface for configuration handling.
Definition: config.h:64
bool check_and_compute(mongo::Query query, std::string collection)
Checks if computable knowledge is queried and calls the compute functions in this case.