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())
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))
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())
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_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())
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())
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())
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())
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())
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())
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())
# 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('----------------------------------------------------------')
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.
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)) print(' - id: {}'.format(imported_id))
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.