Skip to content

Commit

Permalink
Interpreter: skeleton code.
Browse files Browse the repository at this point in the history
  • Loading branch information
agarny committed Apr 8, 2024
1 parent a788603 commit 99672cd
Show file tree
Hide file tree
Showing 17 changed files with 330 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ set(SOURCE_FILES
${CMAKE_CURRENT_SOURCE_DIR}/importer.cpp
${CMAKE_CURRENT_SOURCE_DIR}/importsource.cpp
${CMAKE_CURRENT_SOURCE_DIR}/internaltypes.cpp
${CMAKE_CURRENT_SOURCE_DIR}/interpreter.cpp
${CMAKE_CURRENT_SOURCE_DIR}/issue.cpp
${CMAKE_CURRENT_SOURCE_DIR}/logger.cpp
${CMAKE_CURRENT_SOURCE_DIR}/mathmldtd.cpp
Expand Down Expand Up @@ -94,6 +95,7 @@ set(GIT_API_HEADER_FILES
${CMAKE_CURRENT_SOURCE_DIR}/api/libcellml/importedentity.h
${CMAKE_CURRENT_SOURCE_DIR}/api/libcellml/importer.h
${CMAKE_CURRENT_SOURCE_DIR}/api/libcellml/importsource.h
${CMAKE_CURRENT_SOURCE_DIR}/api/libcellml/interpreter.h
${CMAKE_CURRENT_SOURCE_DIR}/api/libcellml/issue.h
${CMAKE_CURRENT_SOURCE_DIR}/api/libcellml/logger.h
${CMAKE_CURRENT_SOURCE_DIR}/api/libcellml/model.h
Expand Down Expand Up @@ -136,6 +138,7 @@ set(GIT_HEADER_FILES
${CMAKE_CURRENT_SOURCE_DIR}/generatorprofilesha1values.h
${CMAKE_CURRENT_SOURCE_DIR}/generatorprofiletools.h
${CMAKE_CURRENT_SOURCE_DIR}/internaltypes.h
${CMAKE_CURRENT_SOURCE_DIR}/interpreter_p.h
${CMAKE_CURRENT_SOURCE_DIR}/issue_p.h
${CMAKE_CURRENT_SOURCE_DIR}/logger_p.h
${CMAKE_CURRENT_SOURCE_DIR}/mathmldtd.h
Expand Down
75 changes: 75 additions & 0 deletions src/api/libcellml/interpreter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
Copyright libCellML Contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#pragma once

#include "libcellml/exportdefinitions.h"
#include "libcellml/types.h"

namespace libcellml {

/**
* @brief The Interpreter class.
*
* The Interpreter class is for representing a CellML Interpreter.
*/
class LIBCELLML_EXPORT Interpreter
{
public:
~Interpreter(); /**< Destructor, @private. */
Interpreter(const Interpreter &rhs) = delete; /**< Copy constructor, @private. */
Interpreter(Interpreter &&rhs) noexcept = delete; /**< Move constructor, @private. */
Interpreter &operator=(Interpreter rhs) = delete; /**< Assignment operator, @private. */

/**
* @brief Create an @ref Interpreter object.
*
* Factory method to create an @ref Interpreter. Create an interpreter with::
*
* @code
* auto interpreter = libcellml::Interpreter::create();
* @endcode
*
* @return A smart pointer to an @ref Interpreter object.
*/
static InterpreterPtr create() noexcept;

/**
* @brief Get the @ref AnalyserModel.
*
* Get the @ref AnalyserModel used by this @ref Interpreter.
*
* @return The @ref AnalyserModel used.
*/
AnalyserModelPtr model();

/**
* @brief Set the @ref AnalyserModel.
*
* Set the @ref AnalyserModel to be used by this @ref Interpreter.
*
* @param model The @ref AnalyserModel to set.
*/
void setModel(const AnalyserModelPtr &model);

private:
Interpreter(); /**< Constructor, @private. */

struct InterpreterImpl;
InterpreterImpl *mPimpl; /**< Private member to implementation pointer, @private. */
};

} // namespace libcellml
1 change: 1 addition & 0 deletions src/api/libcellml/module/libcellml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ limitations under the License.
#include "libcellml/generatorprofile.h"
#include "libcellml/importer.h"
#include "libcellml/importsource.h"
#include "libcellml/interpreter.h"
#include "libcellml/issue.h"
#include "libcellml/logger.h"
#include "libcellml/model.h"
Expand Down
2 changes: 2 additions & 0 deletions src/api/libcellml/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class GeneratorProfile; /**< Forward declaration of GeneratorProfile class. */
using GeneratorProfilePtr = std::shared_ptr<GeneratorProfile>; /**< Type definition for shared generator variable pointer. */
class Importer; /**< Forward declaration of Importer class. */
using ImporterPtr = std::shared_ptr<Importer>; /**< Type definition for shared importer pointer. */
class Interpreter; /**< Forward declaration of Interpreter class. */
using InterpreterPtr = std::shared_ptr<Interpreter>; /**< Type definition for shared Interpreter pointer. */
class Issue; /**< Forward declaration of Issue class. */
using IssuePtr = std::shared_ptr<Issue>; /**< Type definition for shared issue pointer. */
class Logger; /**< Forward declaration of Parser class. */
Expand Down
29 changes: 29 additions & 0 deletions src/bindings/interface/interpreter.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
%module(package="libcellml") interpreter

#define LIBCELLML_EXPORT

%include <std_string.i>

%import "analysermodel.i"
%import "createconstructor.i"

%feature("docstring") libcellml::Interpreter
"Creates a :class:`Interpreter` object.";

%feature("docstring") libcellml::Interpreter::model
"Returns the model to interpret.";

%feature("docstring") libcellml::Interpreter::setModel
"Sets the model to interpret.";

%{
#include "libcellml/interpreter.h"
%}

%pythoncode %{
# libCellML generated wrapper code starts here.
%}

%create_constructor(Interpreter)

%include "libcellml/interpreter.h"
1 change: 1 addition & 0 deletions src/bindings/interface/types.i
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Provides support for shared pointers declared in types.h.
%shared_ptr(libcellml::Importer)
%shared_ptr(libcellml::ImportSource)
%shared_ptr(libcellml::ImportedEntity)
%shared_ptr(libcellml::Interpreter)
%shared_ptr(libcellml::Issue)
%shared_ptr(libcellml::Logger)
%shared_ptr(libcellml::Model)
Expand Down
1 change: 1 addition & 0 deletions src/bindings/javascript/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ set(SOURCE_FILES
${CMAKE_CURRENT_SOURCE_DIR}/importedentity.cpp
${CMAKE_CURRENT_SOURCE_DIR}/importer.cpp
${CMAKE_CURRENT_SOURCE_DIR}/importsource.cpp
${CMAKE_CURRENT_SOURCE_DIR}/interpreter.cpp
${CMAKE_CURRENT_SOURCE_DIR}/issue.cpp
${CMAKE_CURRENT_SOURCE_DIR}/logger.cpp
${CMAKE_CURRENT_SOURCE_DIR}/model.cpp
Expand Down
30 changes: 30 additions & 0 deletions src/bindings/javascript/interpreter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
Copyright libCellML Contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include <emscripten/bind.h>

#include "libcellml/interpreter.h"

using namespace emscripten;

EMSCRIPTEN_BINDINGS(libcellml_interpreter)
{
class_<libcellml::Interpreter>("Interpreter")
.smart_ptr_constructor("Interpreter", &libcellml::Interpreter::create)
.function("model", &libcellml::Interpreter::model)
.function("setModel", &libcellml::Interpreter::setModel)
;
}
1 change: 1 addition & 0 deletions src/bindings/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ set(SWIG_INTERFACE_SRCS
../interface/importer.i
../interface/importsource.i
../interface/importedentity.i
../interface/interpreter.i
../interface/issue.i
../interface/logger.i
../interface/model.i
Expand Down
1 change: 1 addition & 0 deletions src/bindings/python/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from libcellml.generatorprofile import GeneratorProfile
from libcellml.importer import Importer
from libcellml.importsource import ImportSource
from libcellml.interpreter import Interpreter
from libcellml.issue import Issue
from libcellml.model import Model
from libcellml.parser import Parser
Expand Down
48 changes: 48 additions & 0 deletions src/interpreter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Copyright libCellML Contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include "libcellml/interpreter.h"

#include "interpreter_p.h"

namespace libcellml {

Interpreter::Interpreter()
: mPimpl(new InterpreterImpl())
{
}

Interpreter::~Interpreter()
{
delete mPimpl;
}

InterpreterPtr Interpreter::create() noexcept
{
return std::shared_ptr<Interpreter> {new Interpreter {}};
}

AnalyserModelPtr Interpreter::model()
{
return mPimpl->mModel;
}

void Interpreter::setModel(const AnalyserModelPtr &model)
{
mPimpl->mModel = model;
}

} // namespace libcellml
35 changes: 35 additions & 0 deletions src/interpreter_p.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Copyright libCellML Contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#pragma once

#include "libcellml/interpreter.h"

namespace libcellml {

std::string generateDoubleCode(const std::string &value);

/**
* @brief The Interpreter::InterpreterImpl struct.
*
* The private implementation for the Interpreter class.
*/
struct Interpreter::InterpreterImpl
{
AnalyserModelPtr mModel;
};

} // namespace libcellml
1 change: 1 addition & 0 deletions tests/bindings/javascript/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ set(TEST_SRCS
generator.test.js
generatorprofile.test.js
importsource.test.js
interpreter.test.js
model.test.js
namedentity.test.js
parentedentity.test.js
Expand Down
41 changes: 41 additions & 0 deletions tests/bindings/javascript/interpreter.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Copyright libCellML Contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

const loadLibCellML = require('libcellml.js/libcellml.common')
const { basicModel } = require('./resources')

let libcellml = null

describe("Interpreter tests", () => {
beforeAll(async () => {
libcellml = await loadLibCellML()
})
test('Checking Interpreter model manipulation.', () => {
const i = new libcellml.Interpreter()
const p = new libcellml.Parser(true)

m = p.parseModel(basicModel)
a = new libcellml.Analyser()

a.analyseModel(m)

expect(i.model()).toBe(null)

i.setModel(a.model())

expect(i.model()).toBeDefined()
})
})
1 change: 1 addition & 0 deletions tests/bindings/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ set(TEST_SRCS
test_import_requirement.py
test_import_source.py
test_importer.py
test_interpreter.py
test_issue.py
test_model.py
test_parser.py
Expand Down
42 changes: 42 additions & 0 deletions tests/bindings/python/test_interpreter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#
# Tests the Interpreter class bindings
#
import unittest


class InterpreterTestCase(unittest.TestCase):

def test_create_destroy(self):
from libcellml import Interpreter

x = Interpreter()
del x

def test_algebraic_eqn_computed_var_on_rhs(self):
from libcellml import Analyser
from libcellml import AnalyserModel
from libcellml import Interpreter
from libcellml import Parser
from test_resources import file_contents

p = Parser()
m = p.parseModel(file_contents('generator/algebraic_eqn_computed_var_on_rhs/model.cellml'))

a = Analyser()
a.analyseModel(m)

am = a.model()

self.assertEqual(AnalyserModel.Type.ALGEBRAIC, am.type())

i = Interpreter()

self.assertIsNone(i.model())

i.setModel(am)

self.assertIsNotNone(i.model())


if __name__ == '__main__':
unittest.main()
Loading

0 comments on commit 99672cd

Please sign in to comment.