2022-10-25 17:08:36 -03:00
|
|
|
#ifndef OBELISK_KNOWLEDGE_BASE_H
|
|
|
|
#define OBELISK_KNOWLEDGE_BASE_H
|
|
|
|
|
2022-11-26 00:32:06 -03:00
|
|
|
#include "models/entity.h"
|
|
|
|
#include "models/fact.h"
|
|
|
|
#include "models/verb.h"
|
|
|
|
|
2022-10-25 17:08:36 -03:00
|
|
|
#include <sqlite3.h>
|
|
|
|
|
2022-11-01 01:04:41 -03:00
|
|
|
#include <functional>
|
2022-10-25 17:08:36 -03:00
|
|
|
#include <iostream>
|
2022-11-26 00:32:06 -03:00
|
|
|
#include <memory>
|
2022-10-25 17:08:36 -03:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
namespace obelisk
|
|
|
|
{
|
|
|
|
class KnowledgeBase
|
|
|
|
{
|
|
|
|
private:
|
2022-12-10 16:26:30 -03:00
|
|
|
/**
|
|
|
|
* @brief The filename of the opened knowledge base.
|
|
|
|
*
|
|
|
|
*/
|
2022-10-25 17:08:36 -03:00
|
|
|
const char* filename_;
|
2022-12-10 16:26:30 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief The SQLite connection handle.
|
|
|
|
*
|
|
|
|
*/
|
2022-11-03 15:36:08 -03:00
|
|
|
sqlite3* dbConnection_ = nullptr;
|
2022-12-10 16:26:30 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief The user passed flags to use when opening the database.
|
|
|
|
*
|
|
|
|
*/
|
2022-10-25 17:08:36 -03:00
|
|
|
int flags_;
|
|
|
|
|
2022-12-10 16:26:30 -03:00
|
|
|
/**
|
|
|
|
* @brief Enable foreign key functionality in the open database.
|
|
|
|
*
|
2022-12-10 17:40:30 -03:00
|
|
|
* This must always be done when the connection is opened or it will
|
|
|
|
* not enforce the foreign key constraints.
|
2022-12-10 16:26:30 -03:00
|
|
|
*/
|
2022-11-29 00:06:35 -03:00
|
|
|
void enableForeignKeys();
|
2022-12-10 16:26:30 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Create the tables in the database.
|
|
|
|
*
|
|
|
|
* @param[in] function This function is called to create the table.
|
|
|
|
*/
|
2022-11-01 01:04:41 -03:00
|
|
|
void createTable(std::function<const char*()> function);
|
|
|
|
|
2022-10-25 17:08:36 -03:00
|
|
|
public:
|
2022-12-10 16:26:30 -03:00
|
|
|
/**
|
|
|
|
* @brief Construct a new Knowledge Base object.
|
|
|
|
*
|
2022-12-10 17:40:30 -03:00
|
|
|
* @param[in] filename The name of the file to save the knowledge
|
|
|
|
* base as.
|
2022-12-10 16:26:30 -03:00
|
|
|
* @param[in] flags The flags to open the knowledge base with.
|
|
|
|
*/
|
2022-10-25 17:08:36 -03:00
|
|
|
KnowledgeBase(const char* filename, int flags);
|
2022-11-26 00:32:06 -03:00
|
|
|
|
2022-12-10 16:26:30 -03:00
|
|
|
/**
|
|
|
|
* @brief Construct a new Knowledge Base object.
|
|
|
|
*
|
2022-12-10 17:40:30 -03:00
|
|
|
* @param[in] filename The name of the file to save the knowledge
|
|
|
|
* base as.
|
2022-12-10 16:26:30 -03:00
|
|
|
*/
|
2022-11-26 00:32:06 -03:00
|
|
|
KnowledgeBase(const char* filename) :
|
|
|
|
KnowledgeBase(filename,
|
|
|
|
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-12-10 16:26:30 -03:00
|
|
|
/**
|
|
|
|
* @brief Destroy the Knowledge Base object.
|
|
|
|
*
|
|
|
|
* This will close the opened knowledge base before destroying it.
|
|
|
|
*/
|
2022-10-25 17:08:36 -03:00
|
|
|
~KnowledgeBase();
|
|
|
|
|
2022-12-10 16:26:30 -03:00
|
|
|
/**
|
|
|
|
* @brief Add entities to the knowledge base.
|
|
|
|
*
|
2022-12-10 18:06:30 -03:00
|
|
|
* @param[in,out] entities The entities to add. If the insert is
|
|
|
|
* successful it will have a row ID, if not the ID will be 0.
|
2022-12-10 16:26:30 -03:00
|
|
|
*/
|
2022-11-29 00:06:35 -03:00
|
|
|
void addEntities(std::vector<obelisk::Entity>& entities);
|
2022-12-10 16:26:30 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Add verbs to the knowledge base.
|
|
|
|
*
|
2022-12-10 18:06:30 -03:00
|
|
|
* @param[in,out] verbs The verbs to add. If the insert is
|
|
|
|
* successful it will have a row ID, if not the ID will be 0.
|
2022-12-10 16:26:30 -03:00
|
|
|
*/
|
2022-11-29 00:06:35 -03:00
|
|
|
void addVerbs(std::vector<obelisk::Verb>& verbs);
|
2022-12-10 16:26:30 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Add facts to the database.
|
|
|
|
*
|
2022-12-10 18:06:30 -03:00
|
|
|
* @param[in,out] facts The facts to add. If the insert is
|
|
|
|
* successful it will have a row ID, if not the ID will be 0.
|
2022-12-10 16:26:30 -03:00
|
|
|
*/
|
2022-11-29 00:06:35 -03:00
|
|
|
void addFacts(std::vector<obelisk::Fact>& facts);
|
2022-10-25 17:08:36 -03:00
|
|
|
|
2022-12-10 16:26:30 -03:00
|
|
|
/**
|
|
|
|
* @brief Get an entity object based on the ID it contains.
|
|
|
|
*
|
2022-12-10 18:06:30 -03:00
|
|
|
* @param[in,out] entity The Entity object should contain just the
|
|
|
|
* ID and the rest will be filled in.
|
2022-12-10 16:26:30 -03:00
|
|
|
*/
|
2022-11-29 00:06:35 -03:00
|
|
|
void getEntity(obelisk::Entity& entity);
|
2022-12-10 16:26:30 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get a verb object based on the ID it contains.
|
|
|
|
*
|
2022-12-10 18:06:30 -03:00
|
|
|
* @param[in,out] verb The Verb object should contain just the ID
|
|
|
|
* and the rest will be filled in.
|
2022-12-10 16:26:30 -03:00
|
|
|
*/
|
2022-11-29 00:06:35 -03:00
|
|
|
void getVerb(obelisk::Verb& verb);
|
2022-12-10 16:26:30 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get a fact object based on the ID it contains.
|
|
|
|
*
|
2022-12-10 18:06:30 -03:00
|
|
|
* @param[in,out] fact The fact object should contain just the ID
|
|
|
|
* and the rest will be filled in.
|
2022-12-10 16:26:30 -03:00
|
|
|
*/
|
2022-11-29 00:06:35 -03:00
|
|
|
void getFact(obelisk::Fact& fact);
|
2022-11-27 23:58:40 -03:00
|
|
|
|
2022-12-10 16:26:30 -03:00
|
|
|
/**
|
|
|
|
* @brief Take a float and divide it into 2 floats.
|
|
|
|
*
|
2022-12-10 17:40:30 -03:00
|
|
|
* This is useful to store doubles in SQLite since SQLite doesn't
|
|
|
|
* have a double type.
|
|
|
|
* Instead just store the 2 floats in the database. Then after
|
|
|
|
* selecting them combine them.
|
2022-12-10 16:26:30 -03:00
|
|
|
*
|
|
|
|
* @param[out] result1 The first float generated from the double.
|
|
|
|
* @param[out] result2 The second float generated from the double.
|
|
|
|
* @param[in] var The double to split into the 2 floats.
|
|
|
|
*/
|
2022-11-01 01:04:41 -03:00
|
|
|
void getFloat(float& result1, float& result2, double var);
|
2022-12-10 16:26:30 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Combines 2 separated floats back into a double.
|
|
|
|
*
|
2022-12-10 17:40:30 -03:00
|
|
|
* This will recombine the separated floats from the getFloat
|
|
|
|
* method.
|
2022-12-10 16:26:30 -03:00
|
|
|
*
|
|
|
|
* @param[out] result The double generated from the combined floats.
|
|
|
|
* @param[in] var1 The first float to combine.
|
|
|
|
* @param[in] var2 The second float to combine.
|
|
|
|
*/
|
|
|
|
void getDouble(double& result, float var1, float var2);
|
2022-10-25 17:08:36 -03:00
|
|
|
};
|
|
|
|
|
2022-12-10 16:26:30 -03:00
|
|
|
/**
|
|
|
|
* @brief Exception thrown by the KnowledgeBase.
|
|
|
|
*
|
|
|
|
*/
|
2022-10-25 17:08:36 -03:00
|
|
|
class KnowledgeBaseException : public std::exception
|
|
|
|
{
|
|
|
|
private:
|
2022-12-10 16:26:30 -03:00
|
|
|
/**
|
|
|
|
* @brief The error message given.
|
|
|
|
*
|
|
|
|
*/
|
2022-11-01 01:04:41 -03:00
|
|
|
const std::string errorMessage_;
|
2022-10-25 17:08:36 -03:00
|
|
|
|
|
|
|
public:
|
2022-12-10 16:26:30 -03:00
|
|
|
/**
|
|
|
|
* @brief Construct a new Knowledge Base Exception object.
|
|
|
|
*
|
|
|
|
*/
|
2022-11-01 01:17:45 -03:00
|
|
|
KnowledgeBaseException() :
|
2022-11-01 01:38:54 -03:00
|
|
|
errorMessage_("an unknown error ocurred")
|
2022-11-01 01:17:45 -03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-12-10 16:26:30 -03:00
|
|
|
/**
|
|
|
|
* @brief Construct a new Knowledge Base Exception object.
|
|
|
|
*
|
|
|
|
* @param[in] errorMessage The error message given when thrown.
|
|
|
|
*/
|
2022-11-01 01:04:41 -03:00
|
|
|
KnowledgeBaseException(const std::string& errorMessage) :
|
2022-10-25 17:08:36 -03:00
|
|
|
errorMessage_(errorMessage)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-12-10 16:26:30 -03:00
|
|
|
/**
|
|
|
|
* @brief Get the error message that occurred.
|
|
|
|
*
|
|
|
|
* @return const char* Returns the error message.
|
|
|
|
*/
|
2022-10-25 17:08:36 -03:00
|
|
|
const char* what() const noexcept
|
|
|
|
{
|
|
|
|
return errorMessage_.c_str();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace obelisk
|
|
|
|
|
|
|
|
#endif
|