21 #ifndef _PLUGINS_ROBOT_MEMORY_ROBOT_MEMORY_H_
22 #define _PLUGINS_ROBOT_MEMORY_ROBOT_MEMORY_H_
24 #include "computables/computables_manager.h"
25 #include "event_trigger_manager.h"
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>
34 #include <bsoncxx/json.hpp>
40 class RobotMemoryInterface;
41 #ifdef USE_TIMETRACKER
60 mongocxx::cursor
query(bsoncxx::document::view
query,
61 const std::string & collection_name =
"",
62 mongocxx::options::find query_options = mongocxx::options::find());
64 int insert(bsoncxx::document::view,
const std::string &collection =
"");
65 int insert(std::vector<bsoncxx::document::view> v_obj,
const std::string &collection =
"");
66 int insert(
const std::string &obj_str,
const std::string &collection =
"");
68 const bsoncxx::document::view &
update,
69 const std::string & collection =
"",
72 const std::string & update_str,
73 const std::string & collection =
"",
76 const bsoncxx::document::view &
update,
77 const std::string & collection,
79 bool return_new =
true);
80 int remove(
const bsoncxx::document::view &
query,
const std::string &collection =
"");
81 bsoncxx::document::value
mapreduce(
const bsoncxx::document::view &
query,
82 const std::string & collection,
83 const std::string & js_map_fun,
84 const std::string & js_reduce_fun);
85 mongocxx::cursor
aggregate(mongocxx::pipeline &pipeline,
const std::string &collection =
"");
89 const std::string &directory =
"@CONFDIR@/robot-memory",
90 std::string target_dbcollection =
"");
92 const std::string &directory =
"@CONFDIR@/robot-memory");
94 const std::string & collection =
"",
104 bool mutex_try_lock(
const std::string &
name,
const std::string &identity,
bool force =
false);
117 template <
typename T>
120 const std::string & collection,
121 void (T::*callback)(
const bsoncxx::document::view &),
134 template <
typename T>
137 const std::string &collection,
138 void (T::*callback)(bsoncxx::document::value),
141 return register_trigger(bsoncxx::from_json(query_str), collection, callback, _obj);
156 template <
typename T>
159 const std::string & collection,
160 std::list<bsoncxx::document::value> (
161 T::*compute_func)(
const bsoncxx::document::view &,
const std::string &),
163 double caching_time = 0.0,
167 std::move(query_to_compute), collection, compute_func, obj, caching_time, priority);
173 mongocxx::client * mongodb_client_local_;
174 mongocxx::client * mongodb_client_distributed_;
181 const char * name_ =
"RobotMemory";
182 std::string database_name_;
183 std::string default_collection_;
186 fawkes::RobotMemoryInterface *rm_if_;
189 std::vector<std::string> distributed_dbs_;
191 std::string cfg_coord_database_;
192 std::string cfg_coord_mutex_collection_;
198 void log(
const std::string &what,
const std::string &level =
"info");
199 void log_deb(
const std::string &what,
const std::string &level =
"info");
200 void log(
const bsoncxx::document::view &
query,
201 const std::string & what,
202 const std::string & level =
"info");
203 void log_deb(
const bsoncxx::document::view &
query,
204 const std::string & what,
205 const std::string & level =
"info");
207 bool is_distributed_database(
const std::string &dbcollection);
208 std::string get_hostport(
const std::string &dbcollection);
209 mongocxx::client * get_mongodb_client(
const std::string &collection);
210 mongocxx::collection get_collection(
const std::string &dbcollection);
212 #ifdef USE_TIMETRACKER
214 unsigned int tt_loopcount_;
215 unsigned int ttc_events_;
216 unsigned int ttc_cleanup_;
Class holding information for a single computable this class also enhances computed documents by addi...
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.
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.
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.
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.
mongocxx::cursor aggregate(mongocxx::pipeline &pipeline, const std::string &collection="")
Performs an aggregation operation on the robot memory (https://docs.mongodb.com/v3....
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...
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.
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...
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.
The BlackBoard abstract class.
Clock * clock
By means of this member access to the clock is given.
This is supposed to be the central clock in Fawkes.
Configuration * config
This is the Configuration member used to access the configuration.
Interface for configuration handling.
Logger * logger
This is the Logger member used to access the logger.
Interface for a MongoDB connection creator.
Mutex mutual exclusion lock.
const char * name() const
Get name of thread.
Fawkes library namespace.