From a39a4bcc83286288aa3f0541fac55b29fedc8549 Mon Sep 17 00:00:00 2001 From: Russell Standish Date: Wed, 17 Jan 2024 17:06:08 +1100 Subject: [PATCH] Added input/output variable attribute to global Minsky schema to support saving groups as components. For #1681. --- schema/schema3.cc | 17 +++++++++++++++++ schema/schema3.h | 1 + test/testModel.cc | 23 +++++++++++++++++++++-- 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/schema/schema3.cc b/schema/schema3.cc index b823683a1..f6653b1fa 100644 --- a/schema/schema3.cc +++ b/schema/schema3.cc @@ -315,6 +315,15 @@ namespace schema3 return false; }); + + // add any I/O variables + vector inVars, outVars; + for (auto& i: g.inVariables) + inVars.push_back(itemMap[i.get()]); + for (auto& i: g.outVariables) + outVars.push_back(itemMap[i.get()]); + inVariables=inVars; + outVariables=outVars; // search for and link up integrals to their variables, and Godley table ports g.recursiveDo(&minsky::GroupItems::items,[&](const minsky::Items&,minsky::Items::const_iterator i) { @@ -630,6 +639,14 @@ namespace schema3 if (newItem->variableCast()) schema3VarMap[i.id]=i; } + + if (inVariables) + for (auto i: *inVariables) + g.inVariables.push_back(itemMap[i]); + if (outVariables) + for (auto i: *outVariables) + g.outVariables.push_back(itemMap[i]); + // second loop over items to wire up integrals, and populate Godley table variables for (const auto& i: items) { diff --git a/schema/schema3.h b/schema/schema3.h index 31820f79a..98f0830ed 100644 --- a/schema/schema3.h +++ b/schema/schema3.h @@ -245,6 +245,7 @@ namespace schema3 std::string minskyVersion="unknown"; vector wires; vector items; + Optional> inVariables, outVariables; vector groups; vector lockGroups; minsky::Simulation rungeKutta; diff --git a/test/testModel.cc b/test/testModel.cc index 1d8591422..67182acbc 100644 --- a/test/testModel.cc +++ b/test/testModel.cc @@ -1030,7 +1030,7 @@ SUITE(GodleyIcon) table.resize(3,2); table.cell(2,1)="flow1"; table.cell(0,1)="stock1"; - variableDisplay=true; + toggleVariableDisplay(); update(); // TODO - shouldn't be needed, but there is some font problem causing bottomMargin to be calculated incorrectly @@ -1716,6 +1716,25 @@ SUITE(GodleyTableWindow) } - + TEST_FIXTURE(TestFixture, saveAsGroup) + { + group0->inVariables.push_back(a); + group0->makeSubroutine(); + save("foo.mky"); + CHECK(group0->inVariables.size()); + CHECK(group0->outVariables.size()); + saveGroupAsFile(*group0,"group0.mky"); + insertGroupFromFile("group0.mky"); + Group& newGroup=dynamic_cast(*canvas.itemFocus); + // check I/O variables + CHECK_EQUAL(group0->inVariables.size(),newGroup.inVariables.size()); + CHECK_EQUAL(group0->outVariables.size(),newGroup.outVariables.size()); + for (int i=0; iinVariables.size(); ++i) + CHECK_EQUAL(group0->inVariables[i]->name(), newGroup.inVariables[i]->name()); + for (int i=0; ioutVariables.size(); ++i) + CHECK_EQUAL(group0->outVariables[i]->name(), newGroup.outVariables[i]->name()); + // check items + CHECK_EQUAL(group0->items.size(), newGroup.items.size()); + } }