[Fawkes Git] branch/fzwilling/robot-memory: 4 revs updated. (0.5.0-3102-gb7c0f54)

Frederik Zwilling zwilling at kbsg.rwth-aachen.de
Mon Aug 29 20:30: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 updated
        to  b7c0f54f638b28e485729f3460b0678e45905195 (commit)
       via  9b95b13198e94447548ac95ca82e6a7fed5dbd96 (commit)
       via  e1c90eeaf19de473dc33bc7f5f365daf4668f82c (commit)
       via  adeb72b19f66db0c542457bec914552260dd004c (commit)
      from  b8f723a3d70ad7ff4cfe3360ebbbf0d7dff3ef0c (commit)

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

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- *Log* ---------------------------------------------------------------
commit adeb72b19f66db0c542457bec914552260dd004c
Author:     Frederik Zwilling <zwilling at kbsg.rwth-aachen.de>
AuthorDate: Fri Aug 26 19:05:37 2016 +0200
Commit:     Frederik Zwilling <zwilling at kbsg.rwth-aachen.de>
CommitDate: Mon Aug 29 20:30:16 2016 +0200

    robot-memory: better logging functions

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

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
commit e1c90eeaf19de473dc33bc7f5f365daf4668f82c
Author:     Frederik Zwilling <zwilling at kbsg.rwth-aachen.de>
AuthorDate: Mon Aug 29 19:02:53 2016 +0200
Commit:     Frederik Zwilling <zwilling at kbsg.rwth-aachen.de>
CommitDate: Mon Aug 29 20:30:21 2016 +0200

    robot-memory: dump and restore collections
    
    You can now dump and restore collections. This allows setting the robot
    memory to an initial state or storing memory states (e.g. to repeat
    experiments on the same data).
    The implementation uses mongorestore and mongodump.

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

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
commit 9b95b13198e94447548ac95ca82e6a7fed5dbd96
Author:     Frederik Zwilling <zwilling at kbsg.rwth-aachen.de>
AuthorDate: Mon Aug 29 19:44:31 2016 +0200
Commit:     Frederik Zwilling <zwilling at kbsg.rwth-aachen.de>
CommitDate: Mon Aug 29 20:30:25 2016 +0200

    robot-memory: use BSONObj and Query in API
    
    This allows giving BSONObj and Queries to the robot memory's function as
    well as simple strings in the JSON format.

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

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
commit b7c0f54f638b28e485729f3460b0678e45905195
Author:     Frederik Zwilling <zwilling at kbsg.rwth-aachen.de>
AuthorDate: Mon Aug 29 20:27:26 2016 +0200
Commit:     Frederik Zwilling <zwilling at kbsg.rwth-aachen.de>
CommitDate: Mon Aug 29 20:30:28 2016 +0200

    clips-robot-memory: start for CLIPS feature from refbox clips-mongodb

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

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


- *Summary* -----------------------------------------------------------
 src/plugins/Makefile                               |    2 +-
 src/plugins/clips-robot-memory/Makefile            |   60 ++
 .../clips_robot_memory_plugin.cpp}                 |   20 +-
 .../clips_robot_memory_thread.cpp                  |  680 ++++++++++++++++++++
 .../clips-robot-memory/clips_robot_memory_thread.h |   95 +++
 src/plugins/robot-memory/robot_memory.cpp          |  293 +++++----
 src/plugins/robot-memory/robot_memory.h            |   23 +-
 .../robot-memory/test-plugin/robot_memory_test.cpp |   42 +-
 8 files changed, 1067 insertions(+), 148 deletions(-)
 create mode 100644 src/plugins/clips-robot-memory/Makefile
 copy src/plugins/{robot-memory/test-plugin/robot_memory_test_plugin.cpp => clips-robot-memory/clips_robot_memory_plugin.cpp} (66%)
 create mode 100644 src/plugins/clips-robot-memory/clips_robot_memory_thread.cpp
 create mode 100644 src/plugins/clips-robot-memory/clips_robot_memory_thread.h


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

- *commit* adeb72b19f66db0c542457bec914552260dd004c - - - - - - - - - -
Author:  Frederik Zwilling <zwilling at kbsg.rwth-aachen.de>
Date:    Fri Aug 26 19:05:37 2016 +0200
Subject: robot-memory: better logging functions

 src/plugins/robot-memory/robot_memory.cpp |  154 +++++++++++++++--------------
 src/plugins/robot-memory/robot_memory.h   |   11 ++-
 2 files changed, 89 insertions(+), 76 deletions(-)

