view arrows/@aseq/construct.m @ 0:672052bd81f8

Initial partial import.
author samer
date Wed, 19 Dec 2012 22:38:28 +0000
parents
children
line wrap: on
line source
function u=construct(s,sizes_in)

	current_arrow = head(s.arrows);
	remaining_arrows = next(s.arrows);
	fprintf('aseq: starting with arrow %s...\n',tostring(current_arrow));

	nin=nargin(current_arrow);
	nout=nargout(current_arrow);

	running=0;
	current_unit  = construct(current_arrow,sizes_in(1:nin));
	current_proc  = current_unit.process;

	u=mkunit(s);
	u.sizes_out = current_unit.sizes_out;
	u.starting = @starting;
	u.stopping = @stopping;
	u.dispose  = @dispose;
	u.get_state = @get_state;
	u.set_state = @set_state;
	u.process   = @proc_nn;

	function starting, current_unit.starting(); running=1; end
	function stopping, current_unit.stopping(); running=0; end
	function dispose,  current_unit.dispose(); end

	function s=get_state,  s={current_arrow, remaining_arrows, current_unit.get_state()}; end
	function set_state(s), replace_running_unit(s{1},s{2},s{3}); end

	function varargout=proc_nn(varargin)
		try 
			[varargout{1:nout}]=current_proc(varargin{:});
		catch ex
			if iseof(ex) && ~isempty(remaining_arrows) 
				next_arrow=head(remaining_arrows);
				fprintf('aseq: continuing with next arrow %s...\n',tostring(next_arrow));
				replace_running_unit(next_arrow, next(remaining_arrows));
				[varargout{1:nout}]=proc_nn(varargin{:});
			else 
				rethrow(ex); 
			end
		end
	end

	function replace_running_unit(a,remaining,state)
		if running 
			current_unit.stopping(); 
			current_unit.dispose(); 
		end
		current_arrow=a;
		current_unit=construct(a,sizes_in(1:nin));
		current_proc=current_unit.process;
		remaining_arrows=remaining;
		if nargin>2, current_unit.set_state(state); end
		if running, current_unit.starting(); end
	end
end