Exemplo n.º 1
0
    def test_auto_ids_group(self):
        from libcellml import Annotator, Component, Model
        from libcellml.enums import CellmlElementType_COMPONENT
        annotator = Annotator()
        model = Model()
        component1 = Component("c1")
        component2 = Component("c2")
        component3 = Component("c3")

        model.addComponent(component1)
        model.addComponent(component2)
        component2.addComponent(component3)

        annotator.setModel(model)

        self.assertEqual("", model.id())
        self.assertEqual("", component1.id())
        self.assertEqual("", component2.id())
        self.assertEqual("", component3.id())

        annotator.assignIds(CellmlElementType_COMPONENT)

        self.assertEqual("", model.id())
        self.assertEqual("b4da55", component1.id())
        self.assertEqual("b4da56", component2.id())
        self.assertEqual("b4da57", component3.id())
Exemplo n.º 2
0
    def test_take_component(self):
        from libcellml import Component

        c = Component()
        c1 = Component("component_1")

        c.addComponent(c1)

        cTaken = c.takeComponent(0)
        self.assertEqual("component_1", cTaken.name())
Exemplo n.º 3
0
    def test_replace_component(self):
        from libcellml import Component

        c = Component()
        c1 = Component("component_1")

        c.addComponent(c1)

        c2 = Component("component_2")

        self.assertTrue(c.replaceComponent(0, c2))
        self.assertEqual(1, c.componentCount())
        self.assertEqual("component_2", c.component(0).name())
Exemplo n.º 4
0
    def test_has_ancestor(self):
        from libcellml import Component

        # bool hasParent(Component* c)
        x = Component()
        c1 = Component()
        c2 = Component()
        self.assertFalse(c2.hasAncestor(c1))
        self.assertFalse(c2.hasAncestor(x))

        x.addComponent(c1)
        c1.addComponent(c2)
        self.assertTrue(c2.hasAncestor(c1))
        self.assertTrue(c2.hasAncestor(x))
Exemplo n.º 5
0
    def test_get_component(self):
        from libcellml import ComponentEntity, Component

        # ComponentPtr getComponent(size_t index)
        name = 'testo'
        x = ComponentEntity()
        y = Component()
        y.setName(name)
        self.assertIsNone(x.getComponent(0))
        self.assertIsNone(x.getComponent(1))
        self.assertIsNone(x.getComponent(-1))
        x.addComponent(y)
        self.assertIsNone(x.getComponent(1))
        self.assertIsNone(x.getComponent(-1))
        self.assertIsNotNone(x.getComponent(0), y)
        self.assertEqual(x.getComponent(0).getName(), name)

        # ComponentPtr getComponent(const std::string &name,
        #   bool searchEncapsulated=true)
        name = 'testo'
        x = ComponentEntity()
        y = Component()
        y.setName(name)
        self.assertIsNone(x.getComponent('bonjour'))
        self.assertIsNone(x.getComponent(name))
        self.assertIsNone(x.getComponent(name, True))
        self.assertIsNone(x.getComponent(name, False))
        self.assertIsNone(x.getComponent(name, 1))
        self.assertIsNone(x.getComponent(name, 'hello'))
        x.addComponent(y)
        self.assertIsNone(x.getComponent('hola'))
        self.assertIsNotNone(x.getComponent(name), y)
        self.assertEqual(x.getComponent(name).getName(), name)
        del(x, y, name)
        name = 'hiii'
        z = Component()
        z.setName(name)
        y = Component()
        y.addComponent(z)
        x = ComponentEntity()
        x.addComponent(y)
        self.assertIsNone(x.getComponent(name, False))
        self.assertIsNone(x.getComponent(name, 0))
        self.assertIsNone(x.getComponent(name, []))
        self.assertIsNotNone(x.getComponent(name))
        self.assertIsNotNone(x.getComponent(name, True))
        self.assertIsNotNone(x.getComponent(name, 1))
        self.assertIsNotNone(x.getComponent(name, name))
        del(x, y, z, name)
