コード例 #1
0
    def test_replace_model(self):
        from libcellml import Component, Importer, Model, Parser

        parser = Parser()

        model = parser.parseModel(
            file_contents("importer/generic_no_source.cellml"))

        importer = Importer()

        wrongSourceModel = Model("wrong")
        rightSourceModel = Model("right")
        rightSourceModel.addComponent(Component("a"))
        rightSourceModel.addComponent(Component("b"))
        rightSourceModel.addComponent(Component("c"))
        rightSourceModel.addComponent(Component("d"))

        self.assertTrue(
            importer.addModel(wrongSourceModel, "i_dont_exist.cellml"))

        self.assertFalse(
            importer.replaceModel(rightSourceModel, "not_in_library"))
        self.assertTrue(
            importer.replaceModel(rightSourceModel, "i_dont_exist.cellml"))

        importer.resolveImports(model, "")

        self.assertEqual(0, importer.issueCount())
        self.assertFalse(model.hasUnresolvedImports())
コード例 #2
0
    def test_replace_model(self):
        from libcellml import Component, Importer, Model, Parser

        parser = Parser()

        model = parser.parseModel(
            file_contents('importer/generic_no_source.cellml'))

        importer = Importer()

        wrongSourceModel = Model('wrong')
        rightSourceModel = Model('right')
        rightSourceModel.addComponent(Component('a'))
        rightSourceModel.addComponent(Component('b'))
        rightSourceModel.addComponent(Component('c'))
        rightSourceModel.addComponent(Component('d'))

        self.assertTrue(
            importer.addModel(wrongSourceModel, 'i_dont_exist.cellml'))

        self.assertFalse(
            importer.replaceModel(rightSourceModel, 'not_in_library'))
        self.assertTrue(
            importer.replaceModel(rightSourceModel, 'i_dont_exist.cellml'))

        importer.resolveImports(model, '')

        self.assertEqual(0, importer.issueCount())
        self.assertFalse(model.hasUnresolvedImports())
コード例 #3
0
    def test_importer(self):
        from libcellml import Importer, Parser, Printer

        parser = Parser()
        i = Importer()

        m = parser.parseModel(file_contents('importer/diamond.cellml'))
        printer = Printer()

        i.resolveImports(m, resource_path('importer/'))
        self.assertFalse(m.hasUnresolvedImports())

        # Library should contain left, right, and one instance (not two) of the point.
        self.assertEqual(3, i.libraryCount())
        self.assertEqual(resource_path('importer/diamond_left.cellml'),
                         i.key(0))
        self.assertEqual(resource_path('importer/diamond_point.cellml'),
                         i.key(1))
        self.assertEqual(resource_path('importer/diamond_right.cellml'),
                         i.key(2))

        # Access library items by their URL.
        left = i.library(resource_path('importer/diamond_left.cellml'))

        self.assertEqual(file_contents('importer/diamond_left.cellml'),
                         printer.printModel(left))
コード例 #4
0
ファイル: test_parser.py プロジェクト: MichaelClerx/libcellml
    def test_parse_model(self):
        import libcellml
        from libcellml import Parser

        # ModelPtr parseModel(const std::string &input)
        p = Parser()
        self.assertIsInstance(p.parseModel('rubbish'), libcellml.Model)
コード例 #5
0
    def test_create_destroy(self):
        from libcellml import Parser

        x = Parser()
        del (x)
        y = Parser()
        z = Parser(y)
        del (y, z)
コード例 #6
0
    def test_ids(self):
        from libcellml import Annotator, Parser

        annotator = Annotator()
        parser = Parser()

        model = parser.parseModel(file_contents("annotator/unique_ids.cellml"))
        annotator.setModel(model)

        self.assertEqual(24, len(annotator.ids()))
コード例 #7
0
    def test_duplicate_count(self):
        from libcellml import Annotator, Parser

        annotator = Annotator()
        parser = Parser()

        model = parser.parseModel(file_contents("annotator/lots_of_duplicate_ids.cellml"))
        annotator.setModel(model)

        self.assertEqual(8, annotator.itemCount("duplicateId1"))
        self.assertEqual(7, annotator.itemCount("duplicateId3"))
