view m-toolbox/classes/@ltpda_obj/eq.m @ 14:6d43f39633b8 database-connection-manager

Remove unused functions from utils.jmysql
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Mon, 05 Dec 2011 16:20:06 +0100
parents f0afece42f48
children
line wrap: on
line source

% EQ overloads the == operator for ltpda objects.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: EQ overloads the == operator for ltpda objects.
%
%              All fields are checked.
%
% CALL:        result = eq(obj1,obj2)
%              result = eq(obj1,obj2,  exc_list)
%              result = eq(obj1,obj2, 'property1', 'property2')
%              result = eq(obj1,obj2, '<class>/property', '<class>/property')
%
%              With a PLIST
%
%              r = eq(obj1, obj2, plist('Exceptions', {'prop1', 'prop2'}))
%              r = eq(obj1, obj2, plist('Tol', eps(1)))
%              r = eq(obj1, obj2, plist('Exceptions', 'prop', 'Tol', 1e-14))
%
% EXAMPLES:    result = eq(obj1,obj2, 'name', 'created')
%              result = eq(obj1,obj2, '<class>/name')
%
% INPUTS:      obj1, obj2 - Input objects
%              exc_list   - Exception list
%                           List of properties which are not checked.
%
% OUTPUTS:     If the two objects are considered equal, result == true,
%              otherwise, result == false.
%
% <a href="matlab:utils.helper.displayMethodInfo('ltpda_obj', 'eq')">Parameters Description</a>
%
% VERSION:     $Id: eq.m,v 1.35 2011/05/13 10:05:42 hewitson Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function varargout = eq(obj1, obj2, varargin)
  
  % Check if this is a call for parameters
  hh = [{obj1}, {obj2}, varargin];
  if utils.helper.isinfocall(hh{:})
    varargout{1} = getInfo(varargin{1});
    return
  end
  
  import utils.const.*
  
  message = '';
  
  %%%%% Check class
  if ~strcmp(class(obj1), class(obj2))
    utils.helper.msg(msg.PROC1, 'NOT EQUAL: The objects are not from the same class. [%s] <-> [%s]', class(obj1), class(obj2));
    varargout = setOutputs(nargout, false, message);
    return
  end
  
  %%%%% Check length of obj1 and obj2
  if ~all(size(obj1) == size(obj2))
    utils.helper.msg(msg.PROC1, 'NOT EQUAL: The size of the %s-object''s. [%dx%d] <-> [%dx%d]', class(obj1), size(obj1), size(obj2));
    varargout = setOutputs(nargout, false, message);
    return
  end
  
  plin = [];
  exception_list = varargin;
  if ~isempty(varargin) && isa(varargin{1}, 'plist')
    plin = varargin{1};
  end
  
  %%%%% Get the tolerance from a potential existing plist
  if ~isempty(plin) && plin.isparam('tol')
    tol = plin.find('tol');
  else
    dpl = getDefaultPlist();
    tol = dpl.find('tol');
  end
  
  %%%%% Convert a potential existing plist into a exception
  if ~isempty(plin) && plin.isparam('exceptions')
    exception_list = find(plin, 'exceptions');
    if isempty(exception_list)
      exception_list = cell(0);
    elseif ~iscell(exception_list)
      exception_list = cellstr(exception_list);
    end
  end
  
  result = true;
  
  %%%%% for each element in obj1 and obj2
  for jj = 1:numel(obj1)
    
    if isa(obj1, 'ltpda_uoh')
      utils.helper.msg(msg.PROC1, 'testing %s against %s', obj1(jj).name, obj2(jj).name);
    end
    
    fields = fieldnames(obj1(jj));
    
    for ii = 1:length(fields)
      field   = fields{ii};
      
      %%%%% Creates the exception list for the current field.
      %%%%% For example: {'name', 'ao/name'}
      ck_field = {field, sprintf('%s/%s', class(obj1), field)};
      
      % Special case (for the ao- and history-class):
      % Is the field = 'hist', 'inhists' then add 'history' to the exception list.
      %%%%% For example: {'history', 'ao/history'}
      if utils.helper.ismember(field, {'hist', 'inhists'})
        ck_field{end+1} = 'history';
        ck_field{end+1} = sprintf('%s/history', class(obj1));
      elseif strcmp(field, 'val')
        ck_field{end+1} = 'value';
        ck_field{end+1} = sprintf('%s/value', class(obj1));
      end
      
      %%%%% Check field if it is not in the exception list
      if ~(any(utils.helper.ismember(ck_field, exception_list)))
        
        if isa(obj1(jj).(field), 'sym')
          %%%%%%%%%%   The property is a sym-object   %%%%%%%%%%
          if ~eq(obj1(jj).(field), obj2(jj).(field))
            result = false;
            message = display_msg(obj1, jj, field);
            varargout = setOutputs(nargout, result, message);
            return
          end
          
        elseif isa(obj1(jj).(field), 'ltpda_obj')
          %%%%%%%%%%   The property is a ltpda-object   %%%%%%%%%%
          
          %%%%% Check the length of the property
          if length(obj1(jj).(field)) ~= length(obj2(jj).(field))
            utils.helper.msg(msg.PROC1, 'NOT EQUAL: The property [%s] of the object [%s] have not the same size.', field, class(obj1));
            result = false;
            varargout = setOutputs(nargout, result, message);
            return
          end
          
          %%%%% For each element of the property
          for kk = 1:numel(obj1(jj).(field))
            
            if isempty(plin)
              % Command with a list of exceptions
              res = eq(obj1(jj).(field)(kk), obj2(jj).(field)(kk), exception_list{:});
            else
              % Command with a PLIST
              res = eq(obj1(jj).(field)(kk), obj2(jj).(field)(kk), plin);
            end
            
            if ~res
              result = false;
              message = display_msg(obj1, jj, field);
              varargout = setOutputs(nargout, result, message);
              return
            end
          end
          
        elseif isstruct(obj1(jj).(field))
          %%%%%%%%%%   The property is a structure   %%%%%%%%%%
          
          if ~eqstruct(obj1(jj).(field), obj2(jj).(field), exception_list, tol)
            result = false;
            message = display_msg(obj1, jj, field);
            varargout = setOutputs(nargout, result, message);
            return
          end
          
        elseif iscell(obj1(jj).(field))
          %%%%%%%%%%   The property is a cell array   %%%%%%%%%%
          
          if ~eqcell(obj1(jj).(field), obj2(jj).(field), exception_list, tol)
            result = false;
            message = display_msg(obj1, jj, field);
            varargout = setOutputs(nargout, result, message);
            return
          end
          
        elseif isnumeric(obj1(jj).(field))
          %%%%%%%%%%   The property is an elemental MATLAB datatype   %%%%%%%%%%
          res = utils.math.isequal(obj1(jj).(field), obj2(jj).(field), tol);
          if ~res
            result = false;
            message = display_msg(obj1, jj, field);
            varargout = setOutputs(nargout, result, message);
            return
          end
          
        else
          %%%%%%%%%%   The property is an elemental MATLAB datatype   %%%%%%%%%%
          if ~isequal(obj1(jj).(field), obj2(jj).(field))
            result = false;
            message = display_msg(obj1, jj, field);
            varargout = setOutputs(nargout, result, message);
            return
          end
        end
        
      end
    end
    
  end % End loop over objects
  
  varargout = setOutputs(nargout, result, message);
  
