[Fawkes Git] branch/fzwilling/robot-memory: created (0.5.0-3086-gd7432e5)

Frederik Zwilling zwilling at kbsg.rwth-aachen.de
Wed May 11 14:51:35 CEST 2016


Changes have been pushed for the project "Fawkes Robotics Software Framework".

Gitweb: http://git.fawkesrobotics.org/fawkes.git
Trac:   http://trac.fawkesrobotics.org

The branch, fzwilling/robot-memory has been created
        at  d7432e57f12e81bca648e61ecdcf080d2c25d907 (commit)

http://git.fawkesrobotics.org/fawkes.git/fzwilling/robot-memory

- *Log* ---------------------------------------------------------------
commit bdafaece38bb98bd359cf392b42dc02c02fae4da
Author:     Frederik Zwilling <zwilling at kbsg.rwth-aachen.de>
AuthorDate: Sun May 1 20:14:11 2016 +0200
Commit:     Frederik Zwilling <zwilling at kbsg.rwth-aachen.de>
CommitDate: Wed May 11 14:50:54 2016 +0200

    robot-memory: skeleton for experimental plugin

http://git.fawkesrobotics.org/fawkes.git/commit/bdafaec
http://trac.fawkesrobotics.org/changeset/bdafaec

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
commit 2f7c32a12fec511562ab416ad08f1447e3303708
Author:     Frederik Zwilling <zwilling at kbsg.rwth-aachen.de>
AuthorDate: Sun May 1 22:33:08 2016 +0200
Commit:     Frederik Zwilling <zwilling at kbsg.rwth-aachen.de>
CommitDate: Wed May 11 14:51:01 2016 +0200

    robot-memory: execute queries from interface msg

http://git.fawkesrobotics.org/fawkes.git/commit/2f7c32a
http://trac.fawkesrobotics.org/changeset/2f7c32a

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
commit 08f87a1aef07d3ca8c20b2f46b5c795896e24733
Author:     Frederik Zwilling <zwilling at kbsg.rwth-aachen.de>
AuthorDate: Sun May 1 23:38:09 2016 +0200
Commit:     Frederik Zwilling <zwilling at kbsg.rwth-aachen.de>
CommitDate: Wed May 11 14:51:07 2016 +0200

    robot-memory: get info about parsed query

http://git.fawkesrobotics.org/fawkes.git/commit/08f87a1
http://trac.fawkesrobotics.org/changeset/08f87a1

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
commit 9ef52de1feebc7ed4657da10774c1d579fef2275
Author:     Frederik Zwilling <zwilling at kbsg.rwth-aachen.de>
AuthorDate: Mon May 9 17:50:45 2016 +0200
Commit:     Frederik Zwilling <zwilling at kbsg.rwth-aachen.de>
CommitDate: Wed May 11 14:51:13 2016 +0200

    robot-memory: added insert, update, remove messages

http://git.fawkesrobotics.org/fawkes.git/commit/9ef52de
http://trac.fawkesrobotics.org/changeset/9ef52de

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
commit d7432e57f12e81bca648e61ecdcf080d2c25d907
Author:     Frederik Zwilling <zwilling at kbsg.rwth-aachen.de>
AuthorDate: Mon May 9 18:59:23 2016 +0200
Commit:     Frederik Zwilling <zwilling at kbsg.rwth-aachen.de>
CommitDate: Wed May 11 14:51:19 2016 +0200

    robot-memory: virtual knowledge base experiments

http://git.fawkesrobotics.org/fawkes.git/commit/d7432e5
http://trac.fawkesrobotics.org/changeset/d7432e5

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


- *Summary* -----------------------------------------------------------
 src/plugins/robot-memory/Makefile                  |    4 +-
 .../{pantilt => robot-memory}/interfaces/Makefile  |    6 +-
 .../interfaces/RobotMemoryInterface.cpp            |  596 ++++++++++++++++++++
 .../interfaces/RobotMemoryInterface.h_ext          |  190 +++++++
 .../interfaces/RobotMemoryInterface.tolua          |  182 ++++++
 .../interfaces/RobotMemoryInterface.xml            |   39 ++
 src/plugins/robot-memory/robot_memory_thread.cpp   |  369 +++++++++++--
 src/plugins/robot-memory/robot_memory_thread.h     |   25 +
 8 files changed, 1370 insertions(+), 41 deletions(-)
 copy src/plugins/{pantilt => robot-memory}/interfaces/Makefile (81%)
 create mode 100644 src/plugins/robot-memory/interfaces/RobotMemoryInterface.cpp
 create mode 100644 src/plugins/robot-memory/interfaces/RobotMemoryInterface.h_ext
 create mode 100644 src/plugins/robot-memory/interfaces/RobotMemoryInterface.tolua
 create mode 100644 src/plugins/robot-memory/interfaces/RobotMemoryInterface.xml


- *Diffs* -------------------------------------------------------------

- *commit* bdafaece38bb98bd359cf392b42dc02c02fae4da - - - - - - - - - -
Author:  Frederik Zwilling <zwilling at kbsg.rwth-aachen.de>
Date:    Sun May 1 20:14:11 2016 +0200
Subject: robot-memory: skeleton for experimental plugin

 src/plugins/Makefile                               |    2 +-
 src/plugins/{mongodb_log => robot-memory}/Makefile |   32 +++----
 .../robot_memory_plugin.cpp}                       |   33 ++++---
 src/plugins/robot-memory/robot_memory_thread.cpp   |  105 ++++++++++++++++++++
 .../robot_memory_thread.h}                         |   32 +++---
 5 files changed, 152 insertions(+), 52 deletions(-)

_Diff for modified files_:
diff --git a/src/plugins/Makefile b/src/plugins/Makefile
index caee20b..bfad9d7 100644
--- a/src/plugins/Makefile
+++ b/src/plugins/Makefile
@@ -26,7 +26,7 @@ SUBDIRS	= bbsync bblogger webview ttmainloop rrd rrdweb \
 	  static_transforms navgraph navgraph-clusters navgraph-generator colli \
 	  clips clips-agent clips-protobuf clips-webview clips-navgraph clips-ros \
 	  clips-tf openprs openprs-agent eclipse-clp \
-	  mongodb mongodb_log \
+	  mongodb mongodb_log robot-memory\
 	  openni refboxcomm ros player xmlrpc gossip \
 	  robot_state_publisher gazebo dynamixel navgraph-interactive
 

- *commit* 2f7c32a12fec511562ab416ad08f1447e3303708 - - - - - - - - - -
Author:  Frederik Zwilling <zwilling at kbsg.rwth-aachen.de>
Date:    Sun May 1 22:33:08 2016 +0200
Subject: robot-memory: execute queries from interface msg

 src/plugins/robot-memory/Makefile                  |    4 +-
 .../{pantilt => robot-memory}/interfaces/Makefile  |    6 +-
 .../interfaces/RobotMemoryInterface.cpp            |  271 ++++++++++++++++++++
 .../interfaces/RobotMemoryInterface.h_ext}         |   53 ++--
 .../interfaces/RobotMemoryInterface.tolua}         |   75 ++++---
 .../interfaces/RobotMemoryInterface.xml            |   20 ++
 src/plugins/robot-memory/robot_memory_thread.cpp   |   60 +++++-
 src/plugins/robot-memory/robot_memory_thread.h     |    7 +
 8 files changed, 437 insertions(+), 59 deletions(-)

