From 494080f3030bcc457c5b8de4fc872fed410fc9c0 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Sun, 27 Nov 2022 23:58:40 -0300 Subject: [PATCH] get the insertion of facts working --- src/knowledge_base.cpp | 19 +++++++++++++++++++ src/knowledge_base.h | 4 ++++ src/models/entity.cpp | 25 ++++++++++++++----------- src/models/entity.h | 2 +- src/models/fact.cpp | 33 ++++++++++++++------------------- src/models/fact.h | 6 +----- src/models/verb.cpp | 24 ++++++++++++++---------- src/models/verb.h | 2 +- src/parser.cpp | 29 +++++++++++++++++++++++++++++ 9 files changed, 97 insertions(+), 47 deletions(-) diff --git a/src/knowledge_base.cpp b/src/knowledge_base.cpp index b248b66..51bfde1 100644 --- a/src/knowledge_base.cpp +++ b/src/knowledge_base.cpp @@ -61,6 +61,7 @@ void obelisk::KnowledgeBase::createTable(std::function function) } } +// TODO: change these to throw errors instead of return int int obelisk::KnowledgeBase::addEntities(std::vector& entities) { for (auto& entity : entities) @@ -88,6 +89,24 @@ int obelisk::KnowledgeBase::addFacts(std::vector& facts) return 0; } +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) { diff --git a/src/knowledge_base.h b/src/knowledge_base.h index 4e79c74..e690fe8 100644 --- a/src/knowledge_base.h +++ b/src/knowledge_base.h @@ -41,6 +41,10 @@ namespace obelisk int addVerbs(std::vector& verbs); int addFacts(std::vector& facts); + int getEntity(obelisk::Entity& entity); + int getVerb(obelisk::Verb& verb); + int getFact(obelisk::Fact& fact); + void getDouble(double& result, float var1, float var2); void getFloat(float& result1, float& result2, double var); }; diff --git a/src/models/entity.cpp b/src/models/entity.cpp index 1a4fe78..0cd94a5 100644 --- a/src/models/entity.cpp +++ b/src/models/entity.cpp @@ -11,8 +11,7 @@ const char* obelisk::Entity::createTable() )"; } -obelisk::Entity obelisk::Entity::selectEntity(sqlite3* dbConnection, - std::string name) +int obelisk::Entity::selectEntity(sqlite3* dbConnection) { // TODO: check if database is open sqlite3_stmt* ppStmt = nullptr; @@ -33,7 +32,8 @@ obelisk::Entity obelisk::Entity::selectEntity(sqlite3* dbConnection, // TODO: Something was not used... throw an error } - result = sqlite3_bind_text(ppStmt, 1, name.c_str(), -1, SQLITE_TRANSIENT); + result + = sqlite3_bind_text(ppStmt, 1, getName().c_str(), -1, SQLITE_TRANSIENT); if (result != SQLITE_OK) { // TODO: Something is wrong... throw an error @@ -47,15 +47,15 @@ obelisk::Entity obelisk::Entity::selectEntity(sqlite3* dbConnection, if (result == SQLITE_ROW) { - auto id = sqlite3_column_int(ppStmt, 0); - std::string name((char*) sqlite3_column_text(ppStmt, 1)); + setId(sqlite3_column_int(ppStmt, 0)); + setName((char*) sqlite3_column_text(ppStmt, 1)); result = sqlite3_finalize(ppStmt); if (result != SQLITE_OK) { // TODO: Something is wrong... throw an error } - return Entity(id, name); + return 0; } else { @@ -64,7 +64,7 @@ obelisk::Entity obelisk::Entity::selectEntity(sqlite3* dbConnection, { // TODO: Something is wrong... throw an error } - return Entity(); + return 0; } } @@ -72,11 +72,12 @@ int obelisk::Entity::insertEntity(sqlite3* dbConnection) { // TODO: check if database is open - if (selectEntity(dbConnection, getName()).getId() != 0) + /*selectEntity(dbConnection); + if (getId() != 0) { // TODO: already exists in database, throw an error? Or skip past it? return -1; - } + }*/ sqlite3_stmt* ppStmt = nullptr; const char* pzTail = nullptr; @@ -108,8 +109,10 @@ int obelisk::Entity::insertEntity(sqlite3* dbConnection) { // TODO: Something is wrong... throw an error } - - setId((int) sqlite3_last_insert_rowid(dbConnection)); + else + { + setId((int) sqlite3_last_insert_rowid(dbConnection)); + } result = sqlite3_finalize(ppStmt); if (result != SQLITE_OK) diff --git a/src/models/entity.h b/src/models/entity.h index f83337c..f799f31 100644 --- a/src/models/entity.h +++ b/src/models/entity.h @@ -46,7 +46,7 @@ namespace obelisk std::string& getName(); void setName(std::string name); - Entity selectEntity(sqlite3* dbConnection, std::string name); + int selectEntity(sqlite3* dbConnection); int insertEntity(sqlite3* dbConnection); }; } // namespace obelisk diff --git a/src/models/fact.cpp b/src/models/fact.cpp index 6a02b97..0331fad 100644 --- a/src/models/fact.cpp +++ b/src/models/fact.cpp @@ -16,10 +16,7 @@ const char* obelisk::Fact::createTable() )"; } -obelisk::Fact obelisk::Fact::selectFact(sqlite3* dbConnection, - int idLeftEntity, - int idRightEntity, - int idVerb) +int obelisk::Fact::selectFact(sqlite3* dbConnection) { // TODO: check if database is open @@ -67,17 +64,17 @@ obelisk::Fact obelisk::Fact::selectFact(sqlite3* dbConnection, if (result == SQLITE_ROW) { - auto id = sqlite3_column_int(ppStmt, 0); - auto leftEntity = sqlite3_column_int(ppStmt, 1); - auto rightEntity = sqlite3_column_int(ppStmt, 2); - auto verb = sqlite3_column_int(ppStmt, 3); + setId(sqlite3_column_int(ppStmt, 0)); + getLeftEntity().setId(sqlite3_column_int(ppStmt, 1)); + getRightEntity().setId(sqlite3_column_int(ppStmt, 2)); + getVerb().setId(sqlite3_column_int(ppStmt, 3)); result = sqlite3_finalize(ppStmt); if (result != SQLITE_OK) { // TODO: Something is wrong... throw an error } - return Fact(id, leftEntity, rightEntity, verb); + return 0; } else { @@ -86,7 +83,7 @@ obelisk::Fact obelisk::Fact::selectFact(sqlite3* dbConnection, { // TODO: Something is wrong... throw an error } - return Fact(); + return 0; } } @@ -95,16 +92,12 @@ int obelisk::Fact::insertFact(sqlite3* dbConnection) // TODO: make sure database is open // check if the fact id exists, based on the ids of the entities and verb - if (selectFact(dbConnection, - getLeftEntity().getId(), - getRightEntity().getId(), - getVerb().getId()) - .getId() - != 0) + /*selectFact(dbConnection); + if (getId() != 0) { // TODO: Verb is already in database, throw an error? Or just skip it? return -1; - } + }*/ // TODO: verify that verbId, leftEntityId, and rightEntityId are not 0 @@ -149,8 +142,10 @@ int obelisk::Fact::insertFact(sqlite3* dbConnection) { // TODO: Something is wrong... throw an error } - - setId((int) sqlite3_last_insert_rowid(dbConnection)); + else + { + setId((int) sqlite3_last_insert_rowid(dbConnection)); + } result = sqlite3_finalize(ppStmt); if (result != SQLITE_OK) diff --git a/src/models/fact.h b/src/models/fact.h index 3fc5987..332f089 100644 --- a/src/models/fact.h +++ b/src/models/fact.h @@ -69,11 +69,7 @@ namespace obelisk Verb& getVerb(); void setVerb(obelisk::Verb verb); - Fact selectFact(sqlite3* dbConnection, - int idLeftEntity, - int idRightEntity, - int idVerb); - + int selectFact(sqlite3* dbConnection); int insertFact(sqlite3* dbConnection); }; } // namespace obelisk diff --git a/src/models/verb.cpp b/src/models/verb.cpp index e3820fb..5414fec 100644 --- a/src/models/verb.cpp +++ b/src/models/verb.cpp @@ -13,7 +13,7 @@ const char* obelisk::Verb::createTable() )"; } -obelisk::Verb obelisk::Verb::selectVerb(sqlite3* dbConnection, std::string name) +int obelisk::Verb::selectVerb(sqlite3* dbConnection) { // TODO: check if database is open @@ -35,7 +35,8 @@ obelisk::Verb obelisk::Verb::selectVerb(sqlite3* dbConnection, std::string name) // TODO: Something was not used... throw an error } - result = sqlite3_bind_text(ppStmt, 1, name.c_str(), -1, SQLITE_TRANSIENT); + result + = sqlite3_bind_text(ppStmt, 1, getName().c_str(), -1, SQLITE_TRANSIENT); if (result != SQLITE_OK) { // TODO: Something is wrong... throw an error @@ -49,15 +50,15 @@ obelisk::Verb obelisk::Verb::selectVerb(sqlite3* dbConnection, std::string name) if (result == SQLITE_ROW) { - auto id = sqlite3_column_int(ppStmt, 0); - std::string name((char*) sqlite3_column_text(ppStmt, 1)); + setId(sqlite3_column_int(ppStmt, 0)); + setName((char*) sqlite3_column_text(ppStmt, 1)); result = sqlite3_finalize(ppStmt); if (result != SQLITE_OK) { // TODO: Something is wrong... throw an error } - return Verb(id, name); + return 0; } else { @@ -66,7 +67,7 @@ obelisk::Verb obelisk::Verb::selectVerb(sqlite3* dbConnection, std::string name) { // TODO: Something is wrong... throw an error } - return Verb(); + return 0; } } @@ -74,11 +75,12 @@ int obelisk::Verb::insertVerb(sqlite3* dbConnection) { // TODO: make sure database is open - if (selectVerb(dbConnection, getName()).getId() != 0) + /*selectVerb(dbConnection); + if (getId() != 0) { // TODO: Verb is already in database, throw an error? Or just skip it? return -1; - } + }*/ sqlite3_stmt* ppStmt = nullptr; const char* pzTail = nullptr; @@ -110,8 +112,10 @@ int obelisk::Verb::insertVerb(sqlite3* dbConnection) { // TODO: Something is wrong... throw an error } - - setId((int) sqlite3_last_insert_rowid(dbConnection)); + else + { + setId((int) sqlite3_last_insert_rowid(dbConnection)); + } result = sqlite3_finalize(ppStmt); if (result != SQLITE_OK) diff --git a/src/models/verb.h b/src/models/verb.h index 8e8d241..cf74204 100644 --- a/src/models/verb.h +++ b/src/models/verb.h @@ -46,7 +46,7 @@ namespace obelisk std::string& getName(); void setName(std::string name); - Verb selectVerb(sqlite3* dbConnection, std::string name); + int selectVerb(sqlite3* dbConnection); int insertVerb(sqlite3* dbConnection); }; } // namespace obelisk diff --git a/src/parser.cpp b/src/parser.cpp index 339581d..518786b 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -366,10 +366,39 @@ void obelisk::Parser::handleFact(std::unique_ptr& kb) kb->addEntities(entities); fact.setLeftEntity(entities.front()); + // the id was not inserted, so check if it exists in the database + if (fact.getLeftEntity().getId() == 0) + { + obelisk::Entity entity = fact.getLeftEntity(); + kb->getEntity(entity); + if (entity.getId() == 0) + { + // TODO: throw an error here, it was not inserted, and doesn't exist in the database + } + else + { + fact.setLeftEntity(entity); + } + } + entities = {fact.getRightEntity()}; kb->addEntities(entities); fact.setRightEntity(entities.front()); + if (fact.getRightEntity().getId() == 0) + { + obelisk::Entity entity = fact.getRightEntity(); + kb->getEntity(entity); + if (entity.getId() == 0) + { + // TODO: throw an error here, it was not inserted, and doesn't exist in the database + } + else + { + fact.setRightEntity(entity); + } + } + if (verbId == 0) { std::vector verbs = {fact.getVerb()};