Skip to content
26 changes: 16 additions & 10 deletions src/importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -711,12 +711,8 @@ void updateComponentsVariablesUnitsNames(const std::string &name, const Componen
{
for (size_t variableIndex = 0; variableIndex < component->variableCount(); ++variableIndex) {
auto variable = component->variable(variableIndex);
if (component->isImport()) {
auto importModel = component->importSource()->model();
auto importComponent = importModel->component(component->importReference());
variable = importComponent->variable(variable->name());
}
if (variable->units()->name() == name) {
auto variableUnits = variable->units();
if ((variableUnits != nullptr) && (variableUnits->name() == name)) {
variable->setUnits(units);
}
}
Expand All @@ -739,7 +735,12 @@ StringStringMap transferUnitsRenamingIfRequired(const ModelPtr &sourceModel, con
StringStringMap changedNames;

std::string newName = units->name();
UnitsPtr targetUnits = modelsEquivalentUnits(targetModel, units);
// Compare units against the parented units in the source model, not the detached clone, to avoid false negatives.
UnitsPtr comparisonUnits = units;
if (owningModel(units) == nullptr) {
comparisonUnits = sourceModel->units(units->name());
}
UnitsPtr targetUnits = modelsEquivalentUnits(targetModel, comparisonUnits);
if (targetUnits == nullptr) {
for (size_t unitIndex = 0; unitIndex < units->unitCount(); ++unitIndex) {
std::string reference = units->unitAttributeReference(unitIndex);
Expand Down Expand Up @@ -834,9 +835,6 @@ ComponentPtr flattenComponent(const ComponentEntityPtr &parent, ComponentPtr &co
// Take a copy of the imported component which will be used to replace the import defined in this model.
auto importedComponentCopy = importedComponent->clone();
importedComponentCopy->setName(component->name());
for (size_t i = 0; i < component->componentCount(); ++i) {
importedComponentCopy->addComponent(component->component(i));
}

// Get list of required units from component's variables and math cn elements.
std::vector<UnitsPtr> requiredUnits = unitsUsed(clonedImportModel, importedComponentCopy);
Expand Down Expand Up @@ -932,6 +930,14 @@ ComponentPtr flattenComponent(const ComponentEntityPtr &parent, ComponentPtr &co
UnitsPtr targetUnits = flatModel->units(finalUnitsName);
updateUnitsNameUsages(alias.first, finalUnitsName, importedComponentCopy, targetUnits);
}

std::vector<ComponentPtr> localChildren;
for (size_t i = 0; i < component->componentCount(); ++i) {
localChildren.push_back(component->component(i));
}
for (const auto &localChild : localChildren) {
importedComponentCopy->addComponent(localChild);
}
}

return parent->component(index);
Expand Down
16 changes: 12 additions & 4 deletions src/utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -723,15 +723,23 @@ ComponentNameMap createComponentNamesMap(const ComponentPtr &component)
return nameMap;
}

std::vector<UnitsPtr> referencedUnits(const ModelPtr &model, const UnitsPtr &units)
std::vector<UnitsPtr> referencedUnits(const UnitsPtr &units)
{
std::vector<UnitsPtr> requiredUnits;

auto model = owningModel(units);
if (model == nullptr) {
return requiredUnits;
}

for (size_t index = 0; index < units->unitCount(); ++index) {
const std::string ref = units->unitAttributeReference(index);
if (!isStandardUnitName(ref)) {
auto refUnits = model->units(ref);
auto requiredUnitsUnits = referencedUnits(model, refUnits);
if (refUnits == nullptr) {
continue;
}
auto requiredUnitsUnits = referencedUnits(refUnits);
requiredUnits.insert(requiredUnits.end(), requiredUnitsUnits.begin(), requiredUnitsUnits.end());
requiredUnits.push_back(refUnits);
}
Expand All @@ -752,7 +760,7 @@ std::vector<UnitsPtr> unitsUsed(const ModelPtr &model, const ComponentConstPtr &
if ((u != nullptr) && !isStandardUnitName(u->name()) && (model != nullptr)) {
auto modelUnits = model->units(u->name());
auto availableUnits = modelUnits ? modelUnits : u;
auto requiredUnits = referencedUnits(model, availableUnits);
auto requiredUnits = referencedUnits(availableUnits);
usedUnits.insert(usedUnits.end(), requiredUnits.begin(), requiredUnits.end());
usedUnits.push_back(availableUnits);
} else if (model == nullptr) {
Expand All @@ -769,7 +777,7 @@ std::vector<UnitsPtr> unitsUsed(const ModelPtr &model, const ComponentConstPtr &
// We have used a units in the math but it is not defined in the given model, so send back a units that isn't defined.
u = Units::create(unitsName);
} else {
auto requiredUnits = referencedUnits(model, u);
auto requiredUnits = referencedUnits(u);
usedUnits.insert(usedUnits.end(), requiredUnits.begin(), requiredUnits.end());
}
usedUnits.push_back(u);
Expand Down
140 changes: 138 additions & 2 deletions tests/importer/model_flattening.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -622,10 +622,10 @@ TEST(ModelFlattening, importingComponentThatAlsoHasAnImportedComponentAsAChild)
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<model xmlns=\"http://www.cellml.org/cellml/2.0#\" name=\"a_model\">\n"
" <component name=\"membrane\"/>\n"
" <component name=\"sodium_channel_1\"/>\n"
" <component name=\"sodium_channel\"/>\n"
" <encapsulation>\n"
" <component_ref component=\"membrane\">\n"
" <component_ref component=\"sodium_channel_1\"/>\n"
" <component_ref component=\"sodium_channel\"/>\n"
" </component_ref>\n"
" </encapsulation>\n"
"</model>\n";
Expand Down Expand Up @@ -1940,3 +1940,139 @@ TEST(ModelFlattening, multiLayeredImportOfNonStandardUnits)

EXPECT_NE(nullptr, flattenModel);
}

TEST(ModelFlattening, nestedImportOfUnitsWithNonStandardChildren)
{
const std::string e =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<model xmlns=\"http://www.cellml.org/cellml/2.0#\" name=\"main_model\">\n"
" <units name=\"rate\">\n"
" <unit units=\"mM\"/>\n"
" <unit units=\"per_s\"/>\n"
" </units>\n"
" <units name=\"mM\">\n"
" <unit prefix=\"milli\" units=\"mole\"/>\n"
" </units>\n"
" <units name=\"per_s\">\n"
" <unit exponent=\"-1\" units=\"second\"/>\n"
" </units>\n"
" <units name=\"compound\">\n"
" <unit units=\"rate\"/>\n"
" <unit units=\"volt\"/>\n"
" </units>\n"
" <component name=\"parent\">\n"
" <variable name=\"p\" units=\"rate\" interface=\"public_and_private\"/>\n"
" <variable name=\"q\" units=\"compound\" interface=\"public\"/>\n"
" </component>\n"
" <component name=\"child\">\n"
" <variable name=\"r\" units=\"rate\" interface=\"public\"/>\n"
" </component>\n"
" <connection component_1=\"parent\" component_2=\"child\">\n"
" <map_variables variable_1=\"p\" variable_2=\"r\"/>\n"
" </connection>\n"
" <encapsulation>\n"
" <component_ref component=\"parent\">\n"
" <component_ref component=\"child\"/>\n"
" </component_ref>\n"
" </encapsulation>\n"
"</model>\n";

auto parser = libcellml::Parser::create();
auto model = parser->parseModel(fileContents("modelflattening/nestedimportofunits.xml"));
auto importer = libcellml::Importer::create();

EXPECT_TRUE(model->hasUnresolvedImports());
importer->resolveImports(model, resourcePath("modelflattening/"));
EXPECT_FALSE(model->hasUnresolvedImports());

model = importer->flattenModel(model);
EXPECT_EQ(size_t(0), importer->issueCount());

EXPECT_FALSE(model->hasUnits("rate_1"));

auto printer = libcellml::Printer::create();

auto a = printer->printModel(model);
EXPECT_EQ(e, a);
}

TEST(ModelFlattening, cnUnitsOfEncapsulatedChildNotInImportedModel)
{
const std::string e =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<model xmlns=\"http://www.cellml.org/cellml/2.0#\" name=\"main_model\">\n"
" <units name=\"my_units\"/>\n"
" <component name=\"my_component\"/>\n"
" <component name=\"local_child\">\n"
" <variable name=\"v\" units=\"second\" interface=\"public\"/>\n"
" <math xmlns=\"http://www.w3.org/1998/Math/MathML\" xmlns:cellml=\"http://www.cellml.org/cellml/2.0#\">\n"
" <apply>\n"
" <eq/>\n"
" <ci>v</ci>\n"
" <cn cellml:units=\"my_units\">1</cn>\n"
" </apply>\n"
" </math>\n"
" </component>\n"
" <encapsulation>\n"
" <component_ref component=\"my_component\">\n"
" <component_ref component=\"local_child\"/>\n"
" </component_ref>\n"
" </encapsulation>\n"
"</model>\n";

auto parser = libcellml::Parser::create();
auto model = parser->parseModel(fileContents("modelflattening/cnunitsnotinimportedmodel.xml"));
auto importer = libcellml::Importer::create();

EXPECT_TRUE(model->hasUnresolvedImports());
importer->resolveImports(model, resourcePath("modelflattening/"));
EXPECT_FALSE(model->hasUnresolvedImports());

model = importer->flattenModel(model);
EXPECT_EQ(size_t(0), importer->issueCount());

EXPECT_FALSE(model->hasUnits("my_units_1"));

auto printer = libcellml::Printer::create();

auto a = printer->printModel(model);
EXPECT_EQ(e, a);
}

TEST(ModelFlattening, flatteningDoesNotChangeCachedImportModelsWhenUnitsAreRenamed)
{
const std::string eGrandchild =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<model xmlns=\"http://www.cellml.org/cellml/2.0#\" name=\"model_c\">\n"
" <units name=\"u\">\n"
" <unit units=\"second\"/>\n"
" </units>\n"
" <component name=\"c_component\">\n"
" <variable name=\"cv\" units=\"u\" interface=\"public\"/>\n"
" </component>\n"
"</model>\n";

auto parser = libcellml::Parser::create();
auto importer = libcellml::Importer::create();
auto printer = libcellml::Printer::create();

auto model = parser->parseModel(fileContents("modelflattening/cachedimportmodel.xml"));

EXPECT_TRUE(model->hasUnresolvedImports());
importer->resolveImports(model, resourcePath("modelflattening/"));
EXPECT_FALSE(model->hasUnresolvedImports());

auto grandchildModel = importer->library(resourcePath("modelflattening/") + "import_for_import_for_cachedimportmodel.xml");
ASSERT_NE(nullptr, grandchildModel);
EXPECT_EQ(eGrandchild, printer->printModel(grandchildModel));

auto flatModel = importer->flattenModel(model);
ASSERT_NE(nullptr, flatModel);

// The rename happened ...
EXPECT_TRUE(flatModel->hasUnits("u_1"));
EXPECT_EQ("u_1", flatModel->component("child")->variable("cv")->units()->name());

// ... but the cached model is untouched.
EXPECT_EQ(eGrandchild, printer->printModel(grandchildModel));
}
9 changes: 9 additions & 0 deletions tests/resources/importer/component_with_broken_units.cellml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<model xmlns="http://www.cellml.org/cellml/2.0#" name="model_with_broken_units">
<units name="broken_units">
<unit units="units_that_do_not_exist"/>
</units>
<component name="component_using_broken_units">
<variable name="x" units="broken_units" interface="public"/>
</component>
</model>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<model xmlns="http://www.cellml.org/cellml/2.0#" xmlns:xlink="http://www.w3.org/1999/xlink" name="importing_model">
<import xlink:href="component_with_broken_units.cellml">
<component component_ref="component_using_broken_units" name="imported_component"/>
</import>
</model>
12 changes: 12 additions & 0 deletions tests/resources/modelflattening/cachedimportmodel.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version='1.0' encoding='UTF-8'?>
<model xmlns="http://www.cellml.org/cellml/2.0#" xmlns:xlink="http://www.w3.org/1999/xlink" name="main_model">
<import xlink:href="import_for_cachedimportmodel.xml">
<component component_ref="X" name="X"/>
</import>
<units name="u">
<unit prefix="hecto" units="second"/>
</units>
<component name="holder">
<variable name="hv" units="u" interface="public"/>
</component>
</model>
22 changes: 22 additions & 0 deletions tests/resources/modelflattening/cnunitsnotinimportedmodel.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version='1.0' encoding='UTF-8'?>
<model xmlns="http://www.cellml.org/cellml/2.0#" xmlns:xlink="http://www.w3.org/1999/xlink" name="main_model">
<import xlink:href="import_for_cnunitsnotinimportedmodel.xml">
<component component_ref="my_component" name="my_component"/>
</import>
<units name="my_units"/>
<component name="local_child">
<variable name="v" units="second" interface="public"/>
<math xmlns="http://www.w3.org/1998/Math/MathML" xmlns:cellml="http://www.cellml.org/cellml/2.0#">
<apply>
<eq/>
<ci>v</ci>
<cn cellml:units="my_units">1</cn>
</apply>
</math>
</component>
<encapsulation>
<component_ref component="my_component">
<component_ref component="local_child"/>
</component_ref>
</encapsulation>
</model>
20 changes: 20 additions & 0 deletions tests/resources/modelflattening/import_for_cachedimportmodel.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version='1.0' encoding='UTF-8'?>
<model xmlns="http://www.cellml.org/cellml/2.0#" xmlns:xlink="http://www.w3.org/1999/xlink" name="model_b">
<import xlink:href="import_for_import_for_cachedimportmodel.xml">
<component component_ref="c_component" name="child"/>
</import>
<units name="u">
<unit units="second"/>
</units>
<component name="X">
<variable name="xv" units="u" interface="public_and_private"/>
</component>
<connection component_1="X" component_2="child">
<map_variables variable_1="xv" variable_2="cv"/>
</connection>
<encapsulation>
<component_ref component="X">
<component_ref component="child"/>
</component_ref>
</encapsulation>
</model>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version='1.0' encoding='UTF-8'?>
<model xmlns="http://www.cellml.org/cellml/2.0#" name="imported_model">
<component name="my_component"/>
</model>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version='1.0' encoding='UTF-8'?>
<model xmlns="http://www.cellml.org/cellml/2.0#" name="model_c">
<units name="u">
<unit units="second"/>
</units>
<component name="c_component">
<variable name="cv" units="u" interface="public"/>
</component>
</model>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version='1.0' encoding='UTF-8'?>
<model xmlns="http://www.cellml.org/cellml/2.0#" xmlns:xlink="http://www.w3.org/1999/xlink" name="model_b">
<import xlink:href="units_for_nestedimportofunits.xml">
<units name="rate" units_ref="rate"/>
</import>
<component name="b_component">
<variable name="r" units="rate" interface="public"/>
</component>
</model>
22 changes: 22 additions & 0 deletions tests/resources/modelflattening/import_for_nestedimportofunits.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version='1.0' encoding='UTF-8'?>
<model xmlns="http://www.cellml.org/cellml/2.0#" xmlns:xlink="http://www.w3.org/1999/xlink" name="model_a">
<import xlink:href="units_for_nestedimportofunits.xml">
<units name="rate" units_ref="rate"/>
<units name="compound" units_ref="compound"/>
</import>
<import xlink:href="import_for_import_for_nestedimportofunits.xml">
<component component_ref="b_component" name="child"/>
</import>
<component name="parent">
<variable name="p" units="rate" interface="public_and_private"/>
<variable name="q" units="compound" interface="public"/>
</component>
<connection component_1="parent" component_2="child">
<map_variables variable_1="p" variable_2="r"/>
</connection>
<encapsulation>
<component_ref component="parent">
<component_ref component="child"/>
</component_ref>
</encapsulation>
</model>
9 changes: 9 additions & 0 deletions tests/resources/modelflattening/nestedimportofunits.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version='1.0' encoding='UTF-8'?>
<model xmlns="http://www.cellml.org/cellml/2.0#" xmlns:xlink="http://www.w3.org/1999/xlink" name="main_model">
<import xlink:href="units_for_nestedimportofunits.xml">
<units name="rate" units_ref="rate"/>
</import>
<import xlink:href="import_for_nestedimportofunits.xml">
<component component_ref="parent" name="parent"/>
</import>
</model>
Loading
Loading