view m-toolbox/classes/@ao/search.m @ 38:3aef676a1b20 database-connection-manager

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

% SEARCH selects AOs that match the given name.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: SEARCH selects AOs that match the given name using a regular
%              expression (help regexp).
%
% CALL:        b = search(a, 'foo')  % get all AOs from <a> called 'foo'
%              b = search(a, 'foo*') % get all AOs from <a> with a name like 'foo'
%              b = search(a, pl)
%
%
% This function returns the handles of the AOs that match the regular
% expression. No object copying is done.
%
% <a href="matlab:utils.helper.displayMethodInfo('ao', 'search')">Parameters Description</a>
%
% VERSION:     $Id: search.m,v 1.15 2011/04/08 08:56:11 hewitson Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function varargout = search(varargin)

  % 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);

  % Check output
  if nargout == 0
    error('### The search method can not be used as a modifier.');
  end
  
  % Collect input variable names
  in_names = cell(size(varargin));
  for ii = 1:nargin,in_names{ii} = inputname(ii);end

  % Collect all AOs and plists
  [as, ao_invars, rest] = utils.helper.collect_objects(varargin(:), 'ao', in_names);
  [pl, pl_invars, rest] = utils.helper.collect_objects(rest(:), 'plist', in_names);

  % Combine PLISTs
  pl = combine(pl, getDefaultPlist);
  
  % Decide on a deep copy or a modify
  as = copy(as, nargout);
  
  % Build a cell array of the input AO names
  aonames = {};
  for j=1:numel(as)
    aonames = [aonames {as(j).name}];
  end

  %----- Get expression
  % first look in direct inputs
  exp = '';
  for j=1:numel(rest)
    if ischar(rest{j})
      exp = rest{j};
      break;
    end
  end
  % then in plist
  if isempty(exp)
    exp = find(pl, 'regexp');
  end
  if isempty(exp)
    error('### Please specify an expression to match.');
  end

  % Run regexp
  exact = pl.find('exact');
  if exact
    res = strcmp(aonames, exp);
  else
    res = regexp(aonames, exp);
  end
  
  % Make sure that the search expression is in the history PLIST
  pl.pset('regexp', exp);
  
  % Get indices
  bs = [];
  for j=1:numel(res)
    if exact
      if res(j)
        as(j).addHistory(getInfo('None'), pl, ao_invars(j), as(j).hist);
        bs = [bs as(j)];
      end
    else
      if ~isempty(res{j})
        % Append history
        as(j).addHistory(getInfo('None'), pl, ao_invars(j), as(j).hist);
        bs = [bs as(j)];
      end
    end
  end
  
  % Set output
  if nargout == numel(bs)
    % List of outputs
    for ii = 1:numel(bs)
      varargout{ii} = bs(ii);
    end
  else
    % Single output
    varargout{1} = bs;
  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, 'ao', 'ltpda', utils.const.categories.helper, '$Id: search.m,v 1.15 2011/04/08 08:56:11 hewitson Exp $', sets, pl);
end

%--------------------------------------------------------------------------
% 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();
  
  % regexp
  p = param({'regexp', 'A string specifying the regular expression'}, paramValue.EMPTY_STRING);
  pl.append(p);
  
  % exact
  p = param({'exact', 'A boolean specifying to look for an exact match or not'}, paramValue.FALSE_TRUE);
  pl.append(p);
   
end

% PARAMETERS:  'regexp' - a string specifying the regular expression