Fawkes API Fawkes Development Version
blackboard_computable.cpp
1/***************************************************************************
2 * blackboard_computable.cpp - Computable providing blackboard access
3 *
4 *
5 * Created: 1:22:31 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 "blackboard_computable.h"
23
24#include <bsoncxx/builder/basic/document.hpp>
25
26/** @class BlackboardComputable blackboard_computable.h
27 * Computable providing access to blackboard interfaces.
28 * The Query has to match {interface:{$exists:true}} on the blackboard collection
29 * @author Frederik Zwilling
30 */
31
32using namespace fawkes;
33using namespace bsoncxx;
34using namespace mongocxx;
35
36/**
37 * Constructor with references to objects of the plugin
38 * @param robot_memory Robot Memory
39 * @param blackboard Blackboard
40 * @param logger Logger
41 * @param config Configuration
42 */
44 fawkes::BlackBoard * blackboard,
45 fawkes::Logger * logger,
47{
48 robot_memory_ = robot_memory;
49 blackboard_ = blackboard;
50 logger_ = logger;
51
52 //register computable
53 using namespace bsoncxx::builder;
54 basic::document query;
55 query.append(basic::kvp("interface", [](basic::sub_document subdoc) {
56 subdoc.append(basic::kvp("$exists", true));
57 }));
58 int priority = config->get_int("plugins/robot-memory/computables/blackboard/priority");
59 float caching_time =
60 config->get_float("plugins/robot-memory/computables/blackboard/caching-time");
61 computable = robot_memory_->register_computable(query.extract(),
62 "robmem.blackboard",
63 &BlackboardComputable::compute_interfaces,
64 this,
65 caching_time,
66 priority);
67}
68
69BlackboardComputable::~BlackboardComputable()
70{
71 robot_memory_->remove_computable(computable);
72}
73
74std::list<document::value>
75BlackboardComputable::compute_interfaces(const document::view &query, const std::string &collection)
76{
77 std::list<document::value> res;
78 std::string type = query["interface"].get_utf8().value.to_string();
79 std::string id = "*";
80 auto id_it = query.find("id");
81 if (id_it != query.end()) {
82 id = query["id"].get_utf8().value.to_string();
83 }
84 //get all matching interfaces
85 for (Interface *interface : blackboard_->open_multiple_for_reading(type.c_str(), id.c_str())) {
86 interface->read();
87 //build document
88 using namespace bsoncxx::builder;
89 basic::document doc;
90 doc.append(basic::kvp("interface", interface->type()));
91 doc.append(basic::kvp("id", interface->id()));
92 for (InterfaceFieldIterator it = interface->fields(); it != interface->fields_end(); ++it) {
93 if (it.get_length() > 1 && it.get_type() != IFT_STRING) {
94 doc.append(basic::kvp(std::string(it.get_name()), [it](basic::sub_array array) {
95 for (unsigned int i = 0; i < it.get_length(); i++) {
96 switch (it.get_type()) {
97 case IFT_BOOL: array.append(it.get_bool(i)); break;
98 case IFT_INT8: array.append(it.get_int8(i)); break;
99 case IFT_UINT8: array.append(it.get_uint8(i)); break;
100 case IFT_INT16: array.append(it.get_int16(i)); break;
101 case IFT_UINT16: array.append(it.get_uint16(i)); break;
102 case IFT_INT32: array.append(it.get_int32(i)); break;
103 case IFT_UINT32: array.append(static_cast<int64_t>(it.get_uint32(i))); break;
104 case IFT_INT64: array.append(static_cast<int64_t>(it.get_int64(i))); break;
105 case IFT_UINT64: array.append(static_cast<int64_t>(it.get_uint64(i))); break;
106 case IFT_FLOAT: array.append(it.get_float(i)); break;
107 case IFT_DOUBLE: array.append(it.get_double(i)); break;
108 case IFT_STRING: array.append(it.get_string()); break;
109 case IFT_BYTE: array.append(it.get_byte(i)); break;
110 case IFT_ENUM: array.append(it.get_enum_string(i)); break;
111 }
112 }
113 }));
114 } else {
115 std::string key{it.get_name()};
116 switch (it.get_type()) {
117 case IFT_BOOL: doc.append(basic::kvp(key, it.get_bool())); break;
118 case IFT_INT8: doc.append(basic::kvp(key, it.get_int8())); break;
119 case IFT_UINT8: doc.append(basic::kvp(key, it.get_uint8())); break;
120 case IFT_INT16: doc.append(basic::kvp(key, it.get_int16())); break;
121 case IFT_UINT16: doc.append(basic::kvp(key, it.get_uint16())); break;
122 case IFT_INT32: doc.append(basic::kvp(key, it.get_int32())); break;
123 case IFT_UINT32: doc.append(basic::kvp(key, static_cast<int64_t>(it.get_uint32()))); break;
124 case IFT_INT64: doc.append(basic::kvp(key, static_cast<int64_t>(it.get_int64()))); break;
125 case IFT_UINT64: doc.append(basic::kvp(key, static_cast<int64_t>(it.get_uint64()))); break;
126 case IFT_FLOAT: doc.append(basic::kvp(key, it.get_float())); break;
127 case IFT_DOUBLE: doc.append(basic::kvp(key, it.get_double())); break;
128 case IFT_STRING: doc.append(basic::kvp(key, it.get_string())); break;
129 case IFT_BYTE: doc.append(basic::kvp(key, it.get_byte())); break;
130 case IFT_ENUM: doc.append(basic::kvp(key, it.get_enum_string())); break;
131 }
132 }
133 }
134 res.push_back(doc.extract());
135 blackboard_->close(interface);
136 }
137 return res;
138}
BlackboardComputable(RobotMemory *robot_memory, fawkes::BlackBoard *blackboard, fawkes::Logger *logger, fawkes::Configuration *config)
Constructor with references to objects of the plugin.
Access to the robot memory based on mongodb.
Definition: robot_memory.h:47
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.
Definition: robot_memory.h:158
void remove_computable(Computable *computable)
Remove previously registered computable.
The BlackBoard abstract class.
Definition: blackboard.h:46
virtual std::list< Interface * > open_multiple_for_reading(const char *type_pattern, const char *id_pattern="*", const char *owner=NULL)=0
Open multiple interfaces for reading.
virtual void close(Interface *interface)=0
Close interface.
Interface for configuration handling.
Definition: config.h:68
virtual float get_float(const char *path)=0
Get value from configuration which is of type float.
virtual int get_int(const char *path)=0
Get value from configuration which is of type int.
Interface field iterator.
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:80
Interface for logging.
Definition: logger.h:42
Fawkes library namespace.
@ IFT_INT8
8 bit integer field
Definition: types.h:38
@ IFT_UINT32
32 bit unsigned integer field
Definition: types.h:43
@ IFT_FLOAT
float field
Definition: types.h:46
@ IFT_BYTE
byte field, alias for uint8
Definition: types.h:49
@ IFT_UINT64
64 bit unsigned integer field
Definition: types.h:45
@ IFT_UINT16
16 bit unsigned integer field
Definition: types.h:41
@ IFT_INT32
32 bit integer field
Definition: types.h:42
@ IFT_INT64
64 bit integer field
Definition: types.h:44
@ IFT_DOUBLE
double field
Definition: types.h:47
@ IFT_INT16
16 bit integer field
Definition: types.h:40
@ IFT_STRING
string field
Definition: types.h:48
@ IFT_BOOL
boolean field
Definition: types.h:37
@ IFT_ENUM
field with interface specific enum type
Definition: types.h:50
@ IFT_UINT8
8 bit unsigned integer field
Definition: types.h:39