Skip to content

Commit 28c2f4c

Browse files
committed
Moved parts of our Generator class to our new GeneratorInterpreter class.
1 parent bad703f commit 28c2f4c

6 files changed

+128
-53
lines changed

src/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ set(SOURCE_FILES
4949
${CMAKE_CURRENT_SOURCE_DIR}/entity.cpp
5050
${CMAKE_CURRENT_SOURCE_DIR}/enums.cpp
5151
${CMAKE_CURRENT_SOURCE_DIR}/generator.cpp
52+
${CMAKE_CURRENT_SOURCE_DIR}/generatorinterpreter.cpp
5253
${CMAKE_CURRENT_SOURCE_DIR}/generatorprofile.cpp
5354
${CMAKE_CURRENT_SOURCE_DIR}/generatorprofiletools.cpp
5455
${CMAKE_CURRENT_SOURCE_DIR}/importedentity.cpp
@@ -133,6 +134,8 @@ set(GIT_HEADER_FILES
133134
${CMAKE_CURRENT_SOURCE_DIR}/debug.h
134135
${CMAKE_CURRENT_SOURCE_DIR}/entity_p.h
135136
${CMAKE_CURRENT_SOURCE_DIR}/generator_p.h
137+
${CMAKE_CURRENT_SOURCE_DIR}/generatorinterpreter_p.h
138+
${CMAKE_CURRENT_SOURCE_DIR}/generatorinterpreter.h
136139
${CMAKE_CURRENT_SOURCE_DIR}/generatorprofile_p.h
137140
${CMAKE_CURRENT_SOURCE_DIR}/generatorprofilesha1values.h
138141
${CMAKE_CURRENT_SOURCE_DIR}/generatorprofiletools.h

src/generator.cpp

+31-50
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ limitations under the License.
2828
#include "libcellml/version.h"
2929

3030
#include "generator_p.h"
31+
#include "generatorinterpreter_p.h"
3132
#include "generatorprofilesha1values.h"
3233
#include "generatorprofiletools.h"
3334
#include "utilities.h"
@@ -41,28 +42,6 @@ void Generator::GeneratorImpl::reset()
4142
mCode = {};
4243
}
4344

44-
bool Generator::GeneratorImpl::modelHasOdes() const
45-
{
46-
switch (mModel->type()) {
47-
case AnalyserModel::Type::ODE:
48-
case AnalyserModel::Type::DAE:
49-
return true;
50-
default:
51-
return false;
52-
}
53-
}
54-
55-
bool Generator::GeneratorImpl::modelHasNlas() const
56-
{
57-
switch (mModel->type()) {
58-
case AnalyserModel::Type::NLA:
59-
case AnalyserModel::Type::DAE:
60-
return true;
61-
default:
62-
return false;
63-
}
64-
}
65-
6645
AnalyserVariablePtr analyserVariable(const AnalyserModelPtr &model, const VariablePtr &variable)
6746
{
6847
// Find and return the analyser variable associated with the given variable.
@@ -330,7 +309,7 @@ void Generator::GeneratorImpl::addStateAndVariableCountCode(bool interface)
330309
{
331310
std::string code;
332311

333-
if (modelHasOdes()
312+
if (mModelHasOdes
334313
&& ((interface && !mProfile->interfaceStateCountString().empty())
335314
|| (!interface && !mProfile->implementationStateCountString().empty()))) {
336315
code += interface ?
@@ -416,12 +395,12 @@ void Generator::GeneratorImpl::addInterfaceVariableInfoCode()
416395
{
417396
std::string code;
418397

419-
if (modelHasOdes()
398+
if (mModelHasOdes
420399
&& !mProfile->interfaceVoiInfoString().empty()) {
421400
code += mProfile->interfaceVoiInfoString();
422401
}
423402

424-
if (modelHasOdes()
403+
if (mModelHasOdes
425404
&& !mProfile->interfaceStateInfoString().empty()) {
426405
code += mProfile->interfaceStateInfoString();
427406
}
@@ -482,11 +461,11 @@ void Generator::GeneratorImpl::doAddImplementationVariableInfoCode(const std::st
482461

483462
void Generator::GeneratorImpl::addImplementationVariableInfoCode()
484463
{
485-
if (modelHasOdes()) {
464+
if (mModelHasOdes) {
486465
doAddImplementationVariableInfoCode(mProfile->implementationVoiInfoString(), {mModel->voi()}, true);
487466
}
488467

489-
if (modelHasOdes()) {
468+
if (mModelHasOdes) {
490469
doAddImplementationVariableInfoCode(mProfile->implementationStateInfoString(), mModel->states(), false);
491470
}
492471

@@ -653,7 +632,7 @@ void Generator::GeneratorImpl::addInterfaceCreateDeleteArrayMethodsCode()
653632
{
654633
std::string code;
655634

656-
if (modelHasOdes()
635+
if (mModelHasOdes
657636
&& !mProfile->interfaceCreateStatesArrayMethodString().empty()) {
658637
code += mProfile->interfaceCreateStatesArrayMethodString();
659638
}
@@ -688,7 +667,7 @@ void Generator::GeneratorImpl::addInterfaceCreateDeleteArrayMethodsCode()
688667

689668
void Generator::GeneratorImpl::addImplementationCreateDeleteArrayMethodsCode()
690669
{
691-
if (modelHasOdes()
670+
if (mModelHasOdes
692671
&& !mProfile->implementationCreateStatesArrayMethodString().empty()) {
693672
mCode += newLineIfNeeded()
694673
+ mProfile->implementationCreateStatesArrayMethodString();
@@ -724,7 +703,7 @@ void Generator::GeneratorImpl::addImplementationCreateDeleteArrayMethodsCode()
724703
void Generator::GeneratorImpl::addExternalVariableMethodTypeDefinitionCode()
725704
{
726705
if (mModel->hasExternalVariables()) {
727-
auto externalVariableMethodTypeDefinitionString = mProfile->externalVariableMethodTypeDefinitionString(modelHasOdes());
706+
auto externalVariableMethodTypeDefinitionString = mProfile->externalVariableMethodTypeDefinitionString(mModelHasOdes);
728707

729708
if (!externalVariableMethodTypeDefinitionString.empty()) {
730709
mCode += newLineIfNeeded()
@@ -735,16 +714,16 @@ void Generator::GeneratorImpl::addExternalVariableMethodTypeDefinitionCode()
735714

736715
void Generator::GeneratorImpl::addRootFindingInfoObjectCode()
737716
{
738-
if (modelHasNlas()
739-
&& !mProfile->rootFindingInfoObjectString(modelHasOdes(), mModel->hasExternalVariables()).empty()) {
717+
if (mModelHasNlas
718+
&& !mProfile->rootFindingInfoObjectString(mModelHasOdes, mModel->hasExternalVariables()).empty()) {
740719
mCode += newLineIfNeeded()
741-
+ mProfile->rootFindingInfoObjectString(modelHasOdes(), mModel->hasExternalVariables());
720+
+ mProfile->rootFindingInfoObjectString(mModelHasOdes, mModel->hasExternalVariables());
742721
}
743722
}
744723

745724
void Generator::GeneratorImpl::addExternNlaSolveMethodCode()
746725
{
747-
if (modelHasNlas()
726+
if (mModelHasNlas
748727
&& !mProfile->externNlaSolveMethodString().empty()) {
749728
mCode += newLineIfNeeded()
750729
+ mProfile->externNlaSolveMethodString();
@@ -753,10 +732,10 @@ void Generator::GeneratorImpl::addExternNlaSolveMethodCode()
753732

754733
void Generator::GeneratorImpl::addNlaSystemsCode()
755734
{
756-
if (modelHasNlas()
757-
&& !mProfile->objectiveFunctionMethodString(modelHasOdes(), mModel->hasExternalVariables()).empty()
758-
&& !mProfile->findRootMethodString(modelHasOdes(), mModel->hasExternalVariables()).empty()
759-
&& !mProfile->nlaSolveCallString(modelHasOdes(), mModel->hasExternalVariables()).empty()) {
735+
if (mModelHasNlas
736+
&& !mProfile->objectiveFunctionMethodString(mModelHasOdes, mModel->hasExternalVariables()).empty()
737+
&& !mProfile->findRootMethodString(mModelHasOdes, mModel->hasExternalVariables()).empty()
738+
&& !mProfile->nlaSolveCallString(mModelHasOdes, mModel->hasExternalVariables()).empty()) {
760739
// Note: only states and algebraic variables can be computed through an NLA system. Constants, computed
761740
// constants, and external variables cannot, by definition, be computed through an NLA system.
762741

@@ -804,7 +783,7 @@ void Generator::GeneratorImpl::addNlaSystemsCode()
804783
}
805784

806785
mCode += newLineIfNeeded()
807-
+ replace(replace(mProfile->objectiveFunctionMethodString(modelHasOdes(), mModel->hasExternalVariables()),
786+
+ replace(replace(mProfile->objectiveFunctionMethodString(mModelHasOdes, mModel->hasExternalVariables()),
808787
"[INDEX]", convertToString(equation->nlaSystemIndex())),
809788
"[CODE]", generateMethodBodyCode(methodBody));
810789

@@ -828,7 +807,7 @@ void Generator::GeneratorImpl::addNlaSystemsCode()
828807

829808
methodBody += newLineIfNeeded()
830809
+ mProfile->indentString()
831-
+ replace(replace(mProfile->nlaSolveCallString(modelHasOdes(), mModel->hasExternalVariables()),
810+
+ replace(replace(mProfile->nlaSolveCallString(mModelHasOdes, mModel->hasExternalVariables()),
832811
"[INDEX]", convertToString(equation->nlaSystemIndex())),
833812
"[SIZE]", convertToString(variablesCount));
834813

@@ -849,7 +828,7 @@ void Generator::GeneratorImpl::addNlaSystemsCode()
849828
}
850829

851830
mCode += newLineIfNeeded()
852-
+ replace(replace(replace(mProfile->findRootMethodString(modelHasOdes(), mModel->hasExternalVariables()),
831+
+ replace(replace(replace(mProfile->findRootMethodString(mModelHasOdes, mModel->hasExternalVariables()),
853832
"[INDEX]", convertToString(equation->nlaSystemIndex())),
854833
"[SIZE]", convertToString(variablesCount)),
855834
"[CODE]", generateMethodBodyCode(methodBody));
@@ -1744,16 +1723,16 @@ std::string Generator::GeneratorImpl::generateEquationCode(const AnalyserEquatio
17441723
res += mProfile->indentString()
17451724
+ generateVariableNameCode(variable->variable())
17461725
+ mProfile->equalityString()
1747-
+ replace(mProfile->externalVariableMethodCallString(modelHasOdes()),
1726+
+ replace(mProfile->externalVariableMethodCallString(mModelHasOdes),
17481727
"[INDEX]", convertToString(variable->index()))
17491728
+ mProfile->commandSeparatorString() + "\n";
17501729
}
17511730

17521731
break;
17531732
case AnalyserEquation::Type::NLA:
1754-
if (!mProfile->findRootCallString(modelHasOdes(), mModel->hasExternalVariables()).empty()) {
1733+
if (!mProfile->findRootCallString(mModelHasOdes, mModel->hasExternalVariables()).empty()) {
17551734
res += mProfile->indentString()
1756-
+ replace(mProfile->findRootCallString(modelHasOdes(), mModel->hasExternalVariables()),
1735+
+ replace(mProfile->findRootCallString(mModelHasOdes, mModel->hasExternalVariables()),
17571736
"[INDEX]", convertToString(equation->nlaSystemIndex()));
17581737
}
17591738

@@ -1778,7 +1757,7 @@ std::string Generator::GeneratorImpl::generateEquationCode(const AnalyserEquatio
17781757

17791758
void Generator::GeneratorImpl::addInterfaceComputeModelMethodsCode()
17801759
{
1781-
auto interfaceInitialiseVariablesMethodString = mProfile->interfaceInitialiseVariablesMethodString(modelHasOdes());
1760+
auto interfaceInitialiseVariablesMethodString = mProfile->interfaceInitialiseVariablesMethodString(mModelHasOdes);
17821761
std::string code;
17831762

17841763
if (!interfaceInitialiseVariablesMethodString.empty()) {
@@ -1791,12 +1770,12 @@ void Generator::GeneratorImpl::addInterfaceComputeModelMethodsCode()
17911770

17921771
auto interfaceComputeRatesMethodString = mProfile->interfaceComputeRatesMethodString(mModel->hasExternalVariables());
17931772

1794-
if (modelHasOdes()
1773+
if (mModelHasOdes
17951774
&& !interfaceComputeRatesMethodString.empty()) {
17961775
code += interfaceComputeRatesMethodString;
17971776
}
17981777

1799-
auto interfaceComputeVariablesMethodString = mProfile->interfaceComputeVariablesMethodString(modelHasOdes(),
1778+
auto interfaceComputeVariablesMethodString = mProfile->interfaceComputeVariablesMethodString(mModelHasOdes,
18001779
mModel->hasExternalVariables());
18011780

18021781
if (!interfaceComputeVariablesMethodString.empty()) {
@@ -1836,7 +1815,7 @@ std::string Generator::GeneratorImpl::generateConstantInitialisationCode(const s
18361815

18371816
void Generator::GeneratorImpl::addImplementationInitialiseVariablesMethodCode(std::vector<AnalyserEquationPtr> &remainingEquations)
18381817
{
1839-
auto implementationInitialiseVariablesMethodString = mProfile->implementationInitialiseVariablesMethodString(modelHasOdes());
1818+
auto implementationInitialiseVariablesMethodString = mProfile->implementationInitialiseVariablesMethodString(mModelHasOdes);
18401819

18411820
if (!implementationInitialiseVariablesMethodString.empty()) {
18421821
// Initialise our states (after, if needed, initialising the constant on which it depends).
@@ -1931,7 +1910,7 @@ void Generator::GeneratorImpl::addImplementationComputeRatesMethodCode(std::vect
19311910
{
19321911
auto implementationComputeRatesMethodString = mProfile->implementationComputeRatesMethodString(mModel->hasExternalVariables());
19331912

1934-
if (modelHasOdes()
1913+
if (mModelHasOdes
19351914
&& !implementationComputeRatesMethodString.empty()) {
19361915
std::string methodBody;
19371916

@@ -1958,7 +1937,7 @@ void Generator::GeneratorImpl::addImplementationComputeRatesMethodCode(std::vect
19581937

19591938
void Generator::GeneratorImpl::addImplementationComputeVariablesMethodCode(std::vector<AnalyserEquationPtr> &remainingEquations)
19601939
{
1961-
auto implementationComputeVariablesMethodString = mProfile->implementationComputeVariablesMethodString(modelHasOdes(),
1940+
auto implementationComputeVariablesMethodString = mProfile->implementationComputeVariablesMethodString(mModelHasOdes,
19621941
mModel->hasExternalVariables());
19631942

19641943
if (!implementationComputeVariablesMethodString.empty()) {
@@ -2012,6 +1991,8 @@ AnalyserModelPtr Generator::model()
20121991
void Generator::setModel(const AnalyserModelPtr &model)
20131992
{
20141993
mPimpl->mModel = model;
1994+
mPimpl->mModelHasOdes = modelHasOdes(model);
1995+
mPimpl->mModelHasNlas = modelHasNlas(model);
20151996
}
20161997

20171998
std::string Generator::interfaceCode() const

src/generator_p.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ struct Generator::GeneratorImpl
3737
{
3838
AnalyserModelPtr mModel;
3939

40+
bool mModelHasOdes = false;
41+
bool mModelHasNlas = false;
42+
4043
std::string mCode;
4144

4245
GeneratorProfilePtr mProfile = GeneratorProfile::create();
4346

4447
void reset();
4548

46-
bool modelHasOdes() const;
47-
bool modelHasNlas() const;
48-
4949
double scalingFactor(const VariablePtr &variable) const;
5050

5151
bool isNegativeNumber(const AnalyserEquationAstPtr &ast) const;

src/generatorinterpreter.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Copyright libCellML Contributors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
#include "generatorinterpreter_p.h"
18+
19+
namespace libcellml {
20+
21+
bool modelHasOdes(const AnalyserModelPtr &model)
22+
{
23+
switch (model->type()) {
24+
case AnalyserModel::Type::ODE:
25+
case AnalyserModel::Type::DAE:
26+
return true;
27+
default:
28+
return false;
29+
}
30+
}
31+
32+
bool modelHasNlas(const AnalyserModelPtr &model)
33+
{
34+
switch (model->type()) {
35+
case AnalyserModel::Type::NLA:
36+
case AnalyserModel::Type::DAE:
37+
return true;
38+
default:
39+
return false;
40+
}
41+
}
42+
43+
} // namespace libcellml

src/generatorinterpreter.h

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
Copyright libCellML Contributors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
#pragma once
18+
19+
namespace libcellml {
20+
} // namespace libcellml

src/generatorinterpreter_p.h

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
Copyright libCellML Contributors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
#pragma once
18+
19+
#include "generatorinterpreter.h"
20+
21+
#include "libcellml/analysermodel.h"
22+
23+
namespace libcellml {
24+
25+
bool modelHasOdes(const AnalyserModelPtr &model);
26+
bool modelHasNlas(const AnalyserModelPtr &model);
27+
28+
} // namespace libcellml

0 commit comments

Comments
 (0)