Esempio n. 1
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))
Esempio n. 2
0
    def test_create_destroy(self):
        from libcellml import Printer

        x = Printer()
        del(x)
        y = Printer()
        z = Printer(y)
        del(y, z)
Esempio n. 3
0
    def test_inheritance(self):
        import libcellml
        from libcellml import Printer

        x = Printer()
        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)
Esempio n. 4
0
    def test_inheritance(self):
        import libcellml
        from libcellml import Printer

        x = Printer()
        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)
        x.addIssue(libcellml.Issue())
        self.assertEqual(x.issueCount(), 1)
Esempio n. 5
0
    model.component('uncomputedVariable').setMath(mathml)

    # Variable 'a' in component 'validationErrors' is not computed.
    # Variable 'c' in component 'validationErrors' is not computed.
    model.component('validationErrors').variable('c').setInitialValue(1.0)

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

    analyser.analyseModel(model)
    print('The analyser found {} issues.'.format(analyser.issueCount()))
    for i in range(0, analyser.issueCount()):
        issue = analyser.issue(i)
        print(issue.description())

    # STEP 7
    # Write the flattened, validated, analysed model to a serialised CellML string.
    printer = Printer()
    model_string = printer.printModel(model)

    # Write the serialised string to a file.
    write_file = open("debugAnalysisExampleFixed.cellml", "w")
    write_file.write(model_string)

    # END

    print(' STEP 4: Check and output the corrected model')
    print('----------------------------------------------')

    #  4.a
    #      Validate the corrected model again and check that there are no more issues.
    validator.validateModel(model)
    print('The validator found {} issues in the model.'.format(
        validator.issueCount()))

    #  4.b
    #      Print the corrected model to the terminal.
    print_model(model, True)

    #  4.c
    #      Print corrected model to a file.
    printer = Printer()
    serialised_model = printer.printModel(model)

    out_filename = 'tutorial2_printed.cellml'
    with open(out_filename, 'w') as f:
        f.write(serialised_model)

    #  end 4

    print(
        f"The corrected '{model.name()}' model has been printed to: {out_filename}"
    )

    #  4.d
    #      Go and have a cuppa, you're done!
    # to do the final check before serialising our model for output, we will use the Importer
    # to create a flattened version of the model to submit for analysis.

    #  10.i 
    #      Create a flattened version of the final model using the Importer.flattenModel(model)
    #      function.  Run this through the analyser and expect there to be no issues reported.
    flatModel = importer.flattenModel(model)
    analyser.analyseModel(flatModel)
    print_issues(analyser)

    #  end 10.i
    #  Note that at this point an analysis of the unflattened model will still show errors,
    #  but that's totally fine.

    print('------------------------------------------------------------')
    print('   STEP 11: Output the model                                ')
    print('------------------------------------------------------------')

    #  11.a 
    #      Create a Printer instance and use it to serialise the model.  This creates a string
    #      containing the CellML-formatted version of the model.  Write this to a file called
    #      'PotassiumChannelModel.cellml'.
    printer = Printer()
    write_file = open('PotassiumChannelModel.cellml', 'w')
    write_file.write(printer.printModel(model))
    write_file.close()

    #  end

    print('The created model has been written to PotassiumChannelModel.cellml')
Esempio n. 8
0
    def test_create_destroy(self):
        from libcellml import Printer

        x = Printer()
        del x
Esempio n. 9
0
    def test_print_model(self):
        from libcellml import Printer, Model

        # std::string printModel(ModelPtr model)
        p = Printer()
        self.assertIsInstance(p.printModel(Model()), str)
Esempio n. 10
0
    def test_print_reset(self):
        from libcellml import Printer, Reset

        # std::string printReset(ResetPtr reset) const;
        p = Printer()
        self.assertIsInstance(p.printReset(Reset()), str)
Esempio n. 11
0
    def test_print_component(self):
        from libcellml import Printer, Component

        # std::string printComponent(ComponentPtr component)
        p = Printer()
        self.assertIsInstance(p.printComponent(Component()), str)
Esempio n. 12
0
    def test_print_variable(self):
        from libcellml import Printer, Variable

        # std::string printVariable(VariablePtr variable)\
        p = Printer()
        self.assertIsInstance(p.printVariable(Variable()), str)
Esempio n. 13
0
    def test_print_units(self):
        from libcellml import Printer, Units

        # std::string printUnits(UnitsPtr units)
        p = Printer()
        self.assertIsInstance(p.printUnits(Units()), str)
Esempio n. 14
0
    for i in range(0, validator.issueCount()):
        print('    - '.format(validator.issue(i).description()))

    analyser.analyseModel(flat_model)
    print('The analyser found {} issues.'.format(analyser.issueCount()))
    for i in range(0, analyser.issueCount()):
        print('    - '.format(analyser.issue(i).description()))

    # STEP 7
    # Print the collection of repaired import models to files.
    # To avoid over-writing existing files, we'll write the corrected files to a separate
    # directory called "repaired/".  Note that the relationship between the files needs
    # to be maintained, so even files that have not been changed need to be written
    # to the new location.

    # Write the original model to a file.
    printer = Printer()
    model_string = printer.printModel(original_model)
    write_file = open("repaired/import_debugger/importExample1.cellml", "w")
    write_file.write(model_string)
    
    # Write the dependency models in the importer library to files.
    for m in range(0, importer.libraryCount()):
        write_file = open("repaired/"+importer.key(m), "w")
        model_string = printer.printModel(importer.library(m))
        write_file.write(model_string)
    
    print('The corrected models has been written to the \'repaired/resources/\' directory') 

    # END