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 <bsoncxx/json.hpp>
35 #include <memory>
36 #include <utility>
37 #include <vector>
38 
39 namespace fawkes {
40 class RobotMemoryInterface;
41 #ifdef USE_TIMETRACKER
42 class TimeTracker;
43 #endif
44 } // namespace fawkes
45 
47 {
48  /// Friend the RobotMemoryThread so that only it can access the loop and init functions
49  friend class RobotMemoryThread;
50 
51 public:
55  fawkes::MongoDBConnCreator *mongo_connection_manager,
57  virtual ~RobotMemory();
58 
59  //robot memory functions
60  mongocxx::cursor query(bsoncxx::document::view query,
61  const std::string & collection_name = "",
62  mongocxx::options::find query_options = mongocxx::options::find());
63  bsoncxx::document::value aggregate(const std::vector<bsoncxx::document::view> &pipeline,
64  const std::string & collection = "");
65  // TODO fix int return codes, should be booleans
66  int insert(bsoncxx::document::view, const std::string &collection = "");
67  int insert(std::vector<bsoncxx::document::view> v_obj, const std::string &collection = "");
68  int insert(const std::string &obj_str, const std::string &collection = "");
69  int update(const bsoncxx::document::view &query,
70  const bsoncxx::document::view &update,
71  const std::string & collection = "",
72  bool upsert = false);
73  int update(const bsoncxx::document::view &query,
74  const std::string & update_str,
75  const std::string & collection = "",
76  bool upsert = false);
77  bsoncxx::document::value find_one_and_update(const bsoncxx::document::view &filter,
78  const bsoncxx::document::view &update,
79  const std::string & collection,
80  bool upsert = false,
81  bool return_new = true);
82  int remove(const bsoncxx::document::view &query, const std::string &collection = "");
83  bsoncxx::document::value mapreduce(const bsoncxx::document::view &query,
84  const std::string & collection,
85  const std::string & js_map_fun,
86  const std::string & js_reduce_fun);
87  mongocxx::cursor aggregate(bsoncxx::document::view pipeline, const std::string &collection = "");
88  int drop_collection(const std::string &collection);
89  int clear_memory();
90  int restore_collection(const std::string &dbcollection,
91  const std::string &directory = "@CONFDIR@/robot-memory",
92  std::string target_dbcollection = "");
93  int dump_collection(const std::string &dbcollection,
94  const std::string &directory = "@CONFDIR@/robot-memory");
95  int create_index(bsoncxx::document::view keys,
96  const std::string & collection = "",
97  bool unique = false);
98 
99  //bool semaphore_create(const std::string& name, unsigned int value);
100  //bool semaphore_acquire(const std::string& name, unsigned int v = 1);
101  //bool semaphore_release(const std::string& name, unsigned int v = 1);
102  bool mutex_setup_ttl(float max_age_sec);
103  bool mutex_create(const std::string &name);
104  bool mutex_destroy(const std::string &name);
105  bool mutex_try_lock(const std::string &name, bool force = false);
106  bool mutex_try_lock(const std::string &name, const std::string &identity, bool force = false);
107  bool mutex_unlock(const std::string &name, const std::string &identity);
108  bool mutex_renew_lock(const std::string &name, const std::string &identity);
109  bool mutex_expire_locks(float max_age_sec);
110 
111  /**
112  * Register a trigger to be notified when the robot memory is updated and the updated document matches the query
113  * @param query Query the updated document has to match
114  * @param collection db.collection to use
115  * @param callback Callback function (e.g. &Class::callback)
116  * @param _obj Pointer to class the callback is a function of (usaually this)
117  * @return Trigger object pointer, save it to remove the trigger later
118  */
119  template <typename T>
120  EventTrigger *
121  register_trigger(const bsoncxx::document::view &query,
122  const std::string & collection,
123  void (T::*callback)(const bsoncxx::document::view &),
124  T *_obj)
125  {
126  return trigger_manager_->register_trigger(query, collection, callback, _obj);
127  }
128  /**
129  * Register a trigger to be notified when the robot memory is updated and the updated document matches the query
130  * @param query_str Query as JSON string
131  * @param collection db.collection to use
132  * @param callback Callback function (e.g. &Class::callback)
133  * @param _obj Pointer to class the callback is a function of (usaually this)
134  * @return Trigger object pointer, save it to remove the trigger later
135  */
136  template <typename T>
137  EventTrigger *
138  register_trigger(const std::string &query_str,
139  const std::string &collection,
140  void (T::*callback)(bsoncxx::document::value),
141  T *_obj)
142  {
143  return register_trigger(bsoncxx::from_json(query_str), collection, callback, _obj);
144  }
145  void remove_trigger(EventTrigger *trigger);
146 
147  /**
148  * Registers a Computable which provides information in the robot memory that is computed on demand.
149  *
150  * @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.
151  * @param collection db.collection to fill with computed information
152  * @param compute_func Callback function that computes the information and retruns a list of computed documents
153  * @param obj Pointer to class the callback is a function of (usaually this)
154  * @param caching_time How long should computed results for a query be cached and be used for identical queries in that time?
155  * @param priority Computable priority ordering the evaluation
156  * @return Computable Object pointer used for removing it
157  */
158  template <typename T>
159  Computable *
160  register_computable(bsoncxx::document::value &&query_to_compute,
161  const std::string & collection,
162  std::list<bsoncxx::document::value> (
163  T::*compute_func)(const bsoncxx::document::view &, const std::string &),
164  T * obj,
165  double caching_time = 0.0,
166  int priority = 0)
167  {
168  return computables_manager_->register_computable(
169  std::move(query_to_compute), collection, compute_func, obj, caching_time, priority);
170  }
171  void remove_computable(Computable *computable);
172 
173 private:
174  fawkes::MongoDBConnCreator *mongo_connection_manager_;
175  mongocxx::client * mongodb_client_local_;
176  mongocxx::client * mongodb_client_distributed_;
177  bool distributed_;
178  fawkes::Configuration * config_;
179  fawkes::Logger * logger_;
180  fawkes::Clock * clock_;
181  fawkes::BlackBoard * blackboard_;
182 
183  const char * name_ = "RobotMemory";
184  std::string database_name_;
185  std::string default_collection_;
186  bool debug_;
187  fawkes::Mutex mutex_;
188  fawkes::RobotMemoryInterface *rm_if_;
189  EventTriggerManager * trigger_manager_;
190  ComputablesManager * computables_manager_;
191  std::vector<std::string> distributed_dbs_;
192 
193  std::string cfg_coord_database_;
194  std::string cfg_coord_mutex_collection_;
195 
196  void init();
197  void loop();
198 
199  // TODO make log level an enum (if we need it at all)
200  void log(const std::string &what, const std::string &level = "info");
201  void log_deb(const std::string &what, const std::string &level = "info");
202  void log(const bsoncxx::document::view &query,
203  const std::string & what,
204  const std::string & level = "info");
205  void log_deb(const bsoncxx::document::view &query,
206  const std::string & what,
207  const std::string & level = "info");
208 
209  bool is_distributed_database(const std::string &dbcollection);
210  std::string get_hostport(const std::string &dbcollection);
211  mongocxx::client * get_mongodb_client(const std::string &collection);
212  mongocxx::collection get_collection(const std::string &dbcollection);
213 
214 #ifdef USE_TIMETRACKER
215  fawkes::TimeTracker *tt_;
216  unsigned int tt_loopcount_;
217  unsigned int ttc_events_;
218  unsigned int ttc_cleanup_;
219 #endif
220 };
221 
222 #endif /* FAWKES_SRC_PLUGINS_ROBOT_MEMORY_ROBOT_MEMORY_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.
Manager to realize triggers on events in the robot memory.
EventTrigger * register_trigger(const bsoncxx::document::view &query, std::string dbcollection, void(T::*callback)(const bsoncxx::document::view &), T *obj)
Register a trigger to be notified when the robot memory is updated and the updated document matches t...
Class holding all information about an EventTrigger.
Definition: event_trigger.h:32
Thread that provides a robot memory with MongoDB.
virtual void loop()
Code to execute in the thread.
virtual void init()
Initialize the thread.
Access to the robot memory based on mongodb.
Definition: robot_memory.h:47
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: robot_memory.h:160
bool mutex_create(const std::string &name)
Explicitly create a mutex.
int dump_collection(const std::string &dbcollection, const std::string &directory="@CONFDIR@/robot-memory")
Dump (= save) a collection to the filesystem to restore it later.
bool mutex_destroy(const std::string &name)
Destroy a mutex.
bool mutex_renew_lock(const std::string &name, const std::string &identity)
Renew a mutex.
bool mutex_unlock(const std::string &name, const std::string &identity)
Release lock on mutex.
void remove_trigger(EventTrigger *trigger)
Remove a previously registered trigger.
mongocxx::cursor query(bsoncxx::document::view query, const std::string &collection_name="", mongocxx::options::find query_options=mongocxx::options::find())
Query information from the robot memory.
bsoncxx::document::value find_one_and_update(const bsoncxx::document::view &filter, const bsoncxx::document::view &update, const std::string &collection, bool upsert=false, bool return_new=true)
Atomically update and retrieve document.
bool mutex_setup_ttl(float max_age_sec)
Setup time-to-live index for mutexes.
bsoncxx::document::value mapreduce(const bsoncxx::document::view &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/)
int remove(const bsoncxx::document::view &query, const std::string &collection="")
Remove documents from the robot memory.
int insert(bsoncxx::document::view, const std::string &collection="")
Inserts a document into the robot memory.
EventTrigger * register_trigger(const bsoncxx::document::view &query, const std::string &collection, void(T::*callback)(const bsoncxx::document::view &), T *_obj)
Register a trigger to be notified when the robot memory is updated and the updated document matches t...
Definition: robot_memory.h:121
void remove_computable(Computable *computable)
Remove previously registered computable.
int drop_collection(const std::string &collection)
Drop (= remove) a whole collection and all documents inside it.
int update(const bsoncxx::document::view &query, const bsoncxx::document::view &update, const std::string &collection="", bool upsert=false)
Updates documents in the robot memory.
bsoncxx::document::value aggregate(const std::vector< bsoncxx::document::view > &pipeline, const std::string &collection="")
Aggregation call on the robot memory.
bool mutex_try_lock(const std::string &name, bool force=false)
Try to acquire a lock for a mutex.
int create_index(bsoncxx::document::view keys, const std::string &collection="", bool unique=false)
Create an index on a collection.
EventTrigger * register_trigger(const std::string &query_str, const std::string &collection, void(T::*callback)(bsoncxx::document::value), T *_obj)
Register a trigger to be notified when the robot memory is updated and the updated document matches t...
Definition: robot_memory.h:138
int clear_memory()
Remove the whole database of the robot memory and all documents inside.
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.
int restore_collection(const std::string &dbcollection, const std::string &directory="@CONFDIR@/robot-memory", std::string target_dbcollection="")
Restore a previously dumped collection from a directory.
bool mutex_expire_locks(float max_age_sec)
Expire old locks on mutexes.
BlackBoard * blackboard
This is the BlackBoard instance you can use to interact with the BlackBoard.
Definition: blackboard.h:44
The BlackBoard abstract class.
Definition: blackboard.h:46
Clock * clock
By means of this member access to the clock is given.
Definition: clock.h:42
This is supposed to be the central clock in Fawkes.
Definition: clock.h:35
Configuration * config
This is the Configuration member used to access the configuration.
Definition: configurable.h:41
Interface for configuration handling.
Definition: config.h:65
Interface for logging.
Definition: logger.h:42
Logger * logger
This is the Logger member used to access the logger.
Definition: logging.h:41
Interface for a MongoDB connection creator.
Mutex mutual exclusion lock.
Definition: mutex.h:33
const char * name() const
Get name of thread.
Definition: thread.h:100
Time tracking utility.
Definition: tracker.h:37
Fawkes library namespace.