_Diff for modified files_:
diff --git a/src/plugins/robot-memory/robot_memory.cpp b/src/plugins/robot-memory/robot_memory.cpp
index e3024c9..b4f39ff 100644
--- a/src/plugins/robot-memory/robot_memory.cpp
+++ b/src/plugins/robot-memory/robot_memory.cpp
@@ -52,14 +52,18 @@ RobotMemory::~RobotMemory()
 void RobotMemory::init()
 {
   //load config values
-  logger_->log_info(name_, "Started RobotMemory");
-  default_collection_ = "fawkes.msglog";
+  log("Started RobotMemory");
+  default_collection_ = "robmem.test";
   try {
     default_collection_ = config_->get_string("/plugins/robot-memory/default-collection");
   } catch (Exception &e) {}
   try {
     debug_ = config_->get_bool("/plugins/robot-memory/more-debug-output");
   } catch (Exception &e) {}
+  database_name_ = "mobmem";
+  try {
+    database_name_ = config_->get_string("/plugins/robot-memory/database");
+  } catch (Exception &e) {}
 
   //init blackboard interface
   rm_if_ = blackboard_->open_for_writing<RobotMemoryInterface>(config_->get_string("/plugins/robot-memory/interface-name").c_str());
@@ -67,10 +71,7 @@ void RobotMemory::init()
   rm_if_->set_result("");
   rm_if_->write();
 
-  if(debug_)
-  {
-    logger_->log_info(name_, "Initialized RobotMemory");
-  }
+  log_deb("Initialized RobotMemory");
 }
 
 QResCursor RobotMemory::query(std::string query_string, std::string collection)
@@ -79,10 +80,7 @@ QResCursor RobotMemory::query(std::string query_string, std::string collection)
   {
     collection = default_collection_;
   }
-  if(debug_)
-  {
-    logger_->log_info(name_, "Executing Query %s on collection %s", query_string.c_str(), collection);
-  }
+  log_deb(std::string("Executing Query "+ query_string+" on collection "+collection));
 
   //only one query at a time
   MutexLocker lock(mutex_);
@@ -92,11 +90,9 @@ QResCursor RobotMemory::query(std::string query_string, std::string collection)
   try{
     query = Query(query_string);
   } catch (DBException &e) {
-    if(debug_)
-    {
-      logger_->log_error(name_, "Can't parse query_string '%s'\n Exception: %s",
-          query_string.c_str(), e.toString().c_str());
-    }
+    std::string error = "Can't parse query_string "
+        + query_string + "\n Exception: " + e.toString();
+    log_deb(error, "error");
     return NULL;
   }
 
@@ -122,11 +118,9 @@ QResCursor RobotMemory::query(std::string query_string, std::string collection)
   try{
     cursor = mongodb_client_->query(collection, query);
   } catch (DBException &e) {
-    if(debug_)
-    {
-      logger_->log_error(name_, "Error for query %s\n Exception: %s",
-          query_string.c_str(), e.toString().c_str());
-    }
+    std::string error = std::string("Error for query ")
+      + query_string + "\n Exception: " + e.toString();
+    log(error, "error");
     return NULL;
   }
   return cursor;
@@ -138,10 +132,8 @@ int RobotMemory::insert(std::string insert_string, std::string collection)
   {
     collection = default_collection_;
   }
-  if(debug_)
-  {
-    logger_->log_info(name_, "Executing Query %s on collection %s", insert_string.c_str(), collection);
-  }
+
+  log_deb(std::string("Executing Query "+ insert_string + " on collection " + collection));
 
   //only one query at a time
   MutexLocker lock(mutex_);
@@ -151,11 +143,9 @@ int RobotMemory::insert(std::string insert_string, std::string collection)
   try{
     obj = fromjson(insert_string);
   } catch (DBException &e) {
-    if(debug_)
-    {
-      logger_->log_error(name_, "Can't parse insert_string '%s'\n Exception: %s",
-          insert_string.c_str(), e.toString().c_str());
-    }
+    std::string error = "Can't parse insert_string "
+        + insert_string + "\n Exception: " + e.toString();
+    log_deb(error, "error");
     return 0;
   }
 
