def test_user_notification(test_nodenet, node): api = micropsi.nodenets[test_nodenet].netapi node_obj = api.get_node(node) api.notify_user(node_obj, "Hello there") result, data = micropsi.get_calculation_state(test_nodenet, nodenet={'nodespaces': [None]}) assert 'user_prompt' in data assert data['user_prompt']['node']['uid'] == node assert data['user_prompt']['msg'] == "Hello there"
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_notification(test_nodenet, node): api = micropsi.nodenets[test_nodenet].netapi node_obj = api.get_node(node) api.notify_user(node_obj, "Hello there") result, data = micropsi.get_calculation_state( test_nodenet, nodenet={'nodespaces': [None]}) assert 'user_prompt' in data assert data['user_prompt']['node']['uid'] == node assert data['user_prompt']['msg'] == "Hello there"
def get_calculation_state(nodenet_uid, nodenet=None, nodenet_diff=None, world=None, monitors=None, dashboard=None): """ Return the current simulation state for any of the following: the given nodenet, world, monitors, dashboard Return values depend on the parameters: if you provide the nodenet-parameter (a dict, with all-optional keys: nodespaces, include_links, links_to_nodespaces) you will get the contents of the nodenet if you provide the nodenet_diff-parameter (a dict, with key "step" (the step to which the diff is calculated, and optional nodespaces) you will get a diff of the nodenet if you provide the world-parameter (anything) you will get the state of the nodenet's environment if you provide the monitor-parameter (anything), you will get data of all monitors registered in the nodenet if you provide the dashboard-parameter (anything) you will get a dict of dashboard data """ return runtime.get_calculation_state(nodenet_uid, nodenet=nodenet, nodenet_diff=nodenet_diff, world=world, monitors=monitors, dashboard=dashboard)
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 get_calculation_state(nodenet_uid, nodenet=None, nodenet_diff=None, world=None, monitors=None, dashboard=None): return runtime.get_calculation_state(nodenet_uid, nodenet=nodenet, nodenet_diff=nodenet_diff, world=world, monitors=monitors, dashboard=dashboard)