Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/manager/agents/common-module/constants/command_constants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ std::string const CommandConstants::COMPONENTS_COMMAND_INIT = "init";
std::string const CommandConstants::COMPONENTS_COMMAND_INSTALL = "install";
std::string const CommandConstants::COMPONENTS_COMMAND_SEARCH = "search";

std::string const CommandsConstantsFlags::IDTF = "idtf";
std::string const CommandsConstantsFlags::SET = "set";
std::string const CommandsConstantsFlags::EXPLANATION = "explanation";
std::string const CommandsConstantsFlags::CLASS = "class";
std::string const CommandsConstantsFlags::AUTHOR = "author";
std::string const CommandsConstantsFlags::NOTE = "note";
std::string const CommandsConstantsFlags::PURPOSE = "purpose";
std::string const CommandsConstantsFlags::KEY = "key";
std::string const CommandsConstantsFlags::MAIN_ID = "main-id";
std::string const CommandsSearchFlags::IDTF = "idtf";
std::string const CommandsSearchFlags::SET = "set";
std::string const CommandsSearchFlags::EXPLANATION = "explanation";
std::string const CommandsSearchFlags::CLASS = "class";
std::string const CommandsSearchFlags::AUTHOR = "author";
std::string const CommandsSearchFlags::NOTE = "note";
std::string const CommandsSearchFlags::PURPOSE = "purpose";
std::string const CommandsSearchFlags::KEY = "key";
std::string const CommandsSearchFlags::MAIN_ID = "main-id";

std::string const PathKeysOfConfigPath::KB_PATH = "knowledge_base_components_path";
std::string const PathKeysOfConfigPath::PS_PATH = "problem_solver_components_path";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class CommandConstants
static std::map<std::string, std::string> COMMAND_MAP;
};

class CommandsConstantsFlags
class CommandsSearchFlags
{
public:
static std::string const IDTF;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,7 @@ ScAddr ScComponentManagerKeynodes::nrel_ostis_system_decomposition;
ScAddr ScComponentManagerKeynodes::action_components_init;
ScAddr ScComponentManagerKeynodes::action_components_search;
ScAddr ScComponentManagerKeynodes::action_components_install;
ScAddr ScComponentManagerKeynodes::rrel_author;
ScAddr ScComponentManagerKeynodes::rrel_sets;
ScAddr ScComponentManagerKeynodes::rrel_components;
ScAddr ScComponentManagerKeynodes::rrel_class;
ScAddr ScComponentManagerKeynodes::rrel_explanation;
ScAddr ScComponentManagerKeynodes::rrel_identifier;
ScAddr ScComponentManagerKeynodes::rrel_note;
ScAddr ScComponentManagerKeynodes::rrel_purpose;
ScAddr ScComponentManagerKeynodes::rrel_main_idtf;
} // namespace keynodes
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,6 @@ class ScComponentManagerKeynodes : public ScObject
SC_PROPERTY(Keynode("action_components_install"), ForceCreate(ScType::NodeConstClass))
static ScAddr action_components_install;

SC_PROPERTY(Keynode("rrel_author"), ForceCreate(ScType::NodeConstRole))
static ScAddr rrel_author;

SC_PROPERTY(Keynode("rrel_class"), ForceCreate(ScType::NodeConstRole))
static ScAddr rrel_class;

Expand All @@ -138,21 +135,6 @@ class ScComponentManagerKeynodes : public ScObject

SC_PROPERTY(Keynode("rrel_components"), ForceCreate(ScType::NodeConstRole))
static ScAddr rrel_components;

SC_PROPERTY(Keynode("rrel_explanation"), ForceCreate(ScType::NodeConstRole))
static ScAddr rrel_explanation;

SC_PROPERTY(Keynode("rrel_identifier"), ForceCreate(ScType::NodeConstRole))
static ScAddr rrel_identifier;

SC_PROPERTY(Keynode("rrel_note"), ForceCreate(ScType::NodeConstNoRole))
static ScAddr rrel_note;

SC_PROPERTY(Keynode("rrel_purpose"), ForceCreate(ScType::NodeConstNoRole))
static ScAddr rrel_purpose;

SC_PROPERTY(Keynode("rrel_main_idtf"), ForceCreate(ScType::NodeConstNoRole))
static ScAddr rrel_main_idtf;
};

} // namespace keynodes
108 changes: 10 additions & 98 deletions src/manager/agents/common-module/module/utils/common_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
*/

#include "common_utils.hpp"

#include <sc-agents-common/utils/GenerationUtils.hpp>
#include <sc-agents-common/utils/IteratorUtils.hpp>
#include <sc-agents-common/utils/CommonUtils.hpp>
#include "sc-agents-common/keynodes/coreKeynodes.hpp"

