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
41namespace fawkes {
42}
43
50{
51public:
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() */
64protected:
65 virtual void
67 {
68 Thread::run();
69 }
70
71private:
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();
82 void clips_bson_array_finish(void *bson, std::string field_name, void *array);
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 CLIPS::Value clips_robotmemory_dump_collection(std::string collection, std::string directory);
108 CLIPS::Value clips_robotmemory_restore_collection(std::string collection,
109 std::string directory,
110 std::string target_collection);
111
112 CLIPS::Value clips_robotmemory_mutex_create(std::string name);
113 CLIPS::Value clips_robotmemory_mutex_destroy(std::string name);
114 CLIPS::Value clips_robotmemory_mutex_try_lock(std::string name, std::string identity);
115 CLIPS::Value clips_robotmemory_mutex_renew_lock(std::string name, std::string identity);
116 CLIPS::Value clips_robotmemory_mutex_force_lock(std::string name, std::string identity);
117 CLIPS::Value clips_robotmemory_mutex_unlock(std::string name, std::string identity);
118 CLIPS::Value clips_robotmemory_mutex_setup_ttl(float max_age_sec);
119 CLIPS::Value clips_robotmemory_mutex_expire_locks(float max_age_sec);
120
121 CLIPS::Values clips_robotmemory_mutex_create_async(std::string name);
122 CLIPS::Values clips_robotmemory_mutex_destroy_async(std::string name);
123 CLIPS::Values clips_robotmemory_mutex_try_lock_async(std::string env_name,
124 std::string name,
125 std::string identity);
126 CLIPS::Values clips_robotmemory_mutex_renew_lock_async(std::string env_name,
127 std::string name,
128 std::string identity);
129 CLIPS::Values clips_robotmemory_mutex_force_lock_async(std::string name, std::string identity);
130 CLIPS::Values clips_robotmemory_mutex_unlock_async(std::string name, std::string identity);
131 CLIPS::Value clips_robotmemory_mutex_expire_locks_async(std::string env_name, float max_age_sec);
132
133 CLIPS::Value clips_robotmemory_register_trigger(std::string env_name,
134 std::string collection,
135 void * query,
136 std::string assert_name);
137 void clips_robotmemory_destroy_trigger(void *trigger);
138
139 bool mutex_future_ready(const std::string &name);
140
141private:
142 std::list<ClipsRmTrigger *> clips_triggers_;
143 fawkes::Mutex clips_triggers_mutex_;
144 std::map<std::string, std::future<bool>> mutex_futures_;
145 std::future<bool> mutex_expire_future_;
146};
147
148#endif
CLIPS feature to access the robot memory.
virtual void loop()
Code to execute in the thread.
virtual void finalize()
Finalize the thread.
virtual void clips_context_destroyed(const std::string &env_name)
Notification that a CLIPS environment has been destroyed.
virtual void init()
Initialize the thread.
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.
Thread aspect to provide a feature to CLIPS environments.
Definition: clips_feature.h:58
CLIPS feature maintainer.
Definition: clips_feature.h:42
Thread aspect to access configuration data.
Definition: configurable.h:33
Thread aspect to log output.
Definition: logging.h:33
Mutex mutual exclusion lock.
Definition: mutex.h:33
Thread aspect to get access to a the RobotMemory.
Thread class encapsulation of pthreads.
Definition: thread.h:46
const char * name() const
Get name of thread.
Definition: thread.h:100
Fawkes library namespace.