Skip to content

Commit

Permalink
Import known variables from modules upon loading
Browse files Browse the repository at this point in the history
  • Loading branch information
ohhmm committed Mar 20, 2024
1 parent 9894b46 commit a693f8b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@
CMakeSettings.json
/enc_temp_folder/
*Cache.solutions
/.idea
54 changes: 42 additions & 12 deletions libskrypt/skrypt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
});
}

Expand Down Expand Up @@ -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;
}
1 change: 1 addition & 0 deletions libskrypt/skrypt.h
Original file line number Diff line number Diff line change
Expand Up @@ -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&);

/// <summary>
Expand Down
10 changes: 8 additions & 2 deletions libskrypt/tests/Modules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit a693f8b

Please sign in to comment.