% ADDPARAMETERS Adds the parameters to the model.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: ADDPARAMETERS Adds the parameters to the (array of) model.
%              Parameters are combined to the ones in the field 'params'
%
% CALL:        obj = obj.addParameters('key1', val1, 'key2', val2);
%              obj = obj.addParameters(plist);
%
% <a href="matlab:utils.helper.displayMethodInfo('ssm', 'addParameters')">Parameters Description</a>
%
% VERSION: $Id: addParameters.m,v 1.14 2011/04/08 08:56:23 hewitson Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function varargout = addParameters(varargin)
  
  %% Check if this is a call for parameters
  if utils.helper.isinfocall(varargin{:})
    varargout{1} = getInfo(varargin{3});
    return
  end
  
  %% send starting message
  import utils.const.*
  utils.helper.msg(msg.PROC3, 'running %s/%s', mfilename('class'), mfilename);
  
  %% collecting input
  in_names = cell(size(varargin));
  for ii = 1:nargin,in_names{ii} = inputname(ii);end
  
  [ssms, ssm_invars, rest] = utils.helper.collect_objects(varargin(:), 'ssm', in_names);
  [pl, pl_invars, rest]   = utils.helper.collect_objects(rest(:), 'plist', in_names); %#ok<ASGLU>
  if ~isempty(rest)
    pl = combine(pl, plist(rest{:}));
  end
  pl = combine(pl, getDefaultPlist());
  
  ssmouts = copy(ssms, nargout);
  
  %% Loop over the input ssm objects
  for kk = 1:numel(ssmouts)
    ssmouts(kk).params = pl.combine(ssmouts(kk).params);
    % append history step
    ssmouts(kk).addHistory(getInfo('None'), pl, ssm_invars(kk), ssmouts(kk).hist);
  end % End loop over ssm objects
  
  
  %% Set output depending on nargout
  if nargout == numel(ssmouts)
    for i=1:nargout
      varargout{i} = ssmouts(i);
    end
  else
    varargout{1} = ssmouts;
  end
end

%--------------------------------------------------------------------------
% Get Info Object
%--------------------------------------------------------------------------
function ii = getInfo(varargin)
  
  if nargin == 1 && strcmpi(varargin{1}, 'None')
    sets = {};
    pl   = [];
  else
    sets = {'Default'};
    pl   = getDefaultPlist;
  end
  % Build info object
  ii = minfo(mfilename, 'ssm', 'ltpda', utils.const.categories.helper, '$Id: addParameters.m,v 1.14 2011/04/08 08:56:23 hewitson Exp $', sets, pl);
end

%--------------------------------------------------------------------------
% Get Default Plist
%--------------------------------------------------------------------------
function pl = getDefaultPlist()
  pl = plist();
end


