finish inserting the suggested actions

This commit is contained in:
Chris Cromer 2023-02-18 21:32:05 -03:00
parent 91cce4c170
commit d8ae5e1b95
Signed by: cromer
GPG Key ID: FA91071797BEEEC2
4 changed files with 70 additions and 8 deletions

View File

@ -153,6 +153,27 @@ void obelisk::KnowledgeBase::addFacts(std::vector<obelisk::Fact>& facts)
} }
} }
void obelisk::KnowledgeBase::addSuggestActions(std::vector<obelisk::SuggestAction>& suggestActions)
{
for (auto& suggestAction : suggestActions)
{
try
{
suggestAction.insert(dbConnection_);
}
catch (obelisk::DatabaseConstraintException& exception)
{
// ignore unique constraint error
if (std::strcmp(exception.what(),
"UNIQUE constraint failed: suggest_action.fact, suggest_action.true_action, suggest_action.false_action")
!= 0)
{
throw;
}
}
}
}
void obelisk::KnowledgeBase::getEntity(obelisk::Entity& entity) void obelisk::KnowledgeBase::getEntity(obelisk::Entity& entity)
{ {
entity.selectByName(dbConnection_); entity.selectByName(dbConnection_);
@ -173,6 +194,11 @@ void obelisk::KnowledgeBase::getFact(obelisk::Fact& fact)
fact.selectById(dbConnection_); fact.selectById(dbConnection_);
} }
void obelisk::KnowledgeBase::getSuggestAction(obelisk::SuggestAction& suggestAction)
{
suggestAction.selectById(dbConnection_);
}
void obelisk::KnowledgeBase::getFloat(float& result1, float& result2, double var) void obelisk::KnowledgeBase::getFloat(float& result1, float& result2, double var)
{ {
result1 = (float) var; result1 = (float) var;

View File

@ -111,7 +111,7 @@ namespace obelisk
void addActions(std::vector<obelisk::Action>& actions); void addActions(std::vector<obelisk::Action>& actions);
/** /**
* @brief Add facts to the database. * @brief Add facts to the KnowledgeBase.
* *
* @param[in,out] facts The facts to add. If the insert is * @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. * successful it will have a row ID, if not the ID will be 0.
@ -119,7 +119,16 @@ namespace obelisk
void addFacts(std::vector<obelisk::Fact>& facts); void addFacts(std::vector<obelisk::Fact>& facts);
/** /**
* @brief Get an entity object based on the ID it contains. * @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.
*/
void addSuggestActions(std::vector<obelisk::SuggestAction>& suggestActions);
/**
* @brief Get an Entity object based on the ID it contains.
* *
* @param[in,out] entity The Entity object should contain just the * @param[in,out] entity The Entity object should contain just the
* ID and the rest will be filled in. * ID and the rest will be filled in.
@ -127,7 +136,7 @@ namespace obelisk
void getEntity(obelisk::Entity& entity); void getEntity(obelisk::Entity& entity);
/** /**
* @brief Get a verb object based on the ID it contains. * @brief Get a Verb object based on the ID it contains.
* *
* @param[in,out] verb The Verb object should contain just the ID * @param[in,out] verb The Verb object should contain just the ID
* and the rest will be filled in. * and the rest will be filled in.
@ -135,7 +144,7 @@ namespace obelisk
void getVerb(obelisk::Verb& verb); void getVerb(obelisk::Verb& verb);
/** /**
* @brief Get an action based on the ID it contains. * @brief Get an Action based on the ID it contains.
* *
* @param[in] action The Action object should contain just the ID * @param[in] action The Action object should contain just the ID
* and the rest will be filled in. * and the rest will be filled in.
@ -143,13 +152,21 @@ namespace obelisk
void getAction(obelisk::Action& action); void getAction(obelisk::Action& action);
/** /**
* @brief Get a fact object based on the ID it contains. * @brief Get a Fact object based on the ID it contains.
* *
* @param[in,out] fact The fact object should contain just the ID * @param[in,out] fact The Fact object should contain just the ID
* and the rest will be filled in. * and the rest will be filled in.
*/ */
void getFact(obelisk::Fact& fact); void getFact(obelisk::Fact& fact);
/**
* @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.
*/
void getSuggestAction(obelisk::SuggestAction& suggestAction);
/** /**
* @brief Take a float and divide it into 2 floats. * @brief Take a float and divide it into 2 floats.
* *

View File

@ -549,8 +549,7 @@ void obelisk::Parser::handleAction(std::unique_ptr<obelisk::KnowledgeBase>& kb)
insertFact(kb, suggestAction.getFact()); insertFact(kb, suggestAction.getFact());
insertAction(kb, suggestAction.getTrueAction()); insertAction(kb, suggestAction.getTrueAction());
insertAction(kb, suggestAction.getFalseAction()); insertAction(kb, suggestAction.getFalseAction());
insertSuggestAction(kb, suggestAction);
// TODO: insert the actions, then insert the suggested action
} }
catch (obelisk::ParserException& exception) catch (obelisk::ParserException& exception)
{ {
@ -682,3 +681,21 @@ void obelisk::Parser::insertFact(std::unique_ptr<obelisk::KnowledgeBase>& kb, ob
} }
} }
} }
void obelisk::Parser::insertSuggestAction(std::unique_ptr<obelisk::KnowledgeBase>& kb,
obelisk::SuggestAction& suggestAction)
{
std::vector<obelisk::SuggestAction> suggestActions {suggestAction};
kb->addSuggestActions(suggestActions);
suggestAction = std::move(suggestActions.front());
// the id was not inserted, so check if it exists in the database
if (suggestAction.getId() == 0)
{
kb->getSuggestAction(suggestAction);
if (suggestAction.getId() == 0)
{
throw obelisk::ParserException("suggest_action could not be inserted into the database");
}
}
}

View File

@ -65,6 +65,8 @@ namespace obelisk
void insertVerb(std::unique_ptr<obelisk::KnowledgeBase>& kb, obelisk::Verb& verb); void insertVerb(std::unique_ptr<obelisk::KnowledgeBase>& kb, obelisk::Verb& verb);
void insertAction(std::unique_ptr<obelisk::KnowledgeBase>& kb, obelisk::Action& action); void insertAction(std::unique_ptr<obelisk::KnowledgeBase>& kb, obelisk::Action& action);
void insertFact(std::unique_ptr<obelisk::KnowledgeBase>& kb, obelisk::Fact& fact); void insertFact(std::unique_ptr<obelisk::KnowledgeBase>& kb, obelisk::Fact& fact);
void insertSuggestAction(std::unique_ptr<obelisk::KnowledgeBase>& kb,
obelisk::SuggestAction& suggestAction);
}; };
class ParserException : public std::exception class ParserException : public std::exception