Ejemplo 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))
Ejemplo n.º 2
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())
Ejemplo n.º 3
0
    def test_inheritance(self):
        import libcellml
        from libcellml import Importer

        # Test inheritance.
        x = Importer()
        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)
Ejemplo n.º 4
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())
Ejemplo n.º 5
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())
Ejemplo n.º 6
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())
Ejemplo n.º 7
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())
Ejemplo n.º 8
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())
Ejemplo n.º 9
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())
Ejemplo n.º 10
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())
Ejemplo n.º 11
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())
Ejemplo n.º 12
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())
Ejemplo n.º 13
0
    who_am_i = annotator.item('whoAmIAndWhereDidIComeFrom')
    print('The type of item with ID "whoAmIAndWhereDidIComeFrom" is {}'.format(
        cellmlElementTypeAsString(who_am_i.type())))

    #  5.b
    #      Cast it into a CellML item of the appropriate type.
    units = who_am_i.units()

    #  5.c
    #      Use the Component.isImport() function to verify that it is imported.
    assert (units.isImport())

    #  5.d
    #      Create an Importer instance and use it to resolve this model's imports.
    #      Check that it has not raised any issues.
    importer = Importer()
    importer.resolveImports(model, os.path.dirname(model_file))
    print_issues(importer)

    #  5.e
    #      Retrieve all the information needed to locate any annotations on the
    #      original item:
    #           - the URL from which it was imported and
    #           - the id of the item in the original model.
    #      Print these to the terminal.
    url = units.importSource().url()
    reference = units.importReference()
    imported_id = units.importSource().model().units(reference).id()

    print('The units with id "whoAmIAndWhereDidIComeFrom" came from:')
    print('  - url: {}'.format(url))
    #  (You can verify this for yourself by printing your model to the terminal again if you like.) 
    #  Now the problem we have is that we need to connect to variables inside imported components, 
    #  but these don't exist in our model yet: the import sources that we created in Steps 4 and 5
    #  are simply a recipe they don't actually create anything.
    print_model(model)

    #  In order to connect to variables in imported components, we can create dummy variables inside them.
    #  These will be overwritten when the imports are resolved and the model flattened, at which time
    #  the imported variables will replace the dummy ones.  As with other steps, we have a choice here.
    #  We can manually create variables or clone existing ones into the destination components we have
    #  already created or we can make use of the Importer class to help us manage these. We're going to 
    #  do the latter now.

    #  10.a 
    #      Create an Importer item.
    importer = Importer()

    #  end 10.a 
    #      Resolving imports for a model triggers the importer to go searching for all of the
    #      information required by this model's imports, even through multiple generations of import layers.
    #      It also instantiates each of those requirements into the importer's own library.
    #      You could use the Model.hasUnresolvedImports() function to test whether the operation was
    #      successful or not expecting it to be true before resolution, and false afterwards.

    #  10.b
    #      Pass the model and the path to the GateModel.cellml file into the Importer.resolveImports
    #      function.
    importer.resolveImports(model, '')

    #  10.c  
    #       Check the Importer for issues and print any found to the terminal - we do not expect any at this stage.
Ejemplo n.º 15
0
    who_am_i = annotator.item('whoAmIAndWhereDidIComeFrom')
    print('The type of item with ID "whoAmIAndWhereDidIComeFrom" is {}'.format(
        get_cellml_element_type_from_enum(who_am_i[0])))

    #  5.b
    #      Cast it into a CellML item of the appropriate type.
    units = who_am_i[1]

    #  5.c
    #      Use the Component.isImport() function to verify that it is imported.
    assert (units.isImport())

    #  5.d
    #      Create an Importer instance and use it to resolve this model's imports.
    #      Check that it has not raised any issues.
    importer = Importer()
    importer.resolveImports(model, '')
    print_issues(importer)

    #  5.e
    #      Retrieve all the information needed to locate any annotations on the
    #      original item:
    #           - the URL from which it was imported and
    #           - the id of the item in the original model.
    #      Print these to the terminal.
    url = units.importSource().url()
    reference = units.importReference()
    imported_id = units.importSource().model().units(reference).id()

    print('The units with id "whoAmIAndWhereDidIComeFrom" came from:')
    print('  - url: {}'.format(url))
    #  (You can verify this for yourself by printing your model to the terminal again if you like.)
    #  Now the problem we have is that we need to connect to variables inside imported components,
    #  but these don't exist in our model yet: the import sources that we created in Steps 4 and 5
    #  are simply a recipe they don't actually create anything.
    print_model(model)

    #  In order to connect to variables in imported components, we can create dummy variables inside them.
    #  These will be overwritten when the imports are resolved and the model flattened, at which time
    #  the imported variables will replace the dummy ones.  As with other steps, we have a choice here.
    #  We can manually create variables or clone existing ones into the destination components we have
    #  already created or we can make use of the Importer class to help us manage these. We're going to
    #  do the latter now.

    #  10.a
    #      Create an Importer item.
    importer = Importer()

    #  end 10.a
    #      Resolving imports for a model triggers the importer to go searching for all of the
    #      information required by this model's imports, even through multiple generations of import layers.
    #      It also instantiates each of those requirements into the importer's own library.
    #      You could use the Model.hasUnresolvedImports() function to test whether the operation was
    #      successful or not expecting it to be true before resolution, and false afterwards.

    #  10.b
    #      Pass the model and the path to the GateModel.cellml file into the Importer.resolveImports
    #      function.
    importer.resolveImports(model, import_path)

    #  10.c
    #       Check the Importer for issues and print any found to the terminal - we do not expect any at this stage.
