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 <chrono>
25 
26 using namespace mongo;
27 
28 /** @class Computable computable.h
29  * Class holding information for a single computable
30  * this class also enhances computed documents by additional information, such as the caching time
31  * @author Frederik Zwilling
32  */
33 
34 /**
35  * Constructor for object holding information about a computable
36  * @param query_to_compute Computable specification. Queries matching to this spec invoke the computable
37  * @param collection Collection covered
38  * @param compute_function Reference to the function providing the computation
39  * @param caching_time How long should computed results for a query be cached and be used for identical queries in that time?
40  * @param priority Computable priority ordering the evaluation
41  */
43  Query query_to_compute,
44  std::string collection,
45  const boost::function<std::list<BSONObj>(BSONObj, std::string)> &compute_function,
46  double caching_time,
47  int priority)
48 {
49  this->compute_function = compute_function;
50  this->query_to_compute = query_to_compute;
51  this->collection = collection;
52  //convert caching time to milliseconds
53  this->caching_time = (int)(caching_time * 1000.0);
54  this->priority = priority;
55 }
56 
57 Computable::~Computable()
58 {
59 }
60 
61 /**
62  * Compute demanded information and insert it into the robot memory
63  * @param query The query demanding the computable information
64  * @return Documents to insert extended with computable meta information (e.g. caching time)
65  */
66 std::list<BSONObj>
67 Computable::compute(BSONObj query)
68 {
69  // use provided function to compute demanded documents
70  std::list<BSONObj> docs = compute_function(query, collection);
71  long long milliseconds_since_epoch =
72  std::chrono::system_clock::now().time_since_epoch() / std::chrono::milliseconds(1);
73  long long cached_until = milliseconds_since_epoch + caching_time;
74  //add metainformation for each document
75  for (BSONObj &obj : docs) {
76  BSONObjBuilder info_b;
77  info_b.append("computed", true);
78  info_b.append("cached_until", cached_until);
79  BSONObjBuilder obj_b;
80  obj_b.appendElements(obj);
81  obj_b.append("_robmem_info", info_b.obj());
82  //override
83  obj = obj_b.obj();
84  }
85  return docs;
86 }
87 
88 /**
89  * Gets the query that defines what information is computed by the Computable
90  * @return The query
91  */
92 mongo::Query
94 {
95  return query_to_compute;
96 }
97 
98 /**
99  * Gets the collection the computable adds information to
100  * @return The query
101  */
102 std::string
104 {
105  return collection;
106 }
107 
108 /**
109  * Gets the priority of the computable
110  * @return The query
111  */
112 int
114 {
115  return priority;
116 }
int get_priority()
Gets the priority of the computable.
Definition: computable.cpp:113
std::list< mongo::BSONObj > compute(mongo::BSONObj query)
Compute demanded information and insert it into the robot memory.
Definition: computable.cpp:67
mongo::Query get_query()
Gets the query that defines what information is computed by the Computable.
Definition: computable.cpp:93
Computable(mongo::Query query_to_compute, std::string collection, const boost::function< std::list< mongo::BSONObj >(mongo::BSONObj, std::string)> &compute_function, double caching_time=0.0, int priority=0)
Constructor for object holding information about a computable.
Definition: computable.cpp:42
std::string get_collection()
Gets the collection the computable adds information to.
Definition: computable.cpp:103