From 3bc4b24e0f6cdf801a0560c1771d6e7441674228 Mon Sep 17 00:00:00 2001 From: NikitaZotov Date: Sun, 29 Jan 2023 23:28:07 +0300 Subject: [PATCH] fix(template-manager): argument replacements --- .../manager/TemplateManager.cpp | 100 ++++++++++-------- 1 file changed, 57 insertions(+), 43 deletions(-) diff --git a/problem-solver/cxx/inferenceModule/manager/TemplateManager.cpp b/problem-solver/cxx/inferenceModule/manager/TemplateManager.cpp index a9304868..3da2b9f5 100644 --- a/problem-solver/cxx/inferenceModule/manager/TemplateManager.cpp +++ b/problem-solver/cxx/inferenceModule/manager/TemplateManager.cpp @@ -23,72 +23,86 @@ std::vector TemplateManager::createTemplateParams( ScAddrVector const & argumentList) { std::map> replacementsMultimap; - std::vector templateParamsVector; ScIterator3Ptr varIterator = context->Iterator3(scTemplate, ScType::EdgeAccessConstPosPerm, ScType::NodeVar); while (varIterator->Next()) { ScAddr var = varIterator->Get(2); std::string varName = context->HelperGetSystemIdtf(var); - if (!replacementsMultimap[varName].empty()) - { - continue; - } ScAddr argumentOfVar; - ScIterator5Ptr classesIterator = context->Iterator5( - ScType::NodeConstClass, ScType::EdgeAccessVarPosPerm, var, ScType::EdgeAccessConstPosPerm, scTemplate); - while (classesIterator->Next()) + for (ScAddr const & argument : argumentList) { - ScAddr varClass = classesIterator->Get(0); - for (ScAddr const & argument : argumentList) // this block is executed if inputStructure is valid + ScIterator5Ptr const & classesIterator = context->Iterator5( + ScType::NodeConstClass, ScType::EdgeAccessVarPosPerm, var, ScType::EdgeAccessConstPosPerm, scTemplate); + bool isBelong = false; + while (classesIterator->Next()) { + ScAddr varClass = classesIterator->Get(0); if (context->HelperCheckEdge(varClass, argument, ScType::EdgeAccessConstPosPerm)) - replacementsMultimap[varName].insert(argument); + { + isBelong = true; + } + else + { + isBelong = false; + break; + } } - if (argumentList.empty()) // this block is executed if inputStructure is not valid + + if (isBelong) { - ScIterator3Ptr iterator3 = context->Iterator3(varClass, ScType::EdgeAccessConstPosPerm, ScType::Unknown); - while (iterator3->Next()) - replacementsMultimap[varName].insert(iterator3->Get(2)); + replacementsMultimap[varName].insert(argument); } } - if (templateParamsVector.empty()) + } + + std::vector> paramsVector; + paramsVector.reserve(replacementsMultimap.size()); + for (auto const & pair : replacementsMultimap) + { + std::vector> newParamsVector; + newParamsVector.reserve(replacementsMultimap.size()); + for (auto const & addr : pair.second) { - std::set addresses = replacementsMultimap[varName]; - templateParamsVector.reserve(replacementsMultimap[varName].size()); - for (ScAddr const & address : addresses) + bool isUploaded = false; + for (auto map : paramsVector) { - ScTemplateParams params; - params.Add(varName, address); - templateParamsVector.push_back(params); - } - } - else - { - std::set addresses = replacementsMultimap[varName]; - size_t amountOfAddressesForVar = addresses.size(); - size_t oldParamsSize = templateParamsVector.size(); + bool isDuplicatedValue = false; + for (auto const & mapPair : map) + { + if (mapPair.second == addr) + { + isDuplicatedValue = true; + } + } - if (amountOfAddressesForVar == 0) - continue; + if (!isDuplicatedValue) + { + map.insert({ pair.first, addr }); + newParamsVector.emplace_back(map); + } - size_t amountOfNewElements = oldParamsSize * amountOfAddressesForVar; - std::vector updatedParams; - updatedParams.reserve(amountOfNewElements); + isUploaded = true; + } - size_t beginOfCopy = 0; - size_t endOfCopy = oldParamsSize; - for (ScAddr const & address : addresses) + if (!isUploaded) { - copy_n(templateParamsVector.begin(), oldParamsSize, back_inserter(updatedParams)); - for (size_t i = 0; i < oldParamsSize; ++i) - updatedParams[beginOfCopy + i].Add(varName, address); - beginOfCopy = endOfCopy; - endOfCopy += oldParamsSize; + std::map map{{ pair.first, addr }}; + newParamsVector.emplace_back(map); } + } + paramsVector = newParamsVector; + } - templateParamsVector = std::move(updatedParams); + std::vector templateParamsVector; + for (auto const & params : paramsVector) + { + ScTemplateParams templateParams; + for (auto const & param : params) + { + templateParams.Add(param.first, param.second); } + templateParamsVector.emplace_back(templateParams); } return templateParamsVector;