% KEEPPARAMETERS enables to substitute symbollic patameters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: KEEPPARAMETERS enables the substitution of symbolic parameters.
%              The parameters substituted are then removed from the params field.
%              The function can be applied on an array of objects.
%
% CALL: varargout = keepParameters(varargin)
%             obj = obj.keepParameters(plist)
%             obj = obj.keepParameter % substitutes all parameters
%                                     % i.e., keep none.
%
% <a href="matlab:utils.helper.displayMethodInfo('ssm', 'keepParameters')">Parameters Description</a>
%
% VERSION: $Id: keepParameters.m,v 1.16 2011/04/08 08:56:22 hewitson Exp $
% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function varargout = keepParameters(varargin)
  
  callerIsMethod = utils.helper.callerIsMethod;
  
  %% starting initial checks
  % Check if this is a call for parameters
  if utils.helper.isinfocall(varargin{:})
    varargout{1} = getInfo(varargin{3});
    return
  end
  
  utils.helper.msg(utils.const.msg.MNAME, ['running ', mfilename]);
  
  % Collect input variable names
  in_names = cell(size(varargin));
  for ii = 1:nargin, in_names{ii} = inputname(ii); end
  
  % Collect all SSMs and options
  [sys, ssm_invars, rest] = utils.helper.collect_objects(varargin(:), 'ssm', in_names);
  [pl, pl_invars, rest]  = utils.helper.collect_objects(rest(:), 'plist');
  if ~isempty(rest)
    pl = combine(pl, plist(rest{:}));
  end
  pl = combine(pl, getDefaultPlist());
  
  %% processing input
  keepnames = pl.find('names');
  if ischar(keepnames)
    keepnames = {keepnames};
  elseif ~iscellstr(keepnames)
    error('param names be a cellstr')
  end
  
  %% Decide on a deep copy or a modify, depending on the output
  sys_out = copy(sys, nargout);
  Nsys = numel(sys);
  
  for i_sys = 1:Nsys
    % generating 'subsnames' out of 'keepnames'
    paramnames = sys_out(i_sys).paramnames;
    subsnames = cell(1,0);
    for j=1:numel(paramnames)
      comparison = strcmp(paramnames{j}, keepnames);
      if sum(comparison)==0
        subsnames = [subsnames paramnames{j}]; %#ok<AGROW>
      end
    end
    
    sys_out(i_sys).doSubsParameters(subsnames, callerIsMethod);
    sys_out(i_sys).addHistory(getInfo('None'), pl, {''}, sys_out(i_sys).hist );
  end
  
  if nargout == numel(sys_out)
    for ii = 1:numel(sys_out)
      varargout{ii} = sys_out(ii);
    end
  else
    varargout{1} = sys_out;
  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.op, '$Id: keepParameters.m,v 1.16 2011/04/08 08:56:22 hewitson Exp $', sets, pl);
end

%--------------------------------------------------------------------------
% Get Default Plist
%--------------------------------------------------------------------------
function pl = getDefaultPlist()
  pl = plist();
  
  p = param({'names', 'A cell-array of parameter names to be kept symbolic.'}, {} );
  pl.append(p);
  
end