@@ -165,11 +155,9 @@ int RobotMemory::insert(std::string insert_string, std::string collection)
   try{
     mongodb_client_->insert(collection, obj);
   } catch (DBException &e) {
-    if(debug_)
-    {
-      logger_->log_error(name_, "Error for insert %s\n Exception: %s",
-          insert_string.c_str(), e.toString().c_str());
-    }
+    std::string error = "Error for insert " + insert_string
+        + "\n Exception: " + e.toString();
+    log_deb(error, "error");
     return 0;
   }
   //return success
@@ -183,11 +171,7 @@ int RobotMemory::update(std::string query_string, std::string update_string,
   {
     collection = default_collection_;
   }
-  if(debug_)
-  {
-    logger_->log_info(name_, "Executing Update %s for query %s on collection %s",
-        update_string.c_str(), query_string.c_str(), collection);
-  }
+  log_deb(std::string("Executing Update "+update_string+" for query "+query_string+" on collection "+ collection));
 
   //only one query at a time
   MutexLocker lock(mutex_);
@@ -197,22 +181,16 @@ int RobotMemory::update(std::string query_string, std::string update_string,
   try{
     query = Query(query_string);
   } catch (DBException &e) {
-    if(debug_)
-    {
-      logger_->log_error(name_, "Can't parse query_string '%s'\n Exception: %s",
-          query_string.c_str(), e.toString().c_str());
-    }
+    std::string error = "Can't parse query_string " + query_string
+        + "\n Exception: " + e.toString();
+    log_deb(error, "error");
     return 0;
   }
   BSONObj update;
   try{
     update = fromjson(update_string);
   } catch (DBException &e) {
-    if(debug_)
-    {
-      logger_->log_error(name_, "Can't parse update_string '%s'\n Exception: %s",
-          update_string.c_str(), e.toString().c_str());
-    }
+    log_deb(std::string("Can't parse update_string '"+update_string+"'\n Exception: "+e.toString()),"error");
     return 0;
   }
 
@@ -220,11 +198,7 @@ int RobotMemory::update(std::string query_string, std::string update_string,
   try{
     mongodb_client_->update(collection, query, update);
   } catch (DBException &e) {
-    if(debug_)
-    {
-      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());
-    }
+    log_deb(std::string("Error for update "+update_string+" for query "+query_string+"\n Exception: "+e.toString()), "error");
     return 0;
   }
   //return success
@@ -237,11 +211,7 @@ int RobotMemory::remove(std::string query_string, std::string collection)
   {
     collection = default_collection_;
   }
-  if(debug_)
-  {
-    logger_->log_info(name_, "Executing Remove %s on collection %s",
-        query_string.c_str(), collection);
-  }
+  log_deb(std::string("Executing Remove "+query_string+" on collection "+collection));
 
   //only one query at a time
   MutexLocker lock(mutex_);
@@ -251,11 +221,7 @@ int RobotMemory::remove(std::string query_string, std::string collection)
   try{
     query = Query(query_string);
   } catch (DBException &e) {
-    if(debug_)
-    {
-      logger_->log_error(name_, "Can't parse query_string '%s'\n Exception: %s",
-          query_string.c_str(), e.toString().c_str());
-    }
+    log_deb(std::string("Can't parse query_string '"+query_string+"'\n Exception: "+e.toString()), "error");
     return 0;
   }
 
@@ -263,19 +229,55 @@ int RobotMemory::remove(std::string query_string, std::string collection)
   try{
     mongodb_client_->remove(collection, query);
   } catch (DBException &e) {
-    if(debug_)
-    {
-      logger_->log_error(name_, "Error for query %s\n Exception: %s",
-          query_string.c_str(), e.toString().c_str());
-    }
+    log_deb(std::string("Error for query "+query_string+"\n Exception: "+e.toString()), "error");
     return 0;
   }
   //return success
   return 1;
 }
 
+int RobotMemory::drop_collection(std::string collection)
+{
+  log_deb("Clearing whole robot memory");
+  return remove("{}", collection);
+}
+
+int RobotMemory::clear_memory()
+{
+  log_deb("Clearing whole robot memory");
+  mongodb_client_->dropDatabase(database_name_);
+  return 1;
+}
+
 void
-RobotMemory::log(Query query, std::string what)
+RobotMemory::log(std::string what, std::string info)
+{
+  if(!info.compare("error"))
+      logger_->log_error(name_, "%s", what.c_str());
+  else if(!info.compare("warn"))
+    logger_->log_warn(name_, "%s", what.c_str());
+  else if(!info.compare("debug"))
+    logger_->log_debug(name_, "%s", what.c_str());
+  else
+    logger_->log_info(name_, "%s", what.c_str());
+}
+
+void
+RobotMemory::log_deb(std::string what, std::string level)
+{
+  if(debug_)
+    log(what, level);
+}
+
+void
+RobotMemory::log_deb(Query query, std::string what, std::string level)
+{
+  if(debug_)
+    log(query, what, level);
+}
+
+void
+RobotMemory::log(Query query, std::string what, std::string level)
 {
   std::string output = what
     + "\nFilter: " + query.getFilter().toString()
@@ -283,17 +285,21 @@ RobotMemory::log(Query query, std::string what)
     + "\nSort: " + query.getSort().toString()
     + "\nHint: " + query.getHint().toString()
     + "\nReadPref: " + query.getReadPref().toString();
+  log(output, level);
+}
 
-  logger_->log_info(name_, "%s", output.c_str());
+void
+RobotMemory::log_deb(BSONObj obj, std::string what, std::string level)
+{
+  log(obj, what, level);
 }
 
 void
-RobotMemory::log(BSONObj obj, std::string what)
+RobotMemory::log(BSONObj obj, std::string what, std::string level)
 {
   std::string output = what
     + "\nObject: " + obj.toString();
-
-  logger_->log_info(name_, "%s", output.c_str());
+  log(output, level);
 }
 
 void
diff --git a/src/plugins/robot-memory/robot_memory.h b/src/plugins/robot-memory/robot_memory.h
index 7a095f4..e4efa09 100644
--- a/src/plugins/robot-memory/robot_memory.h
+++ b/src/plugins/robot-memory/robot_memory.h
@@ -56,6 +56,8 @@ class RobotMemory
     int insert(std::string insert, std::string collection = "");
     int update(std::string query, std::string update, std::string collection = "");
     int remove(std::string query, std::string collection = "");
+    int drop_collection(std::string collection);
+    int clear_memory();
 
   private:
     mongo::DBClientBase* mongodb_client_;
@@ -65,6 +67,7 @@ class RobotMemory
     fawkes::BlackBoard* blackboard_;
 
     const char* name_ = "RobotMemory";
+    std::string database_name_;
     std::string default_collection_;
     bool debug_;
     fawkes::Mutex *mutex_;
@@ -72,8 +75,12 @@ class RobotMemory
 
     void init();
 
-    void log(mongo::Query query, std::string what);
-    void log(mongo::BSONObj obj, std::string what);
+    void log(std::string what, std::string level = "info");
+    void log_deb(std::string what, std::string level = "info");
+    void log(mongo::Query query, std::string what, std::string level = "info");
+    void log(mongo::BSONObj obj, std::string what, std::string level = "info");
+    void log_deb(mongo::Query query, std::string what, std::string level = "info");
+    void log_deb(mongo::BSONObj obj, std::string what, std::string level = "info");
 
     void set_fields(mongo::BSONObj &obj, std::string what);
     void set_fields(mongo::Query &q, std::string what);

- *commit* e1c90eeaf19de473dc33bc7f5f365daf4668f82c - - - - - - - - - -
Author:  Frederik Zwilling <zwilling at kbsg.rwth-aachen.de>
Date:    Mon Aug 29 19:02:53 2016 +0200
Subject: robot-memory: dump and restore collections

 src/plugins/robot-memory/robot_memory.cpp          |   96 ++++++++++++++++++++
 src/plugins/robot-memory/robot_memory.h            |    2 +
 .../robot-memory/test-plugin/robot_memory_test.cpp |   21 ++++-
 3 files changed, 118 insertions(+), 1 deletions(-)

_Diff for modified files_:
diff --git a/src/plugins/robot-memory/robot_memory.cpp b/src/plugins/robot-memory/robot_memory.cpp
index b4f39ff..724c1fc 100644
--- a/src/plugins/robot-memory/robot_memory.cpp
+++ b/src/plugins/robot-memory/robot_memory.cpp
@@ -21,8 +21,13 @@
 #include "robot_memory.h"
 #include <core/threading/mutex.h>
 #include <core/threading/mutex_locker.h>
+#include <utils/misc/string_conversions.h>
+#include <utils/misc/string_split.h>
 #include <memory>
 #include <string>
+#include <stdio.h>
+#include <iostream>
+#include <stdlib.h>
 
 // from MongoDB
 #include <mongo/client/dbclient.h>
@@ -249,6 +254,97 @@ int RobotMemory::clear_memory()
   return 1;
 }
 
