view m-toolbox/classes/@smodel/setAliases.m @ 30:317b5f447f3e database-connection-manager

Update workspaceBrowser
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Mon, 05 Dec 2011 16:20:06 +0100
parents f0afece42f48
children
line wrap: on
line source

% SETALIASES Set the key-value pairs to the alias-names and alias-values
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: SETALIASES Set the key-value pairs to the alias-names and
%              alias-values. This method will set the key-value pairs to
%              the properties 'aliasNames' and 'aliasValues'. This method
%              will not keep the existing aliases.
%
% CALL:        obj = obj.setAliases('name', 'value');
%              obj = obj.setAliases(key-value pairs);
%              obj = obj.setAliases(plist);
%              obj = obj.setAliases(two cell-arrays);
%
% 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
%                            'names' and 'values'. The values can be a
%                            single value or a cell array with multiple
%                            values. The number of 'names' and 'values'
%                            must be the same.
%              cell-arrays - Two cell-arrays with the first of the 'names'
%                            and the second with the 'values'.
%
% <a href="matlab:utils.helper.displayMethodInfo('smodel', 'setAliases')">Parameters Description</a>
%
% VERSION:     $Id: setAliases.m,v 1.2 2011/04/28 19:50:57 mauro Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function varargout = setAliases(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 alias-names
    values = varargin{3}; % call-array with alias-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 'names' and 'values' then use also
    %%% the
    if length(pls) == 1 && isa(pls, 'plist') && isparam(pls, 'names') && isparam(pls, 'values')
      names  = find(pls, 'names');
      values = find(pls, 'values');
      
      % Make sure that the names and the values are cell-arrays
      if ~iscell(names)
        names = cellstr(names);
      end
      if ~iscell(values)
        values = num2cell(values);
      end
      
    end
    
    % Check if we have two cell-array for the 'names' and 'values'
    if numel(rest) == 2 && iscell(rest{1}) && iscell(rest{2})
      names  = [names  rest{1}];
      values = [values rest{2}];
    else
      
      % Check for key-value pairs
      if mod(numel(rest), 2) ~= 0
        error('### Please specify for each alias-name a alias-value');
      end
      nrest = numel(rest);
      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 names as the number of values.
  if numel(names) ~= numel(values)
    error('### Please specify for each alias name [%d] one alias value [%d]', numel(names), numel(values));
  end
  
  % Decide on a deep copy or a modify
  sm = copy(sm, nargout);
  
  % Loop over smodel objects
  for jj = 1:numel(sm)
    
    % Append the alias key-value pair
    sm(jj).aliasNames  = names;
    sm(jj).aliasValues = values;
    
    if ~callerIsMethod
      plh = pls.pset('names', names);
      plh.pset('values', values);
      sm(jj).addHistory(getInfo('None'), plh, sm_invars(jj), sm(jj).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: setAliases.m,v 1.2 2011/04/28 19:50:57 mauro Exp $', sets, pl);
end

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

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

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