示例#1
0
    def test_is_unique(self):
        from libcellml import Annotator, Parser

        annotator = Annotator()
        parser = Parser()

        model = parser.parseModel(file_contents("annotator/unique_ids.cellml"))
        annotator.setModel(model)

        self.assertTrue(annotator.isUnique("variable_3"))

        model = parser.parseModel(file_contents("annotator/lots_of_duplicate_ids.cellml"))
        annotator.setModel(model)

        self.assertFalse(annotator.isUnique("duplicateId2"))
    #      ids.  The Annotator class can create a unique id for an item using the assignId function.
    #      This is overloaded so that you can pass in any libCellML item, as well as an AnyItem
    #      pair.  NB: You need to be aware of the default types assigned when passing in CellML items
    #      without a corresponding item type.  These are listed in the documentation.

    #  3.d
    #      Assign an automatic id to all of the items with id 'polo', except for the one whose
    #      type is UNIT.
    polo_unit = polo_items.pop(2)
    for item in polo_items:
        annotator.assignId(item)

    #  3.e
    #      Check that the id of 'polo' is now unique in the model by calling the
    #      isUnique function.
    assert (annotator.isUnique('polo'))

    #  end 3.e

    #  Now we know that there is only one item in the model with id 'polo', and we also know
    #  that it has type UNIT.  This means that we can retrieve a Unit item directly from the
    #  annotator rather than needing to cast it using the std.any_cast.  Instead of calling
    #  the annotator's item function, call the Annotator.unit function with the id 'polo' to return the
    #  unit item directly.

    #  3.f
    #      Retrieve the Units item with id polo without casting.
    polo_unit = annotator.unitsItem('polo')

    #  end 3.f
    #  The Unit item is an object it has methods. **TODO**
示例#3
0
    encapsulation = annotator.encapsulation('brown')

    #  - 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.