Exemplo n.º 6
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)
Exemplo n.º 7
0
    def test_contains_component(self):
        from libcellml import ComponentEntity, Component

        # bool containsComponent(const std::string &name,
        #   bool searchEncapsulated)
        x = ComponentEntity()
        y = Component()
        name = 'blue'
        y.setName(name)
        self.assertFalse(x.containsComponent(name))
        self.assertFalse(x.containsComponent(name, True))
        self.assertFalse(x.containsComponent(name, False))
        self.assertFalse(x.containsComponent(name, name))
        x.addComponent(y)
        self.assertTrue(x.containsComponent(name))
        z = Component()
        name2 = 'green'
        z.setName(name2)
        y.addComponent(z)
        self.assertFalse(x.containsComponent(name2, False))
        self.assertFalse(x.containsComponent(name2, 0))
        self.assertTrue(x.containsComponent(name2, True))
        self.assertTrue(x.containsComponent(name2, name2))
        del(x, y, z, name, name2)

        # bool containsComponent(const ComponentPtr &component,
        #   bool searchEncapsulated)
        x = ComponentEntity()
        y = Component()
        self.assertFalse(x.containsComponent(y))
        self.assertFalse(x.containsComponent(y, True))
        self.assertFalse(x.containsComponent(y, False))
        self.assertFalse(x.containsComponent(y, y))
        x.addComponent(y)
        self.assertTrue(x.containsComponent(y))
        z = Component()
        y.addComponent(z)
        self.assertFalse(x.containsComponent(z, False))
        self.assertFalse(x.containsComponent(z, 0))
        self.assertTrue(x.containsComponent(z, True))
        self.assertTrue(x.containsComponent(z, z))
        del(x, y, z)
Exemplo n.º 8
0
    def test_component_count(self):
        from libcellml import ComponentEntity, Component

        # size_t componentCount()
        x = ComponentEntity()
        self.assertEqual(x.componentCount(), 0)
        x.addComponent(Component())
        self.assertEqual(x.componentCount(), 1)
        x.addComponent(Component())
        self.assertEqual(x.componentCount(), 2)
        y = Component()
        x.addComponent(y)
        self.assertEqual(x.componentCount(), 3)
        y.addComponent(Component())
        self.assertEqual(x.componentCount(), 3)
        y.addComponent(Component())
        self.assertEqual(x.componentCount(), 3)
        self.assertTrue(x.removeComponent(2))
        self.assertEqual(x.componentCount(), 2)
        self.assertTrue(x.removeComponent(0))
        self.assertEqual(x.componentCount(), 1)
        self.assertTrue(x.removeComponent(0))
        self.assertEqual(x.componentCount(), 0)
Exemplo n.º 9
0
    def test_has_parent(self):
        from libcellml import Component

        # bool hasParent(Component* c)
        x = Component()
        c1 = Component()
        self.assertFalse(x.hasParent())
        c1.addComponent(x)
        self.assertTrue(x.hasParent())

        c2 = Component()
        c1.addComponent(c2)
        c2.addComponent(x)
        self.assertTrue(x.hasParent())
    #  1.c 
    #      Add the component to the model.
    model.addComponent(k_channel)

    #  end 1

    print('------------------------------------------------------------')
    print('   STEP 2: Define the potassium channel equations component ')
    print('------------------------------------------------------------')

    #  2.a 
    #      Create a Component instance for the equations and name it 'potassiumChannelEquations'.  
    #      Add it to the wrapper component you created above.
    k_channel_equations = Component('potassiumChannelEquations')
    k_channel.addComponent(k_channel_equations)

    #  end 2.a
    #      The mathematics of a component is specified as a MathML 2 string (NB: higher versions 
    #      of MathML are not supported), and is added to the component using setMath() and 
    #      appendMath() functions.  
    #      Your string needs to contain the namespaces for MathML and for CellML: these have been
    #      provided for you in the math_header string above.

    #  2.b 
    #      Define the maths inside the potassiumChannelEquations component.
    equation_iK = \
        '  <apply><eq/>\n'\
        '    <ci>i_K</ci>\n'\
        '    <apply><times/>\n'\
        '       <apply><power/>\n'\
