Fawkes API Fawkes Development Version
computable.cpp
1/***************************************************************************
2 * computable.cpp - Class holding information for a single computable
3 *
4 *
5 * Created: 6:57:45 PM 2016
6 * Copyright 2016 Frederik Zwilling
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
22#include "computable.h"
23
24#include <bsoncxx/builder/basic/document.hpp>
25#include <bsoncxx/document/value.hpp>
26#include <chrono>
27
28using namespace bsoncxx;
29
30/** @class Computable computable.h
31 * Class holding information for a single computable
32 * this class also enhances computed documents by additional information, such as the caching time
33 * @author Frederik Zwilling
34 */
35
36/**
37 * Constructor for object holding information about a computable
38 * @param query_to_compute Computable specification. Queries matching to this spec invoke the computable
39 * @param collection Collection covered
40 * @param compute_function Reference to the function providing the computation
41 * @param caching_time How long should computed results for a query be cached and be used for identical queries in that time?
42 * @param priority Computable priority ordering the evaluation
43 */
45 bsoncxx::document::value query_to_compute,
46 std::string collection,
47 const boost::function<std::list<document::value>(bsoncxx::document::view, std::string)>
48 & compute_function,
49 double caching_time,
50 int priority)
51: compute_function(compute_function), query_to_compute(query_to_compute), collection(collection)
52{
53 //convert caching time to milliseconds
54 this->caching_time = (int)(caching_time * 1000.0);
55 this->priority = priority;
56}
57
58Computable::~Computable()
59{
60}
61
62/**
63 * Compute demanded information and insert it into the robot memory
64 * @param query The query demanding the computable information
65 * @return Documents to insert extended with computable meta information (e.g. caching time)
66 */
67std::list<bsoncxx::document::value>
68Computable::compute(bsoncxx::document::view query)
69{
70 // use provided function to compute demanded documents
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;
75 //add metainformation for each document
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));
81
82 basic::document doc;
83 doc.append(concatenate(obj.view()));
84 doc.append(basic::kvp("_robmem_info", info));
85 //override
86 obj = doc.extract();
87 }
88 return docs;
89}
90
91/**
92 * Gets the query that defines what information is computed by the Computable
93 * @return The query
94 */
95bsoncxx::document::value
97{
98 return query_to_compute;
99}
100
101/**
102 * Gets the collection the computable adds information to
103 * @return The query
104 */
105std::string
107{
108 return collection;
109}
110
111/**
112 * Gets the priority of the computable
113 * @return The query
114 */
115int
117{
118 return priority;
119}
std::string get_collection()
Gets the collection the computable adds information to.
Definition: computable.cpp:106
int get_priority()
Gets the priority of the computable.
Definition: computable.cpp:116
bsoncxx::document::value get_query()
Gets the query that defines what information is computed by the Computable.
Definition: computable.cpp:96
std::list< bsoncxx::document::value > compute(bsoncxx::document::view query)
Compute demanded information and insert it into the robot memory.
Definition: computable.cpp:68
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.
Definition: computable.cpp:44