end % End eq()

function out = setOutputs(nout, result, message)  
  if nout == 0
    out = {result};
  elseif nout == 1
    out = {result};
  elseif nout == 2
    out = {result, message};
  else
    error('Incorrect outputs for eq()');
  end
  
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                               Local Functions                               %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% FUNCTION:    display_msg
%
% DESCRIPTION: Diesplay a message if the objects are not equal
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function message = display_msg(obj, obj_no, field)
  import utils.const.*
  if numel(obj) > 1
    message = sprintf('NOT EQUAL: %s.%s (%d. object)', class(obj(obj_no)), field, obj_no);
%     utils.helper.msg(msg.PROC1, message);
  else
    message = sprintf('NOT EQUAL: %s.%s', class(obj(obj_no)), field);
%     utils.helper.msg(msg.PROC1, message);
  end
%   bt = warning('query', 'backtrace');
%   warning('backtrace', 'off');
%   warning('LTPDA:eq:ObjectsNotEqual', message);
%   warning('backtrace', bt.state);
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% FUNCTION:    eqstruct
%
% DESCRIPTION: Equal method to compare structures
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function [result, message] = eqstruct(obj1, obj2, exception_list, tol)
  
  import utils.const.*
  
  message = '';
  
  %%%%% Check class
  if ~strcmp(class(obj1), class(obj2))
    utils.helper.msg(msg.PROC1, 'NOT EQUAL: The objects are not from the same class. [%s] <-> [%s]', class(obj1), class(obj2));
    result = false;
    return
  end
  
  %%%%% Check length of obj1 and obj2
  fieldsA = fieldnames(obj1);
  fieldsB = fieldnames(obj2);
  if numel(fieldsA) ~= numel(fieldsB)
    utils.helper.msg(msg.PROC1, 'NOT EQUAL: The size of the %s-object''s. [%d] <-> [%d]', class(obj1), numel(obj1), numel(obj2));
    result = false;
    return
  end
  
  result = true;
  
  for oo = 1:numel(obj1)
    for ii = 1:numel(fieldsA)
      
      if isa(obj1(oo).(fieldsA{ii}), 'ltpda_obj')
        %%%%%%%%%%%%%   LTPDA objects
        if ~eq(obj1(oo).(fieldsA{ii}), obj2(oo).(fieldsA{ii}), plist('exceptions', exception_list, 'tol', tol))
          result = false;
          message = display_msg(obj1, oo, fieldsA{ii});
          return
        end
        
      elseif isstruct(obj1(oo).(fieldsA{ii}))
        %%%%%%%%%%%%%   STRUCTURE
        if ~eqstruct(obj1(oo).(fieldsA{ii}), obj2(oo).(fieldsA{ii}), exception_list, tol)
          result = false;
          message = display_msg(obj1, oo, fieldsA{ii});
          return
        end
        
      elseif iscell(obj1(oo).(fieldsA{ii}))
        %%%%%%%%%%%%%   STRUCTURE
        if ~eqcell(obj1(oo).(fieldsA{ii}), obj2(oo).(fieldsA{ii}), exception_list, tol)
          result = false;
          message = display_msg(obj1, oo, fieldsA{ii});
          return
        end
      elseif isnumeric(obj1(oo).(fieldsA{ii}))
        %%%%%%%%%%%%% Numeric
        res = utils.math.isequal(obj1(oo).(fieldsA{ii}), obj2(oo).(fieldsA{ii}), tol);
        if ~res
          result = false;
          message = display_msg(obj1, ii, 'cell-object');
          return
        end
        
      else
        if ~isequal(obj1(oo).(fieldsA{ii}), obj2(oo).(fieldsA{ii}))
          result = false;
          message = display_msg(obj1, oo, fieldsA{ii});
          return
        end
      end
      
    end % over all fields
  end % over all objects
  
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% FUNCTION:    eqcell
%
% DESCRIPTION: Equal method to compare structures
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function [result, message] = eqcell(obj1, obj2, exception_list, tol)
  
  import utils.const.*
  message = '';
  
  %%%%% Check class
  if ~strcmp(class(obj1), class(obj2))
    utils.helper.msg(msg.PROC1, 'NOT EQUAL: The objects are not from the same class. [%s] <-> [%s]', class(obj1), class(obj2));
    result = false;
    return
  end
  
  %%%%% Check length of obj1 and obj2
  if ~all(size(obj1) == size(obj2))
    utils.helper.msg(msg.PROC1, 'NOT EQUAL: The size of the %s-object''s. [%dx%d] <-> [%dx%d]', class(obj1), size(obj1), size(obj2));
    result = false;
    return
  end
  
  result = true;
  
  for ii = 1:numel(obj1)
    
    if isa(obj1{ii}, 'ltpda_obj')
      %%%%%%%%%%%%%   LTPDA objects
      if ~eq(obj1{ii}, obj2{ii}, plist('exceptions', exception_list, 'tol', tol))
        result = false;
        message = display_msg(obj1, ii, 'cell-object');
        return
      end
      
    elseif isstruct(obj1{ii})
      %%%%%%%%%%%%%   STRUCTURE
      if ~eqstruct(obj1{ii}, obj2{ii}, exception_list, tol)
        result = false;
        message = display_msg(obj1, ii, 'cell-object');
        return
      end
      
    elseif iscell(obj1{ii})
      %%%%%%%%%%%%%   CELL
      if ~eqcell(obj1{ii}, obj2{ii}, exception_list, tol)
        result = false;
        message = display_msg(obj1, ii, 'cell-object');
        return
      end
    elseif isnumeric(obj1{ii})
      %%%%%%%%%%%%% Numeric
      res = utils.math.isequal(obj1{ii}, obj2{ii}, tol);
      if ~res
        result = false;
        message = display_msg(obj1, ii, 'cell-object');
        return
      end
      
    else
      if ~isequal(obj1{ii}, obj2{ii})
        result = false;
        message = display_msg(obj1, ii, 'cell-object');
        return
      end
    end
  end
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, 'ltpda_obj', 'ltpda', utils.const.categories.relop, '$Id: eq.m,v 1.35 2011/05/13 10:05:42 hewitson Exp $', sets, pl);
  ii.setModifier(false);
  ii.setArgsmin(2);
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 plo = buildplist()
  plo = plist();
  
  % Exceptions
  p = param({'Exceptions', 'Test the objects without the given property names'}, paramValue.EMPTY_CELL);
  plo.append(p);
  
  % Tolerance
  p = param({'Tol', 'Test double values with the given tolerance'}, paramValue.DOUBLE_VALUE(eps(1)));
  plo.append(p);
  
end