Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[refactor][install] Add applyAction for dependency loading #110

Merged
merged 5 commits into from
Jun 19, 2024
Merged
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
34 changes: 34 additions & 0 deletions kb/specifications.scs
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,38 @@ sc_component_manager_repository
*);;
*);;
*);;
*);
-> part_figure
(*
<- concept_reusable_component_specification;;
=> nrel_alternative_addresses:
...
(*
<- sc_node_tuple;;
-> rrel_1:
...
(*
-> [https://github.com/MksmOrlov/ostis-kb-lib/tree/main/part_figure/part_figure]
(*
<- concept_github_url;;
*);;
*);;
*);;
*);
-> part_lr
(*
<- concept_reusable_component_specification;;
=> nrel_alternative_addresses:
...
(*
<- sc_node_tuple;;
-> rrel_1:
...
(*
-> [https://github.com/MksmOrlov/ostis-kb-lib/tree/main/part_lr/part_lr]
(*
<- concept_github_url;;
*);;
*);;
*);;
*);;
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace keynodes
{
ScAddr ScComponentManagerKeynodes::myself;
ScAddr ScComponentManagerKeynodes::concept_reusable_component;
ScAddr ScComponentManagerKeynodes::current_components_to_install;
ScAddr ScComponentManagerKeynodes::concept_reusable_kb_component;
ScAddr ScComponentManagerKeynodes::concept_reusable_ps_component;
ScAddr ScComponentManagerKeynodes::concept_reusable_ui_component;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ class ScComponentManagerKeynodes : public ScObject
SC_PROPERTY(Keynode("concept_reusable_component"), ForceCreate(ScType::NodeConstClass))
static ScAddr concept_reusable_component;

SC_PROPERTY(Keynode("concept_current_components_to_install"), ForceCreate(ScType::NodeConst))
static ScAddr current_components_to_install;

SC_PROPERTY(Keynode("concept_reusable_kb_component"), ForceCreate(ScType::NodeConstClass))
static ScAddr concept_reusable_kb_component;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,9 @@ ScAddrUnorderedSet CommonUtils::GetComponentsToInstall(ScMemoryContext & context
{
ScAddrUnorderedSet components;
if (!context.IsElement(parameterNode))
{
return components;
}

ScAddr const & parameterSetNode = utils::IteratorUtils::getAnyByOutRelation(
&context, parameterNode, keynodes::ScComponentManagerKeynodes::rrel_sets);
Expand Down
23 changes: 13 additions & 10 deletions src/manager/agents/common-module/utils/sc_component_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include "constants/command_constants.hpp"
#include "url_parser/repository_url_parser.hpp"

using namespace common_utils;

namespace componentUtils
{
/**
Expand Down Expand Up @@ -52,26 +54,27 @@ ScAddr SearchUtils::GetComponentAddress(ScMemoryContext * context, ScAddr const
* @return ScAddrVector consists of component dependencies sc-addrs,
* return empty vector if component has no dependencies
*/
ScAddrVector SearchUtils::GetComponentDependencies(ScMemoryContext * context, ScAddr const & componentAddr)
ScAddrUnorderedSet SearchUtils::GetComponentDependencies(ScMemoryContext * context, ScAddr const & componentAddr)
{
ScAddrVector componentDependencies;
ScAddrVector componentCurrentDependencies;
ScAddrUnorderedSet componentDependencies;
ScAddr componentDependenciesSet;

ScIterator5Ptr const & componentDependenciesIterator = context->Iterator5(
ScIterator5Ptr const & componentDependenciesSetIterator = context->Iterator5(
componentAddr,
ScType::EdgeDCommonConst,
ScType::NodeConst,
ScType::EdgeAccessConstPosPerm,
keynodes::ScComponentManagerKeynodes::nrel_component_dependencies);

while (componentDependenciesIterator->Next())
while (componentDependenciesSetIterator->Next())
{
componentDependenciesSet = componentDependenciesIterator->Get(2);
componentCurrentDependencies =
utils::IteratorUtils::getAllWithType(context, componentDependenciesSet, ScType::NodeConst);
componentDependencies.insert(
componentDependencies.cend(), componentCurrentDependencies.cbegin(), componentCurrentDependencies.cend());
componentDependenciesSet = componentDependenciesSetIterator->Get(2);
ScIterator3Ptr const & componentDependenciesIterator =
context->Iterator3(componentDependenciesSet, ScType::EdgeAccessConstPosPerm, ScType::NodeConst);
while (componentDependenciesIterator->Next())
{
componentDependencies.insert(componentDependenciesIterator->Get(2));
}
}

return componentDependencies;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@

#include <sc-memory/sc_memory.hpp>

#include "module/utils/common_utils.hpp"

using namespace common_utils;

namespace componentUtils
{
class SearchUtils
{
public:
static ScAddr GetComponentAddress(ScMemoryContext * context, ScAddr const & componentAddr);

static ScAddrVector GetComponentDependencies(ScMemoryContext * context, ScAddr const & componentAddr);
static ScAddrUnorderedSet GetComponentDependencies(ScMemoryContext * context, ScAddr const & componentAddr);

static ScAddr GetComponentInstallationMethod(ScMemoryContext * context, ScAddr const & componentAddr);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ bool ScComponentManagerCommandInit::ProcessRepositories(ScMemoryContext * contex
+ context->HelperGetSystemIdtf(componentSpecificationAddr);
componentUtils::LoadUtils::LoadScsFilesInDir(context, specificationPath);

ScAddrVector componentDependencies =
ScAddrUnorderedSet componentDependencies =
componentUtils::SearchUtils::GetComponentDependencies(context, componentSpecificationAddr);
availableRepositories.insert(
availableRepositories.end(), componentDependencies.begin(), componentDependencies.end());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ SC_AGENT_IMPLEMENTATION(ScComponentManagerInstallAgent)
configManager[PathKeysOfConfigPath::UI_PATH]});

ScComponentManagerCommandInstall command = ScComponentManagerCommandInstall(componentWithConfigPath);
ScAddrUnorderedSet const & identifiersNodes = command.Execute(&m_memoryCtx, actionAddr);
ScAddrVector result(identifiersNodes.begin(), identifiersNodes.end());
utils::AgentUtils::finishAgentWork(&m_memoryCtx, actionAddr, result, true);
ScAddrUnorderedSet const & identifiersNodesSet = command.Execute(&m_memoryCtx, actionAddr);
bool result = !identifiersNodesSet.empty();
ScAddrVector identifiersNodesVector(identifiersNodesSet.begin(), identifiersNodesSet.end());

utils::AgentUtils::finishAgentWork(&m_memoryCtx, actionAddr, identifiersNodesVector, result);
SC_LOG_DEBUG("ScComponentManagerInstallAgent finished");
return SC_RESULT_OK;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@
*/

#include "sc_component_manager_command_install.hpp"

#include <sc-memory/utils/sc_exec.hpp>

#include "utils/sc_component_utils.hpp"

#include "module/utils/common_utils.hpp"

#include "sc-agents-common/utils/AgentUtils.hpp"
#include <sc-agents-common/utils/IteratorUtils.hpp>
#include <sc-agents-common/utils/GenerationUtils.hpp>

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

using namespace common_utils;
Expand Down Expand Up @@ -86,13 +92,28 @@ bool ScComponentManagerCommandInstall::InstallComponent(ScMemoryContext * contex
return true;
}

bool ScComponentManagerCommandInstall::EraseTempOutputEdges(ScMemoryContext * context, ScAddr const & node)
{
bool result = context->IsElement(node);
if (result)
{
ScIterator3Ptr const & edgesIterator = context->Iterator3(
node, // set of components
ScType::EdgeAccessConstPosTemp,
ScType::NodeConst); // component
while (edgesIterator->Next())
{
context->EraseElement(edgesIterator->Get(1));
}
}
return result;
}

ScAddrUnorderedSet ScComponentManagerCommandInstall::Execute(ScMemoryContext * context, ScAddr const & actionAddr)
{
bool executionResult = true;
ScAddr const & parameterNode =
utils::IteratorUtils::getAnyByOutRelation(context, actionAddr, scAgentsCommon::CoreKeynodes::rrel_1);
ScAddrUnorderedSet componentsToInstall = CommonUtils::GetComponentsToInstall(*context, parameterNode);

if (componentsToInstall.empty())
{
SC_LOG_INFO("ScComponentManagerCommandInstall: No identifier provided, can't install");
Expand All @@ -102,26 +123,35 @@ ScAddrUnorderedSet ScComponentManagerCommandInstall::Execute(ScMemoryContext * c
componentsToInstall = GetAvailableComponents(context, componentsToInstall);

ScAddr decompositionAddr;
bool executionResult = true;
for (ScAddr const & componentAddr : componentsToInstall)
{
if (CommonUtils::CheckIfInstalled(*context, componentAddr))
{
SC_LOG_DEBUG("Component \"" << context->HelperGetSystemIdtf(componentAddr) << "\" is already installed");
continue;
}
executionResult = InstallDependencies(context, componentAddr);
executionResult &= DownloadComponent(context, componentAddr);
if (executionResult)
context->CreateEdge(
ScType::EdgeAccessConstPosTemp,
keynodes::ScComponentManagerKeynodes::current_components_to_install,
componentAddr);
executionResult &= InstallDependencies(context, componentAddr);
executionResult &=
EraseTempOutputEdges(context, keynodes::ScComponentManagerKeynodes::current_components_to_install);
if (!executionResult)
{
executionResult &= InstallComponent(context, componentAddr);
return {};
}
executionResult &= DownloadComponent(context, componentAddr);
executionResult &= InstallComponent(context, componentAddr);
// TODO: need to process installation method from component specification in kb

decompositionAddr = CommonUtils::GetSubsystemDecompositionAddr(*context, componentAddr);
if (context->IsElement(decompositionAddr))
context->CreateEdge(ScType::EdgeAccessConstPosPerm, decompositionAddr, componentAddr);
else
SC_LOG_WARNING(
"Component \"" << context->HelperGetSystemIdtf(componentAddr) << "\" can't be added in myself-decomposition");
"Component \"" << context->HelperGetSystemIdtf(componentAddr) << "\" can't be added in myself decomposition");
}

return componentsToInstall;
Expand Down Expand Up @@ -166,6 +196,48 @@ void ScComponentManagerCommandInstall::ValidateComponent(ScMemoryContext * conte
}
}

ScAddr ScComponentManagerCommandInstall::CreateSetToInstallStructure(
ScMemoryContext * context,
ScAddr const & dependenciesSet)
{
if (!context->IsElement(dependenciesSet))
{
return dependenciesSet;
}
ScAddr const & setsParameter = context->CreateNode(ScType::NodeConst);
ScAddr const & mainParameter = context->CreateNode(ScType::NodeConst);
utils::GenerationUtils::generateRelationBetween(
context, mainParameter, setsParameter, keynodes::ScComponentManagerKeynodes::rrel_sets);
context->CreateEdge(ScType::EdgeAccessConstPosPerm, setsParameter, dependenciesSet);

return mainParameter;
}

MksmOrlov marked this conversation as resolved.
Show resolved Hide resolved
ScAddr ScComponentManagerCommandInstall::CheckDependencyDuplication(
ScMemoryContext * context,
ScAddr const & currentInstallationComponentsAddr,
ScAddr const & dependenciesSet)
{
ScAddr recursiveDependency;
if (!context->IsElement(currentInstallationComponentsAddr) && !context->IsElement(dependenciesSet))
{
return recursiveDependency;
}

ScIterator3Ptr const & componentsIterator =
context->Iterator3(dependenciesSet, ScType::EdgeAccessConstPosPerm, ScType::NodeConst);

while (componentsIterator->Next())
{
if (context->HelperCheckEdge(
currentInstallationComponentsAddr, componentsIterator->Get(2), ScType::EdgeAccessConstPosTemp))
{
return componentsIterator->Get(2);
}
}
return recursiveDependency;
}

/**
* Tries to install component dependencies.
* @return Returns {DependencyIdtf1, DependencyIdtf2, ...}
Expand All @@ -175,28 +247,48 @@ void ScComponentManagerCommandInstall::ValidateComponent(ScMemoryContext * conte
bool ScComponentManagerCommandInstall::InstallDependencies(ScMemoryContext * context, ScAddr const & componentAddr)
{
bool result = true;
// Get component dependencies and install them recursively
ScAddrVector const & componentDependencies =
ScAddr const & dependenciesSet = utils::IteratorUtils::getAnyByOutRelation(
context, componentAddr, keynodes::ScComponentManagerKeynodes::nrel_component_dependencies);
ScAddrUnorderedSet const & componentDependencies =
componentUtils::SearchUtils::GetComponentDependencies(context, componentAddr);
for (ScAddr const & componentDependency : componentDependencies)

if (componentDependencies.empty())
{
return result;
}

if (componentDependencies != GetAvailableComponents(context, componentDependencies))
{
SC_LOG_ERROR(
"ScComponentManagerCommandInstall: Can't install dependencies of the component \""
<< context->HelperGetSystemIdtf(componentAddr) << "\"");
return false;
}

ScAddr const & recursiveDependency = CheckDependencyDuplication(
context, keynodes::ScComponentManagerKeynodes::current_components_to_install, dependenciesSet);
if (context->IsElement(recursiveDependency))
{
std::string dependencyIdtf = context->HelperGetSystemIdtf(componentDependency);
SC_LOG_INFO("ScComponentManager: Install dependency \"" << dependencyIdtf << "\"");
CommandParameters dependencyParameters = {{PARAMETER_NAME, {dependencyIdtf}}};
SC_LOG_ERROR(
"ScComponentManagerCommandInstall: Found recursive dependency with component " << context->HelperGetSystemIdtf(
recursiveDependency) << ", can't install component " << context->HelperGetSystemIdtf(componentAddr));
return false;
}

ScAddr const actionAddr =
utils::AgentUtils::formActionNode(context, keynodes::ScComponentManagerKeynodes::action_components_install, {});
CommonUtils::TranslateFromStringToScMemory(*context, actionAddr, dependencyParameters);
// Get component dependencies and install them recursively
std::string dependencyIdtf;
ScAddr const & mainParameter = CreateSetToInstallStructure(context, dependenciesSet);
ScAddr const & actionAddr = utils::AgentUtils::formActionNode(
context, keynodes::ScComponentManagerKeynodes::action_components_install, {mainParameter});

bool dependencyResult = Execute(context, actionAddr).empty();
// Call install components agent
utils::AgentUtils::applyAction(context, actionAddr, 30000);

// Return empty if you couldn't install one from all dependencies why?
if (!dependencyResult)
{
SC_LOG_ERROR("ScComponentManagerCommandInstall: Dependency \"" << dependencyIdtf << "\" is not installed");
// return dependencyResult;
result = false;
}
result = context->HelperCheckEdge(
scAgentsCommon::CoreKeynodes::question_finished_successfully, actionAddr, ScType::EdgeAccessConstPosPerm);
if (!result)
{
SC_LOG_ERROR("ScComponentManagerCommandInstall: Dependencies are not successfully installed");
}

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ class ScComponentManagerCommandInstall : public ScComponentManagerCommand

bool InstallDependencies(ScMemoryContext * context, ScAddr const & componentAddr);

static ScAddr CreateSetToInstallStructure(ScMemoryContext * context, ScAddr const & dependenciesSet);

static ScAddr CheckDependencyDuplication(
ScMemoryContext * context,
ScAddr const & currentInstallationComponentsAddr,
ScAddr const & dependenciesSet);

static bool EraseTempOutputEdges(ScMemoryContext * context, ScAddr const & node);

static ScAddrUnorderedSet GetAvailableComponents(
ScMemoryContext * context,
ScAddrUnorderedSet const & componentsToInstallIdentifiers);
Expand Down
Loading