Fawkes API  Fawkes Development Version
clips_robot_memory_thread.h
1 
2 /***************************************************************************
3  * clips_robot_memory_thread.h - CLIPS feature for accessing the robot memory
4  *
5  * Plugin created: Mon Aug 29 15:41:47 2016
6 
7  * Copyright 2016 Frederik Zwilling
8  * 2013 Tim Niemueller [www.niemueller.de]
9  *
10  ****************************************************************************/
11 
12 /* This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU Library General Public License for more details.
21  *
22  * Read the full text in the LICENSE.GPL file in the doc directory.
23  */
24 
25 #ifndef _PLUGINS_CLIPS_ROBOT_MEMORYTHREAD_H_
26 #define _PLUGINS_CLIPS_ROBOT_MEMORYTHREAD_H_
27 
28 #include "clips_rm_trigger.h"
29 
30 #include <aspect/configurable.h>
31 #include <aspect/logging.h>
32 #include <core/threading/thread.h>
33 #include <plugins/clips/aspect/clips_feature.h>
34 #include <plugins/robot-memory/aspect/robot_memory_aspect.h>
35 
36 #include <clipsmm.h>
37 #include <future>
38 #include <string>
39 
40 namespace fawkes {
41 }
42 
44  public fawkes::LoggingAspect,
46  public fawkes::CLIPSFeature,
49 {
50 public:
52 
53  virtual void init();
54  virtual void finalize();
55  virtual void loop();
56 
57  // for CLIPSFeature
58  virtual void clips_context_init(const std::string & env_name,
60  virtual void clips_context_destroyed(const std::string &env_name);
61 
62  /** Stub to see name in backtrace for easier debugging. @see Thread::run() */
63 protected:
64  virtual void
65  run()
66  {
67  Thread::run();
68  }
69 
70 private:
71  std::map<std::string, fawkes::LockPtr<CLIPS::Environment>> envs_;
72 
73  CLIPS::Value clips_bson_create();
74  CLIPS::Value clips_bson_parse(std::string document);
75  void clips_bson_destroy(void *bson);
76  void clips_bson_append(void *bson, std::string field_name, CLIPS::Value value);
77  void clips_bson_append_array(void *bson, std::string field_name, CLIPS::Values values);
78  void clips_bson_append_time(void *bson, std::string field_name, CLIPS::Values time);
79  CLIPS::Value clips_bson_array_start(void *bson, std::string field_name);
80  void clips_bson_array_finish(void *barr);
81  void clips_bson_array_append(void *barr, CLIPS::Value value);
82  std::string clips_bson_tostring(void *bson);
83  CLIPS::Values clips_bson_field_names(void *bson);
84  CLIPS::Value clips_bson_has_field(void *bson, std::string field_name);
85  CLIPS::Value clips_bson_get(void *bson, std::string field_name);
86  CLIPS::Values clips_bson_get_array(void *bson, std::string field_name);
87  CLIPS::Values clips_bson_get_time(void *bson, std::string field_name);
88 
89  void clips_robotmemory_upsert(std::string collection, void *bson, CLIPS::Value query);
90  void clips_robotmemory_update(std::string collection, void *bson, CLIPS::Value query);
91  void clips_robotmemory_replace(std::string collection, void *bson, CLIPS::Value query);
92  void clips_robotmemory_insert(std::string collection, void *bson);
93  void clips_robotmemory_create_index(std::string collection, void *bson);
94  void clips_robotmemory_create_unique_index(std::string collection, void *bson);
95  void
96  robotmemory_update(std::string &collection, mongo::BSONObj obj, CLIPS::Value &query, bool upsert);
97  CLIPS::Value clips_robotmemory_query_sort(std::string collection, void *bson, void *bson_sort);
98  CLIPS::Value clips_robotmemory_query(const std::string &collection, void *bson);
99  void clips_robotmemory_remove(std::string collection, void *bson);
100  CLIPS::Value clips_robotmemory_cursor_more(void *cursor);
101  CLIPS::Value clips_robotmemory_cursor_next(void *cursor);
102  void clips_robotmemory_cursor_destroy(void *cursor);
103 
104  CLIPS::Value clips_robotmemory_mutex_create(std::string name);
105  CLIPS::Value clips_robotmemory_mutex_destroy(std::string name);
106  CLIPS::Value clips_robotmemory_mutex_try_lock(std::string name, std::string identity);
107  CLIPS::Value clips_robotmemory_mutex_renew_lock(std::string name, std::string identity);
108  CLIPS::Value clips_robotmemory_mutex_force_lock(std::string name, std::string identity);
109  CLIPS::Value clips_robotmemory_mutex_unlock(std::string name, std::string identity);
110  CLIPS::Value clips_robotmemory_mutex_setup_ttl(float max_age_sec);
111  CLIPS::Value clips_robotmemory_mutex_expire_locks(float max_age_sec);
112 
113  CLIPS::Values clips_robotmemory_mutex_create_async(std::string name);
114  CLIPS::Values clips_robotmemory_mutex_destroy_async(std::string name);
115  CLIPS::Values clips_robotmemory_mutex_try_lock_async(std::string env_name,
116  std::string name,
117  std::string identity);
118  CLIPS::Values clips_robotmemory_mutex_renew_lock_async(std::string env_name,
119  std::string name,
120  std::string identity);
121  CLIPS::Values clips_robotmemory_mutex_force_lock_async(std::string name, std::string identity);
122  CLIPS::Values clips_robotmemory_mutex_unlock_async(std::string name, std::string identity);
123  CLIPS::Value clips_robotmemory_mutex_expire_locks_async(std::string env_name, float max_age_sec);
124 
125  CLIPS::Value clips_robotmemory_register_trigger(std::string env_name,
126  std::string collection,
127  void * query,
128  std::string assert_name);
129  void clips_robotmemory_destroy_trigger(void *trigger);
130 
131  bool mutex_future_ready(const std::string &name);
132 
133 private:
134  std::list<ClipsRmTrigger *> clips_triggers_;
135  std::map<std::string, std::future<bool>> mutex_futures_;
136  std::future<bool> mutex_expire_future_;
137 };
138 
139 #endif
Thread aspect to provide a feature to CLIPS environments.
Definition: clips_feature.h:57
Thread aspect to get access to a the RobotMemory.
virtual void finalize()
Finalize the thread.
Fawkes library namespace.
virtual void loop()
Code to execute in the thread.
Thread class encapsulation of pthreads.
Definition: thread.h:45
CLIPS feature to access the robot memory.
virtual void clips_context_init(const std::string &env_name, fawkes::LockPtr< CLIPS::Environment > &clips)
Initialize a CLIPS context to use the provided feature.
virtual void run()
Stub to see name in backtrace for easier debugging.
CLIPS feature maintainer.
Definition: clips_feature.h:41
const char * name() const
Get name of thread.
Definition: thread.h:100
Thread aspect to log output.
Definition: logging.h:32
Thread aspect to access configuration data.
Definition: configurable.h:32
virtual void clips_context_destroyed(const std::string &env_name)
Notification that a CLIPS environment has been destroyed.
virtual void init()
Initialize the thread.