def echo_list(in_stream, out_stream): def _copy(lst): print 'echo, lst = ', lst return lst return stream_agent( inputs=in_stream, outputs= out_stream, f_type='list', f=_copy)
def echo(in_stream, out_stream): def _copy(v): print 'echo, v = ', v return v return stream_agent( inputs=in_stream, outputs= out_stream, f_type='element', f=_copy)
def add_count(in_stream, out_stream): def _add(v,count): print 'output is ', v+1 return (v+1, count+1) return stream_agent( inputs=in_stream, outputs= out_stream, f_type='element', f=_add, state=0)
def number_even_odd(m): if m%2: # since n is odd, the zero-th element is # _no_value, and the first element is n # in the returned list return [_no_value, m] else: # since n is even, the zero-th element is # n, and the first element is _no_value # in the returned list. return [m, _no_value] return stream_agent( inputs=in_stream, outputs=[out_stream_0, out_stream_1], f_type='element', f=number_even_odd)
def echo_n_times(in_stream, out_stream, n): def echo_value(v,count): if count < n: print 'echo_value. receive', v count += 1 return(v+1, count) else: return(_close, count) return stream_agent( inputs=in_stream, outputs=out_stream, f_type='element', f=echo_value, state=0)
return ([v+f_args[0]+state for v in lst], state) def multiply_elements(v, f_args): return v*f_args[0] in_stream = Stream('inputs') out_stream_1 = Stream('outputs 1') out_stream_2 = Stream('outputs 2') out_stream_3 = Stream('outputs 3') out_stream_4 = Stream('outputs 4') out_stream_5 = Stream('outputs 5') out_stream_6 = Stream('outputs 6') agent_1 = stream_agent( inputs=in_stream, outputs=out_stream_1, f_type='element', f=addup, f_args=(4,)) agent_2 = stream_agent( inputs=in_stream, outputs=out_stream_2, f_type='element', f=echo, f_args=None) agent_3 = stream_agent( inputs=in_stream, outputs=out_stream_3, f_type='element', f=add_state,
def make_network(stream_names_tuple, agent_descriptor_dict): """ This function makes a network of agents given the names of the streams in the network and a description of the agents in the network. Parameters ---------- stream_names_tuple: tuple of str A tuple consisting of names of streams in the network. Each stream in the network must have a unique name. agent_descriptor_dict: dict of tuples The key is an agent name The value is a tuple: in_list, out_list, f, f_type, f_args, state where: in_list: list of input stream names out_list: list of output stream names f: function associated with the agent f_type: 'element', 'list', 'window', etc f_args: tuple of arguments for functions f state: the state associated with this agent. Local Variables --------------- stream_dict: dict key: stream name value: Stream agent_dict: dict key: agent name value: agent with the specified description: in_list, out_list, f, f_type, f_args, state, call_streams=[timer_stream] where one timer stream is associated with each agent. agent_timer_dict: dict key: agent_name value: Stream The value is the timer stream associated with the agent. When the timer stream has a message, the agent is made to execute a step. """ # Create streams and insert streams into stream_dict. stream_dict = dict() for stream_name in stream_names_tuple: stream_dict[stream_name] = Stream(stream_name) ## # Only for debugging ## for key, value in stream_dict.items(): ## print 'stream_name: ', key ## print 'stream:', value agent_dict = dict() agent_timer_dict = dict() # Create agents with the specified description # and put the agents into agent_dict. for agent_name in agent_descriptor_dict.keys(): # print 'agent_name:', agent_name in_list, out_list, f, f_type, f_args, state = \ agent_descriptor_dict[agent_name] ## # Only for debugging ## print 'in_list', in_list ## print 'out_list', out_list ## print 'f', f ## print 'f_args', f_args ## print 'f_type', f_type ## print 'state', state # Replace a list consisting of a single input stream # by the stream itself. if len(in_list) == 1: single_input_stream_name = in_list[0] inputs = stream_dict[single_input_stream_name] else: inputs = list() for input_stream_name in in_list: inputs.append(stream_dict[input_stream_name]) # Replace a list consisting of a single output stream # by the stream itself. if len(out_list) == 1: single_output_stream_name = out_list[0] outputs = stream_dict[single_output_stream_name] else: outputs = list() for output_stream_name in out_list: outputs.append(stream_dict[output_stream_name]) # Create timer streams and insert them into agent_timer_dict agent_timer_dict[agent_name] = Stream( agent_name + ':timer') # Create agents and insert them into agent_dict agent_dict[agent_name] = stream_agent( inputs, outputs, f_type, f, f_args, state, call_streams=[agent_timer_dict[agent_name]]) # Set the name for this agent. agent_dict[agent_name].name = agent_name return (stream_dict, agent_dict, agent_timer_dict)