コード例 #8
0
ファイル: test_parser.py プロジェクト: opencor/libcellml
    def test_inheritance(self):
        import libcellml
        from libcellml import Parser

        x = Parser()
        self.assertIsInstance(x, libcellml.logger.Logger)

        # Test access to inherited methods
        self.assertIsNone(x.issue(0))
        self.assertIsNone(x.issue(-1))
        self.assertEqual(x.issueCount(), 0)
コード例 #9
0
    def test_has_model(self):
        from libcellml import Annotator, Parser

        annotator = Annotator()
        parser = Parser()

        self.assertFalse(annotator.hasModel())

        model = parser.parseModel(file_contents("annotator/unique_ids.cellml"))
        annotator.setModel(model)

        self.assertTrue(annotator.hasModel())
コード例 #10
0
    def test_remove_all_models(self):
        from libcellml import Parser, Importer
        parser = Parser()
        model = parser.parseModel(file_contents('importer/diamond.cellml'))
        importer = Importer()
        importer.resolveImports(model, resource_path('importer/'))

        self.assertEqual(3, importer.libraryCount())

        importer.removeAllModels()

        self.assertEqual(0, importer.libraryCount())
コード例 #11
0
    def test_flatten(self):
        from libcellml import Importer, Parser

        parser = Parser()
        importer = Importer()

        model = parser.parseModel(file_contents('importer/diamond.cellml'))

        importer.resolveImports(model, resource_path('importer/'))

        flattenedModel = importer.flattenModel(model)
        self.assertEqual(2, flattenedModel.componentCount())
コード例 #12
0
    def test_auto_ids(self):
        from libcellml import Annotator, Parser, Variable

        annotator = Annotator()
        parser = Parser()
        model_string = file_contents("annotator/unique_ids.cellml")

        model = parser.parseModel(model_string)
        annotator.setModel(model)

        annotator.clearAllIds()
        annotator.assignAllIds()

        self.assertEqual("b4da55", model.id())
        self.assertEqual("b4da56", model.importSource(0).id())
        self.assertEqual("b4da57", model.importSource(1).id())
        self.assertEqual("b4da58", model.units(0).id())
        self.assertEqual("b4da59", model.units(1).id())
        self.assertEqual("b4da5a", model.units(2).id())
        self.assertEqual("b4da5b", model.units(3).id())
        self.assertEqual("b4da5c", model.units(1).unitId(0))
        self.assertEqual("b4da5d", model.component(0).id())
        self.assertEqual("b4da5e", model.component(1).id())
        self.assertEqual("b4da5f", model.component(1).component(0).id())
        self.assertEqual("b4da60", model.component(1).variable(0).id())
        self.assertEqual("b4da61", model.component(1).variable(1).id())
        self.assertEqual("b4da62",
                         model.component(1).component(0).variable(0).id())
        self.assertEqual("b4da63",
                         model.component(1).component(0).variable(1).id())

        self.assertEqual("b4da64", model.component(1).reset(0).id())
        self.assertEqual("b4da65", model.component(1).reset(0).resetValueId())
        self.assertEqual("b4da66", model.component(1).reset(0).testValueId())

        c2v1 = model.component("component2").variable("variable1")
        c2v2 = model.component("component2").variable("variable2")
        c3v1 = model.component("component3").variable("variable1")
        c3v2 = model.component("component3").variable("variable2")

        self.assertEqual("b4da67",
                         Variable.equivalenceConnectionId(c2v1, c3v1))
        self.assertEqual("b4da67",
                         Variable.equivalenceConnectionId(c2v2, c3v2))
        self.assertEqual("b4da68", Variable.equivalenceMappingId(c2v1, c3v1))
        self.assertEqual("b4da69", Variable.equivalenceMappingId(c2v2, c3v2))
        self.assertEqual("b4da6a",
                         model.component("component2").encapsulationId())
        self.assertEqual("b4da6b",
                         model.component("component3").encapsulationId())
        self.assertEqual("b4da6c", model.encapsulationId())