#include "sc-memory/sc_memory.hpp"
Expand Down Expand Up @@ -58,18 +60,9 @@ void CommonUtils::CreateMyselfDecomposition(ScMemoryContext & context)
void CommonUtils::InitParametersMap()
{
managerParametersWithAgentRelations = {
{CommandsConstantsFlags::AUTHOR, keynodes::ScComponentManagerKeynodes::rrel_author},
{CommandsConstantsFlags::CLASS, keynodes::ScComponentManagerKeynodes::rrel_class},
{CommandsConstantsFlags::EXPLANATION, keynodes::ScComponentManagerKeynodes::rrel_explanation},
{CommandsConstantsFlags::NOTE, keynodes::ScComponentManagerKeynodes::rrel_note},
{CommandsConstantsFlags::PURPOSE, keynodes::ScComponentManagerKeynodes::rrel_purpose},
{CommandsConstantsFlags::KEY, scAgentsCommon::CoreKeynodes::rrel_key_sc_element},
{CommandsConstantsFlags::MAIN_ID, keynodes::ScComponentManagerKeynodes::rrel_main_idtf},
{CommandsConstantsFlags::IDTF, keynodes::ScComponentManagerKeynodes::rrel_components},
{CommandsConstantsFlags::SET, keynodes::ScComponentManagerKeynodes::rrel_sets},
{CommandConstants::COMPONENTS_COMMAND_SEARCH, keynodes::ScComponentManagerKeynodes::action_components_search},
{CommandConstants::COMPONENTS_COMMAND_INSTALL, keynodes::ScComponentManagerKeynodes::action_components_install},
{CommandConstants::COMPONENTS_COMMAND_INIT, keynodes::ScComponentManagerKeynodes::action_components_init}};
{CommandsSearchFlags::KEY, scAgentsCommon::CoreKeynodes::rrel_key_sc_element},
{CommandsSearchFlags::IDTF, keynodes::ScComponentManagerKeynodes::rrel_components},
{CommandsSearchFlags::SET, keynodes::ScComponentManagerKeynodes::rrel_sets}};

