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())
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())
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())
def test_assign_id(self): from libcellml import Annotator, Component, Model, Units from libcellml import Unit, CellmlElementType annotator = Annotator() model = Model() component1 = Component("c1") component2 = Component("c2") component3 = Component("c3") component3.setId("id3") units = Units("u1") units.addUnit("volt") model.addComponent(component1) model.addComponent(component2) component2.addComponent(component3) model.addUnits(units) annotator.setModel(model) self.assertEqual("", component1.id()) self.assertEqual("", component2.id()) self.assertEqual("", units.unitId(0)) annotator.assignId(component1) self.assertEqual("b4da55", component1.id()) self.assertEqual("", component2.id()) self.assertEqual("", units.unitId(0)) annotator.assignId(Unit(units, 0)) self.assertEqual("b4da55", component1.id()) self.assertEqual("", component2.id()) self.assertEqual("b4da56", units.unitId(0)) self.assertEqual("", annotator.assignId(None, CellmlElementType.UNDEFINED)) item = annotator.item("id3") annotator.assignId(item) self.assertEqual("b4da57", component3.id()) # For coverage only. annotator._assignId(component2)
# 2.a # Create an Annotator item and use the setModel function to pass in the parsed # mystery model. annotator = Annotator() annotator.setModel(model) # end 2.a # The item function returns a AnyItem, a std.pair whose: # - first attribute is a CellmlElementType enumeration and # - second attribute is a std.any cast of the item itself. # 2.b # Retrieve the item with an id of 'marco'. Use the helper function # cellmlElementTypeAsString to convert the enumeration of its type into a # string for printing to the terminal. marco_item = annotator.item('marco') print('The item with ID "marco" is a {}'.format( cellmlElementTypeAsString(marco_item.type()))) # The item with ID 'marco' is a variable # 2.c # Check that the annotator has not reported any issues. print_issues(annotator) # 2.d # Now that we know the marco item's type using its first attribute (it should # be a CellmlElementType.VARIABLE) we can name its second attribute so we know # what it is. marco_variable = marco_item.variable()
def test_any_cellml_element(self): from libcellml import Annotator, AnyCellmlElement, Parser from libcellml.enums import CellmlElementType self.assertRaises(AttributeError, AnyCellmlElement) parser = Parser() model = parser.parseModel(file_contents('annotator/unique_ids.cellml')) annotator = Annotator() annotator.setModel(model) item = annotator.item('component_1') self.assertEqual(CellmlElementType.COMPONENT, item.type()) self.assertEqual('component1', item.component().name()) item = annotator.item('component_ref_1') self.assertEqual(CellmlElementType.COMPONENT_REF, item.type()) self.assertEqual('component2', item.component().name()) item = annotator.item('connection_1') self.assertEqual(CellmlElementType.CONNECTION, item.type()) self.assertEqual('variable1', item.variablePair().variable1().name()) self.assertEqual('variable1', item.variablePair().variable2().name()) self.assertTrue(item.variablePair().isValid()) item = annotator.item('encapsulation_1') self.assertEqual(CellmlElementType.ENCAPSULATION, item.type()) self.assertEqual('everything', item.model().name()) item = annotator.item('import_1') self.assertEqual(CellmlElementType.IMPORT, item.type()) self.assertEqual('some-other-model.xml', item.importSource().url()) item = annotator.item('map_variables_1') self.assertEqual(CellmlElementType.MAP_VARIABLES, item.type()) self.assertEqual('variable1', item.variablePair().variable1().name()) self.assertEqual('variable1', item.variablePair().variable2().name()) self.assertTrue(item.variablePair().isValid()) item = annotator.item('model_1') self.assertEqual(CellmlElementType.MODEL, item.type()) self.assertEqual('everything', item.model().name()) resetItem = annotator.item('reset_1') self.assertEqual(CellmlElementType.RESET, resetItem.type()) self.assertEqual(1, resetItem.reset().order()) item = annotator.item('reset_value_1') self.assertEqual(CellmlElementType.RESET_VALUE, item.type()) self.assertEqual(resetItem.reset().resetValue(), item.reset().resetValue()) item = annotator.item('test_value_1') self.assertEqual(CellmlElementType.TEST_VALUE, item.type()) self.assertEqual(resetItem.reset().testValue(), item.reset().testValue()) item = annotator.item('unit_1') self.assertEqual(CellmlElementType.UNIT, item.type()) self.assertEqual('units2', item.unitsItem().units().name()) self.assertTrue(item.unitsItem().isValid()) item = annotator.item('units_1') self.assertEqual(CellmlElementType.UNITS, item.type()) self.assertEqual('units1', item.units().name()) item = annotator.item('variable_1') self.assertEqual(CellmlElementType.VARIABLE, item.type()) self.assertEqual('variable1', item.variable().name())
# - resetValue: returns the Reset with this id on its reset value. resetValue = annotator.resetValue('taupe') # - testValue: returns the Reset with this id on its test value. testValue = annotator.testValue('mauve') # In this example reset, resetValue and testValue will be the same because the # 'taupe' reset value and 'mauve' test value are in the 'violet' reset item. # STEP 4 # Dealing with unique id strings where the item has an unknown type. # Check that the id is unique in the model scope: if annotator.isUnique('green'): # Retrieve item from the annotator by their unique id. itemOfUnknownType = annotator.item('green') # Because these could be any kind of item, they are stored in an AnyItem # type. This is a tuple, where the first item is a CellmlElementType enum # indicating the item's type, and the second is the item itself. # The type can be turned into a string using the Annotator.typeAsString # function on the first item in the tuple. print('The item with id of "green" has type of "{}".'.format( annotator.typeAsString(itemOfUnknownType[0]))) print() # STEP 5 # Handling duplicate ID strings. # Find any duplicated ID strings inside the model.