obelisk/src/knowledge_base.cpp

128 lines
2.9 KiB
C++
Raw Normal View History

#include "knowledge_base.h"
2022-11-01 01:04:41 -03:00
#include "models/action.h"
#include "models/entity.h"
#include "models/fact.h"
#include "models/rule.h"
#include "models/suggest_action.h"
#include "models/verb.h"
2022-11-01 01:04:41 -03:00
#include <filesystem>
#include <iostream>
obelisk::KnowledgeBase::KnowledgeBase(const char* filename, int flags)
{
filename_ = std::move(filename);
flags_ = std::move(flags);
2022-11-01 01:04:41 -03:00
std::filesystem::path path {filename};
auto dbExists = std::filesystem::exists(path);
auto result = sqlite3_open_v2(filename, &dbConnection_, flags, NULL);
if (result != SQLITE_OK)
{
logSqliteError(result);
}
2022-11-01 01:04:41 -03:00
if (!dbExists)
{
2022-11-01 01:04:41 -03:00
createTable(obelisk::Action::createTable);
createTable(obelisk::Entity::createTable);
createTable(obelisk::Verb::createTable);
createTable(obelisk::Fact::createTable);
createTable(obelisk::Rule::createTable);
createTable(obelisk::SuggestAction::createTable);
}
}
obelisk::KnowledgeBase::~KnowledgeBase()
{
if (dbConnection_)
{
sqlite3_close_v2(dbConnection_);
}
}
2022-11-01 01:04:41 -03:00
void obelisk::KnowledgeBase::createTable(std::function<const char*()> function)
{
char* tmp;
auto result = sqlite3_exec(dbConnection_, function(), NULL, NULL, &tmp);
if (result != SQLITE_OK)
{
logSqliteError(result);
2022-11-01 01:17:45 -03:00
if (tmp)
{
std::string errmsg(tmp);
throw obelisk::KnowledgeBaseException(errmsg);
}
else
{
throw obelisk::KnowledgeBaseException();
}
2022-11-01 01:04:41 -03:00
}
}
2022-11-27 23:58:40 -03:00
// TODO: change these to throw errors instead of return int
2022-11-26 00:32:06 -03:00
int obelisk::KnowledgeBase::addEntities(std::vector<obelisk::Entity>& entities)
{
for (auto& entity : entities)
{
entity.insertEntity(dbConnection_);
}
return 0;
}
int obelisk::KnowledgeBase::addVerbs(std::vector<obelisk::Verb>& verbs)
{
for (auto& verb : verbs)
{
verb.insertVerb(dbConnection_);
}
return 0;
}
int obelisk::KnowledgeBase::addFacts(std::vector<obelisk::Fact>& facts)
{
for (auto& fact : facts)
{
fact.insertFact(dbConnection_);
}
return 0;
}
2022-11-27 23:58:40 -03:00
int obelisk::KnowledgeBase::getEntity(obelisk::Entity& entity)
{
entity.selectEntity(dbConnection_);
return 0;
}
int obelisk::KnowledgeBase::getVerb(obelisk::Verb& verb)
{
verb.selectVerb(dbConnection_);
return 0;
}
int obelisk::KnowledgeBase::getFact(obelisk::Fact& fact)
{
fact.selectFact(dbConnection_);
return 0;
}
// TODO: log files?
void obelisk::KnowledgeBase::logSqliteError(int result)
{
std::cout << sqlite3_errstr(result) << std::endl;
}
2022-11-01 01:04:41 -03:00
void obelisk::KnowledgeBase::getFloat(float& result1,
float& result2,
double var)
{
2022-11-01 01:04:41 -03:00
result1 = (float) var;
result2 = (float) (var - (double) result1);
}
2022-11-01 01:04:41 -03:00
void obelisk::KnowledgeBase::getDouble(double& result, float var1, float var2)
{
2022-11-01 01:04:41 -03:00
result = (double) ((double) var2 + (double) var1);
}