BehaviorTree
Core Library to create and execute Behavior Trees
Loading...
Searching...
No Matches
bt_sqlite_logger.h
1#pragma once
2
3#include "behaviortree_cpp/loggers/abstract_logger.h"
4
5#include <filesystem>
6
7// forward declaration
8struct sqlite3;
9
10namespace BT
11{
12
13/** SQL schema
14 *
15 * CREATE TABLE IF NOT EXISTS Definitions (
16 * session_id INTEGER PRIMARY KEY AUTOINCREMENT,
17 * date TEXT NOT NULL,
18 * xml_tree TEXT NOT NULL);
19 *
20 * CREATE TABLE IF NOT EXISTS Nodes ("
21 * session_id INTEGER NOT NULL,
22 * fullpath VARCHAR, "
23 * node_uid INTEGER NOT NULL );
24 *
25 * CREATE TABLE IF NOT EXISTS Transitions (
26 * timestamp INTEGER PRIMARY KEY NOT NULL,
27 * session_id INTEGER NOT NULL,
28 * node_uid INTEGER NOT NULL,
29 * duration INTEGER,
30 * state INTEGER NOT NULL,
31 * extra_data VARCHAR );
32 *
33 */
34
35/**
36 * @brief The SqliteLogger is a logger that will store the tree and all the
37 * status transitions in a SQLite database (single file).
38 *
39 * You can append data to the same file; this allows you to store multiple experiments into the database.
40 * Yn that case, each recording has a unique session_id.
41 *
42 * This is primarily meant to be used with Groot2, but the content of
43 * the tables is sufficiently self-explaining, and you can create
44 * your own tools to extract the information.
45 */
47{
48public:
49 /**
50 * @brief To correctly read this log with Groot2, you must use the suffix ".db3".
51 * Constructor will throw otherwise.
52 *
53 * @param tree the tree to log
54 * @param filepath path of the file where info will be stored
55 * @param append if true, add this recording to the database
56 */
57 SqliteLogger(const Tree& tree, std::filesystem::path const& file, bool append = false);
58
59 ~SqliteLogger() override;
60
61 SqliteLogger(const SqliteLogger&) = delete;
62 SqliteLogger& operator=(const SqliteLogger&) = delete;
63 SqliteLogger(SqliteLogger&&) = delete;
64 SqliteLogger& operator=(SqliteLogger&&) = delete;
65
66 // You can inject a function that add a string to the Transitions table,
67 // in the column "extra_data".
68 // The arguments of the function are the same as SqliteLogger::callback()
69 using ExtraCallback =
70 std::function<std::string(Duration, const TreeNode&, NodeStatus, NodeStatus)>;
71 void setAdditionalCallback(ExtraCallback func);
72
73 virtual void callback(Duration timestamp, const TreeNode& node, NodeStatus prev_status,
74 NodeStatus status) override;
75
76 void execSqlStatement(std::string statement);
77
78 virtual void flush() override;
79
80private:
81 sqlite3* db_ = nullptr;
82
83 int64_t monotonic_timestamp_ = 0;
84 std::unordered_map<const BT::TreeNode*, int64_t> starting_time_;
85
86 int session_id_ = -1;
87
88 struct Transition
89 {
90 uint16_t node_uid;
91 int64_t timestamp;
92 int64_t duration;
93 NodeStatus status;
94 std::string extra_data;
95 };
96
97 std::deque<Transition> transitions_queue_;
98 std::condition_variable queue_cv_;
99 std::mutex queue_mutex_;
100
101 std::thread writer_thread_;
102 std::atomic_bool loop_ = true;
103 std::atomic_bool writer_ready_ = false;
104
105 ExtraCallback extra_func_;
106
107 void writerLoop();
108};
109
110} // namespace BT
The SqliteLogger is a logger that will store the tree and all the status transitions in a SQLite data...
Definition: bt_sqlite_logger.h:47
SqliteLogger(const Tree &tree, std::filesystem::path const &file, bool append=false)
To correctly read this log with Groot2, you must use the suffix ".db3". Constructor will throw otherw...
Definition: abstract_logger.h:16
Struct used to store a tree. If this object goes out of scope, the tree is destroyed.
Definition: bt_factory.h:96
Abstract base class for Behavior Tree Nodes.
Definition: tree_node.h:154
Definition: action_node.h:24
NodeStatus
Definition: basic_types.h:34