def test_list_duplicate_ids(self): from libcellml import Annotator, CellmlElementType, Parser model_string = file_contents("annotator/lots_of_duplicate_ids.cellml") parser = Parser() annotator = Annotator() model = parser.parseModel(model_string) annotator.setModel(model) id_list = annotator.duplicateIds() expected_ids = ('duplicateId1', 'duplicateId2', 'duplicateId3', 'duplicateId4') self.assertEqual(expected_ids, id_list) # Get the collections by duplicated id. c2v1 = model.component("component2").variable("variable1") c2v2 = model.component("component2").variable("variable2") c3v1 = model.component("component2").component("component3").variable( "variable1") c3v2 = model.component("component2").component("component3").variable( "variable2") c4v1 = model.component("component4").variable("variable1") c4v2 = model.component("component4").variable("variable2") expected_items = { "duplicateId1": ( (CellmlElementType.UNITS, model.units("units2")), (CellmlElementType.IMPORT, model.importSource(0)), (CellmlElementType.MAP_VARIABLES, (c4v1, c2v1)), (CellmlElementType.COMPONENT, model.component("component2")), (CellmlElementType.CONNECTION, (c2v1, c3v1)), (CellmlElementType.TEST_VALUE, model.component("component2").reset(0)), (CellmlElementType.COMPONENT_REF, model.component("component2").component("component3")), (CellmlElementType.VARIABLE, model.component("component2").component( "component3").variable("variable2")), ), "duplicateId2": ( (CellmlElementType.MODEL, model), (CellmlElementType.UNITS, model.units("units1")), (CellmlElementType.UNITS, model.units("blob")), (CellmlElementType.CONNECTION, (c4v2, c2v2)), (CellmlElementType.VARIABLE, c4v2), (CellmlElementType.COMPONENT_REF, model.component("component2")), (CellmlElementType.RESET, model.component("component2").reset(0)), (CellmlElementType.VARIABLE, c3v1), ), "duplicateId3": ( (CellmlElementType.IMPORT, model.importSource(1)), (CellmlElementType.UNITS, model.units("units3")), (CellmlElementType.VARIABLE, c4v1), (CellmlElementType.VARIABLE, c2v2), (CellmlElementType.MAP_VARIABLES, (c2v2, c4v2)), (CellmlElementType.COMPONENT, model.component("component2").component("component3")), (CellmlElementType.ENCAPSULATION, model), ), "duplicateId4": ( (CellmlElementType.UNIT, ((model.units("units2"), 0))), (CellmlElementType.COMPONENT, model.component("component1")), (CellmlElementType.COMPONENT, model.component("component4")), (CellmlElementType.MAP_VARIABLES, (c2v1, c3v1)), (CellmlElementType.VARIABLE, c2v1), (CellmlElementType.MAP_VARIABLES, (c2v2, c4v2)), (CellmlElementType.RESET_VALUE, model.component("component2").reset(0)), ) } for id in expected_ids: items_with_id = annotator.items(id) count = 0 for item in items_with_id: self.assertEqual(item[0], expected_items[id][count][0]) # SWIG copies the pointers so can't expect a comparison to be true. Not sure how to # compare these ... # self.assertEqual(item[1], expected_items[id][count][1]) count = count + 1
# Retrieve the issues from the annotator and print to the terminal. print_issues(annotator) # end 3.b # Recorded 1 issues: # Issue [0] is a WARNING: # description: The id 'polo' occurs 6 times in the model so a unique item cannot be located. # stored item type: UNDEFINED # Since the id is not unique, we need to retrieve a vector of all items # with that id to investigate them. # 3.c # Use the items function to retrieve the vector of items with id 'polo', # and iterate through it printing the different types to the terminal. polo_items = annotator.items('polo') print('The items with an id of "polo" have types of:') index = 0 for item in polo_items: print(' - [{}] {}'.format(index, cellmlElementTypeAsString(item.type()))) index += 1 # end 3.c # The items with an id of 'polo' have types of: # - [0] UNITS # - [1] UNITS # - [2] UNIT # - [3] VARIABLE # - [4] RESET
# STEP 5 # Handling duplicate ID strings. # Find any duplicated ID strings inside the model. duplicatedIds = annotator.duplicateIds() print('There are {} duplicated ids in the model.'.format( len(duplicatedIds))) for i in duplicatedIds: print(' - ' + i) print() # Retrieve all items with the given id string. This returns a std.vector # of AnyItems which will need to be cast into libcellml items before they # can be used. Note that duplicated ids are not valid CellML, and need # to be fixed before the model can be used. allItemsWithDuplicateId1 = annotator.items('duplicateId1') # A new id string which is automatically generated and unique can be # assigned to these items. print( 'Before assigning automatic ids there are {} items with an id of "duplicateId1".' .format(annotator.itemCount('duplicateId1'))) for item in allItemsWithDuplicateId1: annotator.assignId(item) # Now there are no more items with the duplicated id 'duplicateId1' # remaining in the model. allItemsWithDuplicateId1 = annotator.items('duplicateId1') print( 'After assigning automatic ids there are {} items with an id of "duplicateId1".' .format(annotator.itemCount('duplicateId1')))