コード例 #13
0
    def test_raise_not_found_issue(self):
        from libcellml import Annotator, Parser

        annotator = Annotator()
        parser = Parser()

        message = 'Could not find an item with an id of \'i_dont_exist\' in the model.'

        model = parser.parseModel(file_contents('annotator/unique_ids.cellml'))
        annotator.setModel(model)
        annotator.item('i_dont_exist')

        self.assertEqual(1, annotator.issueCount())
        self.assertEqual(message, annotator.issue(0).description())
コード例 #14
0
    def test_raise_non_unique_issue(self):
        from libcellml import Annotator, Parser

        annotator = Annotator()
        parser = Parser()

        non_unique_message = 'The identifier \'duplicateId\' occurs 29 times in the model so a unique item cannot be located.'

        model = parser.parseModel(file_contents('annotator/duplicate_ids.cellml'))
        annotator.setModel(model)

        annotator.item('duplicateId')
        self.assertEqual(1, annotator.issueCount())
        self.assertEqual(non_unique_message, annotator.issue(0).description())
コード例 #15
0
    def test_algebraic_eqn_computed_var_on_rhs(self):
        from libcellml import Analyser
        from libcellml import AnalyserModel
        from libcellml import Generator
        from libcellml import GeneratorProfile
        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())

        g = Generator()

        self.assertIsNone(g.model())

        g.setModel(am)

        self.assertIsNotNone(g.model())

        self.assertEqual(
            file_contents(
                "generator/algebraic_eqn_computed_var_on_rhs/model.h"),
            g.interfaceCode())
        self.assertEqual(
            file_contents(
                "generator/algebraic_eqn_computed_var_on_rhs/model.c"),
            g.implementationCode())

        self.assertEqual(GeneratorProfile.Profile.C, g.profile().profile())

        profile = GeneratorProfile(GeneratorProfile.Profile.PYTHON)
        g.setProfile(profile)

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

        self.assertEqual(GeneratorProfile.Profile.PYTHON,
                         g.profile().profile())
コード例 #16
0
    def test_item(self):
        from libcellml import Annotator, CellmlElementType, Model, Parser

        annotator = Annotator()
        model = Model()
        parser = Parser()

        model_string = file_contents("annotator/unique_ids.cellml")

        model = parser.parseModel(model_string)
        annotator.setModel(model)

        self.assertEqual(CellmlElementType.UNDEFINED, annotator.item("not_an_id").type())
        self.assertEqual(CellmlElementType.UNDEFINED, annotator.item("not_an_id", 3).type())
        self.assertEqual(CellmlElementType.MAP_VARIABLES, annotator.item("map_variables_2").type())
コード例 #17
0
    def test_resolve_imports(self):
        from libcellml import Importer
        from libcellml import Parser

        i = Importer()
        p = Parser()

        m = p.parseModel(file_contents('sine_approximations_import.xml'))

        self.assertEqual(0, p.issueCount())

        self.assertTrue(m.hasUnresolvedImports())

        i.resolveImports(m, resource_path())
        self.assertFalse(m.hasUnresolvedImports())
コード例 #18
0
    def test_is_unique(self):
        from libcellml import Annotator, Parser

        annotator = Annotator()
        parser = Parser()

        model = parser.parseModel(file_contents("annotator/unique_ids.cellml"))
        annotator.setModel(model)

        self.assertTrue(annotator.isUnique("variable_3"))

        model = parser.parseModel(file_contents("annotator/lots_of_duplicate_ids.cellml"))
        annotator.setModel(model)

        self.assertFalse(annotator.isUnique("duplicateId2"))
コード例 #19
0
    def test_is_resolved_component(self):
        from libcellml import Importer, Parser

        parser = Parser()
        importer = Importer()

        model = parser.parseModel(
            file_contents("importer/component_importer_unresolved.cellml"))
        self.assertEqual(0, parser.issueCount())
        importer.resolveImports(model, resource_path("importer/"))

        self.assertEqual(1, importer.issueCount())

        c = model.component(0)

        self.assertFalse(c.isResolved())
