From 776b7f936b210dc06200184aa4aba82394924799 Mon Sep 17 00:00:00 2001 From: Alan Garny Date: Wed, 5 Feb 2020 13:51:55 +1300 Subject: [PATCH] Some minor cleaning up. --- .../src/cellmlfilecellml10exporter.cpp | 132 +++++------------- 1 file changed, 36 insertions(+), 96 deletions(-) diff --git a/src/plugins/support/CellMLSupport/src/cellmlfilecellml10exporter.cpp b/src/plugins/support/CellMLSupport/src/cellmlfilecellml10exporter.cpp index 4c5651c69d..949955ce11 100644 --- a/src/plugins/support/CellMLSupport/src/cellmlfilecellml10exporter.cpp +++ b/src/plugins/support/CellMLSupport/src/cellmlfilecellml10exporter.cpp @@ -57,9 +57,7 @@ CellmlFileCellml10Exporter::CellmlFileCellml10Exporter(iface::cellml_api::Model { // Create an empty CellML 1.0 model - ObjRef cellmlBootstrap = CreateCellMLBootstrap(); - - mExportedModel = cellmlBootstrap->createModel(L"1.0"); + mExportedModel = CreateCellMLBootstrap()->createModel(L"1.0"); // Set the model's name and cmeta:id, if any @@ -73,15 +71,12 @@ CellmlFileCellml10Exporter::CellmlFileCellml10Exporter(iface::cellml_api::Model // Create an annotation set to manage annotations - ObjRef annotationToolService = CreateAnnotationToolService(); - - mAnnotationSet = annotationToolService->createAnnotationSet(); + mAnnotationSet = CreateAnnotationToolService()->createAnnotationSet(); // Create a CellML Variable Association Service object so that we can find // relevant components - ObjRef cevasBootstrap = CreateCeVASBootstrap(); - ObjRef cevas = cevasBootstrap->createCeVASForModel(pModel); + ObjRef cevas = CreateCeVASBootstrap()->createCeVASForModel(pModel); std::wstring cevasError = cevas->modelError(); @@ -94,9 +89,7 @@ CellmlFileCellml10Exporter::CellmlFileCellml10Exporter(iface::cellml_api::Model // Copy all (i.e. both local and imported) model-level units to our exported // model - ObjRef unitsSet = pModel->allUnits(); - - copyUnitsSet(unitsSet, mExportedModel); + copyUnitsSet(pModel->allUnits(), mExportedModel); // Annotate imported components since they may have been renamed @@ -174,8 +167,7 @@ void CellmlFileCellml10Exporter::copyUnitsSet(iface::cellml_api::UnitsSet *pUnit // Copy the units' unit references to the new units - ObjRef unitSet = units->unitCollection(); - ObjRef unitIter = unitSet->iterateUnits(); + ObjRef unitIter = units->unitCollection()->iterateUnits(); for (ObjRef unit = unitIter->nextUnit(); unit != nullptr; unit = unitIter->nextUnit()) { @@ -218,12 +210,9 @@ iface::cellml_api::CellMLComponent * CellmlFileCellml10Exporter::findRealCompone for (ObjRef importComponent = QueryInterface(component); importComponent != nullptr; importComponent = QueryInterface(component)) { - ObjRef importComponentParentElement = importComponent->parentElement(); - ObjRef import = QueryInterface(importComponentParentElement); - ObjRef model = import->importedModel(); - ObjRef components = model->modelComponents(); + ObjRef import = QueryInterface(importComponent->parentElement()); - component = components->getComponent(importComponent->componentRef()); + component = import->importedModel()->modelComponents()->getComponent(importComponent->componentRef()); if (component == nullptr) { // No 'real' component exists @@ -246,8 +235,7 @@ void CellmlFileCellml10Exporter::annotateImportedComponents(iface::cellml_api::M // Recursively annotate all imported components with the name they are given // by the importing model - ObjRef imports = pModel->imports(); - ObjRef importsIter = imports->iterateImports(); + ObjRef importsIter = pModel->imports()->iterateImports(); for (ObjRef import = importsIter->nextImport(); import != nullptr; import = importsIter->nextImport()) { @@ -255,24 +243,16 @@ void CellmlFileCellml10Exporter::annotateImportedComponents(iface::cellml_api::M // Note: depth first processing means that imported components renamed // twice end up with the right name... - ObjRef importedModel = import->importedModel(); - - annotateImportedComponents(importedModel); - - // Annotate the imported components - - ObjRef importComponents = import->components(); + annotateImportedComponents(import->importedModel()); // Go through the imported components and annotate them with the name // they are given by the importing model - ObjRef importComponentsIter = importComponents->iterateImportComponents(); + ObjRef importComponentsIter = import->components()->iterateImportComponents(); for (ObjRef importComponent = importComponentsIter->nextImportComponent(); importComponent != nullptr; importComponent = importComponentsIter->nextImportComponent()) { - ObjRef realComponent = findRealComponent(importComponent); - - mAnnotationSet->setStringAnnotation(realComponent, L"renamed", importComponent->name()); + mAnnotationSet->setStringAnnotation(findRealComponent(importComponent), L"renamed", importComponent->name()); } } } @@ -350,19 +330,15 @@ iface::dom::Element * CellmlFileCellml10Exporter::copyDomElement(iface::dom::Ele switch (childNode->nodeType()) { case iface::dom::Node::ELEMENT_NODE: { ObjRef elementNode = QueryInterface(childNode); - ObjRef newElementNode = copyDomElement(elementNode); - res->appendChild(newElementNode)->release_ref(); + res->appendChild(copyDomElement(elementNode))->release_ref(); break; } - case iface::dom::Node::TEXT_NODE: { - ObjRef newTextNode = document->createTextNode(childNode->nodeValue()); - - res->appendChild(newTextNode)->release_ref(); + case iface::dom::Node::TEXT_NODE: + res->appendChild(document->createTextNode(childNode->nodeValue()))->release_ref(); break; - } default: // Not a relevant type, so do nothing @@ -386,14 +362,10 @@ void CellmlFileCellml10Exporter::copyExtensionElements(iface::cellml_api::CellML ObjRef elements = pFromElement->extensionElements(); ObjRef cellmlDomElement = QueryInterface(pToElement); - ObjRef domElement = cellmlDomElement->domElement(); - ObjRef document = domElement->ownerDocument(); + ObjRef document = cellmlDomElement->domElement()->ownerDocument(); for (uint i = 0, iMax = elements->length(); i < iMax; ++i) { - ObjRef element = elements->getAt(i); - - ObjRef node = document->importNode(element, true); - ObjRef nodeElement = QueryInterface(node); + ObjRef nodeElement = QueryInterface(document->importNode(elements->getAt(i), true)); pToElement->appendExtensionElement(nodeElement); } @@ -450,14 +422,11 @@ void CellmlFileCellml10Exporter::copyComponent(iface::cellml_api::CellMLComponen // Copy the units to our new component - ObjRef componentUnitsSet = pComponent->units(); - - copyUnitsSet(componentUnitsSet, newComponent); + copyUnitsSet(pComponent->units(), newComponent); // Copy the variables to our new component - ObjRef componentVariables = pComponent->variables(); - ObjRef componentVariablesIter = componentVariables->iterateVariables(); + ObjRef componentVariablesIter = pComponent->variables()->iterateVariables(); for (ObjRef componentVariable = componentVariablesIter->nextVariable(); componentVariable != nullptr; componentVariable = componentVariablesIter->nextVariable()) { @@ -486,8 +455,7 @@ void CellmlFileCellml10Exporter::copyComponent(iface::cellml_api::CellMLComponen // Copy the math nodes to our new component - ObjRef componentMath = pComponent->math(); - ObjRef componentMathIter = componentMath->iterate(); + ObjRef componentMathIter = pComponent->math()->iterate(); for (ObjRef componentMathNode = componentMathIter->next(); componentMathNode != nullptr; componentMathNode = componentMathIter->next()) { @@ -543,13 +511,9 @@ void CellmlFileCellml10Exporter::copyGroup(iface::cellml_api::Model *pModel, continue; } - // Find the real component object + // Find the real component object and check whether it has been copied - ObjRef realComponent = findRealComponent(referencedComponent); - - // Has the real component been copied? - - ObjRef copiedComponent = QueryInterface(mAnnotationSet->getObjectAnnotation(realComponent, L"copy")); + ObjRef copiedComponent = QueryInterface(mAnnotationSet->getObjectAnnotation(findRealComponent(referencedComponent), L"copy")); if (copiedComponent == nullptr) { continue; @@ -587,9 +551,7 @@ void CellmlFileCellml10Exporter::copyGroup(iface::cellml_api::Model *pModel, // Copy any children of our component reference - ObjRef componentReferences = componentReference->componentRefs(); - - copyGroup(pModel, componentReferences, newComponentReference); + copyGroup(pModel, componentReference->componentRefs(), newComponentReference); } } @@ -600,27 +562,20 @@ void CellmlFileCellml10Exporter::copyGroups(iface::cellml_api::Model *pModel) // Iterate only groups defining an encapsulation hierarchy and copy them // into our exported model - ObjRef groups = pModel->groups(); - ObjRef encapsulationGroups = groups->subsetInvolvingEncapsulation(); - ObjRef encapsulationGroupsIter = encapsulationGroups->iterateGroups(); + ObjRef encapsulationGroupsIter = pModel->groups()->subsetInvolvingEncapsulation()->iterateGroups(); for (ObjRef encapsulationGroup = encapsulationGroupsIter->nextGroup(); encapsulationGroup != nullptr; encapsulationGroup = encapsulationGroupsIter->nextGroup()) { - ObjRef encapsulationGroupComponentReferences = encapsulationGroup->componentRefs(); - - copyGroup(pModel, encapsulationGroupComponentReferences); + copyGroup(pModel, encapsulationGroup->componentRefs()); } // Recursively process imported models - ObjRef imports = pModel->imports(); - ObjRef importsIter = imports->iterateImports(); + ObjRef importsIter = pModel->imports()->iterateImports(); for (ObjRef import = importsIter->nextImport(); import != nullptr; import = importsIter->nextImport()) { - ObjRef importedModel = import->importedModel(); - - copyGroups(importedModel); + copyGroups(import->importedModel()); } } @@ -634,18 +589,16 @@ void CellmlFileCellml10Exporter::copyConnection(iface::cellml_api::Connection *p // been copied across first, and hence have a "copy" annotation... ObjRef componentMapping = pConnection->componentMapping(); - ObjRef firstComponent = componentMapping->firstComponent(); - ObjRef secondComponent = componentMapping->secondComponent(); // Check that we have copied the components involved, and get the copies - ObjRef newFirstComponent = QueryInterface(mAnnotationSet->getObjectAnnotation(firstComponent, L"copy")); + ObjRef newFirstComponent = QueryInterface(mAnnotationSet->getObjectAnnotation(componentMapping->firstComponent(), L"copy")); if (newFirstComponent == nullptr) { return; } - ObjRef newSecondComponent = QueryInterface(mAnnotationSet->getObjectAnnotation(secondComponent, L"copy")); + ObjRef newSecondComponent = QueryInterface(mAnnotationSet->getObjectAnnotation(componentMapping->secondComponent(), L"copy")); if (newSecondComponent == nullptr) { return; @@ -666,8 +619,7 @@ void CellmlFileCellml10Exporter::copyConnection(iface::cellml_api::Connection *p // Add the variable mappings - ObjRef variableMappings = pConnection->variableMappings(); - ObjRef variableMappingsIter = variableMappings->iterateMapVariables(); + ObjRef variableMappingsIter = pConnection->variableMappings()->iterateMapVariables(); for (ObjRef variableMapping = variableMappingsIter->nextMapVariables(); variableMapping != nullptr; variableMapping = variableMappingsIter->nextMapVariables()) { @@ -686,8 +638,7 @@ void CellmlFileCellml10Exporter::copyConnections(iface::cellml_api::Model *pMode { // Copy local connections into our exported model - ObjRef connections = pModel->connections(); - ObjRef connectionsIter = connections->iterateConnections(); + ObjRef connectionsIter = pModel->connections()->iterateConnections(); for (ObjRef connection = connectionsIter->nextConnection(); connection != nullptr; connection = connectionsIter->nextConnection()) { @@ -696,14 +647,11 @@ void CellmlFileCellml10Exporter::copyConnections(iface::cellml_api::Model *pMode // Recursively process imported models - ObjRef imports = pModel->imports(); - ObjRef importsIter = imports->iterateImports(); + ObjRef importsIter = pModel->imports()->iterateImports(); for (ObjRef import = importsIter->nextImport(); import != nullptr; import = importsIter->nextImport()) { - ObjRef importedModel = import->importedModel(); - - copyConnections(importedModel); + copyConnections(import->importedModel()); } } @@ -716,8 +664,7 @@ void CellmlFileCellml10Exporter::propagateInitialValues() // it has a numeric initial value, the we use that. If not, then it's // an unavoidable error condition... - ObjRef components = mExportedModel->localComponents(); - ObjRef componentsIter = components->iterateComponents(); + ObjRef componentsIter = mExportedModel->localComponents()->iterateComponents(); for (ObjRef component = componentsIter->nextComponent(); component != nullptr; component = componentsIter->nextComponent()) { @@ -744,17 +691,10 @@ void CellmlFileCellml10Exporter::propagateInitialValues() if (!doubleInitialValueValid) { // The variable is initialised with the initial value of another - // variable, so retrieve that 'initial' variable - - ObjRef initialVariable = componentVariables->getVariable(initialValue); - - // Find its source - - ObjRef sourceVariable = initialVariable->sourceVariable(); - - // And copy its initial value + // variable, so retrieve that 'initial' variable, find its + // source and copy its initial value - componentVariable->initialValue(sourceVariable->initialValue()); + componentVariable->initialValue(componentVariables->getVariable(initialValue)->sourceVariable()->initialValue()); } } }