get the insertion of facts working

This commit is contained in:
Chris Cromer 2022-11-27 23:58:40 -03:00
parent 3bc6ebe4d8
commit 494080f303
Signed by: cromer
GPG Key ID: FA91071797BEEEC2
9 changed files with 97 additions and 47 deletions

View File

@ -61,6 +61,7 @@ void obelisk::KnowledgeBase::createTable(std::function<const char*()> function)
} }
} }
// TODO: change these to throw errors instead of return int
int obelisk::KnowledgeBase::addEntities(std::vector<obelisk::Entity>& entities) int obelisk::KnowledgeBase::addEntities(std::vector<obelisk::Entity>& entities)
{ {
for (auto& entity : entities) for (auto& entity : entities)
@ -88,6 +89,24 @@ int obelisk::KnowledgeBase::addFacts(std::vector<obelisk::Fact>& facts)
return 0; 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? // TODO: log files?
void obelisk::KnowledgeBase::logSqliteError(int result) void obelisk::KnowledgeBase::logSqliteError(int result)
{ {

View File

@ -41,6 +41,10 @@ namespace obelisk
int addVerbs(std::vector<obelisk::Verb>& verbs); int addVerbs(std::vector<obelisk::Verb>& verbs);
int addFacts(std::vector<obelisk::Fact>& facts); int addFacts(std::vector<obelisk::Fact>& 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 getDouble(double& result, float var1, float var2);
void getFloat(float& result1, float& result2, double var); void getFloat(float& result1, float& result2, double var);
}; };

View File

@ -11,8 +11,7 @@ const char* obelisk::Entity::createTable()
)"; )";
} }
obelisk::Entity obelisk::Entity::selectEntity(sqlite3* dbConnection, int obelisk::Entity::selectEntity(sqlite3* dbConnection)
std::string name)
{ {
// TODO: check if database is open // TODO: check if database is open
sqlite3_stmt* ppStmt = nullptr; sqlite3_stmt* ppStmt = nullptr;
@ -33,7 +32,8 @@ obelisk::Entity obelisk::Entity::selectEntity(sqlite3* dbConnection,
// TODO: Something was not used... throw an error // 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) if (result != SQLITE_OK)
{ {
// TODO: Something is wrong... throw an error // TODO: Something is wrong... throw an error
@ -47,15 +47,15 @@ obelisk::Entity obelisk::Entity::selectEntity(sqlite3* dbConnection,
if (result == SQLITE_ROW) if (result == SQLITE_ROW)
{ {
auto id = sqlite3_column_int(ppStmt, 0); setId(sqlite3_column_int(ppStmt, 0));
std::string name((char*) sqlite3_column_text(ppStmt, 1)); setName((char*) sqlite3_column_text(ppStmt, 1));
result = sqlite3_finalize(ppStmt); result = sqlite3_finalize(ppStmt);
if (result != SQLITE_OK) if (result != SQLITE_OK)
{ {
// TODO: Something is wrong... throw an error // TODO: Something is wrong... throw an error
} }
return Entity(id, name); return 0;
} }
else else
{ {
@ -64,7 +64,7 @@ obelisk::Entity obelisk::Entity::selectEntity(sqlite3* dbConnection,
{ {
// TODO: Something is wrong... throw an error // 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 // 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? // TODO: already exists in database, throw an error? Or skip past it?
return -1; return -1;
} }*/
sqlite3_stmt* ppStmt = nullptr; sqlite3_stmt* ppStmt = nullptr;
const char* pzTail = nullptr; const char* pzTail = nullptr;
@ -108,8 +109,10 @@ int obelisk::Entity::insertEntity(sqlite3* dbConnection)
{ {
// TODO: Something is wrong... throw an error // TODO: Something is wrong... throw an error
} }
else
{
setId((int) sqlite3_last_insert_rowid(dbConnection)); setId((int) sqlite3_last_insert_rowid(dbConnection));
}
result = sqlite3_finalize(ppStmt); result = sqlite3_finalize(ppStmt);
if (result != SQLITE_OK) if (result != SQLITE_OK)

View File

@ -46,7 +46,7 @@ namespace obelisk
std::string& getName(); std::string& getName();
void setName(std::string name); void setName(std::string name);
Entity selectEntity(sqlite3* dbConnection, std::string name); int selectEntity(sqlite3* dbConnection);
int insertEntity(sqlite3* dbConnection); int insertEntity(sqlite3* dbConnection);
}; };
} // namespace obelisk } // namespace obelisk

View File

@ -16,10 +16,7 @@ const char* obelisk::Fact::createTable()
)"; )";
} }
obelisk::Fact obelisk::Fact::selectFact(sqlite3* dbConnection, int obelisk::Fact::selectFact(sqlite3* dbConnection)
int idLeftEntity,
int idRightEntity,
int idVerb)
{ {
// TODO: check if database is open // TODO: check if database is open
@ -67,17 +64,17 @@ obelisk::Fact obelisk::Fact::selectFact(sqlite3* dbConnection,
if (result == SQLITE_ROW) if (result == SQLITE_ROW)
{ {
auto id = sqlite3_column_int(ppStmt, 0); setId(sqlite3_column_int(ppStmt, 0));
auto leftEntity = sqlite3_column_int(ppStmt, 1); getLeftEntity().setId(sqlite3_column_int(ppStmt, 1));
auto rightEntity = sqlite3_column_int(ppStmt, 2); getRightEntity().setId(sqlite3_column_int(ppStmt, 2));
auto verb = sqlite3_column_int(ppStmt, 3); getVerb().setId(sqlite3_column_int(ppStmt, 3));
result = sqlite3_finalize(ppStmt); result = sqlite3_finalize(ppStmt);
if (result != SQLITE_OK) if (result != SQLITE_OK)
{ {
// TODO: Something is wrong... throw an error // TODO: Something is wrong... throw an error
} }
return Fact(id, leftEntity, rightEntity, verb); return 0;
} }
else else
{ {
@ -86,7 +83,7 @@ obelisk::Fact obelisk::Fact::selectFact(sqlite3* dbConnection,
{ {
// TODO: Something is wrong... throw an error // 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 // TODO: make sure database is open
// check if the fact id exists, based on the ids of the entities and verb // check if the fact id exists, based on the ids of the entities and verb
if (selectFact(dbConnection, /*selectFact(dbConnection);
getLeftEntity().getId(), if (getId() != 0)
getRightEntity().getId(),
getVerb().getId())
.getId()
!= 0)
{ {
// TODO: Verb is already in database, throw an error? Or just skip it? // TODO: Verb is already in database, throw an error? Or just skip it?
return -1; return -1;
} }*/
// TODO: verify that verbId, leftEntityId, and rightEntityId are not 0 // 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 // TODO: Something is wrong... throw an error
} }
else
{
setId((int) sqlite3_last_insert_rowid(dbConnection)); setId((int) sqlite3_last_insert_rowid(dbConnection));
}
result = sqlite3_finalize(ppStmt); result = sqlite3_finalize(ppStmt);
if (result != SQLITE_OK) if (result != SQLITE_OK)

View File

@ -69,11 +69,7 @@ namespace obelisk
Verb& getVerb(); Verb& getVerb();
void setVerb(obelisk::Verb verb); void setVerb(obelisk::Verb verb);
Fact selectFact(sqlite3* dbConnection, int selectFact(sqlite3* dbConnection);
int idLeftEntity,
int idRightEntity,
int idVerb);
int insertFact(sqlite3* dbConnection); int insertFact(sqlite3* dbConnection);
}; };
} // namespace obelisk } // namespace obelisk

View File

@ -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 // 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 // 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) if (result != SQLITE_OK)
{ {
// TODO: Something is wrong... throw an error // 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) if (result == SQLITE_ROW)
{ {
auto id = sqlite3_column_int(ppStmt, 0); setId(sqlite3_column_int(ppStmt, 0));
std::string name((char*) sqlite3_column_text(ppStmt, 1)); setName((char*) sqlite3_column_text(ppStmt, 1));
result = sqlite3_finalize(ppStmt); result = sqlite3_finalize(ppStmt);
if (result != SQLITE_OK) if (result != SQLITE_OK)
{ {
// TODO: Something is wrong... throw an error // TODO: Something is wrong... throw an error
} }
return Verb(id, name); return 0;
} }
else else
{ {
@ -66,7 +67,7 @@ obelisk::Verb obelisk::Verb::selectVerb(sqlite3* dbConnection, std::string name)
{ {
// TODO: Something is wrong... throw an error // 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 // 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? // TODO: Verb is already in database, throw an error? Or just skip it?
return -1; return -1;
} }*/
sqlite3_stmt* ppStmt = nullptr; sqlite3_stmt* ppStmt = nullptr;
const char* pzTail = nullptr; const char* pzTail = nullptr;
@ -110,8 +112,10 @@ int obelisk::Verb::insertVerb(sqlite3* dbConnection)
{ {
// TODO: Something is wrong... throw an error // TODO: Something is wrong... throw an error
} }
else
{
setId((int) sqlite3_last_insert_rowid(dbConnection)); setId((int) sqlite3_last_insert_rowid(dbConnection));
}
result = sqlite3_finalize(ppStmt); result = sqlite3_finalize(ppStmt);
if (result != SQLITE_OK) if (result != SQLITE_OK)

View File

@ -46,7 +46,7 @@ namespace obelisk
std::string& getName(); std::string& getName();
void setName(std::string name); void setName(std::string name);
Verb selectVerb(sqlite3* dbConnection, std::string name); int selectVerb(sqlite3* dbConnection);
int insertVerb(sqlite3* dbConnection); int insertVerb(sqlite3* dbConnection);
}; };
} // namespace obelisk } // namespace obelisk

View File

@ -366,10 +366,39 @@ void obelisk::Parser::handleFact(std::unique_ptr<obelisk::KnowledgeBase>& kb)
kb->addEntities(entities); kb->addEntities(entities);
fact.setLeftEntity(entities.front()); 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()}; entities = {fact.getRightEntity()};
kb->addEntities(entities); kb->addEntities(entities);
fact.setRightEntity(entities.front()); 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) if (verbId == 0)
{ {
std::vector<obelisk::Verb> verbs = {fact.getVerb()}; std::vector<obelisk::Verb> verbs = {fact.getVerb()};