+int RobotMemory::restore_collection(std::string collection, std::string directory)
+{
+  drop_collection(collection);
+
+  //resolve path to restore
+  if(collection.find(".") == std::string::npos)
+  {
+    log(std::string("Unable to restore collection" + collection), "error");
+    log(std::string("Specify collection like 'db.collection'"), "error");
+    return 0;
+  }
+  std::string path = StringConversions::resolve_path(directory) + "/"
+      + collection.replace(collection.find("."),1,"/") + ".bson";
+  log_deb(std::string("Restore collection " + collection + " from " + path), "warn");
+
+  //call mongorestore from folder with initial restores
+  std::string command = "/usr/bin/mongorestore --dir " + path + " --quiet";
+  log_deb(std::string("Restore command: " + command), "warn");
+  FILE *bash_output = popen(command.c_str(), "r");
+
+  //check if output is ok
+  if(!bash_output)
+  {
+    log(std::string("Unable to restore collection" + collection), "error");
+    return 0;
+  }
+  std::string output_string = "";
+  char buffer[100];
+  while (!feof(bash_output) )
+  {
+    if (fgets(buffer, 100, bash_output) == NULL)
+    {
+      break;
+    }
+    output_string += buffer;
+  }
+  pclose(bash_output);
+  if(output_string.find("Failed") != std::string::npos)
+  {
+    log(std::string("Unable to restore collection" + collection), "error");
+    log_deb(output_string, "error");
+    return 0;
+  }
+  return 1;
+}
+
+int RobotMemory::dump_collection(std::string collection, std::string directory)
+{
+  //resolve path to dump to
+  if(collection.find(".") == std::string::npos)
+  {
+    log(std::string("Unable to dump collection" + collection), "error");
+    log(std::string("Specify collection like 'db.collection'"), "error");
+    return 0;
+  }
+  std::string path = StringConversions::resolve_path(directory);
+  log_deb(std::string("Dump collection " + collection + " into " + path), "warn");
+
+  //call mongorestore from folder with initial restores
+  std::vector<std::string> split = str_split(collection, '.');
+  std::string command = "/usr/bin/mongodump --out=" + path + " --db=" + split[0]
+    + " --collection=" + split[1] + " --quiet";
+  log_deb(std::string("Dump command: " + command), "warn");
+  FILE *bash_output = popen(command.c_str(), "r");
+
+  //check if output is ok
+  if(!bash_output)
+  {
+    log(std::string("Unable to dump collection" + collection), "error");
+    return 0;
+  }
+  std::string output_string = "";
+  char buffer[100];
+  while (!feof(bash_output) )
+  {
+    if (fgets(buffer, 100, bash_output) == NULL)
+    {
+      break;
+    }
+    output_string += buffer;
+  }
+  pclose(bash_output);
+  if(output_string.find("Failed") != std::string::npos)
+  {
+    log(std::string("Unable to dump collection" + collection), "error");
+    log_deb(output_string, "error");
+    return 0;
+  }
+  return 1;
+}
+
 void
 RobotMemory::log(std::string what, std::string info)
 {
diff --git a/src/plugins/robot-memory/robot_memory.h b/src/plugins/robot-memory/robot_memory.h
index e4efa09..0566127 100644
--- a/src/plugins/robot-memory/robot_memory.h
+++ b/src/plugins/robot-memory/robot_memory.h
@@ -58,6 +58,8 @@ class RobotMemory
     int remove(std::string query, std::string collection = "");
     int drop_collection(std::string collection);
     int clear_memory();
+    int restore_collection(std::string collection, std::string directory = "@CONFDIR@/robot-memory");
+    int dump_collection(std::string collection, std::string directory = "@CONFDIR@/robot-memory");
 
   private:
     mongo::DBClientBase* mongodb_client_;
diff --git a/src/plugins/robot-memory/test-plugin/robot_memory_test.cpp b/src/plugins/robot-memory/test-plugin/robot_memory_test.cpp
index 6f8f68e..e9586fb 100644
--- a/src/plugins/robot-memory/test-plugin/robot_memory_test.cpp
+++ b/src/plugins/robot-memory/test-plugin/robot_memory_test.cpp
@@ -129,13 +129,32 @@ TEST_F(RobotMemoryTest, JavaScriptQuery)
   //TODO: implement
 }
 
