Exemple #1
0
    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)
    #   - [4] RESET
    #   - [5] RESET_VALUE

    #      The item we want has type CellMLElements.UNIT, and we'd like it
    #      to be unique.  We need to change the other items to have other (also unique)
    #      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
Exemple #3
0
    def test_auto_id_individual(self):
        from libcellml import Annotator, CellmlElementType, Parser, Variable
        from libcellml import Unit, VariablePair

        annotator = Annotator()
        parser = Parser()
        model_string = file_contents("annotator/no_ids.cellml")
        model = parser.parseModel(model_string)

        annotator.setModel(model)

        self.assertEqual(
            "b4da55",
            annotator.assignId(model.component(0),
                               CellmlElementType.COMPONENT))
        self.assertEqual("b4da55", model.component(0).id())

        self.assertEqual(
            "b4da56",
            annotator.assignId(model.component("component2"),
                               CellmlElementType.COMPONENT_REF))
        self.assertEqual("b4da56",
                         model.component("component2").encapsulationId())

        self.assertEqual(
            "b4da57",
            annotator.assignId(
                VariablePair(
                    model.component("component2").variable("variable1"),
                    model.component("component2").variable(
                        "variable1").equivalentVariable(0)),
                CellmlElementType.CONNECTION))

        self.assertEqual(
            "b4da57",
            Variable.equivalenceConnectionId(
                model.component("component2").variable("variable1"),
                model.component("component2").variable(
                    "variable1").equivalentVariable(0)))

        self.assertEqual("b4da58", annotator.assignId(model.importSource(0)))
        self.assertEqual("b4da58", model.importSource(0).id())

        self.assertEqual(
            "b4da59",
            annotator.assignId(
                VariablePair(
                    model.component("component2").variable("variable2"),
                    model.component("component2").variable(
                        "variable2").equivalentVariable(0)),
                CellmlElementType.MAP_VARIABLES))
        self.assertEqual(
            "b4da59",
            Variable.equivalenceMappingId(
                model.component("component2").variable("variable2"),
                model.component("component2").variable(
                    "variable2").equivalentVariable(0)))
        self.assertEqual("b4da5a",
                         annotator.assignId(model, CellmlElementType.MODEL))
        self.assertEqual("b4da5a", model.id())

        self.assertEqual(
            "b4da5b",
            annotator.assignId(
                model.component("component2").reset(0),
                CellmlElementType.RESET))
        self.assertEqual("b4da5b", model.component("component2").reset(0).id())

        self.assertEqual(
            "b4da5c",
            annotator.assignId(
                model.component("component2").reset(0),
                CellmlElementType.RESET_VALUE))
        self.assertEqual("b4da5c",
                         model.component("component2").reset(0).resetValueId())

        self.assertEqual(
            "b4da5d",
            annotator.assignId(
                model.component("component2").reset(0),
                CellmlElementType.TEST_VALUE))
        self.assertEqual("b4da5d",
                         model.component("component2").reset(0).testValueId())

        self.assertEqual("b4da5e", annotator.assignId(Unit(model.units(1), 0)))
        self.assertEqual("b4da5e", model.units(1).unitId(0))

        self.assertEqual("b4da5f", annotator.assignId(model.units(1)))
        self.assertEqual("b4da5f", model.units(1).id())

        self.assertEqual("b4da60",
                         annotator.assignId(model.component(1).variable(0)))
        self.assertEqual("b4da60", model.component(1).variable(0).id())

        self.assertEqual(
            "b4da61", annotator.assignId(model,
                                         CellmlElementType.ENCAPSULATION))
        self.assertEqual("b4da61", model.encapsulationId())
Exemple #4
0
    def test_assign_by_type(self):
        from libcellml import Annotator, Parser, Variable, CellmlElementType
        from libcellml import UnitsItem, VariablePair

        annotator = Annotator()
        parser = Parser()
        model_string = file_contents("annotator/no_ids.cellml")

        model = parser.parseModel(model_string)
        annotator.setModel(model)

        c = model.component(0)

        self.assertEqual("", model.id())
        annotator.assignId(model)
        self.assertEqual("b4da55", model.id())

        self.assertEqual("", model.encapsulationId())
        annotator.assignId(model, CellmlElementType.ENCAPSULATION)
        self.assertEqual("b4da56", model.encapsulationId())

        self.assertEqual("", c.id())
        annotator.assignId(c)
        self.assertEqual("b4da57", c.id())

        self.assertEqual("", c.encapsulationId())
        annotator.assignId(c, CellmlElementType.COMPONENT_REF)
        self.assertEqual("b4da58", c.encapsulationId())

        c2v1 = model.component("component2").variable("variable1")
        c3v1 = model.component("component3").variable("variable1")

        self.assertEqual("", Variable.equivalenceConnectionId(c2v1, c3v1))
        annotator.assignId(VariablePair(c2v1, c3v1), CellmlElementType.CONNECTION)
        self.assertEqual("b4da59", Variable.equivalenceConnectionId(c2v1, c3v1))

        self.assertEqual("", Variable.equivalenceMappingId(c2v1, c3v1))
        annotator.assignId(VariablePair(c2v1, c3v1))
        self.assertEqual("b4da5a", Variable.equivalenceMappingId(c2v1, c3v1))

        c3v2 = model.component("component3").variable("variable2")

        self.assertEqual("", c3v2.id())
        annotator.assignId(c3v2)
        self.assertEqual("b4da5b", c3v2.id())

        u = model.units(1)

        self.assertEqual("", u.id())
        annotator.assignId(u)
        self.assertEqual("b4da5c", u.id())

        r = model.component("component2").reset(0)

        self.assertEqual("", r.id())
        annotator.assignId(r)
        self.assertEqual("b4da5d", r.id())

        self.assertEqual("", r.testValueId())
        annotator.assignId(r, CellmlElementType.TEST_VALUE)
        self.assertEqual("b4da5e", r.testValueId())

        self.assertEqual("", r.resetValueId())
        annotator.assignId(r, CellmlElementType.RESET_VALUE)
        self.assertEqual("b4da5f", r.resetValueId())

        i = model.component("component1").importSource()

        self.assertEqual("", i.id())
        annotator.assignId(i)
        self.assertEqual("b4da60", i.id())

        self.assertEqual("", u.unitId(0))
        annotator.assignId(UnitsItem(u, 0))
        self.assertEqual("b4da61", u.unitId(0))