Skip to content

Commit e5c256e

Browse files
committed
clean up sqlite
1 parent 0ce11ef commit e5c256e

File tree

4 files changed

+40
-63
lines changed

4 files changed

+40
-63
lines changed

examples/ex08_sqlite_log.cpp

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -111,46 +111,8 @@ int main()
111111
BT::SqliteLogger sqlite_logger(tree, "ex08_sqlitelog.db3", false);
112112

113113
//------------------------------------------------------------------------
114-
// Approach ONE: create additional tables.
115-
// This requires you to execute a "CREATE TABLE IF NOT EXISTS..." statement
116-
// at the beginning and then an "INSERT INTO TaskB VALUES .." statement
117-
// in a callback injected using sqlite_logger.setAdditionalCallback()
118-
119-
sqlite_logger.execSqlStatement("CREATE TABLE IF NOT EXISTS TaskA ("
120-
"name VARCHAR, "
121-
"type INTEGER );");
122-
123-
sqlite_logger.execSqlStatement("CREATE TABLE IF NOT EXISTS TaskB ("
124-
"name VARCHAR, "
125-
"value REAL );");
126-
127-
auto extra_callback = [&](BT::Duration timestamp, const BT::TreeNode& node,
128-
BT::NodeStatus prev_status, BT::NodeStatus status) -> void {
129-
if(prev_status == BT::NodeStatus::RUNNING && BT::isStatusCompleted(status))
130-
{
131-
if(node.name() == "ExecuteTaskA")
132-
{
133-
auto task = node.config().blackboard->get<Command>("task");
134-
auto taskA = std::get<TaskA>(task);
135-
auto str = BT::StrCat("INSERT INTO TaskA VALUES ", "('", taskA.name, "',",
136-
std::to_string(taskA.type), ");");
137-
sqlite_logger.execSqlStatement(str);
138-
}
139-
if(node.name() == "ExecuteTaskB")
140-
{
141-
auto task = node.config().blackboard->get<Command>("task");
142-
auto taskB = std::get<TaskB>(task);
143-
auto str = BT::StrCat("INSERT INTO TaskB VALUES ", "('", taskB.name, "',",
144-
std::to_string(taskB.value), ");");
145-
sqlite_logger.execSqlStatement(str);
146-
}
147-
}
148-
};
149-
sqlite_logger.setAdditionalCallback(extra_callback);
150-
151-
//------------------------------------------------------------------------
152-
// Approach TWO: serialize data into the extra column "metadata"
153-
// We will use JSON serialization
114+
// Write some data (from the blackboard) and write into the
115+
// extra column called "extra_data". We will use JSON serialization
154116

155117
auto meta_callback = [&](BT::Duration timestamp, const BT::TreeNode& node,
156118
BT::NodeStatus prev_status,
@@ -176,7 +138,7 @@ int main()
176138
}
177139
return {};
178140
};
179-
sqlite_logger.setMetadataCallback(meta_callback);
141+
sqlite_logger.setAdditionalCallback(meta_callback);
180142
//------------------------------------------------------------------------
181143
while(1)
182144
{

examples/t12_groot_howto.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ int main()
9898
// Groot2 editor requires a model of your registered Nodes.
9999
// You don't need to write that by hand, it can be automatically
100100
// generated using the following command.
101-
std::string xml_models = BT::writeTreeNodesModelXML(factory);
101+
const std::string xml_models = BT::writeTreeNodesModelXML(factory);
102102

103103
factory.registerBehaviorTreeFromText(xml_text);
104104

@@ -138,7 +138,7 @@ int main()
138138
}
139139
return {};
140140
};
141-
sqlite_logger.setMetadataCallback(sqlite_callback);
141+
sqlite_logger.setAdditionalCallback(sqlite_callback);
142142