+TEST_F(RobotMemoryTest, DumpAndResore)
+{
+  ASSERT_TRUE(robot_memory->drop_collection("robmem.test"));
+  ASSERT_TRUE(robot_memory->insert("{'testkey':'value',v:1}"));
+  ASSERT_TRUE(robot_memory->insert("{'testkey':'value',v:2}"));
+  ASSERT_TRUE(robot_memory->insert("{'testkey':'value',v:3}"));
+  ASSERT_TRUE(robot_memory->dump_collection("robmem.test"));
+  ASSERT_TRUE(robot_memory->drop_collection("robmem.test"));
+  ASSERT_TRUE(robot_memory->restore_collection("robmem.test"));
+  QResCursor qres = robot_memory->query("{'testkey':'value'}");
+  ASSERT_TRUE(qres->more());
+  ASSERT_TRUE(contains_pairs(qres->next(), fromjson("{'testkey':'value',v:1}")));
+  ASSERT_TRUE(qres->more());
+  ASSERT_TRUE(contains_pairs(qres->next(), fromjson("{'testkey':'value',v:2}")));
+  ASSERT_TRUE(qres->more());
+  ASSERT_TRUE(contains_pairs(qres->next(), fromjson("{'testkey':'value',v:3}")));
+  ASSERT_FALSE(qres->more());
+
+}
 
 ::testing::AssertionResult RobotMemoryTest::contains_pairs(BSONObj obj, BSONObj exp)
 {
   for(BSONObjIterator it = exp.begin(); it.more();)
   {
     BSONElement kvpair = it.next();
-    printf("checking %s\n", kvpair.toString().c_str());
+    //printf("checking %s\n", kvpair.toString().c_str());
     if(!obj.hasElement(kvpair.fieldName())
         || obj.getField(kvpair.fieldName()) != kvpair)
     {

- *commit* 9b95b13198e94447548ac95ca82e6a7fed5dbd96 - - - - - - - - - -
Author:  Frederik Zwilling <zwilling at kbsg.rwth-aachen.de>
Date:    Mon Aug 29 19:44:31 2016 +0200
Subject: robot-memory: use BSONObj and Query in API

 src/plugins/robot-memory/robot_memory.cpp          |   89 +++++---------------
 src/plugins/robot-memory/robot_memory.h            |   10 ++-
 .../robot-memory/test-plugin/robot_memory_test.cpp |   21 ++---
 3 files changed, 37 insertions(+), 83 deletions(-)

_Diff for modified files_:
diff --git a/src/plugins/robot-memory/robot_memory.cpp b/src/plugins/robot-memory/robot_memory.cpp
index 724c1fc..d273ce2 100644
--- a/src/plugins/robot-memory/robot_memory.cpp
+++ b/src/plugins/robot-memory/robot_memory.cpp
@@ -79,28 +79,15 @@ void RobotMemory::init()
   log_deb("Initialized RobotMemory");
 }
 
-QResCursor RobotMemory::query(std::string query_string, std::string collection)
+QResCursor RobotMemory::query(Query query, std::string collection)
 {
   if(collection == "")
-  {
     collection = default_collection_;
-  }
-  log_deb(std::string("Executing Query "+ query_string+" on collection "+collection));
+  log_deb(std::string("Executing Query "+ query.toString() +" on collection "+collection));
 
   //only one query at a time
   MutexLocker lock(mutex_);
 
-  //get query from string
-  Query query;
-  try{
-    query = Query(query_string);
-  } catch (DBException &e) {
-    std::string error = "Can't parse query_string "
-        + query_string + "\n Exception: " + e.toString();
-    log_deb(error, "error");
-    return NULL;
-  }
-
 //  //introspect query
 //  log(query, "executing query:");
 
@@ -124,43 +111,30 @@ QResCursor RobotMemory::query(std::string query_string, std::string collection)
     cursor = mongodb_client_->query(collection, query);
   } catch (DBException &e) {
     std::string error = std::string("Error for query ")
-      + query_string + "\n Exception: " + e.toString();
+      + query.toString() + "\n Exception: " + e.toString();
     log(error, "error");
     return NULL;
   }
   return cursor;
 }
 
-int RobotMemory::insert(std::string insert_string, std::string collection)
+int RobotMemory::insert(BSONObj obj, std::string collection)
 {
   if(collection == "")
-  {
     collection = default_collection_;
-  }
 
-  log_deb(std::string("Executing Query "+ insert_string + " on collection " + collection));
+  log_deb(std::string("Executing Query "+ obj.toString() + " on collection " + collection));
 
   //only one query at a time
   MutexLocker lock(mutex_);
 
-  //get query from string
-  BSONObj obj;
-  try{
-    obj = fromjson(insert_string);
-  } catch (DBException &e) {
-    std::string error = "Can't parse insert_string "
-        + insert_string + "\n Exception: " + e.toString();
-    log_deb(error, "error");
-    return 0;
-  }
-
   set_fields(obj, "{type: \"test\"}");
 
   //actually execute insert
   try{
     mongodb_client_->insert(collection, obj);
   } catch (DBException &e) {
-    std::string error = "Error for insert " + insert_string
+    std::string error = "Error for insert " + obj.toString()
         + "\n Exception: " + e.toString();
     log_deb(error, "error");
     return 0;
@@ -169,72 +143,54 @@ int RobotMemory::insert(std::string insert_string, std::string collection)
   return 1;
 }
 
-int RobotMemory::update(std::string query_string, std::string update_string,
-                                    std::string collection)
+int RobotMemory::insert(std::string obj_str, std::string collection)
+{
+  return insert(fromjson(obj_str), collection);
+}
+
+int RobotMemory::update(Query query, BSONObj update, std::string collection)
 {
   if(collection == "")
   {
     collection = default_collection_;
   }
-  log_deb(std::string("Executing Update "+update_string+" for query "+query_string+" on collection "+ collection));
+  log_deb(std::string("Executing Update "+update.toString()+" for query "+query.toString()+" on collection "+ collection));
 
   //only one query at a time
   MutexLocker lock(mutex_);
 
-  //get query from string
-  Query query;
-  try{
-    query = Query(query_string);
-  } catch (DBException &e) {
-    std::string error = "Can't parse query_string " + query_string
-        + "\n Exception: " + e.toString();
-    log_deb(error, "error");
-    return 0;
-  }
-  BSONObj update;
-  try{
-    update = fromjson(update_string);
-  } catch (DBException &e) {
-    log_deb(std::string("Can't parse update_string '"+update_string+"'\n Exception: "+e.toString()),"error");
-    return 0;
-  }
-
   //actually execute update
   try{
     mongodb_client_->update(collection, query, update);
   } catch (DBException &e) {
-    log_deb(std::string("Error for update "+update_string+" for query "+query_string+"\n Exception: "+e.toString()), "error");
+    log_deb(std::string("Error for update "+update.toString()+" for query "+query.toString()+"\n Exception: "+e.toString()), "error");
     return 0;
   }
   //return success
   return 1;
 }
 
-int RobotMemory::remove(std::string query_string, std::string collection)
+int RobotMemory::update(Query query, std::string update_str, std::string collection)
+{
+  return update(query, fromjson(update_str), collection);
+}
+
+int RobotMemory::remove(Query query, std::string collection)
 {
   if(collection == "")
   {
     collection = default_collection_;
   }
-  log_deb(std::string("Executing Remove "+query_string+" on collection "+collection));
+  log_deb(std::string("Executing Remove "+query.toString()+" on collection "+collection));
 
   //only one query at a time
   MutexLocker lock(mutex_);
 
-  //get query from string
-  Query query;
-  try{
-    query = Query(query_string);
-  } catch (DBException &e) {
-    log_deb(std::string("Can't parse query_string '"+query_string+"'\n Exception: "+e.toString()), "error");
-    return 0;
-  }
-
   //actually execute remove
   try{
     mongodb_client_->remove(collection, query);
   } catch (DBException &e) {
-    log_deb(std::string("Error for query "+query_string+"\n Exception: "+e.toString()), "error");
+    log_deb(std::string("Error for query "+query.toString()+"\n Exception: "+e.toString()), "error");
     return 0;
   }
   //return success
@@ -442,4 +398,3 @@ RobotMemory::remove_field(Query &q, std::string what)
   //override
   q = Query(b.obj());
 }
-
diff --git a/src/plugins/robot-memory/robot_memory.h b/src/plugins/robot-memory/robot_memory.h
index 0566127..58c17f7 100644
--- a/src/plugins/robot-memory/robot_memory.h
+++ b/src/plugins/robot-memory/robot_memory.h
@@ -52,10 +52,12 @@ class RobotMemory
     virtual ~RobotMemory();
 
     //robot memory functions
-    QResCursor query(std::string query, std::string collection = "");
-    int insert(std::string insert, std::string collection = "");
-    int update(std::string query, std::string update, std::string collection = "");
-    int remove(std::string query, std::string collection = "");
+    QResCursor query(mongo::Query query, std::string collection = "");
+    int insert(mongo::BSONObj obj, std::string collection = "");
+    int insert(std::string obj_str, std::string collection = "");
+    int update(mongo::Query query, mongo::BSONObj update, std::string collection = "");
+    int update(mongo::Query query, std::string update_str, std::string collection = "");
+    int remove(mongo::Query query, std::string collection = "");
     int drop_collection(std::string collection);
     int clear_memory();
     int restore_collection(std::string collection, std::string directory = "@CONFDIR@/robot-memory");
diff --git a/src/plugins/robot-memory/test-plugin/robot_memory_test.cpp b/src/plugins/robot-memory/test-plugin/robot_memory_test.cpp
index e9586fb..1e35e04 100644
--- a/src/plugins/robot-memory/test-plugin/robot_memory_test.cpp
+++ b/src/plugins/robot-memory/test-plugin/robot_memory_test.cpp
@@ -87,31 +87,28 @@ TEST_F(RobotMemoryTest, StoreRemoveQuery)
   ASSERT_FALSE(qres->more());
 }
 
-TEST_F(RobotMemoryTest, QueryInvalidCaught)
+TEST_F(RobotMemoryTest, QueryInvalid)
 {
-  ASSERT_FALSE(robot_memory->query("{key-:+'not existing'}"));
+  ASSERT_THROW(robot_memory->query("{key-:+'not existing'}"), mongo::DBException);
 }
 
 TEST_F(RobotMemoryTest, InsertInvalidCaught)
 {
-  ASSERT_NO_THROW(robot_memory->insert("{'testkey'::'value'}"));
-  ASSERT_FALSE(robot_memory->insert("warbagarbl"));
+  ASSERT_THROW(robot_memory->insert("{'testkey'::'value'}"), mongo::DBException);
+  ASSERT_THROW(robot_memory->insert("warbagarbl"), mongo::DBException);
 }
 
 TEST_F(RobotMemoryTest, UpdateInvalidCaught)
 {
-  ASSERT_NO_THROW(robot_memory->update("{'testkey'::'value'}",
-      "{'key':'update'}"));
-  ASSERT_NO_THROW(robot_memory->update("{'testkey':'good'}",
-      "{'bad':1.2.3}"));
-  ASSERT_FALSE(robot_memory->update("{([})]", "{'key':4}"));
-  ASSERT_FALSE(robot_memory->update("{'key':'valid'}", "invalid!"));
+  ASSERT_THROW(robot_memory->update("{'testkey':'good'}",
+      "{'bad':1.2.3}"), mongo::DBException);
+  ASSERT_THROW(robot_memory->update("{([})]", "{'key':4}"), mongo::DBException);
 }
 
 TEST_F(RobotMemoryTest, RemoveInvalidCaught)
 {
-  ASSERT_NO_THROW(robot_memory->remove("{____:4.56}"));
-  ASSERT_FALSE(robot_memory->remove("{([})]"));
+  ASSERT_THROW(robot_memory->remove("{____:4.56!}"), mongo::DBException);
+  ASSERT_THROW(robot_memory->remove("{([})]"), mongo::DBException);
 }
 
 TEST_F(RobotMemoryTest, AggregationQuery)

- *commit* b7c0f54f638b28e485729f3460b0678e45905195 - - - - - - - - - -
Author:  Frederik Zwilling <zwilling at kbsg.rwth-aachen.de>
Date:    Mon Aug 29 20:27:26 2016 +0200
Subject: clips-robot-memory: start for CLIPS feature from refbox clips-mongodb

 src/plugins/Makefile                               |    2 +-
 src/plugins/clips-robot-memory/Makefile            |   60 ++
 .../clips_robot_memory_plugin.cpp}                 |   20 +-
 .../clips_robot_memory_thread.cpp                  |  680 ++++++++++++++++++++
 .../clips-robot-memory/clips_robot_memory_thread.h |   95 +++
 5 files changed, 846 insertions(+), 11 deletions(-)

_Diff for modified files_:
diff --git a/src/plugins/Makefile b/src/plugins/Makefile
index bfad9d7..e400377 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 robot-memory\
+	  mongodb mongodb_log robot-memory clips-robot-memory \
 	  openni refboxcomm ros player xmlrpc gossip \
 	  robot_state_publisher gazebo dynamixel navgraph-interactive
 




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


More information about the fawkes-commits mailing list