Fawkes API
Fawkes Development Version
|
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());
63 bsoncxx::document::value
aggregate(
const std::vector<bsoncxx::document::view> &pipeline,
64 const std::string & collection =
"");
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 =
"");
70 const bsoncxx::document::view &
update,
71 const std::string & collection =
"",
74 const std::string & update_str,
75 const std::string & collection =
"",
78 const bsoncxx::document::view &
update,
79 const std::string & collection,
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 =
"");
91 const std::string &directory =
"@CONFDIR@/robot-memory",
92 std::string target_dbcollection =
"");
94 const std::string &directory =
"@CONFDIR@/robot-memory");
96 const std::string & collection =
"",
106 bool mutex_try_lock(
const std::string &
name,
const std::string &identity,
bool force =
false);
119 template <
typename T>
122 const std::string & collection,
123 void (T::*callback)(
const bsoncxx::document::view &),
136 template <
typename T>
139 const std::string &collection,
140 void (T::*callback)(bsoncxx::document::value),
143 return register_trigger(bsoncxx::from_json(query_str), collection, callback, _obj);
158 template <
typename T>
161 const std::string & collection,
162 std::list<bsoncxx::document::value> (
163 T::*compute_func)(
const bsoncxx::document::view &,
const std::string &),
165 double caching_time = 0.0,
169 std::move(query_to_compute), collection, compute_func, obj, caching_time, priority);
175 mongocxx::client * mongodb_client_local_;
176 mongocxx::client * mongodb_client_distributed_;
183 const char * name_ =
"RobotMemory";
184 std::string database_name_;
185 std::string default_collection_;
188 fawkes::RobotMemoryInterface *rm_if_;
191 std::vector<std::string> distributed_dbs_;
193 std::string cfg_coord_database_;
194 std::string cfg_coord_mutex_collection_;
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");
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);
214 #ifdef USE_TIMETRACKER
216 unsigned int tt_loopcount_;
217 unsigned int ttc_events_;
218 unsigned int ttc_cleanup_;
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...
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.
int create_index(bsoncxx::document::view keys, const std::string &collection="", bool unique=false)
Create an index on a collection.
bool mutex_expire_locks(float max_age_sec)
Expire old locks on mutexes.
void remove_trigger(EventTrigger *trigger)
Remove a previously registered trigger.
int insert(bsoncxx::document::view, const std::string &collection="")
Inserts a document into the robot memory.
Mutex mutual exclusion lock.
This class manages registering computables and can check if any computables are invoced by a query.
Interface for a MongoDB connection creator.
Access to the robot memory based on mongodb.
The BlackBoard abstract class.
Class holding all information about an EventTrigger.
const char * name() const
Get name of thread.
Clock * clock
By means of this member access to the clock is given.
bool mutex_renew_lock(const std::string &name, const std::string &identity)
Renew a mutex.
bsoncxx::document::value aggregate(const std::vector< bsoncxx::document::view > &pipeline, const std::string &collection="")
Aggregation call on the robot memory.
Interface for configuration handling.
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.
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.
virtual void init()
Initialize the thread.
void remove_computable(Computable *computable)
Remove previously registered computable.
virtual void loop()
Code to execute in the thread.
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/)
bool mutex_create(const std::string &name)
Explicitly create a mutex.
Logger * logger
This is the Logger member used to access the logger.
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.
Fawkes library namespace.
bool mutex_try_lock(const std::string &name, bool force=false)
Try to acquire a lock for a mutex.
Manager to realize triggers on events in the robot memory.
int remove(const bsoncxx::document::view &query, const std::string &collection="")
Remove documents from the robot memory.
int drop_collection(const std::string &collection)
Drop (= remove) a whole collection and all documents inside it.
int clear_memory()
Remove the whole database of the robot memory and all documents inside.
Configuration * config
This is the Configuration member used to access the configuration.
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...
bool mutex_setup_ttl(float max_age_sec)
Setup time-to-live index for mutexes.
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...
bool mutex_destroy(const std::string &name)
Destroy a mutex.
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.
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.
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.
BlackBoard * blackboard
This is the BlackBoard instance you can use to interact with the BlackBoard.
Class holding information for a single computable this class also enhances computed documents by addi...
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.
Thread that provides a robot memory with MongoDB.
bool mutex_unlock(const std::string &name, const std::string &identity)
Release lock on mutex.
This is supposed to be the central clock in Fawkes.