Exemplo n.º 11
0
    mM = Units()
    mM.setName("mM")
    mM.addUnit("mole", "milli")
    model.addUnits(mM)

    validator.validateModel(model)
    print_errors_to_terminal(validator)

    print("-----------------------------------------------")
    print("  STEP 2: Creating the m-gate")
    print("-----------------------------------------------")

    #  2.a Create the component and add it to the sodium channel component
    mGate = Component()
    mGate.setName("mGate")
    sodium_channel.addComponent(mGate)

    #  2.b Add the MathML strings which govern the behavior of this gate
    if True:
        equation1 = \
            '<apply>\
                <eq/>\
                <ci>alpha_m</ci>\
                <apply>\
                    <divide/>\
                    <apply>\
                        <times/>\
                        <cn cellml:units="per_mV_ms">0.1</cn>\
                        <apply>\
                            <plus/>\
                            <ci>V</ci>\
    #      check it is what you'd expect.
    print_model(model)

    # end 1

    print('----------------------------------------------------------')
    print('   STEP 2: Create the gateEquations component             ')
    print('----------------------------------------------------------')

    #  2.a
    #  Create a gateEquations component and name it 'gateEquations'.
    gateEquations = Component('gateEquations')

    #  2.b
    #  Add the new gateEquations component to the gate component.
    gate.addComponent(gateEquations)

    #  2.c
    #  Add the mathematics to the gateEquations component.
    equation = \
        '  <apply><eq/>\n'\
        '    <apply><diff/>\n'\
        '      <bvar><ci>t</ci></bvar>\n'\
        '      <ci>X</ci>\n'\
        '    </apply>\n'\
        '    <apply><minus/>\n'\
        '      <apply><times/>\n'\
        '        <ci>alpha_X</ci>\n'\
        '        <apply><minus/>\n'\
        '          <cn cellml:units="dimensionless">1</cn>\n'\
        '          <ci>X</ci>\n'\
Exemplo n.º 13
0
    def test_remove_component(self):
        from libcellml import ComponentEntity, Component

        # bool removeComponent(size_t index)
        x = ComponentEntity()
        self.assertFalse(x.removeComponent(0))
        self.assertFalse(x.removeComponent(1))
        self.assertFalse(x.removeComponent(-1))
        y = Component()
        x.addComponent(y)
        self.assertFalse(x.removeComponent(1))
        self.assertFalse(x.removeComponent(-1))
        self.assertTrue(x.removeComponent(0))
        del(x, y)

        # bool removeComponent(const std::string &name,
        #    bool searchEncapsulated=true)
        x = ComponentEntity()
        self.assertFalse(x.removeComponent('aaa'))
        self.assertFalse(x.removeComponent('aaa', True))
        self.assertFalse(x.removeComponent('aaa', False))
        self.assertFalse(x.removeComponent('aaa', 'hello'))
        self.assertFalse(x.removeComponent('aaa', 0))
        self.assertFalse(x.removeComponent('aaa', x))
        y = Component()
        name = 'ys-name'
        y.setName(name)
        x.addComponent(y)
        self.assertFalse(x.removeComponent('aaa'))
        self.assertFalse(x.removeComponent('aaa', True))
        self.assertFalse(x.removeComponent('aaa', False))
        self.assertTrue(x.removeComponent(name))
        z = Component()
        y.addComponent(z)
        x.addComponent(y)
        self.assertTrue(x.removeComponent(name))
        del(x, y, z)
        z = Component()
        z.setName(name)
        y = Component()
        y.addComponent(z)
        x = ComponentEntity()
        x.addComponent(y)
        self.assertFalse(x.removeComponent(name, False))
        self.assertTrue(x.removeComponent(name, True))
        self.assertFalse(x.removeComponent(name, True))
        del(x, y, z, name)

        # bool removeComponent(const ComponentPtr &component,
        #   bool searchEncapsulated=true)
        x = ComponentEntity()
        y = Component()
        self.assertFalse(x.removeComponent(y))
        self.assertFalse(x.removeComponent(y, True))
        self.assertFalse(x.removeComponent(y, False))
        self.assertFalse(x.removeComponent(y, 'hello'))
        self.assertFalse(x.removeComponent(y, 0))
        self.assertFalse(x.removeComponent(y, x))
        x.addComponent(y)
        self.assertTrue(x.removeComponent(y))
        self.assertFalse(x.removeComponent(y))
        del(x, y)
        z = Component()
        y = Component()
        y.addComponent(z)
        x = ComponentEntity()
        x.addComponent(y)
        self.assertFalse(x.removeComponent(z, False))
        self.assertTrue(x.removeComponent(z, True))
        self.assertFalse(x.removeComponent(z, True))
        del(x, y, z)