コード例 #20
0
    def test_clear_imports(self):
        from libcellml import Importer, Parser

        parser = Parser()
        importer = Importer()

        model = parser.parseModel(
            file_contents('importer/nested_components.cellml'))
        self.assertEqual(0, parser.issueCount())

        importer.resolveImports(model, resource_path('importer/'))

        self.assertEqual(0, importer.issueCount())

        self.assertFalse(model.hasUnresolvedImports())
        importer.clearImports(model)
        self.assertTrue(model.hasUnresolvedImports())
コード例 #21
0
    def test_is_resolved_units(self):
        from libcellml import Importer, Parser

        parser = Parser()
        importer = Importer()

        model = parser.parseModel(
            file_contents("importer/master_units.cellml"))
        self.assertEqual(0, parser.issueCount())

        importer.resolveImports(model, resource_path("importer/"))

        self.assertEqual(0, importer.issueCount())

        u = model.units(0)

        self.assertTrue(u.isResolved())
コード例 #22
0
    def test_requirements(self):
        from libcellml import Parser, Importer
        keys = [
            'complicatedComponents.cellml',
            'complicatedUnits.cellml',
        ]

        parser = Parser()
        model = parser.parseModel(file_contents('import-requirements/complicatedExample.cellml'))
        self.assertEqual(0, parser.issueCount())

        requirements = model.importRequirements()

        i = 0
        for r in requirements:
            self.assertEqual(keys[i], r)
            i += 1
コード例 #23
0
    def test_inheritance(self):
        import libcellml
        from libcellml import Parser

        x = Parser()
        self.assertIsInstance(x, libcellml.Logger)

        # Test access to inherited methods
        self.assertIsNone(x.getError(0))
        self.assertIsNone(x.getError(-1))
        self.assertEqual(x.errorCount(), 0)
        x.addError(libcellml.Error())
        self.assertEqual(x.errorCount(), 1)
コード例 #24
0
    def test_add_model(self):
        from libcellml import Component, Importer, Model, Parser

        parser = Parser()
        importer = Importer()

        model = parser.parseModel(
            file_contents('importer/generic_no_source.cellml'))

        sourceModel = Model('source')
        sourceModel.addComponent(Component('a'))
        sourceModel.addComponent(Component('b'))
        sourceModel.addComponent(Component('c'))
        sourceModel.addComponent(Component('d'))

        # Add a model manually to the library, including the URL that it will replace in future imports.
        self.assertTrue(importer.addModel(sourceModel, 'i_dont_exist.cellml'))
        self.assertFalse(importer.addModel(sourceModel, 'i_dont_exist.cellml'))

        importer.resolveImports(model, '')

        self.assertEqual(0, importer.issueCount())
        self.assertFalse(model.hasUnresolvedImports())
コード例 #25
0
    def test_resolve_imports(self):
        from libcellml import Importer
        from libcellml import Parser

        i = Importer()
        p = Parser()

        m1 = p.parseModel(file_contents('importer/units_imported.cellml'))
        m2 = p.parseModel(file_contents('importer/units_imported.cellml'))
        m3 = p.parseModel(file_contents('importer/units_imported.cellml'))

        self.assertEqual(0, p.issueCount())

        self.assertTrue(m1.hasUnresolvedImports())
        i.resolveImports(m1, resource_path('importer/'))
        self.assertFalse(m1.hasUnresolvedImports())

        self.assertTrue(m2.hasUnresolvedImports())
        i.resolveImports(m2, resource_path('importer'))
        self.assertFalse(m2.hasUnresolvedImports())

        self.assertTrue(m3.hasUnresolvedImports())
        i.resolveImports(m3, resource_path('importer\\'))
        self.assertFalse(m3.hasUnresolvedImports())
コード例 #26
0
    model_file = 'MembraneModel.cellml'
    if len(sys.argv) > 1:
        model_file = sys.argv[1]
    # STEP 1: Parse an existing model from a CellML file.
    #         The Parser class is used to deserialise a CellML string into a Model instance.
    #         This means that you're responsible for finding, opening and reading the.cellml
    #         file into a single string.  The Parser will then read that string and return a model.

    #  1.a
    #       Read a CellML file into a std.string.
    with open(model_file) as f:
        content = f.read()

    #  1.b
    #       Create a Parser item.
    parser = Parser()

    #  1.c
    #       Use the parser to deserialise the contents of the string you've read and return the model.
    model = parser.parseModel(content)

    #  1.d
    #       Print the parsed model to the terminal for viewing.
    print_model(model, False)

    #  end 1

    print('----------------------------------------------------------')
    print('   STEP 2: Resolve the imports and flatten                ')
    print('----------------------------------------------------------')
