22#include "computable.h"
24#include <bsoncxx/builder/basic/document.hpp>
25#include <bsoncxx/document/value.hpp>
28using namespace bsoncxx;
45 bsoncxx::document::value query_to_compute,
46 std::string collection,
47 const boost::function<std::list<document::value>(bsoncxx::document::view, std::string)>
51: compute_function(compute_function), query_to_compute(query_to_compute), collection(collection)
54 this->caching_time = (int)(caching_time * 1000.0);
55 this->priority = priority;
58Computable::~Computable()
67std::list<bsoncxx::document::value>
71 std::list<bsoncxx::document::value> docs = compute_function(query, collection);
72 int64_t milliseconds_since_epoch =
73 std::chrono::system_clock::now().time_since_epoch() / std::chrono::milliseconds(1);
74 int64_t cached_until = milliseconds_since_epoch + caching_time;
76 for (
auto obj : docs) {
77 using namespace bsoncxx::builder;
78 auto info = basic::document{};
79 info.append(basic::kvp(
"computed",
true));
80 info.append(basic::kvp(
"cached_until", cached_until));
83 doc.append(concatenate(obj.view()));
84 doc.append(basic::kvp(
"_robmem_info", info));
95bsoncxx::document::value
98 return query_to_compute;
std::string get_collection()
Gets the collection the computable adds information to.
int get_priority()
Gets the priority of the computable.
bsoncxx::document::value get_query()
Gets the query that defines what information is computed by the Computable.
std::list< bsoncxx::document::value > compute(bsoncxx::document::view query)
Compute demanded information and insert it into the robot memory.
Computable(bsoncxx::document::value query_to_compute, std::string collection, const boost::function< std::list< bsoncxx::document::value >(bsoncxx::document::view, std::string)> &compute_function, double caching_time=0.0, int priority=0)
Constructor for object holding information about a computable.