Ejemplo n.º 17
0
    print('   STEP 4: Resolve the imports ')
    print('----------------------------------------------------------')

    #      It's important to remember that the imports are merely instructions for how
    #      components or units items should be located: only their syntax is checked by the
    #      validator, not that the files exist or contain the required information.  To debug
    #      the imported aspects of the model, we need to use an Importer class.

    #      To resolve the imports, we need a path to a base location against which any
    #      relative file addresses can be resolved.  For this tutorial, if you've downloaded
    #      the files into the same directory as the code, so simply using an empty string is sufficient.
    #      If they're another directory, make sure to end your path with a slash, '/'.
    #      Use the function importer.resolveImports(model, path).
    #  4.a
    #      Create an Importer instance and use it to resolve the model.
    importer = Importer()
    importer.resolveImports(model, '')

    #  4.b
    #      Similarly to the validator, the importer will log any issues it encounters.
    #      Retrieve these and print to the terminal (you can do this manually or using the
    #      convenience function as before).
    print_issues(importer)

    #  end 4.b
    #  Importer error[0]:
    #     Description: Import of component 'importedGateH' from 'GateModel.cellml' requires
    #     component named 'i_dont_exist' which cannot be found.

    #     We need to change the import reference for the component to be 'gateEquations' instead
    #     of 'i_dont_exist'.  You can either retrieve the component using its name or directly
Ejemplo n.º 18
0
    print('-----------------------------')

    import_path = os.path.dirname(model_file)
    #      It's important to remember that the imports are merely instructions for how
    #      components or units items should be located: only their syntax is checked by the
    #      validator, not that the files exist or contain the required information.  To debug
    #      the imported aspects of the model, we need to use an Importer class.

    #      To resolve the imports, we need a path to a base location against which any
    #      relative file addresses can be resolved.  For this tutorial, if you've downloaded
    #      the files into the same directory as the code, so simply using an empty string is sufficient.
    #      If they're another directory, make sure to end your path with a slash, '/'.
    #      Use the function importer.resolveImports(model, path).
    #  4.a
    #      Create an Importer instance and use it to resolve the model.
    importer = Importer()
    importer.resolveImports(model, import_path)

    #  4.b
    #      Similarly to the validator, the importer will log any issues it encounters.
    #      Retrieve these and print to the terminal (you can do this manually or using the
    #      convenience function as before).
    print_issues(importer)

    #  end 4.b
    #  Importer error[1]:
    #     Description: Import of component 'importedGateH' from 'GateModel.cellml' requires
    #     component named 'i_dont_exist' which cannot be found.

    #     We need to change the import reference for the component to be 'gateEquations' instead
    #     of 'i_dont_exist'.  You can either retrieve the component using its name or directly
Ejemplo n.º 19
0
    def test_create_destroy(self):
        from libcellml import Importer

        x = Importer()
        del x
Ejemplo n.º 20
0
    def test_imports(self):
        from libcellml import Model, Importer, ImportSource, Units

        m = Model()
        u = Units()
        im = Importer()

        u.setImportSource(ImportSource())

        m.addUnits(u)
        self.assertTrue(m.hasImports())

        self.assertEqual(0, im.importSourceCount())

        i = ImportSource()
        i.setUrl('actual_url')

        im.addImportSource(i)
        self.assertEqual(1, im.importSourceCount())

        i1 = im.importSource(0)

        self.assertTrue(im.hasImportSource(i))

        im.removeImportSource(0)
        i2 = ImportSource()

        self.assertEqual(0, im.importSourceCount())

        im.addImportSource(i2)

        self.assertEqual(1, im.importSourceCount())

        im.removeAllImportSources()

        self.assertEqual(0, im.importSourceCount())
Ejemplo n.º 21
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('resources/importExample1.cellml', 'r')
    parser = Parser()
    original_model = parser.parseModel(read_file.read())

    # STEP 2
    # Create an Importer to resolve the imports in the model.
    importer = Importer()

    # Resolve the imports.
    importer.resolveImports(original_model, 'resources/')

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

    # STEP 3
    # The analysis tools - the Validator and Analyser - will read only the submitted
    # model they do not look into any of the imported items, so they can't check them.
    # In order to retain the import structure but be able to use the diagnostic tools, 
    # we can create a flattened copy of the model for testing.  This can be used to
    # identify mistakes in the unflattened model too.  

    # Create a Validator and Analyser and submit the original, unflattened model.
    # We don't expect either of these to report any issues.
Ejemplo n.º 22
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.
    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('----------------------------------------------------------')

    import_path = os.path.dirname(model_file)
    #  2.a
    #      Create an Importer instance and use it to resolve the imports in your model.
    importer = Importer()
    importer.resolveImports(model, import_path)

    #  2.b
    #      Check that the importer has not raised any issues.
    print_issues(importer)

    #  2.c
    #      Use the importer to create a flattened version of the model.
    flatModel = importer.flattenModel(model)

    #  end 2

    print('----------------------------------------------------------')
    print('   STEP 3: Validate and analyse the flattened model       ')
    print('----------------------------------------------------------')
    #       Use the parser to deserialise the contents of the string you've read and return the model.
    model = parser.parseModel(read_file.read())

    #  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('----------------------------------------------------------')

    #  2.a
    #      Create an Importer instance and use it to resolve the imports in your model.
    importer = Importer()
    importer.resolveImports(model, '')

    #  2.b
    #      Check that the importer has not raised any issues.
    print_issues(importer)

    #  2.c
    #      Use the importer to create a flattened version of the model.
    flatModel = importer.flattenModel(model)

    #  end 2

    print('----------------------------------------------------------')
    print('   STEP 3: Validate and analyse the flattened model       ')
    print('----------------------------------------------------------')