예제 #1
0
def test_output():
    step_code = 'c=a+b'

    m = Model('sum')
    m.inputs_info = [InputObj('a'), InputObj('b')]
    m.outputs_info = [OutputObj('c')]
    m.step_code = step_code

    assert m.run(a=1, b=2) == 3
예제 #2
0
def test_copy():
    m = Model(name='m1')
    m.set_step_code('c=a+b')
    m.inputs_info = [InputObj('a'), InputObj('b')]
    m.outputs_info = [OutputObj('c')]

    m2 = copy.copy(m)
    assert m is not m2
    assert m.step_code == m2.step_code
    assert [inp.name for inp in m2.inputs_info] == ['a', 'b']
    assert [out.name for out in m2.outputs_info] == ['c']
예제 #3
0
def test_multiple_call():
    """
    Check each model instance has is own namespace
    """
    model1 = Model('m1')
    model1.inputs_info = [InputObj('a=0')]
    model1.outputs_info = [OutputObj('a')]
    model1.set_step_code('a+=1')

    model2 = Model('m2')
    model2.outputs_info = [OutputObj('m1'), OutputObj('m2')]
    model2.set_step_code(code)

    register_model(model1)
    ns = namespace()

    m1, m2 = model2.run(namespace=ns)
    assert m1 == 4
    assert m2 == 2
예제 #4
0
def test_steps():
    step_code = 'a += 10'

    m = Model('sum')
    m.inputs_info = [InputObj('a=0')]
    m.outputs_info = [OutputObj('a')]
    m.step_code = step_code

    assert m.run() == 10
    assert m.run(nstep=10) == 100
    def test_world_namespace(self):
        from openalea.core.service.run import namespace
        from openalea.core.model import Model

        proj = pm.create('new_temp_project', projectdir=self.tmpdir)
        user_ns = pm.shell.user_ns

        assert 'world' in user_ns
        w = user_ns['world']
        assert w.keys() == []

        code = "world.add(1, name='i');a=1"
        model = Model()
        proj.add('model', model)
        model.set_code(code)
        model.run(**namespace())

        assert w.keys() == ['i']
        assert w['i'].obj == 1

        pm.cproject = None
        assert 'world' not in user_ns
        assert w.keys() == []
예제 #6
0
def test_output():
    step_code = 'c=a+b'

    m = Model('sum')
    m.inputs_info = [InputObj('a'), InputObj('b')]
    m.outputs_info = [OutputObj('c')]
    m.step_code = step_code

    assert m.run(a=1, b=2) == 3
예제 #7
0
def test_multiple_call():
    """
    Check each model instance has is own namespace
    """
    model1 = Model('m1')
    model1.inputs_info = [InputObj('a=0')]
    model1.outputs_info = [OutputObj('a')]
    model1.set_step_code('a+=1')

    model2 = Model('m2')
    model2.outputs_info = [OutputObj('m1'), OutputObj('m2')]
    model2.set_step_code(code)

    project.add("model", model1)
    ns = {}
    ns['Model'] = project.get_runnable_model

    m1, m2 = project.run_model(model2, namespace=ns)
    assert m1 == 4
    assert m2 == 2
예제 #8
0
    def todo_infile_data(self):
        from openalea.core.data import Model
        # Create a python file
        pyfile = self.tmpdir / 'model.py'
        with open(pyfile, 'w') as f:
            f.write('data 1')

        Model.set_code = debug_parse

        # Check no parse. At this moment we don't want to read content for performance reason
        model = Model(path=pyfile)
        assert hasattr(model, 'parsed') is False

        # To get documentation, Model need to read content
        content = model.read()
        assert model.parsed is True
        model.parsed = False

        # Check that cache works: file don't have changed, no need to read and parse it again
        content = model.read()
        assert model.parsed is False
        assert content == "data 1"

        # Simulate a change
        with open(pyfile, 'w') as f:
            f.write('new data')

        # pyfile has changed, it need to be read again!
        content = model.read()
        assert model.parsed is True
        assert content == "new data"

        model.parsed = False
        model.set_cache_mode(model.NO_CACHE)
        model.read()
        assert model.parsed is True
예제 #9
0
def test_steps():
    step_code = 'a += 10'

    m = Model('sum')
    m.inputs_info = [InputObj('a=0')]
    m.outputs_info = [OutputObj('a')]
    m.step_code = step_code

    assert m.run() == 10
    assert m.run(nstep=10) == 100
