% ADDPARAMETERS Add some parameters to the symbolic model (smodel) object
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: ADDPARAMETERS Add some parameters to the symbolic model
%              (smodel) object. It is possible to set only the parameter
%              name or to set the parameter names with their values.
%              Exist the parameter-name in the object then replace this
%              method the existing value.
%
% CALL:        obj = obj.addParameters(key-value pairs);
%              obj = obj.addParameters(plist);
%              obj = obj.addParameters(two cell-arrays);
%              obj = obj.addParameters(single cell-array of names);
%
% INPUTS:      obj         - a ltpda smodel object.
%              key-value   - A single key-value pair or a list of key-value
%                            pairs. Thereby it is important that the key is
%                            followed by the value.
%              plist       - Parameter list with values for the keys
%                            'params' and 'values'. The values can be a
%                            single value or a cell array with multiple
%                            values.
%              cell-arrays - Two cell-arrays with the first of the 'params'
%                            and the second with the 'values'.
%
% REMARK:      This function will replace the parameter value if the
%              parameter name already exist
%
% <a href="matlab:utils.helper.displayMethodInfo('smodel', 'addParameters')">Parameters Description</a>
%
% VERSION:     $Id: addParameters.m,v 1.1 2011/04/17 15:48:24 ingo Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function varargout = addParameters(varargin)
  
  % Check if this is a call from a class method
  callerIsMethod = utils.helper.callerIsMethod;
  
  if callerIsMethod
    sm     = varargin{1}; % smodel-object(s)
    names  = varargin{2}; % cell-array with parameter-names
    values = varargin{3}; % call-array with parameter-values
    
  else
    % Check if this is a call for parameters
    if utils.helper.isinfocall(varargin{:})
      varargout{1} = getInfo(varargin{3});
      return
    end
    
    import utils.const.*
    utils.helper.msg(msg.PROC3, 'running %s/%s', mfilename('class'), mfilename);
    
    % Collect input variable names
    in_names = cell(size(varargin));
    for ii = 1:nargin,in_names{ii} = inputname(ii);end
    
    % Collect all smodel objects
    [sm,  sm_invars, rest] = utils.helper.collect_objects(varargin(:), 'smodel', in_names);
    [pls, dummy,     rest] = utils.helper.collect_objects(rest(:), 'plist');
    
    names = {};
    values = {};
    %%% If the input PLIST have the keys 'params' and 'values' then use also
    %%% the
    if length(pls) == 1 && isa(pls, 'plist') && isparam(pls, 'params') && isparam(pls, 'values')
      names  = find(pls, 'params');
      values = find(pls, 'values');
      % Make sure that the param names and the values are cell-arrays
      names = cellstr(names);
      if ~iscell(values)
        values = num2cell(values);
      end
    end
    
    % Get the parameter names and the values from the input
    nRest = numel(rest);
    if nRest == 1 && iscell(rest{1})
      % addParameters({'a', 'b'})
      names = [names rest{1}];
    elseif ~isempty(rest) && iscellstr(rest)
      % addParameters('a', 'b', 'c')
      names = [names rest];
    elseif nRest == 2 && iscell(rest{1}) && isnumeric(rest{2})
      % addParameters({'a', 'b'}, [1 2])
      names  = [names  rest{1}];
      values = [values num2cell(rest{2})];
    elseif nRest == 2 && iscell(rest{1}) && iscell(rest{2})
      % addParameters({'a', 'b'}, {1 2})
      names  = [names  rest{1}];
      values = [values rest{2}];
    else
      % addParameters('a', 1, 'b', 2, 'c', 3)
      names  = [names  rest(1:2:nRest)];
      values = [values rest(2:2:nRest)];
    end
    
    % Combine input plists and default PLIST
    pls = combine(pls, getDefaultPlist());
    
  end
  
  % Check that we have the same number of param names as the number of values.
  if ~isempty(values) && numel(names) ~= numel(values)
    error('### Please specify for each parameter name [%d] one parameter value [%d]', numel(names), numel(values));
  end
  
  % Decide on a deep copy or a modify
  sm = copy(sm, nargout);
  
  % Loop over smodel objects
  for oo=1:numel(sm)
    
    % If the parameter values are not empty but the 'values' of the object
    % then create default values for the object 'values'.
    if ~isempty(values) && isempty(sm(oo).values)
      sm(oo).values = cell(size(sm(oo).params));
    end
    
    for kk=1:numel(names)
      % Look for the index we have to change
      idx = strcmp(sm(oo).params, names{kk});
      
      if any(idx)
        % Change at the index the value
        if ~isempty(values)
          sm(oo).values{idx} = values{kk};
        end
      else
        % Append the key-value pair
        sm(oo).params = [sm(oo).params  names(kk)];
        if isempty(values) && ~isempty(sm(oo).values)
          % Append a default value for the parameter names if there are
          % already parameter values
          sm(oo).values = [sm(oo).values {[]}];
        elseif ~isempty(values)
          sm(oo).values = [sm(oo).values values(kk)];
        end
      end
    end
    
    if ~callerIsMethod
      plh = pls.pset('params',  names);
      plh.pset('values', values);
      sm(oo).addHistory(getInfo('None'), plh, sm_invars(oo), sm(oo).hist);
    end
  end
  
  % Set output
  varargout = utils.helper.setoutputs(nargout, sm);
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% FUNCTION:    getInfo
%
% DESCRIPTION: 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, mfilename('class'), 'ltpda', utils.const.categories.helper, '$Id: addParameters.m,v 1.1 2011/04/17 15:48:24 ingo Exp $', sets, pl);
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% FUNCTION:    getDefaultPlist
%
% DESCRIPTION: Get Default Plist
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function plout = getDefaultPlist()
  persistent pl;
  if exist('pl', 'var')==0 || isempty(pl)
    pl = buildplist();
  end
  plout = pl;
end

function pl = buildplist()
  pl = plist();
  
  % parameter names
  p = param({'params', 'A cell-array with the parameter names.'}, paramValue.EMPTY_CELL);
  pl.append(p);
  
  % parameter values
  p = param({'values', 'A cell-array with the parameter values.'}, paramValue.EMPTY_CELL);
  pl.append(p);
end

