Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion src/api/libcellml/generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ limitations under the License.

#pragma once

#include "libcellml/generatorvariabletracker.h"
#include "libcellml/generatorprofile.h"
#include "libcellml/logger.h"

namespace libcellml {
Expand Down Expand Up @@ -62,6 +62,8 @@ class LIBCELLML_EXPORT Generator: public Logger
std::string interfaceCode(const AnalyserModelPtr &analyserModel, const GeneratorProfilePtr &generatorProfile, const GeneratorVariableTrackerPtr &generatorVariableTracker);

/**
* @overload
*
* @brief Get the interface code for the @ref AnalyserModel.
*
* Return the interface code for the @ref AnalyserModel, using the @ref GeneratorProfile.
Expand All @@ -75,6 +77,23 @@ class LIBCELLML_EXPORT Generator: public Logger
std::string interfaceCode(const AnalyserModelPtr &analyserModel, const GeneratorProfilePtr &generatorProfile);

/**
* @overload
*
* @brief Get the interface code for the @ref AnalyserModel.
*
* Return the interface code for the @ref AnalyserModel, using the @ref GeneratorProfile.
*
* @param analyserModel The @ref AnalyserModel for which we want to generate some interface code.
* @param generatorProfile The @ref GeneratorProfile to use to generate the interface code.
*
* @return The interface code as a @c std::string.
*/

std::string interfaceCode(const AnalyserModelPtr &analyserModel, GeneratorProfile::Profile profile);

/**
* @overload
*
* @brief Get the interface code for the @ref AnalyserModel.
*
* Return the interface code for the @ref AnalyserModel, using the @ref GeneratorVariableTracker.
Expand All @@ -88,6 +107,8 @@ class LIBCELLML_EXPORT Generator: public Logger
std::string interfaceCode(const AnalyserModelPtr &analyserModel, const GeneratorVariableTrackerPtr &generatorVariableTracker);

/**
* @overload
*
* @brief Get the interface code for the @ref AnalyserModel.
*
* Return the interface code for the @ref AnalyserModel, using the @ref GeneratorProfile.
Expand All @@ -113,6 +134,8 @@ class LIBCELLML_EXPORT Generator: public Logger
std::string implementationCode(const AnalyserModelPtr &analyserModel, const GeneratorProfilePtr &generatorProfile, const GeneratorVariableTrackerPtr &generatorVariableTracker);

/**
* @overload
*
* @brief Get the implementation code for the @ref AnalyserModel.
*
* Return the implementation code for the @ref AnalyserModel, using the @ref GeneratorProfile.
Expand All @@ -126,6 +149,23 @@ class LIBCELLML_EXPORT Generator: public Logger
std::string implementationCode(const AnalyserModelPtr &analyserModel, const GeneratorProfilePtr &generatorProfile);

/**
* @overload
*
* @brief Get the implementation code for the @ref AnalyserModel.
*
* Return the implementation code for the @ref AnalyserModel, using the @ref GeneratorProfile::Profile.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we not mention using the generator profile enumeration type. like in the interface files because here its clear from the actual type?

*
* @param analyserModel The @ref AnalyserModel for which we want to generate some implementation code.
* @param profile The @ref GeneratorProfile::Profile type to use to generate the implementation code.
*
* @return The implementation code as a @c std::string.
*/

std::string implementationCode(const AnalyserModelPtr &analyserModel, GeneratorProfile::Profile profile);

/**
* @overload
*
* @brief Get the implementation code for the @ref AnalyserModel.
*
* Return the implementation code for the @ref AnalyserModel, using the @ref GeneratorVariableTracker.
Expand All @@ -139,6 +179,8 @@ class LIBCELLML_EXPORT Generator: public Logger
std::string implementationCode(const AnalyserModelPtr &analyserModel, const GeneratorVariableTrackerPtr &generatorVariableTracker);

/**
* @overload
*
* @brief Get the implementation code for the @ref AnalyserModel.
*
* Return the implementation code for the @ref AnalyserModel, using the @ref GeneratorProfile.
Expand All @@ -150,6 +192,8 @@ class LIBCELLML_EXPORT Generator: public Logger
std::string implementationCode(const AnalyserModelPtr &analyserModel);

/**
* @overload
*
* @brief Get the equation code for the given @ref AnalyserEquationAst.
*
* Return the equation code for the given @ref AnalyserEquationAst using @ref GeneratorProfile.
Expand All @@ -164,6 +208,8 @@ class LIBCELLML_EXPORT Generator: public Logger
const GeneratorProfilePtr &generatorProfile);

/**
* @overload
*
* @brief Get the equation code for the given @ref AnalyserEquationAst using a default @ref GeneratorProfile.
*
* Return the equation code for the given @ref AnalyserEquationAst using a default @ref GeneratorProfile.
Expand Down
7 changes: 7 additions & 0 deletions src/bindings/interface/generator.i
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
%feature("docstring") libcellml::Generator::interfaceCode(const AnalyserModelPtr &analyserModel, const GeneratorProfilePtr &generatorProfile)
"Returns the interface code for the analyser model using the generator profile.";

%feature("docstring") libcellml::Generator::interfaceCode(const AnalyserModelPtr &analyserModel, GeneratorProfile::Profile profile)
"Returns the interface code for the analyser model using the generator profile enumeration type.";

%feature("docstring") libcellml::Generator::interfaceCode(const AnalyserModelPtr &analyserModel, const GeneratorVariableTrackerPtr &generatorVariableTracker)
"Returns the interface code for the analyser model using the generator variable tracker.";

Expand All @@ -31,6 +34,9 @@
%feature("docstring") libcellml::Generator::implementationCode(const AnalyserModelPtr &analyserModel, const GeneratorProfilePtr &generatorProfile)
"Returns the implementation code for the analyser model using the generator profile.";

%feature("docstring") libcellml::Generator::implementationCode(const AnalyserModelPtr &analyserModel, GeneratorProfile::Profile profile)
"Returns the implementation code for the analyser model using the generator profile enumeration type.";

%feature("docstring") libcellml::Generator::implementationCode(const AnalyserModelPtr &analyserModel, const GeneratorVariableTrackerPtr &generatorVariableTracker)
"Returns the implementation code for the analyser model using the generator variable tracker.";

Expand All @@ -45,6 +51,7 @@

%{
#include "libcellml/generator.h"
#include "libcellml/generatorvariabletracker.h"
%}

%pythoncode %{
Expand Down
2 changes: 2 additions & 0 deletions src/bindings/javascript/generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ EMSCRIPTEN_BINDINGS(libcellml_generator)
.smart_ptr_constructor("Generator", &libcellml::Generator::create)
.function("interfaceCode", select_overload<std::string(const libcellml::AnalyserModelPtr &)>(&libcellml::Generator::interfaceCode))
.function("interfaceCodeByProfile", select_overload<std::string(const libcellml::AnalyserModelPtr &, const libcellml::GeneratorProfilePtr &)>(&libcellml::Generator::interfaceCode))
.function("interfaceCodeByProfileEnumeration", select_overload<std::string(const libcellml::AnalyserModelPtr &, libcellml::GeneratorProfile::Profile)>(&libcellml::Generator::interfaceCode))
.function("interfaceCodeByVariableTracker", select_overload<std::string(const libcellml::AnalyserModelPtr &, const libcellml::GeneratorVariableTrackerPtr &)>(&libcellml::Generator::interfaceCode))
.function("interfaceCodeByProfileAndVariableTracker", select_overload<std::string(const libcellml::AnalyserModelPtr &, const libcellml::GeneratorProfilePtr &, const libcellml::GeneratorVariableTrackerPtr &)>(&libcellml::Generator::interfaceCode))
.function("implementationCode", select_overload<std::string(const libcellml::AnalyserModelPtr &)>(&libcellml::Generator::implementationCode))
.function("implementationCodeByProfile", select_overload<std::string(const libcellml::AnalyserModelPtr &, const libcellml::GeneratorProfilePtr &)>(&libcellml::Generator::implementationCode))
.function("implementationCodeByProfileEnumeration", select_overload<std::string(const libcellml::AnalyserModelPtr &, libcellml::GeneratorProfile::Profile)>(&libcellml::Generator::implementationCode))
.function("implementationCodeByVariableTracker", select_overload<std::string(const libcellml::AnalyserModelPtr &, const libcellml::GeneratorVariableTrackerPtr &)>(&libcellml::Generator::implementationCode))
.function("implementationCodeByProfileAndVariableTracker", select_overload<std::string(const libcellml::AnalyserModelPtr &, const libcellml::GeneratorProfilePtr &, const libcellml::GeneratorVariableTrackerPtr &)>(&libcellml::Generator::implementationCode))
.class_function("equationCode", select_overload<std::string(const libcellml::AnalyserEquationAstPtr &)>(&libcellml::Generator::equationCode))
Expand Down
10 changes: 10 additions & 0 deletions src/generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2293,6 +2293,11 @@ std::string Generator::interfaceCode(const AnalyserModelPtr &analyserModel, cons
return interfaceCode(analyserModel, generatorProfile, nullptr);
}

std::string Generator::interfaceCode(const AnalyserModelPtr &analyserModel, GeneratorProfile::Profile profile)
{
return interfaceCode(analyserModel, GeneratorProfile::create(profile), nullptr);
}

std::string Generator::interfaceCode(const AnalyserModelPtr &analyserModel,
const GeneratorVariableTrackerPtr &generatorVariableTracker)
{
Expand Down Expand Up @@ -2420,6 +2425,11 @@ std::string Generator::implementationCode(const AnalyserModelPtr &analyserModel,
return implementationCode(analyserModel, generatorProfile, nullptr);
}

std::string Generator::implementationCode(const AnalyserModelPtr &analyserModel, GeneratorProfile::Profile profile)
{
return implementationCode(analyserModel, GeneratorProfile::create(profile), nullptr);
}

std::string Generator::implementationCode(const AnalyserModelPtr &analyserModel, const GeneratorVariableTrackerPtr &generatorVariableTracker)
{
return implementationCode(analyserModel, pFunc()->mDefaultProfile, generatorVariableTracker);
Expand Down
6 changes: 6 additions & 0 deletions tests/bindings/javascript/generator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ describe("Generator tests", () => {
const interface_lines_profile = g.interfaceCodeByProfile(am, gp).split('\n')
expect(interface_lines_profile.length).toBe(38)

const interface_lines_profile_enumeration = g.interfaceCodeByProfileEnumeration(am, libcellml.GeneratorProfile.Profile.C).split('\n')
expect(interface_lines_profile_enumeration.length).toBe(38)

const gvt = new libcellml.GeneratorVariableTracker()

const interface_lines_variable_tracker = g.interfaceCodeByVariableTracker(am, gvt).split('\n')
Expand All @@ -65,6 +68,9 @@ describe("Generator tests", () => {
const implementation_lines_profile = g.implementationCodeByProfile(am, gp).split('\n')
expect(implementation_lines_profile.length).toBe(97)

const implementation_lines_profile_enumeration = g.implementationCodeByProfileEnumeration(am, libcellml.GeneratorProfile.Profile.C).split('\n')
expect(implementation_lines_profile_enumeration.length).toBe(97)

const implementation_lines_variable_tracker = g.implementationCodeByVariableTracker(am, gvt).split('\n')
expect(implementation_lines_variable_tracker.length).toBe(97)

Expand Down
3 changes: 3 additions & 0 deletions tests/bindings/python/test_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ def test_algebraic_eqn_computed_var_on_rhs(self):
self.assertEqual("", g.interfaceCode(am, profile))
self.assertEqual(file_contents("generator/algebraic_eqn_computed_var_on_rhs/model.py"), g.implementationCode(am, profile))

self.assertEqual("", g.interfaceCode(am, GeneratorProfile.Profile.PYTHON))
self.assertEqual(file_contents("generator/algebraic_eqn_computed_var_on_rhs/model.py"), g.implementationCode(am, GeneratorProfile.Profile.PYTHON))

variable_tracker = GeneratorVariableTracker()

self.assertEqual("", g.interfaceCode(am, profile, variable_tracker))
Expand Down
22 changes: 22 additions & 0 deletions tests/generator/generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1732,3 +1732,25 @@ TEST(Generator, modelWithComplexUnitsOutOfScope)

EXPECT_EQ_FILE_CONTENTS("generator/cellml_slc_example/model.py", generator->implementationCode(analyserModel, profile));
}

TEST(Generator, generateCodeUsingProfileEnum)
{
auto parser = libcellml::Parser::create();
auto model = parser->parseModel(fileContents("generator/algebraic_eqn_computed_var_on_rhs/model.cellml"));

EXPECT_EQ(size_t(0), parser->issueCount());

auto analyser = libcellml::Analyser::create();

analyser->analyseModel(model);

EXPECT_EQ(size_t(0), analyser->errorCount());

auto analyserModel = analyser->analyserModel();
auto generator = libcellml::Generator::create();

EXPECT_EQ_FILE_CONTENTS("generator/algebraic_eqn_computed_var_on_rhs/model.h", generator->interfaceCode(analyserModel, libcellml::GeneratorProfile::Profile::C));
EXPECT_EQ_FILE_CONTENTS("generator/algebraic_eqn_computed_var_on_rhs/model.c", generator->implementationCode(analyserModel, libcellml::GeneratorProfile::Profile::C));

EXPECT_EQ_FILE_CONTENTS("generator/algebraic_eqn_computed_var_on_rhs/model.py", generator->implementationCode(analyserModel, libcellml::GeneratorProfile::Profile::PYTHON));
}
Loading