예제 #10
0
def test_copy():
    m = Model(name='m1')
    m.set_step_code('c=a+b')
    m.inputs_info = [InputObj('a'), InputObj('b')]
    m.outputs_info = [OutputObj('c')]

    m2 = copy.copy(m)
    assert m is not m2
    assert m.step_code == m2.step_code
    assert [inp.name for inp in m2.inputs_info] == ['a', 'b']
    assert [out.name for out in m2.outputs_info] == ['c']
예제 #11
0
    def todo_inmemory_data(self):
        from openalea.core.model import Model
        Model.parse = debug_parse
        Model.parsed = False

        # Check content is parsed
        model = Model(content=b'Hi ho', filename='model.py')
        assert model.parsed is True
        model.parsed = False

        model = Model(content=b'Hi ho', filename='model.py')

        # Check content is not parsed because content has not changed
        model.parsed = False
        content = model.read()
        assert model.parsed is False

        # Check content is parsed again because content has been explicitly changed
        model.content = b'captain'
        assert model.parsed is True
예제 #12
0
    def todo_inmemory_data(self):
        from openalea.core.model import Model
        Model.parse = debug_parse
        Model.parsed = False

        # Check content is parsed
        model = Model(content='Hi ho', filename='model.py')
        assert model.parsed is True
        model.parsed = False

        model = Model(content='Hi ho', filename='model.py')

        # Check content is not parsed because content has not changed
        model.parsed = False
        content = model.read()
        assert model.parsed is False

        # Check content is parsed again because content has been explicitly changed
        model.content = 'captain'
        assert model.parsed is True
예제 #13
0
    def test_world_namespace(self):

        from openalea.core.model import Model

        proj = pm.create('new_temp_project', projectdir=self.tmpdir)
        user_ns = pm.shell.user_ns

        assert 'world' in user_ns
        w = user_ns['world']
        assert w.keys() == []

        code = "world.add(1, name='i');a=1"
        model = Model()
        proj.add('model', model)
        model.set_code(code)
        model.run(**proj.ns)

        assert w.keys() == ['i']
        assert w['i'].obj == 1

        pm.cproject = None
        assert 'world' not in user_ns
        assert w.keys() == []
예제 #14
0
def test_multiple_call():
    """
    Check each model instance has is own namespace
    """
    model1 = Model('m1')
    model1.inputs_info = [InputObj('a=0')]
    model1.outputs_info = [OutputObj('a')]
    model1.set_step_code('a+=1')

    model2 = Model('m2')
    model2.outputs_info = [OutputObj('m1'), OutputObj('m2')]
    model2.set_step_code(code)

    project.add("model", model1)
    ns = {}
    ns['Model'] = project.get_runnable_model

    m1, m2 = project.run_model(model2, namespace=ns)
    assert m1 == 4
    assert m2 == 2
예제 #15
0
def test_global_and_control_it():
    from openalea.core.service.control import new_control

    m1 = Model('IterateIOFullyDefined')
    m1.inputs_info = [InputObj('a=0')]
    m1.outputs_info = [OutputObj('a')]
    m1.set_step_code('a+=1')

    m2 = Model('IterateIOWithoutDefault')
    m2.inputs_info = [InputObj('a')]
    m2.outputs_info = [OutputObj('a')]
    m2.set_step_code('a+=1')

    m3 = Model('IterateNoIO')
    m3.outputs_info = [OutputObj('a')]
    m3.set_step_code('a+=1')

    m4 = Model('IterateNoIO')
    m4.set_step_code('a+=1')

    ms1 = Model('IterateCheckModelCall')
    ms1.outputs_info = [OutputObj('c')]

    register_model(m1)
    register_model(m2)
    register_model(m3)

    new_control('a', 'IInt', 10)
    a = 123456789
    ns = namespace()

    nstep = 3

    ms1.set_step_code('m = Model("IterateIOFullyDefined")\nc = m.run()')
    assert ms1.run(namespace=ns, nstep=nstep) == 1
    ms1.set_step_code('m = Model("IterateIOFullyDefined")\nc = m.run(nstep=3)')
    assert ms1.run(namespace=ns, nstep=nstep) == (0 + 3)

    assert m1.run(nstep=nstep) == (0 + nstep)
    assert m2.run(1, nstep=nstep) == (1 + nstep)
    assert m2.run(a=2, nstep=nstep) == (2 + nstep)

    # a, b not defined, use default a=0, b=0. Do not use global a because a is not a free variable
    assert m1.run(namespace=ns, nstep=nstep) == (0 + nstep)
    # m2.run(namespace=ns)  # FAIL, b not defined
    # m3.run(namespace=ns)  # FAIL, b not defined

    assert m1.run(1, namespace=ns, nstep=nstep) == (1 + nstep)
    assert m2.run(2, namespace=ns, nstep=nstep) == (2 + nstep)
    assert m2.run(a=10, namespace=ns, nstep=nstep) == (10 + nstep)
    assert m3.run(a=10, namespace=ns, nstep=nstep) == (10 + nstep)

    clear_controls()
