Exemple #1
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()
Exemple #2
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()
Exemple #3
0
def namespace(model, **kwargs):
    ns = {}
    # Project namespace if available
    if hasattr(model, "package"):
        if hasattr(model.package, "ns"):
            ns.update(model.package.ns)
        if hasattr(model.package, "namespace"):
            ns.update(model.package.namespace())

    # Control namespace
    ns.update(control_namespace())

    # User namespace
    ns.update(kwargs)
    return ns
Exemple #4
0
def namespace(model, **kwargs):
    ns = {}
    # Project namespace if available
    if hasattr(model, 'package'):
        if hasattr(model.package, 'ns'):
            ns.update(model.package.ns)
        if hasattr(model.package, 'namespace'):
            ns.update(model.package.namespace())

    # Control namespace
    ns.update(control_namespace())

    # User namespace
    ns.update(kwargs)
    return ns
Exemple #5
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()
Exemple #6
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()