From a693f8b3def5fffe11865b8c46fec3e189d9c0f7 Mon Sep 17 00:00:00 2001 From: Serg Creevanose Date: Wed, 20 Mar 2024 07:33:08 -0700 Subject: [PATCH] Import known variables from modules upon loading --- .gitignore | 1 + libskrypt/skrypt.cpp | 54 ++++++++++++++++++++++++++++--------- libskrypt/skrypt.h | 1 + libskrypt/tests/Modules.cpp | 10 +++++-- 4 files changed, 52 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index 2f5f9a3..a1d39a7 100644 --- a/.gitignore +++ b/.gitignore @@ -42,3 +42,4 @@ CMakeSettings.json /enc_temp_folder/ *Cache.solutions +/.idea diff --git a/libskrypt/skrypt.cpp b/libskrypt/skrypt.cpp index bf172af..10e83f0 100644 --- a/libskrypt/skrypt.cpp +++ b/libskrypt/skrypt.cpp @@ -105,6 +105,13 @@ void Skrypt::PrintVarKnowns(const Variable& v) std::cout << std::endl; } +void Skrypt::PrintAllKnowns() +{ + for (auto& [name, var] : vars) { + PrintVarKnowns(var); + } +} + bool Skrypt::Add(std::string_view line) { Valuable v(line, varHost); BackgroudLoadingModules(v); @@ -151,9 +158,7 @@ void Skrypt::ProcessQuestionLine(std::string_view& line) Valuable::YesNoMaybe is = Valuable::YesNoMaybe::Maybe; auto questionless = Questionless(line); if (questionless.empty()) { - for (auto& [name, var] : vars) { - PrintVarKnowns(var); - } + PrintAllKnowns(); } else { Valuable expression(questionless, varHost); @@ -471,13 +476,34 @@ Skrypt::module_t Skrypt::Module(std::string_view name) { return module; } -Skrypt::loading_module_t Skrypt::StartLoadingModule(std::string_view name) { - std::cout << "Module " << name << " loading started" << std::endl; - return std::async( - std::launch::async, [this, name]() { - auto module = Module(name); - std::cout << "Module " << name << " loaded" << std::endl; - return module; +Skrypt::loading_module_t Skrypt::StartLoadingModule(std::string_view moduleName) { + std::cout << "Module " << moduleName << " loading started" << std::endl; + return std::async(std::launch::async, [this, moduleName]() { + auto module = Module(moduleName); + std::cout << "Module " << moduleName << " loaded:" << std::endl; + module->PrintAllKnowns(); + + // Import known variables from the module TODO: through Binder + const auto& moduleVarNames = module->GetVarNames(); + for (auto& [name, var] : moduleVarNames) { + auto& knowns = module->Known(var); + auto knownSolutionsNumber = knowns.size(); + if (knownSolutionsNumber) { + std::string varName(moduleName); + varName += '.'; + varName += name; + auto& v = varHost->Host(varName); + if (knownSolutionsNumber == 1) + Add(v, *knowns.begin()); + else { + auto& solutions = Yarns(v)[{}]; + for (auto& solution : knowns) { + solutions.insert(solution); + } + } + } + } + return module; }); } @@ -601,8 +627,12 @@ const ::omnn::math::Valuable::solutions_t& Skrypt::Known(const ::omnn::math::Var } else { WaitAllModulesLoadingComplete(); known = std::cref(base::Known(v)); - } - } + if (known.get().size() == 0) { + Solve(v); + known = std::cref(base::Known(v)); + } + } + } } return known; } \ No newline at end of file diff --git a/libskrypt/skrypt.h b/libskrypt/skrypt.h index 79eef7c..25b5c1b 100644 --- a/libskrypt/skrypt.h +++ b/libskrypt/skrypt.h @@ -62,6 +62,7 @@ class Skrypt bool Add(std::string_view); bool ParseNextLine(std::istream&, std::string_view&); void PrintVarKnowns(const omnn::math::Variable&); + void PrintAllKnowns(); void ProcessQuestionLine(std::string_view&); /// diff --git a/libskrypt/tests/Modules.cpp b/libskrypt/tests/Modules.cpp index 43dd446..f2649cf 100644 --- a/libskrypt/tests/Modules.cpp +++ b/libskrypt/tests/Modules.cpp @@ -18,12 +18,18 @@ BOOST_AUTO_TEST_CASE(ModulePropertyTest) { // get the variable auto variableName = "x"s; auto varhost = skrypt.GetVarHost(); + auto& variable = varhost->Host(variableName); + auto& solutions = skrypt.Known(variable); + BOOST_TEST(solutions.size() == 1); + for (auto& solution : solutions) { + BOOST_TEST(solution == 1); + } + auto& moduleVariableX = varhost->Host("TestExpOrderWithBrackets.x"s); + auto& moduleVariableSolutions = skrypt.Known(moduleVariableX); // check that the variable is already solved - auto moduleVariableSolutions = skrypt.Known(moduleVariableX); - skrypt.Known(variable); BOOST_TEST(moduleVariableSolutions.size() == 1); for (auto& solution : moduleVariableSolutions) { BOOST_TEST(solution == 1);