예제 #16
0
def test_global_and_control():
    m1 = Model('IOFullyDefined')
    m1.inputs_info = [InputObj('a=0'), InputObj('b=0')]
    m1.outputs_info = [OutputObj('c')]
    m1.set_step_code('c=a+b')

    m2 = Model('IOWithoutDefault')
    m2.inputs_info = [InputObj('a'), InputObj('b')]
    m2.outputs_info = [OutputObj('c')]
    m2.set_step_code('c=a+b')

    m3 = Model('NoIO')
    m3.outputs_info = [OutputObj('c')]
    m3.set_step_code('c=a+b')

    ms1 = Model('CheckModelCall')
    ms1.outputs_info = [OutputObj('c')]

    register_model(m1)
    register_model(m2)
    register_model(m3)

    from openalea.core.service.control import new_control
    new_control('a', 'IInt', 10)
    a = 123456789
    ns = {}
    ns.update(control_namespace())
    ns['Model'] = project.get_runnable_model

    ms1.set_step_code('m = Model("IOFullyDefined")\nc = m.run()')
    assert ms1.run(namespace=ns) == 0
    ms1.set_step_code('m = Model("IOFullyDefined")\na=44\nc = m.run(b=22)')
    assert ms1.run(namespace=ns) == 22
    ms1.set_step_code(
        'm = Model("IOFullyDefined")\na=44\nc = m.run(a=11, b=22)')
    assert ms1.run(namespace=ns) == 33
    ms1.set_step_code('m = Model("IOFullyDefined")\na=44\nc = m.run(a)')
    assert ms1.run(namespace=ns) == 44

    assert m1.run() == 0  # 0
    assert m2.run(1, 2) == 3  # 3
    assert m2.run(2, b=3) == 5  # 5
    assert m2.run(a=3, b=4) == 7  # 7

    # a, b not defined, use default a=0, b=0. Do not use global a because a is not a free variable
    assert m1.run(namespace=ns) == 0
    # m2.run(namespace=ns)  # FAIL, b not defined
    # m3.run(namespace=ns)  # FAIL, b not defined

    assert m1.run(1, 2, namespace=ns) == 3  # 3
    assert m2.run(2, b=3, namespace=ns) == 5  # 5
    assert m2.run(a=3, b=4, namespace=ns) == 7  # 7
    assert m3.run(a=3, b=4, namespace=ns) == 7  # 7

    b = 20
    ns = locals()
    ns.update(control_namespace())
    ns['Model'] = project.get_runnable_model

    ms1.set_step_code('m = Model("IOWithoutDefault")\nc = m.run()')
    assert ms1.run(
        namespace=ns) == 30  # 30 free variables a, b. use global var a, b
    ms1.set_step_code(
        'm = Model("IOWithoutDefault")\na=456\nc = m.run(a=11, b=22)')
    assert ms1.run(namespace=ns) == 33  # 33
    ms1.set_step_code('m = Model("IOWithoutDefault")\nc = m.run(b=22)')
    assert ms1.run(
        namespace=ns) == 32  # 32 a use control value, b set explicitly
    ms1.set_step_code('m = Model("IOWithoutDefault")\na=33\nc = m.run(b=22)')
    assert ms1.run(
        namespace=ns) == 55  # 55 a use parent model value, b set explicitly
    ms1.set_step_code('m = Model("IOWithoutDefault")\na=44\nc = m.run(a)')
    assert ms1.run(namespace=ns) == 64  # 64

    assert m1.run() == 0  # 0
    assert m2.run(1, 2) == 3  # 3
    assert m2.run(2, b=3) == 5  # 5
    assert m2.run(a=3, b=4) == 7  # 7

    assert m1.run(
        namespace=ns
    ) == 0  # Do not use global a or b because a and b are not free variables
    assert m2.run(namespace=ns) == 30  # 30
    assert m3.run(namespace=ns) == 30  # 30

    assert m1.run(1, 2, namespace=ns) == 3  # 3
    assert m2.run(2, b=3, namespace=ns) == 5  # 5
    assert m2.run(a=3, b=4, namespace=ns) == 7  # 7
    assert m3.run(a=3, b=4, namespace=ns) == 7  # 7

    clear_controls()
