def test_user_prompt(fixed_nodenet):
    options = [{'key': 'foo_parameter', 'label': 'Please give value for "foo"', 'values':  [23, 42]}]
    micropsi.nodenets[fixed_nodenet].netapi.ask_user_for_parameter(
        micropsi.nodenets[fixed_nodenet].get_node('A1'),
        "foobar",
        options
    )
    data = micropsi.get_nodenet_data(fixed_nodenet, 'Root')
    assert 'user_prompt' in data
    assert data['user_prompt']['msg'] == 'foobar'
    assert data['user_prompt']['node']['uid'] == 'A1'
    assert data['user_prompt']['options'] == options
    # response
    micropsi.user_prompt_response(fixed_nodenet, 'A1', {'foo_parameter': 42}, True)
    assert micropsi.nodenets[fixed_nodenet].get_node('A1').get_parameter('foo_parameter') == 42
    assert micropsi.nodenets[fixed_nodenet].is_active
    from micropsi_core.nodenet import nodefunctions
    nodefunc = mock.Mock()
    nodefunctions.concept = nodefunc
    micropsi.nodenets[fixed_nodenet].step()
    foo = micropsi.nodenets[fixed_nodenet].get_node('A1').clone_parameters()
    foo.update({'foo_parameter': 42})
    assert nodefunc.called_with(micropsi.nodenets[fixed_nodenet].netapi, micropsi.nodenets[fixed_nodenet].get_node('A1'), foo)
    micropsi.nodenets[fixed_nodenet].get_node('A1').clear_parameter('foo_parameter')
    assert micropsi.nodenets[fixed_nodenet].get_node('A1').get_parameter('foo_parameter') is None
Exemplo n.º 2
0
def test_user_prompt(fixed_nodenet, resourcepath):
    import os
    nodetype_file = os.path.join(resourcepath, 'Test', 'nodetypes.json')
    nodefunc_file = os.path.join(resourcepath, 'Test', 'nodefunctions.py')
    with open(nodetype_file, 'w') as fp:
        fp.write('{"Testnode": {\
            "name": "Testnode",\
            "slottypes": ["gen", "foo", "bar"],\
            "gatetypes": ["gen", "foo", "bar"],\
            "nodefunction_name": "testnodefunc",\
            "parameters": ["testparam"],\
            "parameter_defaults": {\
                "testparam": 13\
              }\
            }}')
    with open(nodefunc_file, 'w') as fp:
        fp.write(
            "def testnodefunc(netapi, node=None, **prams):\r\n    return 17")

    micropsi.reload_native_modules()
    res, uid = micropsi.add_node(fixed_nodenet,
                                 "Testnode", [10, 10],
                                 name="Test")
    nativemodule = micropsi.nodenets[fixed_nodenet].get_node(uid)

    options = [{
        'key': 'foo_parameter',
        'label': 'Please give value for "foo"',
        'values': [23, 42]
    }]
    micropsi.nodenets[fixed_nodenet].netapi.ask_user_for_parameter(
        nativemodule, "foobar", options)
    result, data = micropsi.get_calculation_state(fixed_nodenet, nodenet={})
    assert 'user_prompt' in data
    assert data['user_prompt']['msg'] == 'foobar'
    assert data['user_prompt']['node']['uid'] == uid
    assert data['user_prompt']['options'] == options
    # response
    micropsi.user_prompt_response(fixed_nodenet, uid, {'foo_parameter': 42},
                                  True)
    assert micropsi.nodenets[fixed_nodenet].get_node(uid).get_parameter(
        'foo_parameter') == 42
    assert micropsi.nodenets[fixed_nodenet].is_active
    from micropsi_core.nodenet import nodefunctions
    tmp = nodefunctions.concept
    nodefunc = mock.Mock()
    nodefunctions.concept = nodefunc
    micropsi.nodenets[fixed_nodenet].step()
    foo = micropsi.nodenets[fixed_nodenet].get_node('n0001').clone_parameters()
    foo.update({'foo_parameter': 42})
    assert nodefunc.called_with(
        micropsi.nodenets[fixed_nodenet].netapi,
        micropsi.nodenets[fixed_nodenet].get_node('n0001'), foo)
    micropsi.nodenets[fixed_nodenet].get_node('n0001').clear_parameter(
        'foo_parameter')
    assert micropsi.nodenets[fixed_nodenet].get_node('n0001').get_parameter(
        'foo_parameter') is None
    nodefunctions.concept = tmp