_Diff for modified files_:
diff --git a/src/plugins/robot-memory/Makefile b/src/plugins/robot-memory/Makefile
index d24702f..78fe64a 100644
--- a/src/plugins/robot-memory/Makefile
+++ b/src/plugins/robot-memory/Makefile
@@ -19,9 +19,11 @@ include $(BASEDIR)/etc/buildsys/config.mk
 include $(BUILDCONFDIR)/tf/tf.mk
 include $(BASEDIR)/src/plugins/mongodb/mongodb.mk
 
+PRESUBDIRS = interfaces
+
 LIBS_robot_memory = fawkescore fawkesaspects fawkesblackboard fawkesinterface \
 		fawkesutils fawkeslogging fawkesmongodbaspect fvutils \
-		fawkestf
+		fawkestf RobotMemoryInterface
 OBJS_robot_memory = $(patsubst %.cpp,%.o,$(patsubst qa/%,,$(subst $(SRCDIR)/,,$(realpath $(wildcard $(SRCDIR)/*.cpp)))))
 
 CFLAGS  += $(CFLAGS_MONGODB)
diff --git a/src/plugins/robot-memory/robot_memory_thread.cpp b/src/plugins/robot-memory/robot_memory_thread.cpp
index be08e53..2c35769 100644
--- a/src/plugins/robot-memory/robot_memory_thread.cpp
+++ b/src/plugins/robot-memory/robot_memory_thread.cpp
@@ -21,6 +21,10 @@
  */
 
 #include "robot_memory_thread.h"
+#include "interfaces/RobotMemoryInterface.h"
+#include <core/threading/mutex.h>
+#include <core/threading/mutex_locker.h>
+#include <memory>
 
 // from MongoDB
 #include <mongo/client/dbclient.h>
@@ -35,24 +39,34 @@ using namespace fawkes;
 
 /** Constructor. */
 RobotMemoryThread::RobotMemoryThread()
-  : Thread("RobotMemoryThread", Thread::OPMODE_WAITFORWAKEUP)
+	: Thread("RobotMemoryThread", Thread::OPMODE_WAITFORWAKEUP),
+	  BlockedTimingAspect(BlockedTimingAspect::WAKEUP_HOOK_WORLDSTATE)
 {
+  __mutex = new Mutex();
 }
 
 
 /** Destructor. */
 RobotMemoryThread::~RobotMemoryThread()
 {
+  delete __mutex;
 }
 
 
 void
 RobotMemoryThread::init()
 {
+	logger->log_info(name(), "Started RobotMemory");
 	__collection = "fawkes.msglog";
   try {
     __collection = config->get_string("/plugins/mongodb/logger_collection");
   } catch (Exception &e) {}
+
+  __rm_if = blackboard->open_for_writing<RobotMemoryInterface>(config->get_string("/plugins/robot-memory/interface-name").c_str());
+  __rm_if->set_error("");
+  __rm_if->set_result("");
+  
+  __rm_if->write();
 }
 
 
@@ -65,8 +79,52 @@ RobotMemoryThread::finalize()
 void
 RobotMemoryThread::loop()
 {
+	// process interface messages
+  while (! __rm_if->msgq_empty() ) {
+    if (__rm_if->msgq_first_is<RobotMemoryInterface::QueryMessage>()) {
+	    RobotMemoryInterface::QueryMessage* query = (RobotMemoryInterface::QueryMessage*) __rm_if->msgq_first();
+	    exec_query(query->query());
+    } else {
+      logger->log_warn(name(), "Unknown message received");
+    }
+
+    __rm_if->msgq_pop();
+  }
 }
 
+void RobotMemoryThread::exec_query(std::string query_string)
+{
+	logger->log_info(name(), "Executing Query: %s", query_string.c_str());
+
+	//only one query at a time
+	MutexLocker lock(__mutex);
+
+	//get query from string
+	Query query;
+	try{
+	  query = Query(query_string);
+	} catch (DBException &e) {
+		logger->log_error(name(), "Can't parse query_string '%s'\n Exception: %s",
+		                  query_string.c_str(), e.toString().c_str());
+		__rm_if->set_error((std::string("Can't parse query_string ") +  query_string
+		                    + "\nException: " + e.toString()).c_str());
+		return;
+	}
+
+	//actually execute query
+	std::unique_ptr<DBClientCursor> cursor;
+	try{
+	  cursor = mongodb_client->query(__collection, query);
+	} catch (DBException &e) {
+		logger->log_error(name(), "Error for query %s\n Exception: %s",
+		                  query_string.c_str(), e.toString().c_str());
+		__rm_if->set_error((std::string("Query error for ") +  query_string
+		                    + "\nException: " + e.toString()).c_str());
+		return;
+	}
+
+	logger->log_info(name(), "Query One result:\n%s", cursor->next().toString().c_str());
+}
 
 // void
 // RobotMemoryThread::insert_message(LogLevel ll, const char *component,
diff --git a/src/plugins/robot-memory/robot_memory_thread.h b/src/plugins/robot-memory/robot_memory_thread.h
index ca3ee06..004a454 100644
--- a/src/plugins/robot-memory/robot_memory_thread.h
+++ b/src/plugins/robot-memory/robot_memory_thread.h
@@ -29,11 +29,13 @@
 #include <aspect/clock.h>
 #include <plugins/mongodb/aspect/mongodb.h>
 #include <aspect/blackboard.h>
+#include <aspect/blocked_timing.h>
 
 #include <string>
 
 namespace fawkes {
   class Mutex;
+  class RobotMemoryInterface;
 }
 
 class RobotMemoryThread
@@ -42,6 +44,7 @@ class RobotMemoryThread
   public fawkes::ConfigurableAspect,
   public fawkes::ClockAspect,
   public fawkes::MongoDBAspect,
+	public fawkes::BlockedTimingAspect,
   public fawkes::BlackBoardAspect
 {
  public:
@@ -57,6 +60,10 @@ class RobotMemoryThread
 
  private:
   std::string    __collection;
+  fawkes::Mutex *__mutex;
+  fawkes::RobotMemoryInterface *__rm_if;
+
+  void exec_query(std::string query);
 };
 
 #endif

- *commit* 08f87a1aef07d3ca8c20b2f46b5c795896e24733 - - - - - - - - - -
Author:  Frederik Zwilling <zwilling at kbsg.rwth-aachen.de>
Date:    Sun May 1 23:38:09 2016 +0200
Subject: robot-memory: get info about parsed query

 src/plugins/robot-memory/robot_memory_thread.cpp |   12 ++++++++++++
 1 files changed, 12 insertions(+), 0 deletions(-)

_Diff for modified files_:
diff --git a/src/plugins/robot-memory/robot_memory_thread.cpp b/src/plugins/robot-memory/robot_memory_thread.cpp
index 2c35769..32facf8 100644
--- a/src/plugins/robot-memory/robot_memory_thread.cpp
+++ b/src/plugins/robot-memory/robot_memory_thread.cpp
@@ -108,9 +108,18 @@ void RobotMemoryThread::exec_query(std::string query_string)
 		                  query_string.c_str(), e.toString().c_str());
 		__rm_if->set_error((std::string("Can't parse query_string ") +  query_string
 		                    + "\nException: " + e.toString()).c_str());
+		__rm_if->write();
 		return;
 	}
 
+	//introspect query
+	logger->log_info(name(), "Filter: %s", query.getFilter().toString().c_str());
+	logger->log_info(name(), "Modifiers: %s", query.getModifiers().toString().c_str());
+	logger->log_info(name(), "Sort: %s", query.getSort().toString().c_str());
+	logger->log_info(name(), "Hint: %s", query.getHint().toString().c_str());
+	logger->log_info(name(), "ReadPref: %s", query.getReadPref().toString().c_str());
+
+	
 	//actually execute query
 	std::unique_ptr<DBClientCursor> cursor;
 	try{
@@ -120,10 +129,13 @@ void RobotMemoryThread::exec_query(std::string query_string)
 		                  query_string.c_str(), e.toString().c_str());
 		__rm_if->set_error((std::string("Query error for ") +  query_string
 		                    + "\nException: " + e.toString()).c_str());
+		__rm_if->write();
 		return;
 	}
 
 	logger->log_info(name(), "Query One result:\n%s", cursor->next().toString().c_str());
+	__rm_if->set_result(cursor->next().toString().c_str());
+	__rm_if->write();
 }
 
 // void

- *commit* 9ef52de1feebc7ed4657da10774c1d579fef2275 - - - - - - - - - -
Author:  Frederik Zwilling <zwilling at kbsg.rwth-aachen.de>
Date:    Mon May 9 17:50:45 2016 +0200
Subject: robot-memory: added insert, update, remove messages

 .../interfaces/RobotMemoryInterface.cpp            |  327 +++++++++++++++++++-
 .../interfaces/RobotMemoryInterface.h_ext          |   85 +++++
 .../interfaces/RobotMemoryInterface.tolua          |   87 ++++++
 .../interfaces/RobotMemoryInterface.xml            |   19 ++
 src/plugins/robot-memory/robot_memory_thread.cpp   |  219 ++++++++++---
 src/plugins/robot-memory/robot_memory_thread.h     |    7 +
 6 files changed, 696 insertions(+), 48 deletions(-)

_Diff for modified files_:
diff --git a/src/plugins/robot-memory/interfaces/RobotMemoryInterface.cpp b/src/plugins/robot-memory/interfaces/RobotMemoryInterface.cpp
index 40250f8..54650b9 100644
--- a/src/plugins/robot-memory/interfaces/RobotMemoryInterface.cpp
+++ b/src/plugins/robot-memory/interfaces/RobotMemoryInterface.cpp
@@ -53,7 +53,10 @@ RobotMemoryInterface::RobotMemoryInterface() : Interface()
   add_fieldinfo(IFT_STRING, "error", 1024, data->error);
   add_fieldinfo(IFT_STRING, "result", 1024, data->result);
   add_messageinfo("QueryMessage");
-  unsigned char tmp_hash[] = {0xb5, 0xf6, 0xa0, 0x13, 0xd1, 0x30, 0x84, 0x19, 0xb6, 0x98, 0x2c, 0x3d, 0x92, 0xa4, 0x7a, 0xf};
+  add_messageinfo("InsertMessage");
+  add_messageinfo("UpdateMessage");
+  add_messageinfo("RemoveMessage");
+  unsigned char tmp_hash[] = {0x61, 0x88, 0x21, 0xcd, 0x5a, 0x65, 0x18, 0x8d, 0x60, 0xaf, 0xf5, 0x57, 0x30, 0x4c, 0x6b, 0xd8};
   set_hash(tmp_hash);
 }
 
