Пример #1
0
    def test_component(self):
        from libcellml import Issue, Component

        # ComponentPtr component()
        e = Issue()
        self.assertIsNone(e.component())
        name = 'cellml'
        c = Component()
        c.setName(name)
        e.setComponent(c)
        self.assertIsInstance(e.component(), Component)
        self.assertEqual(e.component().name(), name)
Пример #2
0
    def test_get_component(self):
        from libcellml import Error, Component

        # ComponentPtr getComponent()
        e = Error()
        self.assertIsNone(e.getComponent())
        name = 'cellml'
        c = Component()
        c.setName(name)
        e.setComponent(c)
        self.assertIsInstance(e.getComponent(), Component)
        self.assertEqual(e.getComponent().getName(), name)
Пример #3
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)
Пример #4
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)
Пример #5
0
if __name__ == "__main__":
    #  0 Setup stuff that is used throughout
    validator = Validator()
    model = Model()
    model.setName("Tutorial7_SodiumChannelModel")

    math_header = '<math xmlns="http://www.w3.org/1998/Math/MathML" xmlns:cellml="http://www.cellml.org/cellml/2.0#">'
    math_footer = '</math>'

    print("-----------------------------------------------")
    print("  STEP 1: Creating the sodium channel")
    print("-----------------------------------------------")

    #  1.a Create the component instance, name it, and add to the model
    sodium_channel = Component()
    sodium_channel.setName("sodiumChannel")
    model.addComponent(sodium_channel)

    #  1.b Add the MathML representing the governing equations
    if True:
        equation1 = \
            '<apply>\
                <eq/>\
                <ci>E_Na</ci>\
                <apply>\
                    <times/>\
                    <ci>RTF</ci>\
                    <apply>\
                        <log/>\
                        <apply>\
                            <divide/>\
if __name__ == "__main__":
    print("-----------------------------------------------------")
    print("     TUTORIAL 3: CREATE A MODEL USING THE API        ")
    print("-----------------------------------------------------")

    # ---------------------------------------------------------------------------
    #   STEP 1: Create the model instance
    #
    #   1.a   Allocate the ModelPtr
    model = Model()
    model.setName("Tutorial3_model")

    #   1.b   Create a component to use as an integrator, set its attributes and
    #        add it to the model
    component = Component()
    component.setName("component")
    model.addComponent(component)

    #   Checking that it worked
    print_model_to_terminal(model)

    #   1.c,d Create the MathML2 string representing the governing equation.  The
    #        header and footer are below already.
    math_header = '<math xmlns="http://www.w3.org/1998/Math/MathML" xmlns:cellml="http://www.cellml.org/cellml/2.0#">'
    math_footer = '</math>'

    equation = "<apply><eq/>\
                    <apply><diff/>\
                        <bvar>\
                            <ci>t</ci>\
                        </bvar>\
    leakage_current.removeParent()
    membrane.addComponent(leakage_current)

    #  4.c Validate the combined model.
    validator.validateModel(model)
    print_errors_to_terminal(validator)

    print("\n-----------------------------------------------")
    print("   STEP 5: Create the environment component")
    print("-----------------------------------------------")

    print_encapsulation_structure_to_terminal(model)

    #  5.a Creating the environment component and adding it to the model
    environment = Component()
    environment.setName("environment")
    model.addComponent(environment)

    #  5.b Add variables to the environment component.
    if True:
        V = Variable()
        V.setName("V")
        V.setInitialValue(-85)
        V.setUnits("mV")
        environment.addVariable(V)

        t = Variable()
        t.setName("t")
        t.setUnits("ms")
        environment.addVariable(t)
Пример #8
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)
Пример #9
0
    def test_replace_component(self):
        from libcellml import ComponentEntity, Component

        # bool replaceComponent(size_t index, const ComponentPtr &c)
        x = ComponentEntity()
        a = Component()
        b = Component()
        self.assertFalse(x.replaceComponent(0, b))
        self.assertFalse(x.replaceComponent(1, b))
        self.assertFalse(x.replaceComponent(-1, b))
        x.addComponent(a)
        self.assertTrue(x.containsComponent(a))
        self.assertFalse(x.containsComponent(b))
        self.assertFalse(x.replaceComponent(1, b))
        self.assertFalse(x.replaceComponent(-1, b))
        self.assertTrue(x.replaceComponent(0, b))
        self.assertTrue(x.containsComponent(b))
        self.assertFalse(x.containsComponent(a))

        # bool replaceComponent(const std::string &name, const ComponentPtr &c,
        #   bool searchEncapsulated = true)
        x = ComponentEntity()
        a = Component()
        a.setName('a')
        b = Component()
        b.setName('b')
        self.assertFalse(x.replaceComponent('a', b))
        self.assertFalse(x.replaceComponent('b', a))
        x.addComponent(a)
        self.assertTrue(x.containsComponent(a))
        self.assertFalse(x.containsComponent(b))
        self.assertFalse(x.replaceComponent('b', a))
        self.assertTrue(x.replaceComponent('a', b))
        self.assertTrue(x.containsComponent(b))
        self.assertFalse(x.containsComponent(a))
        self.assertTrue(x.replaceComponent('b', a, True))
        self.assertTrue(x.replaceComponent('a', b, False))
        self.assertTrue(x.replaceComponent('b', a, 100))
        self.assertTrue(x.replaceComponent('a', b, []))
        self.assertTrue(x.replaceComponent('b', a, 'yes'))
        self.assertTrue(x.replaceComponent('a', b, a))
        del(x, a, b)

        # replaceComponent(const ComponentPtr &c1, const ComponentPtr &c2,
        #   bool searchEncapsulated = true)
        x = ComponentEntity()
        a = Component()
        b = Component()
        self.assertFalse(x.replaceComponent(a, b))
        self.assertFalse(x.replaceComponent(b, a))
        x.addComponent(a)
        self.assertTrue(x.containsComponent(a))
        self.assertFalse(x.containsComponent(b))
        self.assertFalse(x.replaceComponent(b, a))
        self.assertTrue(x.replaceComponent(a, b))
        self.assertTrue(x.containsComponent(b))
        self.assertFalse(x.containsComponent(a))
        self.assertTrue(x.replaceComponent(b, a, True))
        self.assertTrue(x.replaceComponent(a, b, False))
        self.assertTrue(x.replaceComponent(b, a, 100))
        self.assertTrue(x.replaceComponent(a, b, []))
        self.assertTrue(x.replaceComponent(b, a, 'yes'))
        self.assertTrue(x.replaceComponent(a, b, a))