#      Use the Annotator.ids function to return a vector of id strings used in the model, and
    #      print them to the terminal.
    print('The id strings used in the model are:')
    ids = annotator.ids()
    for id in ids:
        print('  - "{}"'.format(id))

    #  end 4.a
    #  The hex strings printed are those which have been automatically generated by the assignId
    #  function we can also see the 'marco' and 'polo' ids as expected.

    #  4.b
    #      Use the duplicateIds function to return a vector of those ids which have been duplicated in
    #      the model, and print them to the terminal.
    print('Duplicated id strings are:')
    duplicated_ids = annotator.duplicateIds()
    for id in duplicated_ids:
        print('  - "{}" occurs {} times'.format(id, annotator.itemCount(id)))

    #  end 4

    print('----------------------------------------------------------')
    print('   STEP 5: See who else is lurking around the corner      ')
    print('----------------------------------------------------------')

    #      The final step is to make sure that imported items can have their annotations
    #      tracked back to their sources too.

    #  5.a
    #      Retrieve an item with id of 'whoAmIAndWhereDidIComeFrom' and print its item type
    #      to the terminal.
Beispiel #2
0
    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