diff --git a/src/lib/knowledge_base.cpp b/src/lib/knowledge_base.cpp index e0b48f6..37ba0a8 100644 --- a/src/lib/knowledge_base.cpp +++ b/src/lib/knowledge_base.cpp @@ -153,6 +153,27 @@ void obelisk::KnowledgeBase::addFacts(std::vector& facts) } } +void obelisk::KnowledgeBase::addSuggestActions(std::vector& 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) { entity.selectByName(dbConnection_); @@ -173,6 +194,11 @@ void obelisk::KnowledgeBase::getFact(obelisk::Fact& fact) fact.selectById(dbConnection_); } +void obelisk::KnowledgeBase::getSuggestAction(obelisk::SuggestAction& suggestAction) +{ + suggestAction.selectById(dbConnection_); +} + void obelisk::KnowledgeBase::getFloat(float& result1, float& result2, double var) { result1 = (float) var; diff --git a/src/lib/knowledge_base.h b/src/lib/knowledge_base.h index 7fb2bde..ddb1165 100644 --- a/src/lib/knowledge_base.h +++ b/src/lib/knowledge_base.h @@ -111,7 +111,7 @@ namespace obelisk void addActions(std::vector& 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 * successful it will have a row ID, if not the ID will be 0. @@ -119,7 +119,16 @@ namespace obelisk void addFacts(std::vector& 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& suggestActions); + + /** + * @brief Get an Entity object based on the ID it contains. * * @param[in,out] entity The Entity object should contain just the * ID and the rest will be filled in. @@ -127,7 +136,7 @@ namespace obelisk 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 * and the rest will be filled in. @@ -135,7 +144,7 @@ namespace obelisk 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 * and the rest will be filled in. @@ -143,13 +152,21 @@ namespace obelisk 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. */ 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. * diff --git a/src/parser.cpp b/src/parser.cpp index 46f4a31..e9f0b2e 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -549,8 +549,7 @@ void obelisk::Parser::handleAction(std::unique_ptr& kb) insertFact(kb, suggestAction.getFact()); insertAction(kb, suggestAction.getTrueAction()); insertAction(kb, suggestAction.getFalseAction()); - - // TODO: insert the actions, then insert the suggested action + insertSuggestAction(kb, suggestAction); } catch (obelisk::ParserException& exception) { @@ -682,3 +681,21 @@ void obelisk::Parser::insertFact(std::unique_ptr& kb, ob } } } + +void obelisk::Parser::insertSuggestAction(std::unique_ptr& kb, + obelisk::SuggestAction& suggestAction) +{ + std::vector 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"); + } + } +} diff --git a/src/parser.h b/src/parser.h index 7c6ac1d..7131a9e 100644 --- a/src/parser.h +++ b/src/parser.h @@ -65,6 +65,8 @@ namespace obelisk void insertVerb(std::unique_ptr& kb, obelisk::Verb& verb); void insertAction(std::unique_ptr& kb, obelisk::Action& action); void insertFact(std::unique_ptr& kb, obelisk::Fact& fact); + void insertSuggestAction(std::unique_ptr& kb, + obelisk::SuggestAction& suggestAction); }; class ParserException : public std::exception