143143
while(1)
144144
{

include/behaviortree_cpp/loggers/bt_sqlite_logger.h

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,28 @@ class Connection;
1111
namespace BT
1212
{
1313

14+
/** SQL schema
15+
*
16+
* CREATE TABLE IF NOT EXISTS Definitions (
17+
* session_id INTEGER PRIMARY KEY AUTOINCREMENT,
18+
* date TEXT NOT NULL,
19+
* xml_tree TEXT NOT NULL);
20+
*
21+
* CREATE TABLE IF NOT EXISTS Nodes ("
22+
* session_id INTEGER NOT NULL,
23+
* fullpath VARCHAR, "
24+
* node_uid INTEGER NOT NULL );
25+
*
26+
* CREATE TABLE IF NOT EXISTS Transitions (
27+
* timestamp INTEGER PRIMARY KEY NOT NULL,
28+
* session_id INTEGER NOT NULL,
29+
* node_uid INTEGER NOT NULL,
30+
* duration INTEGER,
31+
* state INTEGER NOT NULL,
32+
* extra_data VARCHAR );
33+
*
34+
*/
35+
1436
/**
1537
* @brief The SqliteLogger is a logger that will store the tree and all the
1638
* status transitions in a SQLite database (single file).
@@ -37,14 +59,11 @@ class SqliteLogger : public StatusChangeLogger
3759

3860
virtual ~SqliteLogger() override;
3961

40-
// You can inject a function that add a metadata filed (a string) to the raw in the table.
62+
// You can inject a function that add a string to the Transitions table,
63+
// in the column "extra_data".
4164
// The arguments of the function are the same as SqliteLogger::callback()
42-
using MetadataCallback =
43-
std::function<std::string(Duration, const TreeNode&, NodeStatus, NodeStatus)>;
44-
void setMetadataCallback(MetadataCallback func);
45-
4665
using ExtraCallback =
47-
std::function<void(Duration, const TreeNode&, NodeStatus, NodeStatus)>;
66+
std::function<std::string(Duration, const TreeNode&, NodeStatus, NodeStatus)>;
4867
void setAdditionalCallback(ExtraCallback func);
4968

5069
virtual void callback(Duration timestamp, const TreeNode& node, NodeStatus prev_status,
@@ -68,7 +87,7 @@ class SqliteLogger : public StatusChangeLogger
6887
int64_t timestamp;
6988
int64_t duration;
7089
NodeStatus status;
71-
std::string metadata;
90+
std::string extra_data;
7291
};
7392

7493
std::deque<Transition> transitions_queue_;
@@ -78,7 +97,6 @@ class SqliteLogger : public StatusChangeLogger
7897
std::thread writer_thread_;
7998
std::atomic_bool loop_ = true;
8099

81-
MetadataCallback meta_func_;
82100
ExtraCallback extra_func_;
83101

84102
void writerLoop();

src/loggers/bt_sqlite_logger.cpp

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ SqliteLogger::SqliteLogger(const Tree& tree, std::filesystem::path const& filepa
2222
sqlite::Statement(*db_, "CREATE TABLE IF NOT EXISTS Transitions ("
2323
"timestamp INTEGER PRIMARY KEY NOT NULL, "
2424
"session_id INTEGER NOT NULL, "
25-
"uid INTEGER NOT NULL, "
25+
"node_uid INTEGER NOT NULL, "
2626
"duration INTEGER, "
2727
"state INTEGER NOT NULL,"
28-
"metadata VARCHAR );");
28+
"extra_data VARCHAR );");
2929

3030
sqlite::Statement(*db_, "CREATE TABLE IF NOT EXISTS Nodes ("
3131
"session_id INTEGER NOT NULL, "
3232
"fullpath VARCHAR, "
33-
"uid INTEGER NOT NULL );");
33+
"node_uid INTEGER NOT NULL );");
3434

3535
sqlite::Statement(*db_, "CREATE TABLE IF NOT EXISTS Definitions ("
3636
"session_id INTEGER PRIMARY KEY AUTOINCREMENT, "
@@ -41,6 +41,7 @@ SqliteLogger::SqliteLogger(const Tree& tree, std::filesystem::path const& filepa
4141
{
4242
sqlite::Statement(*db_, "DELETE from Transitions;");
4343
sqlite::Statement(*db_, "DELETE from Definitions;");
44+
sqlite::Statement(*db_, "DELETE from Nodes;");
4445
}
4546

4647
auto tree_xml = WriteTreeToXML(tree, true, true);
@@ -49,7 +50,8 @@ SqliteLogger::SqliteLogger(const Tree& tree, std::filesystem::path const& filepa
4950
"VALUES (datetime('now','localtime'),?);",
5051
tree_xml);
5152

52-
auto res = sqlite::Query(*db_, "SELECT MAX(session_id) FROM Definitions LIMIT 1;");
53+
auto res = sqlite::Query(*db_, "SELECT MAX(session_id) "
54+
"FROM Definitions LIMIT 1;");
5355

5456
while(res.Next())
5557
{
@@ -77,11 +79,6 @@ SqliteLogger::~SqliteLogger()
7779
sqlite::Statement(*db_, "PRAGMA optimize;");
7880
}
7981

80-
void SqliteLogger::setMetadataCallback(MetadataCallback func)
81-
{
82-
meta_func_ = func;
83-
}
84-
8582
void SqliteLogger::setAdditionalCallback(ExtraCallback func)
8683
{
8784
extra_func_ = func;
@@ -117,9 +114,9 @@ void SqliteLogger::callback(Duration timestamp, const TreeNode& node,
117114
trans.node_uid = node.UID();
118115
trans.status = status;
119116

120-
if(meta_func_)
117+
if(extra_func_)
121118
{
122-
trans.metadata = meta_func_(timestamp, node, prev_status, status);
119+
trans.extra_data = extra_func_(timestamp, node, prev_status, status);
123120
}
124121

125122
{
@@ -159,7 +156,7 @@ void SqliteLogger::writerLoop()
159156

160157
sqlite::Statement(*db_, "INSERT INTO Transitions VALUES (?, ?, ?, ?, ?, ?)",
161158
trans.timestamp, session_id_, trans.node_uid, trans.duration,
162-
static_cast<int>(trans.status), trans.metadata);
159+
static_cast<int>(trans.status), trans.extra_data);
163160
}
164161
}
165162
}

0 commit comments

Comments
 (0)