obelisk/src/lib/knowledge_base.h

275 lines
8.9 KiB
C
Raw Normal View History

#ifndef OBELISK_KNOWLEDGE_BASE_H
#define OBELISK_KNOWLEDGE_BASE_H
#include "models/action.h"
2022-11-26 00:32:06 -03:00
#include "models/entity.h"
#include "models/fact.h"
#include "models/rule.h"
#include "models/suggest_action.h"
2022-11-26 00:32:06 -03:00
#include "models/verb.h"
#include <sqlite3.h>
2022-11-01 01:04:41 -03:00
#include <functional>
#include <iostream>
2022-11-26 00:32:06 -03:00
#include <memory>
#include <string>
namespace obelisk
{
/**
* @brief The KnowledgeBase class represents a collection of facts, rules,
* actions, and related language connectors.
*
*/
class KnowledgeBase
{
private:
2022-12-10 16:26:30 -03:00
/**
* @brief The filename of the opened KnowledgeBase.
2022-12-10 16:26:30 -03:00
*
*/
const char* filename_;
2022-12-10 16:26:30 -03:00
/**
* @brief The SQLite connection handle.
*
*/
sqlite3* dbConnection_ = nullptr;
2022-12-10 16:26:30 -03:00
/**
* @brief The user passed flags to use when opening the database.
*
*/
int flags_;
2022-12-10 16:26:30 -03:00
/**
* @brief Enable foreign key functionality in the open database.
*
* 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
*/
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);
public:
2022-12-10 16:26:30 -03:00
/**
* @brief Construct a new KnowledgeBase object.
2022-12-10 16:26:30 -03:00
*
* @param[in] filename The name of the file to save the knowledge
* base as.
* @param[in] flags The flags to open the KnowledgeBase with.
2022-12-10 16:26:30 -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 KnowledgeBase object.
2022-12-10 16:26: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-11-26 00:32:06 -03:00
{
}
2022-12-10 16:26:30 -03:00
/**
* @brief Destroy the KnowledgeBase object.
2022-12-10 16:26:30 -03:00
*
* This will close the opened KnowledgeBase before destroying it.
2022-12-10 16:26:30 -03:00
*/
~KnowledgeBase();
2022-12-10 16:26:30 -03:00
/**
* @brief Add entities to the KnowledgeBase.
2022-12-10 16:26: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
*/
void addEntities(std::vector<obelisk::Entity>& entities);
2022-12-10 16:26:30 -03:00
/**
* @brief Add verbs to the KnowledgeBase.
2022-12-10 16:26: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
*/
void addVerbs(std::vector<obelisk::Verb>& verbs);
2022-12-10 16:26:30 -03:00
/**
* @brief Add actions to the KnowledgeBase.
*
* @param[in,out] actions The actions to add. If the insert is
* successful it will have a row ID, if nto the ID will be 0.
*/
void addActions(std::vector<obelisk::Action>& actions);
2022-12-10 16:26:30 -03:00
/**
2023-02-18 21:32:05 -03:00
* @brief Add facts to the KnowledgeBase.
2022-12-10 16:26: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
*/
void addFacts(std::vector<obelisk::Fact>& facts);
2022-12-10 16:26:30 -03:00
/**
2023-02-18 21:32:05 -03:00
* @brief Add suggested actions to the KnowledgeBase.
*
* @param[in,out] suggestActions The suggested actions to add. If
* the insert is successful it will have a row ID, if not the ID
* will be 0.
2023-02-18 21:32:05 -03:00
*/
void addSuggestActions(
std::vector<obelisk::SuggestAction>& suggestActions);
2023-02-18 21:32:05 -03:00
2023-02-19 01:03:35 -03:00
/**
* @brief Add rules to the KnowledgeBase.
*
* @param[in,out] rules The rules to add. If the insert is
* successful it will have a row ID, if not the ID will be 0.
2023-02-19 01:03:35 -03:00
*/
void addRules(std::vector<obelisk::Rule>& rules);
2023-02-18 21:32:05 -03:00
/**
* @brief Get an Entity object based on the ID it contains.
2022-12-10 16:26: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
*/
void getEntity(obelisk::Entity& entity);
2022-12-10 16:26:30 -03:00
/**
2023-02-18 21:32:05 -03:00
* @brief Get a Verb object based on the ID it contains.
2022-12-10 16:26: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
*/
void getVerb(obelisk::Verb& verb);
2022-12-10 16:26:30 -03:00
/**
2023-02-18 21:32:05 -03:00
* @brief Get an Action based on the ID it contains.
*
* @param[in] action The Action object should contain just the ID
* and the rest will be filled in.
*/
void getAction(obelisk::Action& action);
2022-12-10 16:26:30 -03:00
/**
2023-02-18 21:32:05 -03:00
* @brief Get a Fact object based on the ID it contains.
2022-12-10 16:26: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
*/
void getFact(obelisk::Fact& fact);
2022-11-27 23:58:40 -03:00
2023-02-18 21:32:05 -03:00
/**
* @brief Get a SuggestAction based on the ID it contains.
*
* @param[in,out] suggestAction The SuggestAction object should
* contain just the ID and the rest will be filled in.
2023-02-18 21:32:05 -03:00
*/
void getSuggestAction(obelisk::SuggestAction& suggestAction);
2023-02-19 01:03:35 -03:00
/**
* @brief Get a Rule based on the ID it contains.
*
* @param[in,out] rule The Rule object should contain just the ID
* and the rest will be filled in.
2023-02-19 01:03:35 -03:00
*/
void getRule(obelisk::Rule& rule);
2023-02-20 09:11:10 -03:00
/**
* @brief Check if a rule looks for this Fact, if so update its
* truth.
2023-02-20 09:11:10 -03:00
*
* @param[in,out] fact The Fact to check for existing rules.
*/
void checkRule(obelisk::Fact& fact);
/**
* @brief Update the is true field in the KnowledgeBase.
*
* @param[in,out] fact The fact to update.
*/
void updateIsTrue(obelisk::Fact& fact);
2022-12-10 16:26:30 -03:00
/**
* @brief Take a float and divide it into 2 floats.
*
* 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.
*
* 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-12-10 16:26:30 -03:00
/**
* @brief Exception thrown by the KnowledgeBase.
*
*/
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_;
public:
2022-12-10 16:26:30 -03:00
/**
* @brief Construct a new KnowledgeBaseException object.
2022-12-10 16:26:30 -03:00
*
*/
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 KnowledgeBaseException object.
2022-12-10 16:26:30 -03:00
*
* @param[in] errorMessage The error message given when thrown.
*/
2022-11-01 01:04:41 -03:00
KnowledgeBaseException(const std::string& errorMessage) :
errorMessage_(errorMessage)
{
}
2022-12-10 16:26:30 -03:00
/**
* @brief Get the error message that occurred.
*
* @return const char* Returns the error message.
*/
const char* what() const noexcept
{
return errorMessage_.c_str();
}
};
} // namespace obelisk
#endif