Skip to content

Commit

Permalink
refactor: identifiers module directories and sources
Browse files Browse the repository at this point in the history
  • Loading branch information
NikitaZotov committed Feb 27, 2024
1 parent f0da41e commit 4886793
Show file tree
Hide file tree
Showing 15 changed files with 237 additions and 226 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
add_subdirectory(identifiersModule)
add_subdirectory(identifiers-module)
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
set(IDENTIFIERS_MODULE_SRC "${PROBLEM_SOLVER_PATH}/cxx/identifiersModule")
set(IDENTIFIERS_MODULE_SRC "${PROBLEM_SOLVER_PATH}/cxx/identifiers-module")

set(SOURCES
"agent/TranslateMainSystemIdtfsFromScToFileAgent.cpp" "agent/TranslateMainSystemIdtfsFromScToFileAgent.hpp"
"keynodes/IdentifiersKeynodes.cpp" "keynodes/IdentifiersKeynodes.hpp"
"IdentifiersModule.cpp" "IdentifiersModule.hpp"
"agent/translate_main_system_idtfs_from_sc_to_file_agent.cpp"
"agent/translate_main_system_idtfs_from_sc_to_file_agent.hpp"
"keynodes/identifiers_keynodes.cpp"
"keynodes/identifiers_keynodes.hpp"
"identifiers_module.cpp"
"identifiers_module.hpp"
)