def test_user_prompt(fixed_nodenet, resourcepath):
    import os
    nodetype_file = os.path.join(resourcepath, 'Test', 'nodetypes.json')
    nodefunc_file = os.path.join(resourcepath, 'Test', 'nodefunctions.py')
    with open(nodetype_file, 'w') as fp:
        fp.write('{"Testnode": {\
            "name": "Testnode",\
            "slottypes": ["gen", "foo", "bar"],\
            "gatetypes": ["gen", "foo", "bar"],\
            "nodefunction_name": "testnodefunc",\
            "parameters": ["testparam"],\
            "parameter_defaults": {\
                "testparam": 13\
              }\
            }}')
    with open(nodefunc_file, 'w') as fp:
        fp.write("def testnodefunc(netapi, node=None, **prams):\r\n    return 17")

    micropsi.reload_native_modules()
    res, uid = micropsi.add_node(fixed_nodenet, "Testnode", [10, 10], name="Test")
    nativemodule = micropsi.nodenets[fixed_nodenet].get_node(uid)

    options = [{'key': 'foo_parameter', 'label': 'Please give value for "foo"', 'values': [23, 42]}]
    micropsi.nodenets[fixed_nodenet].netapi.ask_user_for_parameter(
        nativemodule,
        "foobar",
        options
    )
    result, data = micropsi.get_calculation_state(fixed_nodenet, nodenet={})
    assert 'user_prompt' in data
    assert data['user_prompt']['msg'] == 'foobar'
    assert data['user_prompt']['node']['uid'] == uid
    assert data['user_prompt']['options'] == options
    # response
    micropsi.user_prompt_response(fixed_nodenet, uid, {'foo_parameter': 42}, True)
    assert micropsi.nodenets[fixed_nodenet].get_node(uid).get_parameter('foo_parameter') == 42
    assert micropsi.nodenets[fixed_nodenet].is_active
    from micropsi_core.nodenet import nodefunctions
    tmp = nodefunctions.concept
    nodefunc = mock.Mock()
    nodefunctions.concept = nodefunc
    micropsi.nodenets[fixed_nodenet].step()
    foo = micropsi.nodenets[fixed_nodenet].get_node('n0001').clone_parameters()
    foo.update({'foo_parameter': 42})
    assert nodefunc.called_with(micropsi.nodenets[fixed_nodenet].netapi, micropsi.nodenets[fixed_nodenet].get_node('n0001'), foo)
    micropsi.nodenets[fixed_nodenet].get_node('n0001').clear_parameter('foo_parameter')
    assert micropsi.nodenets[fixed_nodenet].get_node('n0001').get_parameter('foo_parameter') is None
    nodefunctions.concept = tmp
Exemplo n.º 4
0
def user_prompt_response(nodenet_uid, node_uid, values, resume_nodenet):
    runtime.user_prompt_response(nodenet_uid, node_uid, values, resume_nodenet)
    return True
Exemplo n.º 5
0
def user_prompt_response(nodenet_uid, node_uid, values, resume_nodenet):
    runtime.user_prompt_response(nodenet_uid, node_uid, values, resume_nodenet);
    return dict(status='success')
Exemplo n.º 6
0
def user_prompt_response(nodenet_uid, node_uid, key, parameters, resume_nodenet):
    """ Respond to a user-prompt issued by a node. """
    runtime.user_prompt_response(nodenet_uid, node_uid, key, parameters, resume_nodenet)
    return True