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 /** @class BlackboardComputable blackboard_computable.h
25  * Computable providing access to blackboard interfaces.
26  * The Query has to match {interface:{$exists:true}} on the blackboard collection
27  * @author Frederik Zwilling
28  */
29 
30 using namespace fawkes;
31 using namespace mongo;
32 
33 /**
34  * Constructor with references to objects of the plugin
35  * @param robot_memory Robot Memory
36  * @param blackboard Blackboard
37  * @param logger Logger
38  * @param config Configuration
39  */
41  fawkes::BlackBoard * blackboard,
42  fawkes::Logger * logger,
43  fawkes::Configuration *config)
44 {
45  robot_memory_ = robot_memory;
46  blackboard_ = blackboard;
47  logger_ = logger;
48 
49  //register computable
50  Query query = fromjson("{interface:{$exists:true}}");
51  int priority = config->get_int("plugins/robot-memory/computables/blackboard/priority");
52  float caching_time =
53  config->get_float("plugins/robot-memory/computables/blackboard/caching-time");
54  computable = robot_memory_->register_computable(query,
55  "robmem.blackboard",
56  &BlackboardComputable::compute_interfaces,
57  this,
58  caching_time,
59  priority);
60 }
61 
62 BlackboardComputable::~BlackboardComputable()
63 {
64  robot_memory_->remove_computable(computable);
65 }
66 
67 std::list<mongo::BSONObj>
68 BlackboardComputable::compute_interfaces(const mongo::BSONObj &query, const std::string &collection)
69 {
70  std::list<mongo::BSONObj> res;
71  std::string type = query.getField("interface").String();
72  std::string id = "*";
73  if (query.hasField("id"))
74  id = query.getField("id").String();
75  //get all matching interfaces
76  for (Interface *interface : blackboard_->open_multiple_for_reading(type.c_str(), id.c_str())) {
77  interface->read();
78  //build document
79  BSONObjBuilder b;
80  b << "interface" << interface->type() << "id" << interface->id();
81  for (InterfaceFieldIterator it = interface->fields(); it != interface->fields_end(); ++it) {
82  if (it.get_length() > 1 && it.get_type() != IFT_STRING) {
83  b << it.get_name() << get_interface_fields(it);
84  } else {
85  switch (it.get_type()) {
86  case IFT_BOOL: b << it.get_name() << it.get_bool(); break;
87  case IFT_INT8: b << it.get_name() << it.get_int8(); break;
88  case IFT_UINT8: b << it.get_name() << it.get_uint8(); break;
89  case IFT_INT16: b << it.get_name() << it.get_int16(); break;
90  case IFT_UINT16: b << it.get_name() << it.get_uint16(); break;
91  case IFT_INT32: b << it.get_name() << it.get_int32(); break;
92  case IFT_UINT32: b << it.get_name() << it.get_uint32(); break;
93  case IFT_INT64: b << it.get_name() << (long long int)it.get_int64(); break;
94  case IFT_UINT64: b << it.get_name() << (unsigned int)it.get_uint64(); break;
95  case IFT_FLOAT: b << it.get_name() << it.get_float(); break;
96  case IFT_DOUBLE: b << it.get_name() << it.get_double(); break;
97  case IFT_STRING: b << it.get_name() << it.get_string(); break;
98  case IFT_BYTE: b << it.get_name() << it.get_byte(); break;
99  case IFT_ENUM: b << it.get_name() << it.get_enum_string(); break;
100  }
101  }
102  }
103  res.push_back(b.obj());
104  blackboard_->close(interface);
105  }
106  return res;
107 }
108 
109 mongo::BSONArray
110 BlackboardComputable::get_interface_fields(fawkes::InterfaceFieldIterator it)
111 {
112  BSONArrayBuilder arr_b;
113  for (unsigned int i = 0; i < it.get_length(); i++) {
114  switch (it.get_type()) {
115  case IFT_BOOL: arr_b.appendBool(it.get_bool(i)); break;
116  case IFT_INT8: arr_b.append(it.get_int8(i)); break;
117  case IFT_UINT8: arr_b.append(it.get_uint8(i)); break;
118  case IFT_INT16: arr_b.append(it.get_int16(i)); break;
119  case IFT_UINT16: arr_b.append(it.get_uint16(i)); break;
120  case IFT_INT32: arr_b.append(it.get_int32(i)); break;
121  case IFT_UINT32: arr_b.append(it.get_uint32(i)); break;
122  case IFT_INT64: arr_b.append((long long int)it.get_int64(i)); break;
123  case IFT_UINT64: arr_b.append((unsigned int)it.get_uint64(i)); break;
124  case IFT_FLOAT: arr_b.append(it.get_float(i)); break;
125  case IFT_DOUBLE: arr_b.append(it.get_double(i)); break;
126  case IFT_STRING: arr_b.append(it.get_string()); return arr_b.arr();
127  case IFT_BYTE: arr_b.append(it.get_byte(i)); break;
128  case IFT_ENUM: arr_b.append(it.get_enum_string(i)); break;
129  }
130  }
131  return arr_b.arr();
132 }
64 bit integer field
Definition: types.h:44
Interface field iterator.
uint16_t get_uint16(unsigned int index=0) const
Get value of current field as unsigned integer.
double get_double(unsigned int index=0) const
Get value of current field as double.
BlackboardComputable(RobotMemory *robot_memory, fawkes::BlackBoard *blackboard, fawkes::Logger *logger, fawkes::Configuration *config)
Constructor with references to objects of the plugin.
Fawkes library namespace.
bool get_bool(unsigned int index=0) const
Get value of current field as bool.
8 bit unsigned integer field
Definition: types.h:39
16 bit unsigned integer field
Definition: types.h:41
interface_fieldtype_t get_type() const
Get type of current field.
string field
Definition: types.h:48
byte field, alias for uint8
Definition: types.h:49
float get_float(unsigned int index=0) const
Get value of current field as float.
virtual int get_int(const char *path)=0
Get value from configuration which is of type int.
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:78
16 bit integer field
Definition: types.h:40
uint8_t get_byte(unsigned int index=0) const
Get value of current field as byte.
int16_t get_int16(unsigned int index=0) const
Get value of current field as integer.
int8_t get_int8(unsigned int index=0) const
Get value of current field as integer.
uint8_t get_uint8(unsigned int index=0) const
Get value of current field as unsigned integer.
uint64_t get_uint64(unsigned int index=0) const
Get value of current field as unsigned integer.
64 bit unsigned integer field
Definition: types.h:45
const char * get_enum_string(unsigned int index=0) const
Get value of current enum field as string.
const char * get_string() const
Get value of current field as string.
uint32_t get_uint32(unsigned int index=0) const
Get value of current field as unsigned integer.
float field
Definition: types.h:46
size_t get_length() const
Get length of current field.
32 bit integer field
Definition: types.h:42
Access to the robot memory based on mongodb.
Definition: robot_memory.h:48
The BlackBoard abstract class.
Definition: blackboard.h:45
boolean field
Definition: types.h:37
int32_t get_int32(unsigned int index=0) const
Get value of current field as integer.
Interface for configuration handling.
Definition: config.h:64
virtual float get_float(const char *path)=0
Get value from configuration which is of type float.
32 bit unsigned integer field
Definition: types.h:43
field with interface specific enum type
Definition: types.h:50
8 bit integer field
Definition: types.h:38
double field
Definition: types.h:47
Interface for logging.
Definition: logger.h:41
int64_t get_int64(unsigned int index=0) const
Get value of current field as integer.