Fawkes API  Fawkes Development Version
robot_memory.h
1 /***************************************************************************
2  * robot_memory.h - Class for storing and querying information in the RobotMemory
3  *
4  * Created: Aug 23, 2016 1:34:32 PM 2016
5  * Copyright 2016 Frederik Zwilling
6  * 2017 Tim Niemueller [www.niemueller.de]
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 #ifndef _PLUGINS_ROBOT_MEMORY_ROBOT_MEMORY_H_
22 #define _PLUGINS_ROBOT_MEMORY_ROBOT_MEMORY_H_
23 
24 #include "computables/computables_manager.h"
25 #include "event_trigger_manager.h"
26 
27 #include <aspect/blackboard.h>
28 #include <aspect/clock.h>
29 #include <aspect/configurable.h>
30 #include <aspect/logging.h>
31 #include <core/threading/mutex.h>
32 #include <plugins/mongodb/aspect/mongodb_conncreator.h>
33 
34 #include <memory>
35 #include <vector>
36 
37 namespace fawkes {
38 class RobotMemoryInterface;
39 }
40 namespace mongo {
41 class DBClientBase;
42 class DBClientCursor;
43 } // namespace mongo
44 
45 ///typedef for shorter type description
46 typedef std::unique_ptr<mongo::DBClientCursor> QResCursor;
47 
49 {
50  /// Friend the RobotMemoryThread so that only it can access the loop and init functions
51  friend class RobotMemoryThread;
52 
53 public:
57  fawkes::MongoDBConnCreator *mongo_connection_manager,
59  virtual ~RobotMemory();
60 
61  //robot memory functions
62  QResCursor query(mongo::Query query, const std::string &collection = "");
63  mongo::BSONObj aggregate(const std::vector<mongo::BSONObj> &pipeline,
64  const std::string & collection = "");
65  int insert(mongo::BSONObj obj, const std::string &collection = "");
66  int insert(std::vector<mongo::BSONObj> v_obj, const std::string &collection = "");
67  int insert(const std::string &obj_str, const std::string &collection = "");
68  int update(mongo::Query query,
69  mongo::BSONObj update,
70  const std::string &collection = "",
71  bool upsert = false);
72  int update(mongo::Query query,
73  const std::string &update_str,
74  const std::string &collection = "",
75  bool upsert = false);
76  mongo::BSONObj find_one_and_update(const mongo::BSONObj &filter,
77  const mongo::BSONObj &update,
78  const std::string & collection,
79  bool upsert = false,
80  bool return_new = true);
81  int remove(mongo::Query query, const std::string &collection = "");
82  mongo::BSONObj mapreduce(mongo::Query query,
83  const std::string &collection,
84  const std::string &js_map_fun,
85  const std::string &js_reduce_fun);
86  QResCursor aggregate(mongo::BSONObj pipeline, const std::string &collection = "");
87  int drop_collection(const std::string &collection);
88  int clear_memory();
89  int restore_collection(const std::string &collection,
90  const std::string &directory = "@CONFDIR@/robot-memory");
91  int dump_collection(const std::string &collection,
92  const std::string &directory = "@CONFDIR@/robot-memory");
93  int create_index(mongo::BSONObj keys, const std::string &collection = "", bool unique = false);
94 
95  //bool semaphore_create(const std::string& name, unsigned int value);
96  //bool semaphore_acquire(const std::string& name, unsigned int v = 1);
97  //bool semaphore_release(const std::string& name, unsigned int v = 1);
98  bool mutex_setup_ttl(float max_age_sec);
99  bool mutex_create(const std::string &name);
100  bool mutex_destroy(const std::string &name);
101  bool mutex_try_lock(const std::string &name, bool force = false);
102  bool mutex_try_lock(const std::string &name, const std::string &identity, bool force = false);
103  bool mutex_unlock(const std::string &name, const std::string &identity);
104  bool mutex_renew_lock(const std::string &name, const std::string &identity);
105  bool mutex_expire_locks(float max_age_sec);
106 
107  /**
108  * Register a trigger to be notified when the robot memory is updated and the updated document matches the query
109  * @param query Query the updated document has to match
110  * @param collection db.collection to use
111  * @param callback Callback function (e.g. &Class::callback)
112  * @param _obj Pointer to class the callback is a function of (usaually this)
113  * @return Trigger object pointer, save it to remove the trigger later
114  */
115  template <typename T>
116  EventTrigger *
117  register_trigger(mongo::Query query,
118  const std::string &collection,
119  void (T::*callback)(mongo::BSONObj),
120  T *_obj)
121  {
122  return trigger_manager_->register_trigger(query, collection, callback, _obj);
123  }
124  /**
125  * Register a trigger to be notified when the robot memory is updated and the updated document matches the query
126  * @param query_str Query as JSON string
127  * @param collection db.collection to use
128  * @param callback Callback function (e.g. &Class::callback)
129  * @param _obj Pointer to class the callback is a function of (usaually this)
130  * @return Trigger object pointer, save it to remove the trigger later
131  */
132  template <typename T>
133  EventTrigger *
134  register_trigger(const std::string &query_str,
135  const std::string &collection,
136  void (T::*callback)(mongo::BSONObj),
137  T *_obj)
138  {
139  return register_trigger(mongo::fromjson(query_str), collection, callback, _obj);
140  }
141  void remove_trigger(EventTrigger *trigger);
142 
143  /**
144  * Registers a Computable which provides information in the robot memory that is computed on demand.
145  *
146  * @param query_to_compute Query describing what the function computes. Yor computable is called when an new query matches the key value fields in the identifiyer.
147  * @param collection db.collection to fill with computed information
148  * @param compute_func Callback function that computes the information and retruns a list of computed documents
149  * @param obj Pointer to class the callback is a function of (usaually this)
150  * @param caching_time How long should computed results for a query be cached and be used for identical queries in that time?
151  * @param priority Computable priority ordering the evaluation
152  * @return Computable Object pointer used for removing it
153  */
154  template <typename T>
155  Computable *
156  register_computable(const mongo::Query &query_to_compute,
157  const std::string & collection,
158  std::list<mongo::BSONObj> (T::*compute_func)(const mongo::BSONObj &,
159  const std::string &),
160  T * obj,
161  double caching_time = 0.0,
162  int priority = 0)
163  {
164  return computables_manager_->register_computable(
165  query_to_compute, collection, compute_func, obj, caching_time, priority);
166  }
167  void remove_computable(Computable *computable);
168 
169 private:
170  fawkes::MongoDBConnCreator *mongo_connection_manager_;
171  mongo::DBClientBase * mongodb_client_local_;
172  mongo::DBClientBase * mongodb_client_distributed_;
173  bool distributed_;
174  fawkes::Configuration * config_;
175  fawkes::Logger * logger_;
176  fawkes::Clock * clock_;
177  fawkes::BlackBoard * blackboard_;
178 
179  const char * name_ = "RobotMemory";
180  std::string database_name_;
181  std::string default_collection_;
182  bool debug_;
183  fawkes::Mutex mutex_;
184  fawkes::RobotMemoryInterface *rm_if_;
185  EventTriggerManager * trigger_manager_;
186  ComputablesManager * computables_manager_;
187  std::vector<std::string> distributed_dbs_;
188 
189  unsigned int cfg_startup_grace_period_;
190  std::string cfg_coord_database_;
191  std::string cfg_coord_mutex_collection_;
192 
193  void init();
194  void loop();
195 
196  void log(const std::string &what, const std::string &level = "info");
197  void log_deb(const std::string &what, const std::string &level = "info");
198  void log(const mongo::Query &query, const std::string &what, const std::string &level = "info");
199  void log(const mongo::BSONObj &obj, const std::string &what, const std::string &level = "info");
200  void
201  log_deb(const mongo::Query &query, const std::string &what, const std::string &level = "info");
202  void
203  log_deb(const mongo::BSONObj &obj, const std::string &what, const std::string &level = "info");
204 
205  void set_fields(mongo::BSONObj &obj, const std::string &what);
206  void set_fields(mongo::Query &q, const std::string &what);
207  void remove_field(mongo::Query &q, const std::string &what);
208 
209  mongo::DBClientBase *get_mongodb_client(const std::string &collection);
210 };
211 
212 #endif /* FAWKES_SRC_PLUGINS_ROBOT_MEMORY_ROBOT_MEMORY_H_ */
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.
Definition: robot_memory.h:156
QResCursor query(mongo::Query query, const std::string &collection="")
Query information from the robot memory.
Manager to realize triggers on events in the robot memory.
This class manages registering computables and can check if any computables are invoced by a query.
bool mutex_unlock(const std::string &name, const std::string &identity)
Release lock on mutex.
int create_index(mongo::BSONObj keys, const std::string &collection="", bool unique=false)
Create an index on a collection.
int remove(mongo::Query query, const std::string &collection="")
Remove documents from the robot memory.
Fawkes library namespace.
int update(mongo::Query query, mongo::BSONObj update, const std::string &collection="", bool upsert=false)
Updates documents in the robot memory.
mongo::BSONObj aggregate(const std::vector< mongo::BSONObj > &pipeline, const std::string &collection="")
Aggregation call on the robot memory.
This is supposed to be the central clock in Fawkes.
Definition: clock.h:34
Class holding information for a single computable this class also enhances computed documents by addi...
Definition: computable.h:29
mongo::BSONObj find_one_and_update(const mongo::BSONObj &filter, const mongo::BSONObj &update, const std::string &collection, bool upsert=false, bool return_new=true)
Atomically update and retrieve document.
void remove_computable(Computable *computable)
Remove previously registered computable.
void remove_trigger(EventTrigger *trigger)
Remove a previously registered trigger.
int restore_collection(const std::string &collection, const std::string &directory="@CONFDIR@/robot-memory")
Restore a previously dumped collection from a directory.
Class holding all information about an EventTrigger.
Definition: event_trigger.h:32
Logger * logger
This is the Logger member used to access the logger.
Definition: logging.h:41
mongo::BSONObj mapreduce(mongo::Query query, const std::string &collection, const std::string &js_map_fun, const std::string &js_reduce_fun)
Performs a MapReduce operation on the robot memory (https://docs.mongodb.com/manual/core/map-reduce/)
Clock * clock
By means of this member access to the clock is given.
Definition: clock.h:42
virtual void init()
Initialize the thread.
int insert(mongo::BSONObj obj, const std::string &collection="")
Inserts a document into the robot memory.
int drop_collection(const std::string &collection)
Drop (= remove) a whole collection and all documents inside it.
bool mutex_try_lock(const std::string &name, bool force=false)
Try to acquire a lock for a mutex.
bool mutex_renew_lock(const std::string &name, const std::string &identity)
Renew a mutex.
Thread that provides a robot memory with MongoDB.
RobotMemory(fawkes::Configuration *config, fawkes::Logger *logger, fawkes::Clock *clock, fawkes::MongoDBConnCreator *mongo_connection_manager, fawkes::BlackBoard *blackboard)
Robot Memory Constructor with objects of the thread.
bool mutex_setup_ttl(float max_age_sec)
Setup time-to-live index for mutexes.
virtual void loop()
Code to execute in the thread.
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.
const char * name() const
Get name of thread.
Definition: thread.h:100
Interface for a MongoDB connection creator.
bool mutex_create(const std::string &name)
Explicitly create a mutex.
EventTrigger * register_trigger(mongo::Query query, const std::string &collection, void(T::*callback)(mongo::BSONObj), T *_obj)
Register a trigger to be notified when the robot memory is updated and the updated document matches t...
Definition: robot_memory.h:117
bool mutex_destroy(const std::string &name)
Destroy a mutex.
EventTrigger * register_trigger(const std::string &query_str, const std::string &collection, void(T::*callback)(mongo::BSONObj), T *_obj)
Register a trigger to be notified when the robot memory is updated and the updated document matches t...
Definition: robot_memory.h:134
int clear_memory()
Remove the whole database of the robot memory and all documents inside.
EventTrigger * register_trigger(mongo::Query query, std::string collection, void(T::*callback)(mongo::BSONObj), T *obj)
Register a trigger to be notified when the robot memory is updated and the updated document matches t...
Access to the robot memory based on mongodb.
Definition: robot_memory.h:48
The BlackBoard abstract class.
Definition: blackboard.h:45
Mutex mutual exclusion lock.
Definition: mutex.h:32
bool mutex_expire_locks(float max_age_sec)
Expire old locks on mutexes.
Configuration * config
This is the Configuration member used to access the configuration.
Definition: configurable.h:41
Interface for configuration handling.
Definition: config.h:64
int dump_collection(const std::string &collection, const std::string &directory="@CONFDIR@/robot-memory")
Dump (= save) a collection to the filesystem to restore it later.
BlackBoard * blackboard
This is the BlackBoard instance you can use to interact with the BlackBoard.
Definition: blackboard.h:44
Interface for logging.
Definition: logger.h:41