mainFlagWithSubFlags = {
{"rrel_1",
Expand Down Expand Up @@ -149,10 +142,10 @@ bool CommonUtils::TranslateFromStringToScMemory(

for (std::string const & parameterValue : parameter.second)
{
if (parameter.first == CommandsConstantsFlags::EXPLANATION ||
parameter.first == CommandsConstantsFlags::NOTE ||
parameter.first == CommandsConstantsFlags::PURPOSE ||
parameter.first == CommandsConstantsFlags::MAIN_ID)
if (parameter.first == CommandsSearchFlags::EXPLANATION ||
parameter.first == CommandsSearchFlags::NOTE ||
parameter.first == CommandsSearchFlags::PURPOSE ||
parameter.first == CommandsSearchFlags::MAIN_ID)
{
parameterValueAddr = context.CreateNode(ScType::LinkConst);
context.SetLinkContent(parameterValueAddr, parameterValue);
Expand Down Expand Up @@ -215,88 +208,6 @@ ScAddrUnorderedSet CommonUtils::GetComponentsToInstall(ScMemoryContext & context
return components;
}

// TODO: there is a lot of repetitive code, it needs to be optimized
std::map<std::string, std::vector<std::string>> CommonUtils::GetCommandParameters(
ScMemoryContext & context,
ScAddr const & actionAddr)
{
std::map<std::string, std::vector<std::string>> commandParameters;

ScAddr const & authorsSetAddr = utils::IteratorUtils::getAnyByOutRelation(
&context, actionAddr, keynodes::ScComponentManagerKeynodes::rrel_author);
std::map<std::string, ScAddr> const & authors = GetSetElements(context, authorsSetAddr);

ScAddr const & classesSetAddr =
utils::IteratorUtils::getAnyByOutRelation(&context, actionAddr, keynodes::ScComponentManagerKeynodes::rrel_class);
std::map<std::string, ScAddr> const & classes = GetSetElements(context, classesSetAddr);

ScAddr const & explanationsSetAddr = utils::IteratorUtils::getAnyByOutRelation(
&context, actionAddr, keynodes::ScComponentManagerKeynodes::rrel_explanation);
std::map<std::string, ScAddr> const & explanations = GetElementsLinksOfSet(context, explanationsSetAddr);

ScAddr const & notesSetAddr = utils::IteratorUtils::getAnyByOutRelation(
&context, actionAddr, keynodes::ScComponentManagerKeynodes::rrel_note);
std::map<std::string, ScAddr> const & notes = GetElementsLinksOfSet(context, notesSetAddr);

ScAddr const & purposesSetAddr = utils::IteratorUtils::getAnyByOutRelation(
&context, actionAddr, keynodes::ScComponentManagerKeynodes::rrel_purpose);
std::map<std::string, ScAddr> const & purposes = GetElementsLinksOfSet(context, purposesSetAddr);

ScAddr const & keysSetAddr = utils::IteratorUtils::getAnyByOutRelation(
&context, actionAddr, scAgentsCommon::CoreKeynodes::rrel_key_sc_element);
std::map<std::string, ScAddr> const & keys = GetElementsLinksOfSet(context, keysSetAddr);

ScAddr const & idsSetAddr = utils::IteratorUtils::getAnyByOutRelation(
&context, actionAddr, keynodes::ScComponentManagerKeynodes::rrel_main_idtf);
std::map<std::string, ScAddr> const & ids = GetElementsLinksOfSet(context, idsSetAddr);

std::vector<std::string> authorsList, classesList, explanationsList, notesList, purposesList, keysList, idsList;

if (context.IsElement(authorsSetAddr))
{
for (auto & el : authors)
authorsList.push_back(el.first);
commandParameters.insert({CommandsConstantsFlags::AUTHOR, authorsList});
}
if (context.IsElement(classesSetAddr))
{
for (auto & el : classes)
classesList.push_back(el.first);
commandParameters.insert({CommandsConstantsFlags::CLASS, classesList});
}
if (context.IsElement(explanationsSetAddr))
{
for (auto & el : explanations)
explanationsList.push_back(el.first);
commandParameters.insert({CommandsConstantsFlags::EXPLANATION, explanationsList});
}
if (context.IsElement(notesSetAddr))
{
for (auto & el : notes)
notesList.push_back(el.first);
commandParameters.insert({CommandsConstantsFlags::NOTE, notesList});
}
if (context.IsElement(purposesSetAddr))
{
for (auto & el : purposes)
purposesList.push_back(el.first);
commandParameters.insert({CommandsConstantsFlags::PURPOSE, purposesList});
}
if (context.IsElement(keysSetAddr))
{
for (auto & el : keys)
keysList.push_back(el.first);
commandParameters.insert({CommandsConstantsFlags::KEY, keysList});
}
if (context.IsElement(idsSetAddr))
{
for (auto & el : ids)
idsList.push_back(el.first);
commandParameters.insert({CommandsConstantsFlags::MAIN_ID, idsList});
}
return commandParameters;
}

std::map<std::string, ScAddr> CommonUtils::GetSetElements(ScMemoryContext & context, ScAddr const & setAddr)
{
std::map<std::string, ScAddr> elementsIdtfAndAddr;
Expand Down Expand Up @@ -422,4 +333,5 @@ bool CommonUtils::CheckIfFullMyselfDecompositionExists(ScMemoryContext & context
}
return true;
}

} // namespace common_utils
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ class CommonUtils
static ScAddrUnorderedSet GetComponentsToInstall(ScMemoryContext & context, ScAddr const & actionAddr);
static std::map<std::string, ScAddr> GetSetElements(ScMemoryContext & context, ScAddr const & setAddr);
static std::map<std::string, ScAddr> GetElementsLinksOfSet(ScMemoryContext & context, ScAddr const & setAddr);
static std::map<std::string, std::vector<std::string>> GetCommandParameters(
ScMemoryContext & context,
ScAddr const & actionAddr);
static ScAddr GetSubsystemDecompositionAddr(ScMemoryContext & context, ScAddr const & component);
static bool CheckIfInstalled(ScMemoryContext & context, ScAddr const & component);
static ScAddr GetComponentBySpecification(ScMemoryContext & context, ScAddr const & specification);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ ScAddrUnorderedSet ScComponentManagerCommandInit::Execute(ScMemoryContext * cont
* @param context current sc-memory context
* @param repository ScAddr of a repository of components to load
*/
void ScComponentManagerCommandInit::ProcessRepository(ScMemoryContext * context, ScAddr & repository, ScAddrUnorderedSet & specifications)
void ScComponentManagerCommandInit::ProcessRepository(
ScMemoryContext * context,
ScAddr & repository,
ScAddrUnorderedSet & specifications)
{
ScAddr const & specificationsSetAddr = utils::IteratorUtils::getAnyByOutRelation(
context, repository, keynodes::ScComponentManagerKeynodes::rrel_components_specifications);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <algorithm>

#include "sc-agents-common/keynodes/coreKeynodes.hpp"
#include "sc-agents-common/utils/IteratorUtils.hpp"

#include "sc_component_manager_command_search.hpp"

Expand All @@ -17,108 +18,27 @@ using namespace common_utils;

ScAddrUnorderedSet ScComponentManagerCommandSearch::Execute(ScMemoryContext * context, ScAddr const & actionAddr)
{
std::map<std::string, std::vector<std::string>> commandParameters =
CommonUtils::GetCommandParameters(*context, actionAddr);
for (auto const & param : commandParameters)
{
if (std::find(possibleSearchParameters.cbegin(), possibleSearchParameters.cend(), param.first)
== possibleSearchParameters.cend())
{
SC_THROW_EXCEPTION(
utils::ExceptionParseError, "ScComponentManagerCommandSearch: Unsupported search parameter " << param.first);
}
}
ScAddrUnorderedSet componentsSpecifications;
ScAddr const searchStructure = utils::IteratorUtils::getAnyByOutRelation(context, actionAddr, scAgentsCommon::CoreKeynodes::rrel_1);

ScTemplate searchComponentTemplate;
searchComponentTemplate.Quintuple(
ScType::NodeVarStruct >> SPECIFICATION_ALIAS,
ScType::EdgeAccessVarPosPerm,
ScType::NodeVar >> COMPONENT_ALIAS,
ScType::EdgeAccessVarPosPerm,
scAgentsCommon::CoreKeynodes::rrel_key_sc_element);
searchComponentTemplate.Triple(
keynodes::ScComponentManagerKeynodes::concept_reusable_component, ScType::EdgeAccessVarPosPerm, COMPONENT_ALIAS);
ScTemplate searchTemplate;
context->HelperBuildTemplate(searchTemplate, searchStructure);

if (commandParameters.find(CLASS) != commandParameters.cend())
context->HelperSearchTemplate(searchTemplate,
[&context, searchStructure](ScTemplateResultItem const & item)
{
SearchComponentsByClass(context, searchComponentTemplate, commandParameters.at(CLASS));
}

for (size_t sequenceNumber = 0; sequenceNumber < searchByRelation.size(); sequenceNumber++)
{
if (commandParameters.find(std::get<0>(searchByRelation[sequenceNumber])) != commandParameters.cend())
ScIterator5Ptr const reusableComponentIterator = context->Iterator5(searchStructure, ScType::EdgeAccessConstPosPerm, keynodes::ScComponentManagerKeynodes::concept_reusable_component, ScType::EdgeAccessVarPosPerm, ScType::NodeVar);
if (reusableComponentIterator->Next())
{
SearchComponentsByRelationSet(
context,
std::get<1>(searchByRelation[sequenceNumber]),
std::get<2>(searchByRelation[sequenceNumber]),
searchComponentTemplate,
commandParameters.at(std::get<0>(searchByRelation[sequenceNumber])));
}
}

std::map<std::string, ScAddrUnorderedSet> linksValues;
for (size_t sequenceNumber = 0; sequenceNumber < searchByLine.size(); sequenceNumber++)
{
if (commandParameters.find(std::get<0>(searchByLine[sequenceNumber])) != commandParameters.cend()
&& !commandParameters.find(std::get<0>(searchByLine[sequenceNumber]))->second.empty())
{
ScAddrUnorderedSet links = SearchComponentsByRelationLink(
context,
std::get<1>(searchByLine[sequenceNumber]),
std::get<2>(searchByLine[sequenceNumber]),
searchComponentTemplate,
commandParameters.at(std::get<0>(searchByLine[sequenceNumber])));
linksValues.insert({std::get<2>(searchByLine[sequenceNumber]), links});
}
}

ScAddrUnorderedSet componentsSpecifications =
SearchComponentsSpecifications(context, searchComponentTemplate, linksValues);

return componentsSpecifications;
}

void ScComponentManagerCommandSearch::SearchComponentsByRelationSet(
ScMemoryContext * context,
ScAddr const & relationAddr,
std::string const & setAlias,
ScTemplate & searchComponentTemplate,
std::vector<std::string> const & parameters)
{
searchComponentTemplate.Quintuple(
COMPONENT_ALIAS,
ScType::EdgeDCommonVar,
ScType::NodeVar >> setAlias, // NodeVarTuple
ScType::EdgeAccessVarPosPerm,
relationAddr);
for (std::string const & parameterIdentifier : parameters)
},
[&context](ScAddr const & item)
{
ScAddr parameterAddr = context->HelperFindBySystemIdtf(parameterIdentifier);
if (!parameterAddr.IsValid())
{
searchComponentTemplate.Clear();
break;
}
searchComponentTemplate.Triple(setAlias, ScType::EdgeAccessVarPosPerm, parameterAddr);
}
}
return false;
});

void ScComponentManagerCommandSearch::SearchComponentsByClass(
ScMemoryContext * context,
ScTemplate & searchComponentTemplate,
std::vector<std::string> const & parameters)
{
for (std::string const & classIdentifier : parameters)
{
ScAddr classAddr = context->HelperFindBySystemIdtf(classIdentifier);
if (!classAddr.IsValid())
{
searchComponentTemplate.Clear();
break;
}
searchComponentTemplate.Triple(classAddr, ScType::EdgeAccessVarPosPerm, COMPONENT_ALIAS);
}
return componentsSpecifications;
}

ScAddrUnorderedSet ScComponentManagerCommandSearch::SearchComponentsByRelationLink(
Expand Down
Loading