コード例 #27
0
from libcellml import Analyser, Importer, Model, Parser, Printer, Validator

if __name__ == '__main__':
    # STEP 1
    # Parse an existing CellML model from a file.
    read_file = open('debugAnalysisExample.cellml', 'r')
    parser = Parser()
    model = parser.parseModel(read_file.read())

    # STEP 2
    # Resolve any imports and flatten the model for analysis.
    importer = Importer()

    # Resolve the imports.
    importer.resolveImports(model, '')

    # Check for issues.
    print('The importer found {} issues.'.format(importer.issueCount()))
    for i in range(0, importer.issueCount()):
        issue = importer.issue(i)
        print(issue.description())

    # Flatten the model.
    model = importer.flattenModel(model)

    # STEP 3
    # Create an Validator instance and pass the model to it for processing.
    validator = Validator()
    validator.validateModel(model)

    # Print any issues to the terminal.
コード例 #28
0
ファイル: test_parser.py プロジェクト: opencor/libcellml
    def test_create_destroy(self):
        from libcellml import Parser

        x = Parser()
        del x
コード例 #29
0
    def test_issue_types(self):
        from libcellml import Parser, Issue

        p = Parser()

        i1 = Issue()
        i1.setLevel(Issue.Level.ERROR)
        i1.setDescription("error error")
        p.addIssue(i1)

        i4 = Issue()
        i4.setLevel(Issue.Level.WARNING)
        i4.setDescription("warning warning")
        p.addIssue(i4)

        i2 = Issue()
        i2.setLevel(Issue.Level.HINT)
        i2.setDescription("hint hint")
        p.addIssue(i2)

        i3 = Issue()
        i3.setLevel(Issue.Level.MESSAGE)
        i3.setDescription("message message")
        p.addIssue(i3)

        self.assertEqual(1, p.errorCount())
        error = p.error(0)
        self.assertEqual("error error", error.description())

        self.assertEqual(1, p.warningCount())
        warning = p.warning(0)
        self.assertEqual("warning warning", warning.description())

        self.assertEqual(1, p.hintCount())
        hint = p.hint(0)
        self.assertEqual("hint hint", hint.description())

        self.assertEqual(1, p.messageCount())
        message = p.message(0)
        self.assertEqual("message message", message.description())

        self.assertEqual(4, p.issueCount())
        p.removeAllIssues()
        self.assertEqual(0, p.issueCount())