include_directories(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@

#include "sc-agents-common/utils/CommonUtils.hpp"
#include "sc-agents-common/utils/AgentUtils.hpp"
#include "keynodes/IdentifiersKeynodes.hpp"
#include "TranslateMainSystemIdtfsFromScToFileAgent.hpp"
#include "keynodes/identifiers_keynodes.hpp"
#include "translate_main_system_idtfs_from_sc_to_file_agent.hpp"

using namespace identifiersModule;

SC_AGENT_IMPLEMENTATION(TranslateMainSystemIdtfsFromScToFileAgent)
{
ScAddr const & actionAddr = otherAddr;

if (!checkAction(actionAddr)) {
if (!CheckAction(actionAddr)) {
return SC_RESULT_OK;
}

Expand Down Expand Up @@ -43,9 +43,9 @@ SC_AGENT_IMPLEMENTATION(TranslateMainSystemIdtfsFromScToFileAgent)
edgeBelongsToNrelSystemIdtf = iterator3PtrEdgeBelongsToNrelSystemIdtf->Get(2);
sourceOfEdgeBelongsToNrelSystemIdtf = m_memoryCtx.GetEdgeSource(edgeBelongsToNrelSystemIdtf);
try {
systemIdentifier = getSystemIdtfAndVerifyNode(m_memoryCtx, sourceOfEdgeBelongsToNrelSystemIdtf);
mainIdentifier = getMainIdtfAndVerifyNode(m_memoryCtx, sourceOfEdgeBelongsToNrelSystemIdtf);
stringType = getStrScType(sourceOfEdgeBelongsToNrelSystemIdtf);
systemIdentifier = GetSystemIdtfAndVerifyNode(m_memoryCtx, sourceOfEdgeBelongsToNrelSystemIdtf);
mainIdentifier = GetMainIdtfAndVerifyNode(m_memoryCtx, sourceOfEdgeBelongsToNrelSystemIdtf);
stringType = GetStrScType(sourceOfEdgeBelongsToNrelSystemIdtf);

if (!systemIdentifier.empty() && !mainIdentifier.empty() && !stringType.empty()) {
streamIdtfs << "{\"" << mainIdentifier << "\", "
Expand All @@ -69,7 +69,7 @@ SC_AGENT_IMPLEMENTATION(TranslateMainSystemIdtfsFromScToFileAgent)
}
}

bool const & resultOfWrite = writeInFile(strIdtfs);
bool const & resultOfWrite = WriteInFile(strIdtfs);

if (resultOfWrite) {
SC_LOG_DEBUG("File has been created");
Expand All @@ -81,15 +81,15 @@ SC_AGENT_IMPLEMENTATION(TranslateMainSystemIdtfsFromScToFileAgent)
return SC_RESULT_OK;
}

bool TranslateMainSystemIdtfsFromScToFileAgent::checkAction(ScAddr const & actionAddr) {
bool TranslateMainSystemIdtfsFromScToFileAgent::CheckAction(ScAddr const & actionAddr) {
return m_memoryCtx.HelperCheckEdge(
IdentifiersKeynodes::action_find_identifiers,
actionAddr,
ScType::EdgeAccessConstPosPerm
);
}

std::string TranslateMainSystemIdtfsFromScToFileAgent::getSystemIdtfAndVerifyNode(ScMemoryContext & m_memoryCtx, ScAddr const & node) {
std::string TranslateMainSystemIdtfsFromScToFileAgent::GetSystemIdtfAndVerifyNode(ScMemoryContext & m_memoryCtx, ScAddr const & node) {
std::string identifier;
ScAddr identifierLink;
ScIterator5Ptr const & iterator5PtrCheckOnlyOneIdtf = m_memoryCtx.Iterator5(
Expand All @@ -110,7 +110,7 @@ std::string TranslateMainSystemIdtfsFromScToFileAgent::getSystemIdtfAndVerifyNod
return identifier;
}

std::string TranslateMainSystemIdtfsFromScToFileAgent::getMainIdtfAndVerifyNode(ScMemoryContext & m_memoryCtx, ScAddr const & node) {
std::string TranslateMainSystemIdtfsFromScToFileAgent::GetMainIdtfAndVerifyNode(ScMemoryContext & m_memoryCtx, ScAddr const & node) {
std::string identifier;
ScAddr mainIdentifierLink;
ScAddr mainAnotherIdentifierLink;
Expand Down Expand Up @@ -145,7 +145,7 @@ std::string TranslateMainSystemIdtfsFromScToFileAgent::getMainIdtfAndVerifyNode(
return identifier;
}

std::string TranslateMainSystemIdtfsFromScToFileAgent::getStrScType(ScAddr const & node) {
std::string TranslateMainSystemIdtfsFromScToFileAgent::GetStrScType(ScAddr const & node) {
std::string strType;
ScType const & type = m_memoryCtx.GetElementType(node);
if (ScTypesOfNodesWithSCsClasses.count(type)) {
Expand All @@ -156,7 +156,7 @@ std::string TranslateMainSystemIdtfsFromScToFileAgent::getStrScType(ScAddr const
return strType;
}

bool TranslateMainSystemIdtfsFromScToFileAgent::writeInFile(std::string const & strIdentifiers) {
bool TranslateMainSystemIdtfsFromScToFileAgent::WriteInFile(std::string const & strIdentifiers) {
try {
std::ofstream file(IDENTIFIERS_MODULE_PATH "identifiers.txt");
if (file.is_open()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "sc-memory/kpm/sc_agent.hpp"
#include "sc-agents-common/keynodes/coreKeynodes.hpp"

#include "TranslateMainSystemIdtfsFromScToFileAgent.generated.hpp"
#include "translate_main_system_idtfs_from_sc_to_file_agent.generated.hpp"

namespace identifiersModule
{
Expand Down Expand Up @@ -83,11 +83,11 @@ class TranslateMainSystemIdtfsFromScToFileAgent : public ScAgent
{ScType::EdgeAccessVarFuzTemp, "sc_edge_main"}};

private:
bool checkAction(ScAddr const & actionAddr);
std::string getStrScType(ScAddr const & node);
static std::string getSystemIdtfAndVerifyNode(ScMemoryContext & m_memoryCtx, ScAddr const & node);
static std::string getMainIdtfAndVerifyNode(ScMemoryContext & m_memoryCtx, ScAddr const & node);
static bool writeInFile(std::string const & strIdtfs);
bool CheckAction(ScAddr const & actionAddr);
std::string GetStrScType(ScAddr const & node);
static std::string GetSystemIdtfAndVerifyNode(ScMemoryContext & m_memoryCtx, ScAddr const & node);
static std::string GetMainIdtfAndVerifyNode(ScMemoryContext & m_memoryCtx, ScAddr const & node);
static bool WriteInFile(std::string const & strIdtfs);
};

} // namespace identifiersModule
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
* (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
*/

#include "IdentifiersModule.hpp"
#include "identifiers_module.hpp"

#include "keynodes/IdentifiersKeynodes.hpp"
#include "agent/TranslateMainSystemIdtfsFromScToFileAgent.hpp"
#include "keynodes/identifiers_keynodes.hpp"
#include "agent/translate_main_system_idtfs_from_sc_to_file_agent.hpp"

using namespace identifiersModule;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "sc-memory/sc_module.hpp"

#include "IdentifiersModule.generated.hpp"
#include "identifiers_module.generated.hpp"

namespace identifiersModule
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
*/

#include "IdentifiersKeynodes.hpp"
#include "identifiers_keynodes.hpp"

#include "sc-memory/sc_memory.hpp"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "sc-memory/sc_addr.hpp"
#include "sc-memory/sc_object.hpp"

#include "IdentifiersKeynodes.generated.hpp"
#include "identifiers_keynodes.generated.hpp"

namespace identifiersModule
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
#include "scs_loader.hpp"
#include "sc_wait.hpp"
#include "sc_test.hpp"
#include "sc-agents-common/utils/CommonUtils.hpp"
#include "sc-agents-common/utils/AgentUtils.hpp"
#include "keynodes/identifiers_keynodes.hpp"
#include "agent/translate_main_system_idtfs_from_sc_to_file_agent.hpp"
#include "sc-agents-common/keynodes/coreKeynodes.hpp"

using namespace identifiersModule;
using AgentTest = ScMemoryTest;

namespace ModuleTest
{
ScsLoader loader;
std::string const TEST_FILES_DIR_PATH = MODULE_TEST_SRC_PATH "/test-structures/";

int const WAIT_TIME = 1000;

void Initialize()
{
scAgentsCommon::CoreKeynodes::InitGlobal();
IdentifiersKeynodes::InitGlobal();
}

void Shutdown()
{
SC_AGENT_UNREGISTER(TranslateMainSystemIdtfsFromScToFileAgent);
}

std::string GetFileContent()
{
char fileName[] = IDENTIFIERS_MODULE_PATH "identifiers.txt";
std::ifstream file(fileName);
std::stringstream fileContent;
std::string line;
try
{
if (file.is_open())
{
while (std::getline(file, line))
fileContent << line;
file.close();
std::remove(fileName);
}
else
{
SC_THROW_EXCEPTION(utils::ScException, "Cant find file with identifiers");
}
}
catch (utils::ScException const & exception)
{
SC_LOG_ERROR(exception.Description());
}
return fileContent.str();
}

bool IsSubstringOfFile(std::vector<std::string> const & input, std::string const & content)
{
return std::all_of(begin(input), end(input),
[&](std::string const & el)
{
return content.find(el) != std::string::npos;
});
}

TEST_F(AgentTest, VoidTest)
{
ScMemoryContext & context = *m_ctx;

loader.loadScsFile(context, TEST_FILES_DIR_PATH + "void_test.scs");
ScAddr const & testActionNode = context.HelperFindBySystemIdtf("test_action_node");

ScAgentInit(true);
Initialize();

SC_AGENT_REGISTER(TranslateMainSystemIdtfsFromScToFileAgent)

context.CreateEdge(
ScType::EdgeAccessConstPosPerm,
scAgentsCommon::CoreKeynodes::question_initiated,
testActionNode);

utils::AgentUtils::applyAction(&context, testActionNode, WAIT_TIME);

EXPECT_TRUE(context.HelperCheckEdge(
scAgentsCommon::CoreKeynodes::question_finished_successfully,
testActionNode,
ScType::EdgeAccessConstPosPerm));

std::string const fileContent = GetFileContent();

EXPECT_TRUE(fileContent.empty());

SC_AGENT_UNREGISTER(TranslateMainSystemIdtfsFromScToFileAgent)
}

TEST_F(AgentTest, CorrectTest)
{
ScMemoryContext & context = *m_ctx;
loader.loadScsFile(context, TEST_FILES_DIR_PATH + "correct_test.scs");
ScAddr const & testActionNode = context.HelperFindBySystemIdtf("test_action_node");

ScAgentInit(true);
Initialize();
SC_AGENT_REGISTER(TranslateMainSystemIdtfsFromScToFileAgent)

context.CreateEdge(
ScType::EdgeAccessConstPosPerm,
scAgentsCommon::CoreKeynodes::question_initiated,
testActionNode);

utils::AgentUtils::applyAction(&context, testActionNode, WAIT_TIME);

EXPECT_TRUE(context.HelperCheckEdge(
scAgentsCommon::CoreKeynodes::question_finished_successfully,
testActionNode,
ScType::EdgeAccessConstPosPerm));

std::string const & space = R"({"space", "sc_node_class"})";
std::string const & knowledge = R"({"знание", {"knowledge", "sc_node_class"}})";
std::string const & variable = R"({"переменная", {"no_name", "sc_node"}})";
std::string const & query = R"({"очередь", {"query", "sc_node"}})";

std::vector<std::string> const trueInput{knowledge, variable, query};
std::vector<std::string> const falseInput{space};

std::string const fileContent = GetFileContent();

bool const trueResult = IsSubstringOfFile(trueInput, fileContent);
bool const falseResult = IsSubstringOfFile(falseInput, fileContent);

EXPECT_TRUE(!falseResult && trueResult);
SC_AGENT_UNREGISTER(TranslateMainSystemIdtfsFromScToFileAgent)
}

TEST_F(AgentTest, IncorrectTest)
{
ScMemoryContext & context = *m_ctx;
loader.loadScsFile(context, TEST_FILES_DIR_PATH + "incorrect_test.scs");
ScAddr const & testActionNode = context.HelperFindBySystemIdtf("test_action_node");

ScAgentInit(true);
Initialize();
SC_AGENT_REGISTER(TranslateMainSystemIdtfsFromScToFileAgent)

context.CreateEdge(
ScType::EdgeAccessConstPosPerm,
scAgentsCommon::CoreKeynodes::question_initiated,
testActionNode);

utils::AgentUtils::applyAction(&context, testActionNode, WAIT_TIME);

EXPECT_TRUE(context.HelperCheckEdge(
scAgentsCommon::CoreKeynodes::question_finished_successfully,
testActionNode,
ScType::EdgeAccessConstPosPerm));

std::string const fileContent = GetFileContent();

std::string const & incorrectVariable = R"({"переменная", {"no_name", "sc_node"}})";
std::string const & correctVariable = R"({"Переменная", {"No_name", "sc_node"}})";
std::string const & knowledge = R"({"знание", {"knowledge", "sc_node_class"}})";
std::string const & space = R"({"пространство", {"space", "sc_node_class"}})";
std::string const & query = R"({"очередь", {"query", "sc_node"}})";

std::vector<std::string> const falseInput = {knowledge, space, query, incorrectVariable};
std::vector<std::string> const trueInput = {correctVariable};

bool const trueResult = IsSubstringOfFile(trueInput, fileContent);
bool const falseResult = IsSubstringOfFile(falseInput, fileContent);

EXPECT_TRUE(!falseResult && trueResult);

SC_AGENT_UNREGISTER(TranslateMainSystemIdtfsFromScToFileAgent)
}

TEST_F(AgentTest, manySystemIdtfsTest)
{
ScMemoryContext & context = *m_ctx;
loader.loadScsFile(context, TEST_FILES_DIR_PATH + "many_system_idtfs_test.scs");
ScAddr const & testActionNode = context.HelperFindBySystemIdtf("test_action_node");

ScAgentInit(true);
Initialize();
SC_AGENT_REGISTER(TranslateMainSystemIdtfsFromScToFileAgent)

context.CreateEdge(
ScType::EdgeAccessConstPosPerm,
scAgentsCommon::CoreKeynodes::question_initiated,
testActionNode);

utils::AgentUtils::applyAction(&context, testActionNode, WAIT_TIME);

bool result = context.HelperCheckEdge(
scAgentsCommon::CoreKeynodes::question_finished_unsuccessfully,
testActionNode,
ScType::EdgeAccessConstPosPerm);

EXPECT_TRUE(result);

SC_AGENT_UNREGISTER(TranslateMainSystemIdtfsFromScToFileAgent)
}

} // namespace ModuleTest
Loading

0 comments on commit 4886793

Please sign in to comment.