@@ -131,6 +134,12 @@ RobotMemoryInterface::create_message(const char *type) const
 {
   if ( strncmp("QueryMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
     return new QueryMessage();
+  } else if ( strncmp("InsertMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
+    return new InsertMessage();
+  } else if ( strncmp("UpdateMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
+    return new UpdateMessage();
+  } else if ( strncmp("RemoveMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
+    return new RemoveMessage();
   } else {
     throw UnknownTypeException("The given type '%s' does not match any known "
                                "message type for this interface type.", type);
@@ -249,6 +258,310 @@ RobotMemoryInterface::QueryMessage::clone() const
 {
   return new RobotMemoryInterface::QueryMessage(this);
 }
+/** @class RobotMemoryInterface::InsertMessage <interfaces/RobotMemoryInterface.h>
+ * InsertMessage Fawkes BlackBoard Interface Message.
+ * 
+    
+ */
+
+
+/** Constructor with initial values.
+ * @param ini_insert initial value for insert
+ */
+RobotMemoryInterface::InsertMessage::InsertMessage(const char * ini_insert) : Message("InsertMessage")
+{
+  data_size = sizeof(InsertMessage_data_t);
+  data_ptr  = malloc(data_size);
+  memset(data_ptr, 0, data_size);
+  data      = (InsertMessage_data_t *)data_ptr;
+  data_ts   = (message_data_ts_t *)data_ptr;
+  strncpy(data->insert, ini_insert, 1024);
+  add_fieldinfo(IFT_STRING, "insert", 1024, data->insert);
+}
+/** Constructor */
+RobotMemoryInterface::InsertMessage::InsertMessage() : Message("InsertMessage")
+{
+  data_size = sizeof(InsertMessage_data_t);
+  data_ptr  = malloc(data_size);
+  memset(data_ptr, 0, data_size);
+  data      = (InsertMessage_data_t *)data_ptr;
+  data_ts   = (message_data_ts_t *)data_ptr;
+  add_fieldinfo(IFT_STRING, "insert", 1024, data->insert);
+}
+
+/** Destructor */
+RobotMemoryInterface::InsertMessage::~InsertMessage()
+{
+  free(data_ptr);
+}
+
+/** Copy constructor.
+ * @param m message to copy from
+ */
+RobotMemoryInterface::InsertMessage::InsertMessage(const InsertMessage *m) : Message("InsertMessage")
+{
+  data_size = m->data_size;
+  data_ptr  = malloc(data_size);
+  memcpy(data_ptr, m->data_ptr, data_size);
+  data      = (InsertMessage_data_t *)data_ptr;
+  data_ts   = (message_data_ts_t *)data_ptr;
+}
+
+/* Methods */
+/** Get insert value.
+ * Document to insert as JSON string
+ * @return insert value
+ */
+char *
+RobotMemoryInterface::InsertMessage::insert() const
+{
+  return data->insert;
+}
+
+/** Get maximum length of insert value.
+ * @return length of insert value, can be length of the array or number of 
+ * maximum number of characters for a string
+ */
+size_t
+RobotMemoryInterface::InsertMessage::maxlenof_insert() const
+{
+  return 1024;
+}
+
+/** Set insert value.
+ * Document to insert as JSON string
+ * @param new_insert new insert value
+ */
+void
+RobotMemoryInterface::InsertMessage::set_insert(const char * new_insert)
+{
+  strncpy(data->insert, new_insert, sizeof(data->insert));
+}
+
+/** Clone this message.
+ * Produces a message of the same type as this message and copies the
+ * data to the new message.
+ * @return clone of this message
+ */
+Message *
+RobotMemoryInterface::InsertMessage::clone() const
+{
+  return new RobotMemoryInterface::InsertMessage(this);
+}
+/** @class RobotMemoryInterface::UpdateMessage <interfaces/RobotMemoryInterface.h>
+ * UpdateMessage Fawkes BlackBoard Interface Message.
+ * 
+    
+ */
+
+
+/** Constructor with initial values.
+ * @param ini_query initial value for query
+ * @param ini_update initial value for update
+ */
+RobotMemoryInterface::UpdateMessage::UpdateMessage(const char * ini_query, const char * ini_update) : Message("UpdateMessage")
+{
+  data_size = sizeof(UpdateMessage_data_t);
+  data_ptr  = malloc(data_size);
+  memset(data_ptr, 0, data_size);
+  data      = (UpdateMessage_data_t *)data_ptr;
+  data_ts   = (message_data_ts_t *)data_ptr;
+  strncpy(data->query, ini_query, 1024);
+  strncpy(data->update, ini_update, 1024);
+  add_fieldinfo(IFT_STRING, "query", 1024, data->query);
+  add_fieldinfo(IFT_STRING, "update", 1024, data->update);
+}
+/** Constructor */
+RobotMemoryInterface::UpdateMessage::UpdateMessage() : Message("UpdateMessage")
+{
+  data_size = sizeof(UpdateMessage_data_t);
+  data_ptr  = malloc(data_size);
+  memset(data_ptr, 0, data_size);
+  data      = (UpdateMessage_data_t *)data_ptr;
+  data_ts   = (message_data_ts_t *)data_ptr;
+  add_fieldinfo(IFT_STRING, "query", 1024, data->query);
+  add_fieldinfo(IFT_STRING, "update", 1024, data->update);
+}
+
+/** Destructor */
+RobotMemoryInterface::UpdateMessage::~UpdateMessage()
+{
+  free(data_ptr);
+}
+
+/** Copy constructor.
+ * @param m message to copy from
+ */
+RobotMemoryInterface::UpdateMessage::UpdateMessage(const UpdateMessage *m) : Message("UpdateMessage")
+{
+  data_size = m->data_size;
+  data_ptr  = malloc(data_size);
+  memcpy(data_ptr, m->data_ptr, data_size);
+  data      = (UpdateMessage_data_t *)data_ptr;
+  data_ts   = (message_data_ts_t *)data_ptr;
+}
+
+/* Methods */
+/** Get query value.
+ * Query as JSON string
+ * @return query value
+ */
+char *
+RobotMemoryInterface::UpdateMessage::query() const
+{
+  return data->query;
+}
+
+/** Get maximum length of query value.
+ * @return length of query value, can be length of the array or number of 
+ * maximum number of characters for a string
+ */
+size_t
+RobotMemoryInterface::UpdateMessage::maxlenof_query() const
+{
+  return 1024;
+}
+
+/** Set query value.
+ * Query as JSON string
+ * @param new_query new query value
+ */
+void
+RobotMemoryInterface::UpdateMessage::set_query(const char * new_query)
+{
+  strncpy(data->query, new_query, sizeof(data->query));
+}
+
+/** Get update value.
+ * Update as JSON string
+ * @return update value
+ */
+char *
+RobotMemoryInterface::UpdateMessage::update() const
+{
+  return data->update;
+}
+
+/** Get maximum length of update value.
+ * @return length of update value, can be length of the array or number of 
+ * maximum number of characters for a string
+ */
+size_t
+RobotMemoryInterface::UpdateMessage::maxlenof_update() const
+{
+  return 1024;
+}
+
+/** Set update value.
+ * Update as JSON string
+ * @param new_update new update value
+ */
+void
+RobotMemoryInterface::UpdateMessage::set_update(const char * new_update)
+{
+  strncpy(data->update, new_update, sizeof(data->update));
+}
+
+/** Clone this message.
+ * Produces a message of the same type as this message and copies the
+ * data to the new message.
+ * @return clone of this message
+ */
+Message *
+RobotMemoryInterface::UpdateMessage::clone() const
+{
+  return new RobotMemoryInterface::UpdateMessage(this);
+}
+/** @class RobotMemoryInterface::RemoveMessage <interfaces/RobotMemoryInterface.h>
+ * RemoveMessage Fawkes BlackBoard Interface Message.
+ * 
+    
+ */
+
+
+/** Constructor with initial values.
+ * @param ini_query initial value for query
+ */
+RobotMemoryInterface::RemoveMessage::RemoveMessage(const char * ini_query) : Message("RemoveMessage")
+{
+  data_size = sizeof(RemoveMessage_data_t);
+  data_ptr  = malloc(data_size);
+  memset(data_ptr, 0, data_size);
+  data      = (RemoveMessage_data_t *)data_ptr;
+  data_ts   = (message_data_ts_t *)data_ptr;
+  strncpy(data->query, ini_query, 1024);
+  add_fieldinfo(IFT_STRING, "query", 1024, data->query);
+}
+/** Constructor */
+RobotMemoryInterface::RemoveMessage::RemoveMessage() : Message("RemoveMessage")
+{
+  data_size = sizeof(RemoveMessage_data_t);
+  data_ptr  = malloc(data_size);
+  memset(data_ptr, 0, data_size);
+  data      = (RemoveMessage_data_t *)data_ptr;
+  data_ts   = (message_data_ts_t *)data_ptr;
+  add_fieldinfo(IFT_STRING, "query", 1024, data->query);
+}
+
+/** Destructor */
+RobotMemoryInterface::RemoveMessage::~RemoveMessage()
+{
+  free(data_ptr);
+}
+
+/** Copy constructor.
+ * @param m message to copy from
+ */
+RobotMemoryInterface::RemoveMessage::RemoveMessage(const RemoveMessage *m) : Message("RemoveMessage")
+{
+  data_size = m->data_size;
+  data_ptr  = malloc(data_size);
+  memcpy(data_ptr, m->data_ptr, data_size);
+  data      = (RemoveMessage_data_t *)data_ptr;
+  data_ts   = (message_data_ts_t *)data_ptr;
+}
+
+/* Methods */
+/** Get query value.
+ * Query as JSON string
+ * @return query value
+ */
+char *
+RobotMemoryInterface::RemoveMessage::query() const
+{
+  return data->query;
+}
+
+/** Get maximum length of query value.
+ * @return length of query value, can be length of the array or number of 
+ * maximum number of characters for a string
+ */
+size_t
+RobotMemoryInterface::RemoveMessage::maxlenof_query() const
+{
+  return 1024;
+}
+
+/** Set query value.
+ * Query as JSON string
+ * @param new_query new query value
+ */
+void
+RobotMemoryInterface::RemoveMessage::set_query(const char * new_query)
+{
+  strncpy(data->query, new_query, sizeof(data->query));
+}
+
+/** Clone this message.
+ * Produces a message of the same type as this message and copies the
+ * data to the new message.
+ * @return clone of this message
+ */
+Message *
+RobotMemoryInterface::RemoveMessage::clone() const
+{
+  return new RobotMemoryInterface::RemoveMessage(this);
+}
 /** Check if message is valid and can be enqueued.
  * @param message Message to check
  * @return true if the message is valid, false otherwise.
@@ -260,6 +573,18 @@ RobotMemoryInterface::message_valid(const Message *message) const
   if ( m0 != NULL ) {
     return true;
   }
+  const InsertMessage *m1 = dynamic_cast<const InsertMessage *>(message);
+  if ( m1 != NULL ) {
+    return true;
+  }
+  const UpdateMessage *m2 = dynamic_cast<const UpdateMessage *>(message);
+  if ( m2 != NULL ) {
+    return true;
+  }
+  const RemoveMessage *m3 = dynamic_cast<const RemoveMessage *>(message);
+  if ( m3 != NULL ) {
+    return true;
+  }
   return false;
 }
 
diff --git a/src/plugins/robot-memory/interfaces/RobotMemoryInterface.h_ext b/src/plugins/robot-memory/interfaces/RobotMemoryInterface.h_ext
index e0abd1a..ddd2d0b 100644
--- a/src/plugins/robot-memory/interfaces/RobotMemoryInterface.h_ext
+++ b/src/plugins/robot-memory/interfaces/RobotMemoryInterface.h_ext
@@ -80,6 +80,91 @@ class RobotMemoryInterface : public Interface
     virtual Message * clone() const;
   };
 
+  class InsertMessage : public Message
+  {
+   private:
+#pragma pack(push,4)
+    /** Internal data storage, do NOT modify! */
+    typedef struct {
+      int64_t timestamp_sec;  /**< Interface Unix timestamp, seconds */
+      int64_t timestamp_usec; /**< Interface Unix timestamp, micro-seconds */
+      char insert[1024]; /**< Document to insert as JSON string */
+    } InsertMessage_data_t;
+#pragma pack(pop)
+
+    InsertMessage_data_t *data;
+
+   public:
+    InsertMessage(const char * ini_insert);
+    InsertMessage();
+    ~InsertMessage();
+
+    InsertMessage(const InsertMessage *m);
+    /* Methods */
+    char * insert() const;
+    void set_insert(const char * new_insert);
+    size_t maxlenof_insert() const;
+    virtual Message * clone() const;
+  };
+
+  class UpdateMessage : public Message
+  {
+   private:
+#pragma pack(push,4)
+    /** Internal data storage, do NOT modify! */
+    typedef struct {
+      int64_t timestamp_sec;  /**< Interface Unix timestamp, seconds */
+      int64_t timestamp_usec; /**< Interface Unix timestamp, micro-seconds */
+      char query[1024]; /**< Query as JSON string */
+      char update[1024]; /**< Update as JSON string */
+    } UpdateMessage_data_t;
+#pragma pack(pop)
+
+    UpdateMessage_data_t *data;
+
+   public:
+    UpdateMessage(const char * ini_query, const char * ini_update);
+    UpdateMessage();
+    ~UpdateMessage();
+
+    UpdateMessage(const UpdateMessage *m);
+    /* Methods */
+    char * query() const;
+    void set_query(const char * new_query);
+    size_t maxlenof_query() const;
+    char * update() const;
+    void set_update(const char * new_update);
+    size_t maxlenof_update() const;
+    virtual Message * clone() const;
+  };
+
+  class RemoveMessage : public Message
+  {
+   private:
+#pragma pack(push,4)
+    /** Internal data storage, do NOT modify! */
+    typedef struct {
+      int64_t timestamp_sec;  /**< Interface Unix timestamp, seconds */
+      int64_t timestamp_usec; /**< Interface Unix timestamp, micro-seconds */
+      char query[1024]; /**< Query as JSON string */
+    } RemoveMessage_data_t;
+#pragma pack(pop)
+
+    RemoveMessage_data_t *data;
+
+   public:
+    RemoveMessage(const char * ini_query);
+    RemoveMessage();
+    ~RemoveMessage();
+
+    RemoveMessage(const RemoveMessage *m);
+    /* Methods */
+    char * query() const;
+    void set_query(const char * new_query);
+    size_t maxlenof_query() const;
+    virtual Message * clone() const;
+  };
+
   virtual bool message_valid(const Message *message) const;
  private:
   RobotMemoryInterface();
diff --git a/src/plugins/robot-memory/interfaces/RobotMemoryInterface.tolua b/src/plugins/robot-memory/interfaces/RobotMemoryInterface.tolua
index 1b06b6b..67095b2 100644
--- a/src/plugins/robot-memory/interfaces/RobotMemoryInterface.tolua
+++ b/src/plugins/robot-memory/interfaces/RobotMemoryInterface.tolua
@@ -34,6 +34,93 @@ class RobotMemoryInterface : public Interface
     int maxlenof_query() const;
   };
 
+  class InsertMessage : public Message
+  {
+    InsertMessage(char * ini_insert);
+    InsertMessage();
+    ~InsertMessage();
+
+    unsigned int      id() const;
+
+    unsigned int      sender_id() const;
+    const char *      sender_thread_name() const;
+    Interface *       interface() const;
+    const char *      type() const;
+
+    const void *      datachunk() const;
+    unsigned int      datasize() const;
+
+    void              set_from_chunk(const void *chunk);
+
+    /* from RefCount */
+    void              ref();
+    void              unref();
+    unsigned int      refcount();
+
+    char * insert();
+    void set_insert(const char * new_insert);
+    int maxlenof_insert() const;
+  };
+
+  class UpdateMessage : public Message
+  {
+    UpdateMessage(char * ini_query, char * ini_update);
+    UpdateMessage();
+    ~UpdateMessage();
+
+    unsigned int      id() const;
+
+    unsigned int      sender_id() const;
+    const char *      sender_thread_name() const;
+    Interface *       interface() const;
+    const char *      type() const;
+
+    const void *      datachunk() const;
+    unsigned int      datasize() const;
+
+    void              set_from_chunk(const void *chunk);
+
+    /* from RefCount */
+    void              ref();
+    void              unref();
+    unsigned int      refcount();
+
+    char * query();
+    void set_query(const char * new_query);
+    int maxlenof_query() const;
+    char * update();
+    void set_update(const char * new_update);
+    int maxlenof_update() const;
+  };
+
+  class RemoveMessage : public Message
+  {
+    RemoveMessage(char * ini_query);
+    RemoveMessage();
+    ~RemoveMessage();
+
+    unsigned int      id() const;
+
+    unsigned int      sender_id() const;
+    const char *      sender_thread_name() const;
+    Interface *       interface() const;
+    const char *      type() const;
+
+    const void *      datachunk() const;
+    unsigned int      datasize() const;
+
+    void              set_from_chunk(const void *chunk);
+
+    /* from RefCount */
+    void              ref();
+    void              unref();
+    unsigned int      refcount();
+
+    char * query();
+    void set_query(const char * new_query);
+    int maxlenof_query() const;
+  };
+
   char * error();
   void set_error(const char * new_error);
   int maxlenof_error() const;
diff --git a/src/plugins/robot-memory/interfaces/RobotMemoryInterface.xml b/src/plugins/robot-memory/interfaces/RobotMemoryInterface.xml
index ae43d12..56dcfe7 100644
--- a/src/plugins/robot-memory/interfaces/RobotMemoryInterface.xml
+++ b/src/plugins/robot-memory/interfaces/RobotMemoryInterface.xml
@@ -16,5 +16,24 @@
     </comment>
     <field type="string"  length="1024" name="query">Query as JSON string</field>
   </message>
+  <message name="Insert">
+    <comment>
+      Message to insert a document into the database.
+    </comment>
+    <field type="string"  length="1024" name="insert">Document to insert as JSON string</field>
+  </message>
+  <message name="Update">
+    <comment>
+      Message to update documents mathing a query.
+    </comment>
+    <field type="string"  length="1024" name="query">Query as JSON string</field>
+    <field type="string"  length="1024" name="update">Update as JSON string</field>
+  </message>
+  <message name="Remove">
+    <comment>
+      Message to remove documents matching the query.
+    </comment>
+    <field type="string"  length="1024" name="query">Query as JSON string</field>
+  </message>
 
 </interface>
diff --git a/src/plugins/robot-memory/robot_memory_thread.cpp b/src/plugins/robot-memory/robot_memory_thread.cpp
index 32facf8..1821a83 100644
--- a/src/plugins/robot-memory/robot_memory_thread.cpp
+++ b/src/plugins/robot-memory/robot_memory_thread.cpp
@@ -59,7 +59,7 @@ RobotMemoryThread::init()
 	logger->log_info(name(), "Started RobotMemory");
 	__collection = "fawkes.msglog";
   try {
-    __collection = config->get_string("/plugins/mongodb/logger_collection");
+    __collection = config->get_string("/plugins/mongodb/test-collection");
   } catch (Exception &e) {}
 
   __rm_if = blackboard->open_for_writing<RobotMemoryInterface>(config->get_string("/plugins/robot-memory/interface-name").c_str());
@@ -82,8 +82,17 @@ RobotMemoryThread::loop()
 	// process interface messages
   while (! __rm_if->msgq_empty() ) {
     if (__rm_if->msgq_first_is<RobotMemoryInterface::QueryMessage>()) {
-	    RobotMemoryInterface::QueryMessage* query = (RobotMemoryInterface::QueryMessage*) __rm_if->msgq_first();
-	    exec_query(query->query());
+	    RobotMemoryInterface::QueryMessage* msg = (RobotMemoryInterface::QueryMessage*) __rm_if->msgq_first();
+	    exec_query(msg->query());
+    } else if (__rm_if->msgq_first_is<RobotMemoryInterface::InsertMessage>()) {
+	    RobotMemoryInterface::InsertMessage* msg = (RobotMemoryInterface::InsertMessage*) __rm_if->msgq_first();
+	    exec_insert(msg->insert());
+    } else if (__rm_if->msgq_first_is<RobotMemoryInterface::UpdateMessage>()) {
+	    RobotMemoryInterface::UpdateMessage* msg = (RobotMemoryInterface::UpdateMessage*) __rm_if->msgq_first();
+	    exec_update(msg->query(), msg->update());
+    } else if (__rm_if->msgq_first_is<RobotMemoryInterface::RemoveMessage>()) {
+	    RobotMemoryInterface::RemoveMessage* msg = (RobotMemoryInterface::RemoveMessage*) __rm_if->msgq_first();
+	    exec_remove(msg->query());
     } else {
       logger->log_warn(name(), "Unknown message received");
     }
@@ -113,17 +122,12 @@ void RobotMemoryThread::exec_query(std::string query_string)
 	}
 
 	//introspect query
-	logger->log_info(name(), "Filter: %s", query.getFilter().toString().c_str());
-	logger->log_info(name(), "Modifiers: %s", query.getModifiers().toString().c_str());
-	logger->log_info(name(), "Sort: %s", query.getSort().toString().c_str());
-	logger->log_info(name(), "Hint: %s", query.getHint().toString().c_str());
-	logger->log_info(name(), "ReadPref: %s", query.getReadPref().toString().c_str());
-
+	log(query, "executing query:");
 	
 	//actually execute query
 	std::unique_ptr<DBClientCursor> cursor;
 	try{
-	  cursor = mongodb_client->query(__collection, query);
+	  cursor = mongodb_client->query(dyn_collection, query);
 	} catch (DBException &e) {
 		logger->log_error(name(), "Error for query %s\n Exception: %s",
 		                  query_string.c_str(), e.toString().c_str());
@@ -133,43 +137,164 @@ void RobotMemoryThread::exec_query(std::string query_string)
 		return;
 	}
 
-	logger->log_info(name(), "Query One result:\n%s", cursor->next().toString().c_str());
-	__rm_if->set_result(cursor->next().toString().c_str());
+	if(cursor->more()){
+		BSONObj res = cursor->next();
+		logger->log_info(name(), "Query One result:\n%s", res.toString().c_str());
+		__rm_if->set_result(res.toString().c_str());
+		__rm_if->write();
+	}
+	else {
+		logger->log_info(name(), "Query result empty");
+	}
+}
+
+void RobotMemoryThread::exec_insert(std::string insert_string)
+{
+	logger->log_info(name(), "Executing Query: %s", insert_string.c_str());
+
+	//only one query at a time
+	MutexLocker lock(__mutex);
+
+	//get query from string
+  BSONObj obj;
+	try{
+		obj = fromjson(insert_string);
+	} catch (DBException &e) {
+		logger->log_error(name(), "Can't parse insert_string '%s'\n Exception: %s",
+		                  insert_string.c_str(), e.toString().c_str());
+		__rm_if->set_error((std::string("Can't parse insert_string ") +  insert_string
+		                    + "\nException: " + e.toString()).c_str());
+		__rm_if->write();
+		return;
+	}
+
+	log(obj, "Inserting:");
+	
+	//actually execute insert
+	try{
+	  mongodb_client->insert(__collection, obj);
+	} catch (DBException &e) {
+		logger->log_error(name(), "Error for insert %s\n Exception: %s",
+		                  insert_string.c_str(), e.toString().c_str());
+		__rm_if->set_error((std::string("Query error for ") +  insert_string
+		                    + "\nException: " + e.toString()).c_str());
+		__rm_if->write();
+		return;
+	}
+
+	__rm_if->set_result("insert successful");
 	__rm_if->write();
 }
 
-// void
-// RobotMemoryThread::insert_message(LogLevel ll, const char *component,
-// 				    const char *format, va_list va)
-// {
-//   if (log_level <= ll ) {
-//     MutexLocker lock(__mutex);
-//     struct timeval now;
-//     gettimeofday(&now, NULL);
-//     Date_t nowd = now.tv_sec * 1000 + now.tv_usec / 1000;
-
-//     char *msg;
-//     if (vasprintf(&msg, format, va) == -1) {
-//       // Cannot do anything useful, drop log message
-//       return;
-//     }
-
-//     BSONObjBuilder b;
-//     switch (ll) {
-//     case LL_DEBUG: b.append("level", "DEBUG"); break;
-//     case LL_INFO:  b.append("level", "INFO");  break;
-//     case LL_WARN:  b.append("level", "WARN");  break;
-//     case LL_ERROR: b.append("level", "ERROR"); break;
-//     default:       b.append("level", "UNKN");  break;
-//     }
-//     b.append("component", component);
-//     b.appendDate("time", nowd);
-//     b.append("message", msg);
-
-//     free(msg);
-
-//     try {
-//       mongodb_client->insert(__collection, b.obj());
-//     } catch (mongo::DBException &e) {} // ignored
-//   }
-// }
+void RobotMemoryThread::exec_update(std::string query_string, std::string update_string)
+{
+	logger->log_info(name(), "Executing Update %s for query %s",
+	                 update_string.c_str(), query_string.c_str());
+
+	//only one query at a time
+	MutexLocker lock(__mutex);
+
+	//get query from string
+	Query query;
+	try{
+	  query = Query(query_string);
+	} catch (DBException &e) {
+		logger->log_error(name(), "Can't parse query_string '%s'\n Exception: %s",
+		                  query_string.c_str(), e.toString().c_str());
+		__rm_if->set_error((std::string("Can't parse query_string ") +  query_string
+		                    + "\nException: " + e.toString()).c_str());
+		__rm_if->write();
+		return;
+	}
+	BSONObj update;
+	try{
+		update = fromjson(update_string);
+	} catch (DBException &e) {
+		logger->log_error(name(), "Can't parse update_string '%s'\n Exception: %s",
+		                  update_string.c_str(), e.toString().c_str());
+		__rm_if->set_error((std::string("Can't parse update_string ") +  update_string
+		                    + "\nException: " + e.toString()).c_str());
+		__rm_if->write();
+		return;
+	}
+
+	log(query, "Updating documents for query:");
+	log(update, "Updating with:");
+	
+	//actually execute update
+	try{
+		mongodb_client->update(__collection, query, update);
+	} catch (DBException &e) {
+		logger->log_error(name(), "Error for update %s for query %s\n Exception: %s",
+		                  update_string.c_str(), query_string.c_str(), e.toString().c_str());
+		__rm_if->set_error((std::string("Query error for ") +  query_string + " and update "
+		                    + update_string + "\nException: " + e.toString()).c_str());
+		__rm_if->write();
+		return;
+	}
+
+	__rm_if->set_result("update successful");
+	__rm_if->write();
+}
+
+void RobotMemoryThread::exec_remove(std::string query_string)
+{
+	logger->log_info(name(), "Executing Remove: %s", query_string.c_str());
+
+	//only one query at a time
+	MutexLocker lock(__mutex);
+
+	//get query from string
+	Query query;
+	try{
+	  query = Query(query_string);
+	} catch (DBException &e) {
+		logger->log_error(name(), "Can't parse query_string '%s'\n Exception: %s",
+		                  query_string.c_str(), e.toString().c_str());
+		__rm_if->set_error((std::string("Can't parse query_string ") +  query_string
+		                    + "\nException: " + e.toString()).c_str());
+		__rm_if->write();
+		return;
+	}
+
+	//introspect
+	log(query, "Removing documents for query:");
+	
+	//actually execute remove
+	try{
+	  mongodb_client->remove(__collection, query);
+	} catch (DBException &e) {
+		logger->log_error(name(), "Error for query %s\n Exception: %s",
+		                  query_string.c_str(), e.toString().c_str());
+		__rm_if->set_error((std::string("Query error for ") +  query_string
+		                    + "\nException: " + e.toString()).c_str());
+		__rm_if->write();
+		return;
+	}
+
+	__rm_if->set_result("remove successful");
+	__rm_if->write();
+}
+
+void
+RobotMemoryThread::log(Query query, std::string what)
+{
+	std::string output = what
+		+ "\nFilter: " + query.getFilter().toString()
+		+ "\nModifiers: " + query.getModifiers().toString()
+		+ "\nSort: " + query.getSort().toString()
+		+ "\nHint: " + query.getHint().toString()
+		+ "\nReadPref: " + query.getReadPref().toString();
+		
+	logger->log_info(name(), "%s", output.c_str());
+}
+
+void
+RobotMemoryThread::log(BSONObj obj, std::string what)
+{
+	std::string output = what
+		+ "\nObject: " + obj.toString();
+		
+	logger->log_info(name(), "%s", output.c_str());
+}
+
diff --git a/src/plugins/robot-memory/robot_memory_thread.h b/src/plugins/robot-memory/robot_memory_thread.h
index 004a454..ed32903 100644
--- a/src/plugins/robot-memory/robot_memory_thread.h
+++ b/src/plugins/robot-memory/robot_memory_thread.h
@@ -30,6 +30,7 @@
 #include <plugins/mongodb/aspect/mongodb.h>
 #include <aspect/blackboard.h>
 #include <aspect/blocked_timing.h>
+#include <mongo/client/dbclient.h>
 
 #include <string>
 
@@ -64,6 +65,12 @@ class RobotMemoryThread
   fawkes::RobotMemoryInterface *__rm_if;
 
   void exec_query(std::string query);
+  void exec_insert(std::string insert);
+  void exec_update(std::string query, std::string update);
+  void exec_remove(std::string query);
+
+  void log(mongo::Query query, std::string what);
+  void log(mongo::BSONObj obj, std::string what);
 };
 
 #endif

- *commit* d7432e57f12e81bca648e61ecdcf080d2c25d907 - - - - - - - - - -
Author:  Frederik Zwilling <zwilling at kbsg.rwth-aachen.de>
Date:    Mon May 9 18:59:23 2016 +0200
Subject: robot-memory: virtual knowledge base experiments

 src/plugins/robot-memory/robot_memory_thread.cpp |  108 +++++++++++++++++++++-
 src/plugins/robot-memory/robot_memory_thread.h   |   11 ++
 2 files changed, 115 insertions(+), 4 deletions(-)

_Diff for modified files_:
diff --git a/src/plugins/robot-memory/robot_memory_thread.cpp b/src/plugins/robot-memory/robot_memory_thread.cpp
index 1821a83..7774ab7 100644
--- a/src/plugins/robot-memory/robot_memory_thread.cpp
+++ b/src/plugins/robot-memory/robot_memory_thread.cpp
@@ -103,6 +103,10 @@ RobotMemoryThread::loop()
 
 void RobotMemoryThread::exec_query(std::string query_string)
 {
+	exec_query(query_string, __collection);
+}
+void RobotMemoryThread::exec_query(std::string query_string, std::string collection)
+{
 	logger->log_info(name(), "Executing Query: %s", query_string.c_str());
 
 	//only one query at a time
@@ -123,11 +127,26 @@ void RobotMemoryThread::exec_query(std::string query_string)
 
 	//introspect query
 	log(query, "executing query:");
+
+	//check if virtual knowledge is queried
+	//rename field in query
+	if(query.getFilter().hasField("class")){
+		set_fields(query, std::string("{type:\"") +
+		           query.getFilter()["class"].String() + "\"}");
+		remove_field(query, "class");
+	}
+	log(query, "Virtual query:");
+	//computation on request
+	if(query.getFilter().hasField("bbinterface")){
+		collection = config->get_string("plugins/robot-memory/blackboard-collection");
+		gen_blackboard_data(query.getFilter()["bbinterface"].String());
+	}
+	log(query, "Virtual query:");
 	
 	//actually execute query
 	std::unique_ptr<DBClientCursor> cursor;
 	try{
-	  cursor = mongodb_client->query(dyn_collection, query);
+	  cursor = mongodb_client->query(collection, query);
 	} catch (DBException &e) {
 		logger->log_error(name(), "Error for query %s\n Exception: %s",
 		                  query_string.c_str(), e.toString().c_str());
@@ -150,6 +169,10 @@ void RobotMemoryThread::exec_query(std::string query_string)
 
 void RobotMemoryThread::exec_insert(std::string insert_string)
 {
+	exec_insert(insert_string, __collection);
+}
+void RobotMemoryThread::exec_insert(std::string insert_string, std::string collection)
+{
 	logger->log_info(name(), "Executing Query: %s", insert_string.c_str());
 
 	//only one query at a time
@@ -169,10 +192,12 @@ void RobotMemoryThread::exec_insert(std::string insert_string)
 	}
 
 	log(obj, "Inserting:");
+	set_fields(obj, "{type: \"test\"}");
+	log(obj, "Updated Inserting:");
 	
 	//actually execute insert
 	try{
-	  mongodb_client->insert(__collection, obj);
+	  mongodb_client->insert(collection, obj);
 	} catch (DBException &e) {
 		logger->log_error(name(), "Error for insert %s\n Exception: %s",
 		                  insert_string.c_str(), e.toString().c_str());
@@ -188,6 +213,11 @@ void RobotMemoryThread::exec_insert(std::string insert_string)
 
 void RobotMemoryThread::exec_update(std::string query_string, std::string update_string)
 {
+	exec_update(query_string, update_string, __collection);
+}
+void RobotMemoryThread::exec_update(std::string query_string, std::string update_string,
+                                    std::string collection)
+{
 	logger->log_info(name(), "Executing Update %s for query %s",
 	                 update_string.c_str(), query_string.c_str());
 
@@ -223,7 +253,7 @@ void RobotMemoryThread::exec_update(std::string query_string, std::string update
 	
 	//actually execute update
 	try{
-		mongodb_client->update(__collection, query, update);
+		mongodb_client->update(collection, query, update);
 	} catch (DBException &e) {
 		logger->log_error(name(), "Error for update %s for query %s\n Exception: %s",
 		                  update_string.c_str(), query_string.c_str(), e.toString().c_str());
@@ -239,6 +269,11 @@ void RobotMemoryThread::exec_update(std::string query_string, std::string update
 
 void RobotMemoryThread::exec_remove(std::string query_string)
 {
+	exec_remove(query_string, __collection);
+}
+
+void RobotMemoryThread::exec_remove(std::string query_string, std::string collection)
+{
 	logger->log_info(name(), "Executing Remove: %s", query_string.c_str());
 
 	//only one query at a time
@@ -262,7 +297,7 @@ void RobotMemoryThread::exec_remove(std::string query_string)
 	
 	//actually execute remove
 	try{
-	  mongodb_client->remove(__collection, query);
+	  mongodb_client->remove(collection, query);
 	} catch (DBException &e) {
 		logger->log_error(name(), "Error for query %s\n Exception: %s",
 		                  query_string.c_str(), e.toString().c_str());
@@ -298,3 +333,68 @@ RobotMemoryThread::log(BSONObj obj, std::string what)
 	logger->log_info(name(), "%s", output.c_str());
 }
 
+void
+RobotMemoryThread::set_fields(BSONObj &obj, std::string what)
+{
+	BSONObjBuilder b;
+	b.appendElements(obj);
+	b.appendElements(fromjson(what));
+	//override
+	obj = b.obj();
+}
+
+void
+RobotMemoryThread::set_fields(Query &q, std::string what)
+{
+	BSONObjBuilder b;
+	b.appendElements(q.getFilter());
+	b.appendElements(fromjson(what));
+
+	//TODO keep other stuff in query
+	// + "\nFilter: " + query.getFilter().toString()
+	// + "\nModifiers: " + query.getModifiers().toString()
+	// + "\nSort: " + query.getSort().toString()
+	// + "\nHint: " + query.getHint().toString()
+	// + "\nReadPref: " + query.getReadPref().toString();
+     
+	//override
+	q = Query(b.obj());
+}
+
+void
+RobotMemoryThread::remove_field(Query &q, std::string what)
+{
+	BSONObjBuilder b;
+	b.appendElements(q.getFilter().removeField(what));
+	
+	//TODO keep other stuff in query
+	// + "\nFilter: " + query.getFilter().toString()
+	// + "\nModifiers: " + query.getModifiers().toString()
+	// + "\nSort: " + query.getSort().toString()
+	// + "\nHint: " + query.getHint().toString()
+	// + "\nReadPref: " + query.getReadPref().toString();
+	
+	//override
+	q = Query(b.obj());
+}
+
+void
+RobotMemoryThread::gen_blackboard_data(std::string field)
+{
+	logger->log_info(name(), "Generating virtual kb for bb");
+
+	std::string collection = config->get_string("plugins/robot-memory/blackboard-collection");
+	
+	//remove old data first
+	mongodb_client->remove(collection, Query{"{}"});
+	
+	BSONObjBuilder b;
+	b.append("type", "bbinterface");
+	__rm_if->read();
+	b.append("bbinterface", __rm_if->uid());
+	b.append("error", __rm_if->error());
+	b.append("result", __rm_if->result());
+	
+	mongodb_client->insert(collection, b.obj());  
+}
+
diff --git a/src/plugins/robot-memory/robot_memory_thread.h b/src/plugins/robot-memory/robot_memory_thread.h
index ed32903..8a5013c 100644
--- a/src/plugins/robot-memory/robot_memory_thread.h
+++ b/src/plugins/robot-memory/robot_memory_thread.h
@@ -64,13 +64,24 @@ class RobotMemoryThread
   fawkes::Mutex *__mutex;
   fawkes::RobotMemoryInterface *__rm_if;
 
+  void exec_query(std::string query, std::string collection);
   void exec_query(std::string query);
+  void exec_insert(std::string insert, std::string collection);
   void exec_insert(std::string insert);
+  void exec_update(std::string query, std::string update, std::string collection);
   void exec_update(std::string query, std::string update);
+  void exec_remove(std::string query, std::string collection);
   void exec_remove(std::string query);
 
   void log(mongo::Query query, std::string what);
   void log(mongo::BSONObj obj, std::string what);
+
+  void set_fields(mongo::BSONObj &obj, std::string what);
+  void set_fields(mongo::Query &q, std::string what);
+  void remove_field(mongo::Query &q, std::string what);
+
+  //funtions to generate virtual knowledge
+  void gen_blackboard_data(std::string field);
 };
 
 #endif




-- 
Fawkes Robotics Framework                 http://www.fawkesrobotics.org


More information about the fawkes-commits mailing list