コード例 #30
0
ファイル: test_analyser.py プロジェクト: awickens/libcellml
    def test_coverage(self):
        from libcellml import Analyser
        from libcellml import AnalyserEquation
        from libcellml import AnalyserEquationAst
        from libcellml import AnalyserExternalVariable
        from libcellml import AnalyserModel
        from libcellml import AnalyserVariable
        from libcellml import Model
        from libcellml import Parser
        from test_resources import file_contents

        # Try to create an analyser equation/model/variable, something that is not allowed.

        self.assertRaises(AttributeError, AnalyserEquation)
        self.assertRaises(AttributeError, AnalyserModel)
        self.assertRaises(AttributeError, AnalyserVariable)

        # Analyse a model, so we can then do some coverage.

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

        a = Analyser()
        a.analyseModel(m)

        # Ensure coverage for Analyser.

        c = m.component(1)
        v0 = c.variable(0)

        aev = AnalyserExternalVariable(v0)

        self.assertTrue(a.addExternalVariable(aev))

        self.assertTrue(a.containsExternalVariable(aev))
        self.assertTrue(a.containsExternalVariable(m, c.name(), v0.name()))

        self.assertEqual(aev.variable().name(), a.externalVariable(0).variable().name())
        self.assertEqual(aev.variable().name(), a.externalVariable(m, c.name(), v0.name()).variable().name())

        v2 = c.variable(2)

        self.assertTrue(a.addExternalVariable(AnalyserExternalVariable(c.variable(1))))
        self.assertTrue(a.addExternalVariable(AnalyserExternalVariable(v2)))
        self.assertTrue(a.addExternalVariable(AnalyserExternalVariable(c.variable(3))))

        self.assertEqual(4, a.externalVariableCount())

        self.assertTrue(a.removeExternalVariable(1))
        self.assertTrue(a.removeExternalVariable(aev))
        self.assertTrue(a.removeExternalVariable(m, c.name(), v2.name()))

        a.removeAllExternalVariables()

        # Ensure coverage for AnalyserModel.

        am = a.model()

        self.assertTrue(am.isValid())

        self.assertFalse(am.hasExternalVariables())

        self.assertIsNotNone(am.voi())

        self.assertEqual(4, am.stateCount())
        self.assertIsNotNone(am.states())
        self.assertIsNotNone(am.state(3))

        self.assertEqual(17, am.variableCount())
        self.assertIsNotNone(am.variables())
        self.assertIsNotNone(am.variable(3))

        self.assertEqual(16, am.equationCount())
        self.assertIsNotNone(am.equations())
        self.assertIsNotNone(am.equation(3))

        self.assertFalse(am.needEqFunction())
        self.assertFalse(am.needNeqFunction())
        self.assertFalse(am.needLtFunction())
        self.assertFalse(am.needLeqFunction())
        self.assertFalse(am.needGtFunction())
        self.assertFalse(am.needGeqFunction())
        self.assertFalse(am.needAndFunction())
        self.assertFalse(am.needOrFunction())
        self.assertFalse(am.needXorFunction())
        self.assertFalse(am.needNotFunction())
        self.assertFalse(am.needMinFunction())
        self.assertFalse(am.needMaxFunction())
        self.assertFalse(am.needSecFunction())
        self.assertFalse(am.needCscFunction())
        self.assertFalse(am.needCotFunction())
        self.assertFalse(am.needSechFunction())
        self.assertFalse(am.needCschFunction())
        self.assertFalse(am.needCothFunction())
        self.assertFalse(am.needAsecFunction())
        self.assertFalse(am.needAcscFunction())
        self.assertFalse(am.needAcotFunction())
        self.assertFalse(am.needAsechFunction())
        self.assertFalse(am.needAcschFunction())
        self.assertFalse(am.needAcothFunction())

        self.assertTrue(am.areEquivalentVariables(am.voi().variable(), am.voi().variable()))

        # Ensure coverage for AnalyserVariable.

        av = am.variable(3)

        self.assertEqual(AnalyserVariable.Type.CONSTANT, av.type())
        self.assertEqual(3, av.index())
        self.assertIsNotNone(av.initialisingVariable())
        self.assertIsNotNone(av.variable())
        self.assertIsNone(av.equation())

        # Ensure coverage for AnalyserEquation.

        ae = am.equation(3)

        self.assertEqual(AnalyserEquation.Type.RATE, ae.type())
        self.assertIsNotNone(ae.ast())
        self.assertIsNotNone(ae.dependencies())
        self.assertTrue(ae.isStateRateBased())
        self.assertIsNotNone(ae.variable())

        # Ensure coverage for AnalyserEquationAst.

        aea = ae.ast()

        self.assertEqual(AnalyserEquationAst.Type.ASSIGNMENT, aea.type())
        self.assertEqual('', aea.value())
        self.assertIsNone(aea.variable())
        self.assertIsNone(aea.parent())
        self.assertIsNotNone(aea.leftChild())
        self.assertIsNotNone(aea.rightChild())

        aea.setType(AnalyserEquationAst.Type.EQ)
        aea.setValue(AnalyserTestCase.VALUE)
        aea.setVariable(av.variable())
        aea.setParent(aea)
        aea.setLeftChild(None)
        aea.setRightChild(None)

        self.assertEqual(AnalyserEquationAst.Type.EQ, aea.type())
        self.assertEqual(AnalyserTestCase.VALUE, aea.value())
        self.assertIsNotNone(aea.variable())
        self.assertIsNotNone(aea.parent())
        self.assertIsNone(aea.leftChild())
        self.assertIsNone(aea.rightChild())