% RUNPIPELINE runs the currently active pipeline from the beginning.
%
% CALL: wb.runPipeline
%
% M Hewitson 13-11-08
%
% $Id: runPipeline.m,v 1.21 2011/03/29 13:40:16 hewitson Exp $
%
function varargout = runPipeline(varargin)
  
  wb = varargin{1};
  if ~isa(wb, 'LTPDAworkbench')
    error('runPipeline needs an LTPDAworkbench instance as the first input.');
  end
  
  % Assume running is successful
  res = true;
  
  setappdata(0, 'LTPDAworkbench', wb);
  wb.reset(false);
  
  wb.mp.setExecuting(true);
  if wb.mp.popupConsoleOnExecution()
    wb.mp.showConsole();
  end
  
  try
    
    LTPDAworkbench.executeConstants(wb);
    
    % Now execute all other blocks
    cmds = {};
    while awtinvoke(wb.mp, 'countReadyBlocks') > 0
      try
        wb.stepForward;
      catch exception
        utils.helper.errorDlg([exception.message], 'Execution error');
        wb.addErrorMessage(exception.cause{1}.message);
        utils.helper.err(exception.message);
        res = false;
      end
      drawnow;
    end
  catch
    wb.mp.setExecuting(false);
    res = false;
  end
  wb.mp.setExecuting(false);
  
  setappdata(0, 'LTPDAworkbench', []);
  
  if nargout == 1
    varargout{1} = res;
  end
  
end

