def test_main(): # # Define Messages # buzz = Buzz('view_of_dice', ['dice_1', 'dice_2', 'dice_3', 'dice_4', 'dice_5']) belief = Belief('world_is_good', ['joy_index']) # # Define Organs and their associated messages # sensor = Sensor('check_roll', dice_sensor, buzz) interpreter = Interpreter('was_it_good_roll', roll_interpreter, buzz, belief, belief_updater=True) # # Initialize Agent # agent = Agent('test_agent', strict_engine=True) agent.set_organ(sensor) agent.set_organ(interpreter) beliefs = [] for k in range(0, 5): agent.sense('check_roll') agent.interpret('was_it_good_roll') beliefs.append(agent.belief['world_is_good'].values()[0]) assert (beliefs == REF_OUTCOME)
def test_main(): a1 = Agent('A1') a2 = Agent('A2') ams = ABC('dummy', [a1, a2], 1, 2) a1.create_socket('tester', 'sense', 'S1', True) a1.socket_offered['tester'].whitelist.add(a2.agent_id_system) SOCK1 = [('tester', a1.socket_offered['tester'].token)] a2.create_socket('tester', 'sense', 'S2', True) a2.socket_offered['tester'].whitelist.add(a1.agent_id_system) SOCK2 = [('tester', a2.socket_offered['tester'].token)] # # Agent 1 connects to socket of Agent 2 and executes # ttt1 = SOCK2[0][1] conn = a1.connect_to(a2, 'tester', ttt1) conn.execute() conn.close() assert (a2.buzz['stuff'].values()[0] == 2) a2.sense('S2') assert (a2.buzz['stuff'].values()[0] == 2) try: v = a1.buzz['stuff'].values() raise AssertionError('Agent 1 Buzz non-empty despite not executed') except EmptyFlashError: pass a1.sense('S1') assert (a1.buzz['stuff'].values()[0] == 1) try: conn = a2.connect_to(a1, 'tester', -1) raise AssertionError('Faulty token not caught') except SocketConnectionError: pass try: conn.execute() raise AssertionError('Did raise exception with unopened connection') except RuntimeError: pass
def test_main(): buzz = Buzz('nose_tingle', ['nerve_1', 'nerve_2']) sensor = Sensor('smell_the_roses', smeller, buzz) belief = Belief('world_blossoms', ['certainty']) interpreter = Interpreter('does_the_world_blossom', nerve_analyzer, buzz, belief) direction = Direction('words_to_say', ['first_word', 'second_word']) moulder = Moulder('what_to_say', word_smith, belief, direction) actuator = Actuator('say_it', speak, direction, actuator_func_kwargs={'well': True}) agent = Agent('simple human', strict_engine=True) agent.set_organs(sensor, interpreter, moulder, actuator) agent.sense('smell_the_roses') agent.interpret('does_the_world_blossom') agent.mould('what_to_say') agent.act('say_it') agent.sense('smell_the_roses') agent.interpret('does_the_world_blossom') agent.mould('what_to_say') agent.act('say_it') agent.sense('smell_the_roses') agent.interpret('does_the_world_blossom') agent.mould('what_to_say') agent.act('say_it') agent.sense('smell_the_roses') agent.interpret('does_the_world_blossom') agent.mould('what_to_say') agent.act('say_it') agent.sense('smell_the_roses') agent.interpret('does_the_world_blossom') agent.mould('what_to_say') agent.act('say_it') assert (read_env() == REF)
def test_main(): agent_core = Agent('Coordinator') essence = Essence('Coordinator Essence', ('param_1', 'param_2', 'param_3', 'param_4', 'param_5', 'param_6')) essence.set_values([1.0,2.0,3.0,4.0,5.0,6.0]) resource = Resource('Coordinator Resource', ('Energy Status',)) resource.set_values([75.0]) agent_core.set_scaffolds(essence, resource) agents = [agent_core] nodes = [Node('Central', agent_core)] for k in range(4): agent = Agent('Sensor Leaf %s' %(str(k)), strict_engine=True) essence = Essence('Sensor Essence', ('Sensitivity', 'Quality')) essence.set_values([78.0 + float(k), 99.0]) resource = Resource('Sensor Resource', ('Battery Power',)) resource.set_values([100.0 - 3.0 * k]) buzz = Buzz('Power Spectrum Sample', ('amplitude_max', 'freq_mode')) belief = Belief('Is There Motion', ('size', 'speed')) motion_classifier = Interpreter('From Freq to Object', pw2obj, buzz, belief) agent.set_scaffolds(essence, resource) agent.set_messages(buzz, belief) agent.set_organ(motion_classifier) agents.append(agent) nodes.append(Node('Sensor', agent, LocalEnv(k * k))) nodes.append(Node('dummy', None)) nodes.append(Node('dummy', None)) star_graph = nx.generators.classic.star_graph(nodes) ams = House('A House', agents, star_graph) for node in ams: agent = node.agent_content if not agent is None: if 'Is There Motion' in agent.belief: agent.sense('Feel microwaves') agent.interpret('From Freq to Object') central_a_sampler = AgentSampler('central_sampler', essence_args=[('Coordinator Essence', 'param_2'), ('Coordinator Essence', 'param_6')], resource_args=[('Coordinator Resource', 'Energy Status')], agent_matcher=_match_c) leaf_a_sampler = AgentSampler('leaf_sampler', resource_args=[('Sensor Resource', 'Battery Power')], belief_args=[('Is There Motion', 'size'), ('Is There Motion', 'speed')], agent_matcher=_match_l) env_sampler = EnvSampler('env_sampler', env_stuff, agent_matcher=_match_l) graph_sampler = GraphSampler('g_sampler', lambda x: x.name) io = SystemIO() io.set_write_rule('central', central_a_sampler, 'to_csv') io.set_write_rule('leaf', leaf_a_sampler, 'to_csv') io.set_write_rule('env', env_sampler, 'to_json') io.set_write_rule('graph_props', graph_sampler, 'gexf.write_gexf') io.try_stamp(ams, 0) exist_1 = os.path.isfile('central0.csv') exist_2 = os.path.isfile('leaf0.csv') exist_3 = os.path.isfile('env0.json') exist_4 = os.path.isfile('graph_props0.gexf') assert (exist_1) assert (exist_2) assert (exist_3) assert (exist_4) if exist_1: data = open('central0.csv').read() assert ('essence:Coordinator Essence:param_2,2.0' in data) assert ('essence:Coordinator Essence:param_6,6.0' in data) assert (not 'essence:Coordinator Essence:param_1' in data) assert (not 'essence:Coordinator Essence:param_3' in data) assert (not 'essence:Coordinator Essence:param_4' in data) assert (not 'essence:Coordinator Essence:param_5' in data) assert ('resource:Coordinator Resource:Energy Status,75.0' in data) os.remove('central0.csv') if exist_2: data = open('leaf0.csv').read() assert ('0,Sensor Leaf 0,' in data) assert ('0,Sensor Leaf 1,' in data) assert ('0,Sensor Leaf 2,' in data) assert ('0,Sensor Leaf 3,' in data) assert ('belief:Is There Motion:size,small' in data) assert (not 'belief:Is There Motion:size,large' in data) assert ('belief:Is There Motion:speed,fast' in data) assert ('belief:Is There Motion:speed,slow' in data) assert ('belief:Is There Motion:speed,n/a' in data) os.remove('leaf0.csv') if exist_3: data = open('env0.json').read() assert ('"[0,"Sensor Leaf 0"' in data) assert ('"[0,"Sensor Leaf 1"' in data) assert ('"[0,"Sensor Leaf 2"' in data) assert ('"[0,"Sensor Leaf 3"' in data) assert ('"env_stuff"]":0' in data) assert ('"env_stuff"]":1' in data) assert ('"env_stuff"]":4' in data) assert ('"env_stuff"]":9' in data) os.remove('env0.json') if exist_4: data = open('graph_props0.gexf').read().split('\n') REFS = ['Sensor_0', 'Sensor_1', 'Sensor_2', 'Sensor_3', 'unoccupied_0', 'unoccupied_1'] for row in data: if '<edge ' in row: assert ('"Central"' in row) for r in REFS: if r in row: REFS.remove(r) break else: raise AssertionError('Missed node %s' %(r)) assert (len(REFS) == 0) os.remove('graph_props0.gexf')