예제 #17
0
    def todo_infile_data(self):
        from openalea.core.data import Model
        # Create a python file
        pyfile = self.tmpdir / 'model.py'
        with open(pyfile, 'wb') as f:
            f.write(b'data 1')

        Model.set_code = debug_parse

        # Check no parse. At this moment we don't want to read content for performance reason
        model = Model(path=pyfile)
        assert hasattr(model, 'parsed') is False

        # To get documentation, Model need to read content
        content = model.read()
        assert model.parsed is True
        model.parsed = False

        # Check that cache works: file don't have changed, no need to read and parse it again
        content = model.read()
        assert model.parsed is False
        assert content == b"data 1"

        # Simulate a change
        with open(pyfile, 'wb') as f:
            f.write(b'new data')

        # pyfile has changed, it need to be read again!
        content = model.read()
        assert model.parsed is True
        assert content == b"new data"

        model.parsed = False
        model.set_cache_mode(model.NO_CACHE)
        model.read()
        assert model.parsed is True
예제 #18
0
def test_global_and_control_it():
    from openalea.core.service.control import new_control

    m1 = Model('IterateIOFullyDefined')
    m1.inputs_info = [InputObj('a=0')]
    m1.outputs_info = [OutputObj('a')]
    m1.set_step_code('a+=1')

    m2 = Model('IterateIOWithoutDefault')
    m2.inputs_info = [InputObj('a')]
    m2.outputs_info = [OutputObj('a')]
    m2.set_step_code('a+=1')

    m3 = Model('IterateNoIO')
    m3.outputs_info = [OutputObj('a')]
    m3.set_step_code('a+=1')

    m4 = Model('IterateNoIO')
    m4.set_step_code('a+=1')

    ms1 = Model('IterateCheckModelCall')
    ms1.outputs_info = [OutputObj('c')]

    register_model(m1)
    register_model(m2)
    register_model(m3)

    new_control('a', 'IInt', 10)
    a = 123456789
    ns = {}
    ns.update(control_namespace())
    ns['Model'] = project.get_runnable_model

    nstep = 3

    ms1.set_step_code('m = Model("IterateIOFullyDefined")\nc = m.run()')
    assert ms1.run(namespace=ns, nstep=nstep) == 1
    ms1.set_step_code('m = Model("IterateIOFullyDefined")\nc = m.run(nstep=3)')
    assert ms1.run(namespace=ns, nstep=nstep) == (0 + 3)

    assert m1.run(nstep=nstep) == (0 + nstep)
    assert m2.run(1, nstep=nstep) == (1 + nstep)
    assert m2.run(a=2, nstep=nstep) == (2 + nstep)

    # a, b not defined, use default a=0, b=0. Do not use global a because a is not a free variable
    assert m1.run(namespace=ns, nstep=nstep) == (0 + nstep)
    # m2.run(namespace=ns)  # FAIL, b not defined
    # m3.run(namespace=ns)  # FAIL, b not defined

    assert m1.run(1, namespace=ns, nstep=nstep) == (1 + nstep)
    assert m2.run(2, namespace=ns, nstep=nstep) == (2 + nstep)
    assert m2.run(a=10, namespace=ns, nstep=nstep) == (10 + nstep)
    assert m3.run(a=10, namespace=ns, nstep=nstep) == (10 + nstep)

    clear_controls()
