% STATTEST constructor for stattest class.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: STATTEST constructor for stattest class.
%
% CONSTRUCTOR:
%
%     st = stattest()               - creates an empty stattest object
%     st = stattest(ao,ao,...)      - creates a stattest with the given data
%     st = stattest(ao,ao,..., pl)  - creates a stattest with the given data
%
% <a href="matlab:utils.helper.displayMethodInfo('stattest', 'stattest')">Parameters Description</a>
%
% VERSION:     $Id: stattest.m,v 1.6 2011/04/08 08:56:38 hewitson Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

classdef stattest < ltpda_uoh
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %                            Property definition                            %
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
  %---------- Public (read/write) Properties  ----------
  properties
  end
  
  %---------- Protected read-only Properties ----------
  properties (SetAccess = protected)
    data    = {}; % The data vector of ltpda user objects
    result  = false; % Store the result of the last test performed
    pvalue  = [];
  end
  
  %---------- Private Properties ----------
  properties (GetAccess = protected, SetAccess = protected)
  end
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %                          Check property setting                           %
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
  methods
    %--- expr
    function obj = set.data(obj, val)
      if isempty(val)
        obj.data = {};
        return;
      end
      if numel(val)==1 && isa(val, 'ltpda_uoh')
        obj.data = val;
        return;
      end
      if iscell(val)
        % check each element
        newdata = [];
        for ll=1:numel(val)
          if isa(val{ll}, 'ltpda_uoh')
            newdata = [newdata val(ll)];
          end
        end
        obj.data = newdata;
        return;
      end
      error('### The data array should contain only ltpda user objects');
    end
    %--- params
    function obj = set.result(obj, val)
      if ~islogical(val)
        error('### The result should be a logical (true/false)');
      end
      obj.result = val;
    end
    %--- values
    function obj = set.pvalue(obj, val)
      if ~isnumeric(val)
        error('### The pvalue must be numeric');
      end
      obj.pvalue = val;
    end
  end
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %                                Constructor                                %
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
  methods
    function obj = stattest(varargin)
      
      import utils.const.*
      utils.helper.msg(msg.OMNAME, 'running %s/%s', mfilename('class'), mfilename);
      
      % Collect all stattest objects
      [sts, invars, rest] = utils.helper.collect_objects(varargin(:), 'stattest');
      
      if isempty(rest) && ~isempty(sts)
        % Do copy constructor and return
        utils.helper.msg(msg.OPROC1, 'copy constructor');
        obj = copy(sts, 1);
        for kk=1:numel(obj)
          obj(kk).addHistory(smodel.getInfo('stattest', 'None'), [], [], obj(kk).hist);
        end
        return
      end
      
      switch nargin
        case 0
          %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
          %%%%%%%%%%%%%%%%%%%%%%%%%%%%   no input   %%%%%%%%%%%%%%%%%%%%%%%%%%%*
          %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
          utils.helper.msg(msg.OPROC1, 'empty constructor');
          obj.addHistory(stattest.getInfo('stattest', 'None'), plist(), [], []);
          
        case 1
          %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
          %%%%%%%%%%%%%%%%%%%%%%%%%%%   One input   %%%%%%%%%%%%%%%%%%%%%%%%%%%
          %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
          if ischar(varargin{1})
            %%%%%%%%%%   mdl = stattest('foo.mat')   %%%%%%%%%%
            %%%%%%%%%%   mdl = stattest('foo.xml')   %%%%%%%%%%
            
            % Is this a file?
            [path, name, ext] = fileparts(varargin{1});
            
            if ismember(ext, {'.xml', '.mat', '.txt', '.dat', '.fil'})
              utils.helper.msg(msg.OPROC1, 'constructing from file %s', varargin{1});
              obj = obj.fromFile(varargin{1});
            else
              error('Unknown file extension %s', ext);
            end
            
          elseif isa(varargin{1}, 'ltpda_uoh')
            %%%%%%%%%%   st = stattest(ltpda_objects)   %%%%%%%%%%
            utils.helper.msg(msg.OPROC1, 'constructing from ltpda objects');
            obj.data = varargin{1};
            
          elseif iscell(varargin{1})
            %%%%%%%%%% st = stattest({a,b,c}) %%%%%%%%%%
            
            pl = plist('data', varargin{1});
            obj = obj.fromData(pl);
            
            
          elseif isa(varargin{1}, 'plist')
            %%%%%%%%%%  st = stattest(plist-object)   %%%%%%%%%%
            
            pl = varargin{1};
            
            if pl.isparam('data')
              utils.helper.msg(msg.OPROC1, 'constructing from data');
              obj = obj.fromData(pl);
              
            else
              obj.setProperties(pl);
              obj.addHistory(stattest.getInfo('stattest', 'None'), pl, [], []);
            end
            
          else
            error('### Unknown single argument constructor.');
          end
        case 2
          
          if isa(varargin{1}, 'org.apache.xerces.dom.DeferredElementImpl') && ...
              isa(varargin{2}, 'history')
            %%%%%%%%%%   obj = ao(DOM node, history-objects)   %%%%%%%%%%
            obj = fromDom(obj, varargin{1}, varargin{2});
          else
            error('### Unknown constructor with two inputs');
          end
        otherwise
          [objs, invars, rest] = utils.helper.collect_objects(varargin, 'ltpda_uoh');
          [pls, invars, rest] = utils.helper.collect_objects(varargin, 'plist');
          pl = combine(pls, stattest.getDefaultPlist('From Data'));
          pl.pset('data', objs);
          obj = obj.fromData(pl);
          
      end
      
    end % End constructor
    
  end
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %                             Methods (protected)                           %
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  methods (Access = protected)
  end
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %                            Methods (static)                               %
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  methods (Static)
    
    function sts = getBuiltInModels(varargin)
      sts = ltpda_uo.getBuiltInModels('stattest');
    end
    
    function out = VEROUT()
      out = '$Id: stattest.m,v 1.6 2011/04/08 08:56:38 hewitson Exp $';
    end
    
    function ii = getInfo(varargin)
      ii = utils.helper.generic_getInfo(varargin{:}, 'stattest');
    end
    
    function out = SETS()
      out = [SETS@ltpda_uoh, ...
        {'Default'}, ...
        {'From Data'}...
        ];
    end
    
    
    function plout = getDefaultPlist(set)
      persistent pl;
      persistent lastset;
      if exist('pl', 'var')==0 || isempty(pl) || ~strcmp(lastset, set)
        pl = stattest.buildplist(set);
        lastset = set;
      end
      plout = pl;
    end
    
    function out = buildplist(set)
      
      if ~utils.helper.ismember(lower(stattest.SETS), lower(set))
        error('### Unknown set [%s]', set);
      end
      
      out = plist();
      out = stattest.addGlobalKeys(out);
      out = buildplist@ltpda_uoh(out, set);
      
      switch lower(set)
        case 'from data'
          
          % Expression
          p = param({'data','Array of LTPDA user objects to set as the data.'}, paramValue.EMPTY_DOUBLE);
          out.append(p);
          
      end
    end % function out = getDefaultPlist(varargin)
    
    function obj = initObjectWithSize(n,m)
      obj = stattest.newarray([n m]);
      for ii = 1:numel(obj)
        obj(ii).UUID = char(java.util.UUID.randomUUID);
      end
    end
    
  end % End static methods
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %                         Methods (static, private)                         %
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
  methods (Static, Access=private)
    varargout = elementOp(varargin)
  end % End static, private methods
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %                         Methods (static, hidden)                          %
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
  methods (Static = true, Hidden = true)
    varargout = loadobj(varargin)
    varargout = update_struct(varargin);
  end
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %                             Methods (public)                              %
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  methods
    varargout = char(varargin)
    varargout = display(varargin)
    varargout = copy(varargin)
    
    varargout = setData(varargin)
    
  end
  
  methods (Hidden = true)
    varargout = attachToDom(varargin)
  end
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %                            Methods (protected)                            %
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  methods (Access = protected)
    varargout = fromStruct(varargin)
    varargout = fromDom(varargin)
  end
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %                           Methods (private)                               %
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  methods (Access = private)
  end
  
end % End classdef

