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 <bsoncxx/document/view.hpp>
37 #include <clipsmm.h>
38 #include <future>
39 #include <string>
40 
41 namespace fawkes {
42 }
43 
45  public fawkes::LoggingAspect,
47  public fawkes::CLIPSFeature,
50 {
51 public:
53 
54  virtual void init();
55  virtual void finalize();
56  virtual void loop();
57 
58  // for CLIPSFeature
59  virtual void clips_context_init(const std::string & env_name,
61  virtual void clips_context_destroyed(const std::string &env_name);
62 
63  /** Stub to see name in backtrace for easier debugging. @see Thread::run() */
64 protected:
65  virtual void
66  run()
67  {
68  Thread::run();
69  }
70 
71 private:
72  std::map<std::string, fawkes::LockPtr<CLIPS::Environment>> envs_;
73 
74  CLIPS::Value clips_bson_create();
75  CLIPS::Value clips_bson_parse(std::string document);
76  void clips_bson_destroy(void *bson);
77  void clips_bson_append(void *bson, std::string field_name, CLIPS::Value value);
78  void clips_bson_append_regex(void *bson, std::string field_name, CLIPS::Value regex_string);
79  void clips_bson_append_array(void *bson, std::string field_name, CLIPS::Values values);
80  void clips_bson_append_time(void *bson, std::string field_name, CLIPS::Values time);
81  CLIPS::Value clips_bson_array_start(void *bson, std::string field_name);
82  void clips_bson_array_finish(void *barr);
83  void clips_bson_array_append(void *barr, CLIPS::Value value);
84  std::string clips_bson_tostring(void *bson);
85  CLIPS::Values clips_bson_field_names(void *bson);
86  CLIPS::Value clips_bson_has_field(void *bson, std::string field_name);
87  CLIPS::Value clips_bson_get(void *bson, std::string field_name);
88  CLIPS::Values clips_bson_get_array(void *bson, std::string field_name);
89  CLIPS::Values clips_bson_get_time(void *bson, std::string field_name);
90 
91  void clips_robotmemory_upsert(std::string collection, void *bson, CLIPS::Value query);
92  void clips_robotmemory_update(std::string collection, void *bson, CLIPS::Value query);
93  void clips_robotmemory_replace(std::string collection, void *bson, CLIPS::Value query);
94  void clips_robotmemory_insert(std::string collection, void *bson);
95  void clips_robotmemory_create_index(std::string collection, void *bson);
96  void clips_robotmemory_create_unique_index(std::string collection, void *bson);
97  void robotmemory_update(std::string & collection,
98  const bsoncxx::document::view &obj,
99  CLIPS::Value & query,
100  bool upsert);
101  CLIPS::Value clips_robotmemory_query_sort(std::string collection, void *bson, void *bson_sort);
102  CLIPS::Value clips_robotmemory_query(const std::string &collection, void *bson);
103  void clips_robotmemory_remove(std::string collection, void *bson);
104  CLIPS::Value clips_robotmemory_cursor_more(void *cursor);
105  CLIPS::Value clips_robotmemory_cursor_next(void *cursor);
106  void clips_robotmemory_cursor_destroy(void *cursor);
107 
108  CLIPS::Value clips_robotmemory_mutex_create(std::string name);
109  CLIPS::Value clips_robotmemory_mutex_destroy(std::string name);
110  CLIPS::Value clips_robotmemory_mutex_try_lock(std::string name, std::string identity);
111  CLIPS::Value clips_robotmemory_mutex_renew_lock(std::string name, std::string identity);
112  CLIPS::Value clips_robotmemory_mutex_force_lock(std::string name, std::string identity);
113  CLIPS::Value clips_robotmemory_mutex_unlock(std::string name, std::string identity);
114  CLIPS::Value clips_robotmemory_mutex_setup_ttl(float max_age_sec);
115  CLIPS::Value clips_robotmemory_mutex_expire_locks(float max_age_sec);
116 
117  CLIPS::Values clips_robotmemory_mutex_create_async(std::string name);
118  CLIPS::Values clips_robotmemory_mutex_destroy_async(std::string name);
119  CLIPS::Values clips_robotmemory_mutex_try_lock_async(std::string env_name,
120  std::string name,
121  std::string identity);
122  CLIPS::Values clips_robotmemory_mutex_renew_lock_async(std::string env_name,
123  std::string name,
124  std::string identity);
125  CLIPS::Values clips_robotmemory_mutex_force_lock_async(std::string name, std::string identity);
126  CLIPS::Values clips_robotmemory_mutex_unlock_async(std::string name, std::string identity);
127  CLIPS::Value clips_robotmemory_mutex_expire_locks_async(std::string env_name, float max_age_sec);
128 
129  CLIPS::Value clips_robotmemory_register_trigger(std::string env_name,
130  std::string collection,
131  void * query,
132  std::string assert_name);
133  void clips_robotmemory_destroy_trigger(void *trigger);
134 
135  bool mutex_future_ready(const std::string &name);
136 
137 private:
138  std::list<ClipsRmTrigger *> clips_triggers_;
139  fawkes::Mutex clips_triggers_mutex_;
140  std::map<std::string, std::future<bool>> mutex_futures_;
141  std::future<bool> mutex_expire_future_;
142 };
143 
144 #endif
fawkes::LockPtr< CLIPS::Environment >
fawkes::Mutex
Definition: mutex.h:36
ClipsRobotMemoryThread::finalize
virtual void finalize()
Finalize the thread.
Definition: clips_robot_memory_thread.cpp:59
ClipsRobotMemoryThread::loop
virtual void loop()
Code to execute in the thread.
Definition: clips_robot_memory_thread.cpp:54
ClipsRobotMemoryThread::clips_context_init
virtual void clips_context_init(const std::string &env_name, fawkes::LockPtr< CLIPS::Environment > &clips)
Definition: clips_robot_memory_thread.cpp:68
fawkes::Thread::name
const char * name() const
Definition: thread.h:99
ClipsRobotMemoryThread::init
virtual void init()
Initialize the thread.
Definition: clips_robot_memory_thread.cpp:49
fawkes
fawkes::LoggingAspect
Definition: logging.h:36
ClipsRobotMemoryThread::clips_context_destroyed
virtual void clips_context_destroyed(const std::string &env_name)
Definition: clips_robot_memory_thread.cpp:243
fawkes::CLIPSFeature
Definition: clips_feature.h:40
fawkes::Thread
Definition: thread.h:44
ClipsRobotMemoryThread::run
virtual void run()
Stub to see name in backtrace for easier debugging.
Definition: clips_robot_memory_thread.h:65
ClipsRobotMemoryThread
Definition: clips_robot_memory_thread.h:43
fawkes::ConfigurableAspect
Definition: configurable.h:36
fawkes::RobotMemoryAspect
Definition: robot_memory_aspect.h:37
fawkes::CLIPSFeatureAspect
Definition: clips_feature.h:56