예제 #19
0
def test_global_and_control():
    m1 = Model('IOFullyDefined')
    m1.inputs_info = [InputObj('a=0'), InputObj('b=0')]
    m1.outputs_info = [OutputObj('c')]
    m1.set_step_code('c=a+b')

    m2 = Model('IOWithoutDefault')
    m2.inputs_info = [InputObj('a'), InputObj('b')]
    m2.outputs_info = [OutputObj('c')]
    m2.set_step_code('c=a+b')

    m3 = Model('NoIO')
    m3.outputs_info = [OutputObj('c')]
    m3.set_step_code('c=a+b')

    ms1 = Model('CheckModelCall')
    ms1.outputs_info = [OutputObj('c')]

    register_model(m1)
    register_model(m2)
    register_model(m3)

    from openalea.core.service.control import new_control
    new_control('a', 'IInt', 10)
    a = 123456789
    ns = namespace()

    ms1.set_step_code('m = Model("IOFullyDefined")\nc = m.run()')
    assert ms1.run(namespace=ns) == 0
    ms1.set_step_code('m = Model("IOFullyDefined")\na=44\nc = m.run(b=22)')
    assert ms1.run(namespace=ns) == 22
    ms1.set_step_code('m = Model("IOFullyDefined")\na=44\nc = m.run(a=11, b=22)')
    assert ms1.run(namespace=ns) == 33
    ms1.set_step_code('m = Model("IOFullyDefined")\na=44\nc = m.run(a)')
    assert ms1.run(namespace=ns) == 44

    assert m1.run() == 0  # 0
    assert m2.run(1, 2) == 3  # 3
    assert m2.run(2, b=3) == 5  # 5
    assert m2.run(a=3, b=4) == 7  # 7

    # a, b not defined, use default a=0, b=0. Do not use global a because a is not a free variable
    assert m1.run(namespace=ns) == 0
    # m2.run(namespace=ns)  # FAIL, b not defined
    # m3.run(namespace=ns)  # FAIL, b not defined

    assert m1.run(1, 2, namespace=ns) == 3  # 3
    assert m2.run(2, b=3, namespace=ns) == 5  # 5
    assert m2.run(a=3, b=4, namespace=ns) == 7  # 7
    assert m3.run(a=3, b=4, namespace=ns) == 7  # 7

    b = 20
    ns = locals()
    ns.update(namespace())

    ms1.set_step_code('m = Model("IOWithoutDefault")\nc = m.run()')
    assert ms1.run(namespace=ns) == 30  # 30 free variables a, b. use global var a, b
    ms1.set_step_code('m = Model("IOWithoutDefault")\na=456\nc = m.run(a=11, b=22)')
    assert ms1.run(namespace=ns) == 33  # 33
    ms1.set_step_code('m = Model("IOWithoutDefault")\nc = m.run(b=22)')
    assert ms1.run(namespace=ns) == 32  # 32 a use control value, b set explicitly
    ms1.set_step_code('m = Model("IOWithoutDefault")\na=33\nc = m.run(b=22)')
    assert ms1.run(namespace=ns) == 55  # 55 a use parent model value, b set explicitly
    ms1.set_step_code('m = Model("IOWithoutDefault")\na=44\nc = m.run(a)')
    assert ms1.run(namespace=ns) == 64  # 64

    assert m1.run() == 0  # 0
    assert m2.run(1, 2) == 3  # 3
    assert m2.run(2, b=3) == 5  # 5
    assert m2.run(a=3, b=4) == 7  # 7

    assert m1.run(namespace=ns) == 0  # Do not use global a or b because a and b are not free variables
    assert m2.run(namespace=ns) == 30  # 30
    assert m3.run(namespace=ns) == 30  # 30

    assert m1.run(1, 2, namespace=ns) == 3  # 3
    assert m2.run(2, b=3, namespace=ns) == 5  # 5
    assert m2.run(a=3, b=4, namespace=ns) == 7  # 7
    assert m3.run(a=3, b=4, namespace=ns) == 7  # 7

    clear_controls()
예제 #20
0
def test_multiple_call():
    """
    Check each model instance has is own namespace
    """
    model1 = Model('m1')
    model1.inputs_info = [InputObj('a=0')]
    model1.outputs_info = [OutputObj('a')]
    model1.set_step_code('a+=1')

    model2 = Model('m2')
    model2.outputs_info = [OutputObj('m1'), OutputObj('m2')]
    model2.set_step_code(code)

    register_model(model1)
    ns = namespace()

    m1, m2 = model2.run(namespace=ns)
    assert m1 == 4
    assert m2 == 2