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())
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))
# 10.c # Check the Importer for issues and print any found to the terminal - we do not expect any at this stage. print_issues(importer) # end 10.c # The components that we want to reuse from the GateModel.cellml and PotassiumChannelController.cellml # are now available to us in two ways: # - through the model() function of the destination component's ImportSource item or # - as an item in the importer's library. The library items can be retrieved either by index # or by key, where the key is the name of the file that was resolved. # 10.d # Iterate through the items in the library (Importer.libraryCount() will give you # the total), and print its keys to the terminal. The keys can be retrieved as a # string from the Importer.key(index) function. At this stage we expect only one model in the library. print('The importer has {} models in the library.'.format(importer.libraryCount())) for i in range(0, importer.libraryCount()): print(' library({}) = {}'.format(i, importer.key(i))) print() # 10.e # We can simply use a clone of the imported components to define dummy variables in the # destination component. # Create dummy components from the resolved imported components. You can get these from the # library or from the import source's model (or one of each, to prove to yourself that it works # either way!). dummy_gate = imported_gate.importSource().model().component(imported_gate.importReference()).clone() dummy_controller = importer.library('PotassiumChannelController.cellml').component(controller.importReference()).clone() # GOTCHA: Note that when an item is added to a new parent, it is automatically removed from # its original parent. Iterating through a set of children is best done in descending
# 10.c # Check the Importer for issues and print any found to the terminal - we do not expect any at this stage. print_issues(importer) # end 10.c # The components that we want to reuse from the GateModel.cellml and PotassiumChannelController.cellml # are now available to us in two ways: # - through the model() function of the destination component's ImportSource item or # - as an item in the importer's library. The library items can be retrieved either by index # or by key, where the key is the name of the file that was resolved. # 10.d # Iterate through the items in the library (Importer.libraryCount() will give you # the total), and print its keys to the terminal. The keys can be retrieved as a # string from the Importer.key(index) function. At this stage we expect only one model in the library. print('The importer has {} models in the library.'.format( importer.libraryCount())) for i in range(0, importer.libraryCount()): print(' library({}) = {}'.format(i, importer.key(i))) print() # 10.e # We can simply use a clone of the imported components to define dummy variables in the # destination component. # Create dummy components from the resolved imported components. You can get these from the # library or from the import source's model (or one of each, to prove to yourself that it works # either way!). dummy_gate = imported_gate.importSource().model().component( imported_gate.importReference()).clone() dummy_controller = importer.library( os.path.join(import_path, 'PotassiumChannelController.cellml')).component(
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())) print() # STEP 4 # The Validator and Analyser classes process only the contents of concrete items (ie: not the contents of # imported items) of a model. # After successfully resolving a model's imports using an importer, the importer will store instances # of all of the dependencies of the resolved model. These are accessible through the "library" function. # We can ascertain that all of import dependencies meet the diagnostic checks of the Validator and the # Analyser individually by iterating through the importer's library. # Loop through the importer library and call the validator for each model. for m in range(0, importer.libraryCount()): # Retrieve the library model by index, m. validator.validateModel(importer.library(m)) # Retrieve the key under which it's stored: this will be the URL at which the imported model was found. print("The validator found {} issues in {}.".format(validator.issueCount(),importer.key(m))) for i in range(0, validator.issueCount()): print(" - {}".format(validator.issue(i).description())) print() # STEP 5 # Fix the validation errors in the imported files. # According to the printout above, we need to add units to the "iNeedUnits" # variable, to be found inside the "importExample3.cellml" file.