/** The following introductory example demonstrates the basic principles how arbitrary configurations of tasks can perform triggered computations.
Although tasks are implemented as (somewhat artificial) active object threads, they may in general run as independent OS processes on different machines requesting (possibly complex) services.
AMICO control flow: from variable filter to variable process

Note that if the default viewer of .txt files is your browser, the 'add template' source code lines below are not displayed correctly. Use 'View' / 'Source' in your browser to view the actual source code of this example.
**/


:-object pipeline.

	var host = localhost, port = 3320.


	main :-
		new(data_generator(host, port, filter), _),
		new(filter_data(host, port, filter, process), _),
		new(process_data(host, port, process), _).

:-end_object pipeline.


%% 

:-object tcptrace: [amicolib].

	tcp_put_data(OStr, This, Format, Args) :-
		format_to_atom(Atom, Format, Args),
		format('~w~ttcp_put_data: ~w~n', [This, Atom]),	%% trace
		tcp_put_line(OStr, Atom, []).

	tcp_get_data(IStr, This, Data) :-
		tcp_get_line(IStr, Data),
		format('~w~ttcp_get_data: ~w~n', [This, Data]).	%% trace

:-end_object tcptrace.


:-object data_generator : [tcptrace].	%% tcptrace

	data_generator(Host, Port, To) :-
		tcp_client(Host, Port, _, OStr),

		repeat,
			data(Data),
			tcp_put_data(OStr, this, 'UPDATE ~w ~w~n', [To, Data]),
		Data == end_of_data,
		!.

	var ctr = 10.

	data(ctr) :-
		ctr < 15,
		!,
		++ ctr,
		delay(1000).	%% milliseconds
	data(end_of_data).

:-end_object data_generator.


:-object filter_data : [tcptrace].

	filter_data(Host, Port, From, To) :-
		tcp_client(Host, Port, IStr, OStr),
		tcp_put_data(OStr, this, 'ADD TEMPLATE ~w <%=~w%>~n', [From, From]),
		repeat,
			tcp_get_data(IStr, this, Data),
			tcp_put_data(OStr, this, 'UPDATE ~w ~w~n', [To, Data]),
		Data == end_of_data,
		!.

:-end_object filter_data.


:-object process_data : [tcptrace].

	process_data(Host, Port, From) :-
		tcp_client(Host, Port, IStr, OStr),
		tcp_put_data(OStr, this, 'ADD TEMPLATE ~w <%=~w%>~n', [From, From]),
		repeat,
			tcp_get_data(IStr, this, Data),
		Data == end_of_data,
		!.

:-end_object process_data.

/**
**/