Home > classes > @plist > plist.m

plist

PURPOSE ^

PLIST Plist class object constructor.

SYNOPSIS ^

function pl = plist(varargin)

DESCRIPTION ^

 PLIST Plist class object constructor.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 DESCRIPTION: PLIST Plist class object constructor.

 CONSTRUCTORS:

       pl = plist()                            - create an empty plist object.
       pl = plist(p)                           - create a plist with elements p
                                                 where p is an array of param objects.
       pl = plist('key1, val1, 'key2', 'val2') - create a plist with the
                                                 key/values pairs
       pl = plist(pl)                          - create a plist from a
                                                 plist.

 PARAMETERS:

   If no recognised parameters are found in the input plist, the input
   plist is simply returned. This is the copy constructor.

   'Hostname' - construct a plist by retrieving it from an LTPDA repository
                specified by the given hostname. Only those objects which
                are plists are returned.
                Additional parameters:
                'Database'   - The database name [default: 'ltpda']
                'ID'         - A vector of object IDs.


 VERSION:     $Id: plist.m,v 1.27 2008/03/17 09:28:51 mauro Exp $

 HISTORY:     30-01-07 M Hewitson
                Creation

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function pl = plist(varargin)
0002 % PLIST Plist class object constructor.
0003 %
0004 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0005 %
0006 % DESCRIPTION: PLIST Plist class object constructor.
0007 %
0008 % CONSTRUCTORS:
0009 %
0010 %       pl = plist()                            - create an empty plist object.
0011 %       pl = plist(p)                           - create a plist with elements p
0012 %                                                 where p is an array of param objects.
0013 %       pl = plist('key1, val1, 'key2', 'val2') - create a plist with the
0014 %                                                 key/values pairs
0015 %       pl = plist(pl)                          - create a plist from a
0016 %                                                 plist.
0017 %
0018 % PARAMETERS:
0019 %
0020 %   If no recognised parameters are found in the input plist, the input
0021 %   plist is simply returned. This is the copy constructor.
0022 %
0023 %   'Hostname' - construct a plist by retrieving it from an LTPDA repository
0024 %                specified by the given hostname. Only those objects which
0025 %                are plists are returned.
0026 %                Additional parameters:
0027 %                'Database'   - The database name [default: 'ltpda']
0028 %                'ID'         - A vector of object IDs.
0029 %
0030 %
0031 % VERSION:     $Id: plist.m,v 1.27 2008/03/17 09:28:51 mauro Exp $
0032 %
0033 % HISTORY:     30-01-07 M Hewitson
0034 %                Creation
0035 %
0036 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0037 
0038 VERSION  = '$Id: plist.m,v 1.27 2008/03/17 09:28:51 mauro Exp $';
0039 CATEGORY = 'Constructor';
0040 
0041 %%%%%   Check if this is a call for parameters   %%%%%
0042 if nargin == 2
0043   if isa(varargin{1}, 'plist') && ischar(varargin{2})
0044     in = char(varargin{2});
0045     if strcmp(in, 'Params')
0046       pl = getDefaultPlist();
0047       return
0048     elseif strcmp(in, 'Version')
0049       pl = VERSION;
0050       return
0051     elseif strcmp(varargin{2}, 'Category')
0052       pl = CATEGORY;
0053       return
0054     end
0055   end
0056 end
0057 
0058 %%%%%%%%%%%%%%%%%%%%   define parameter properties list   %%%%%%%%%%%%%%%%%%%%%
0059 
0060   function pl = init(version)
0061     pl.name     = 'None';
0062     pl.params   = [];
0063     pl.version  = version;
0064     pl.created  = time;
0065     pl.plist    = '';
0066     pl = class(pl, 'plist');
0067   end
0068 
0069 %%%%%%%%%%%%%%%%%%%%%%   Create parameter list object   %%%%%%%%%%%%%%%%%%%%%%%
0070 
0071 %%%%%%%%%%   pl = plist()   %%%%%%%%%%
0072 % create default parameter list object
0073 if nargin == 0
0074 
0075   pl = init(VERSION);
0076 
0077 elseif nargin == 1
0078 
0079   %%%%%%%%%%  p = param(plist)   %%%%%%%%%%
0080   if isa(varargin{1}, 'plist')
0081     pli = varargin{1};
0082 
0083     %% is the plist is empty then return an empty plist object
0084     if nparams(varargin{1}) == 0
0085       pl = init(VERSION);
0086       pl.plist = varargin{1};
0087     else
0088 
0089       % Retrieve from repository?
0090       hostname = find(pli, 'hostname');
0091       if ~isempty(hostname)
0092         pl = retrievePlistFromRepository(pli);
0093         % Store the input plist
0094         pl.plist = remove(pli, 'conn');
0095       else
0096         pl = varargin{1};
0097         % Store the input plist
0098         pl.plist = pli;
0099       end
0100 
0101       % Store the input plist
0102       pl.plist = remove(pli, 'conn');
0103 
0104     end
0105 
0106     %%%%%%%%%%   pl = plist(struct)   %%%%%%%%%%
0107   elseif isstruct(varargin{1})
0108     plstruct = varargin{1};
0109 
0110     pi = plstruct.params;
0111     params = [];
0112     for j=1:length(pi)
0113       if isstruct(pi(j))
0114         params = [params param(pi(j))];
0115       elseif isa(pi(j), 'param')
0116         params = [params pi(j)];
0117       else
0118         error ('### the parameter is not from class param.')
0119       end
0120     end
0121 
0122     pl         = init(VERSION);
0123     pl.name    = plstruct.name;
0124     pl.created    = plstruct.created;
0125     if isstruct(pl.created)
0126       pl.created = time(pl.created);
0127     end
0128     pl.params  = params;
0129     pl.version = plstruct.version;
0130 
0131     %%%%%%%%%%% From file %%%%%%%%%%%%%%%%%%%%%%%%
0132   elseif ischar(varargin{1})
0133 
0134     filename = varargin{1};
0135     [path, name, ext, vers] = fileparts(filename);
0136     switch ext
0137       case '.mat'
0138         pl = load(filename);
0139         pl = pl.a;
0140       case '.xml'
0141         root_node = xmlread(filename);
0142         pl = ltpda_xmlread(root_node, 'plist');
0143       otherwise
0144         error('### Unknown file type.');
0145     end
0146     %%%%%%%%%%   pl = plist(param)   %%%%%%%%%%
0147   elseif isa(varargin{1}, 'param')
0148     pl        = init(VERSION);
0149 
0150     %% Use only upper case characters for the 'key'
0151     param_list = varargin{1};
0152     for kk = 1:length(param_list)
0153       param_list(kk).key = upper(param_list(kk).key);
0154     end
0155 
0156     pl.params = param_list;
0157 
0158   else
0159     error ('### unknown arguments to construct a parameter list')
0160   end
0161 elseif nargin == 2
0162   %%%%%%%%%%%   From DATABASE   %%%%%%%%%%%
0163   if isa(varargin{1}, 'database')
0164 
0165     pl = retrieve(varargin{1}, varargin{2:end});
0166 
0167     %%%%%%%%%%   pl = plist('key1', val1)   %%%%%%%%%%
0168   else
0169     pl  = init(VERSION);
0170     key = upper(varargin{1});
0171     val =       varargin{2};
0172 
0173     pl.params  = param(key, val);
0174   end
0175 
0176 elseif nargin > 2
0177 
0178   %%%%%%%%%%   pl = plist('key1, val1, 'key2', 'val2' , ...)   %%%%%%%%%%
0179   params = [];
0180   argin  = varargin;
0181   while length(argin) >= 2
0182     key = upper(argin{1});
0183     val =       argin{2};
0184     argin = argin(3:end);
0185 
0186     if ~ischar(key)
0187       error('### the key ''%s'' must be a character string', key)
0188     end
0189 
0190     found = 0;
0191     for ii = 1:length(params)
0192       if strcmpi(params(ii).key, key)
0193         % If the key exist in pl then do nothing, otherwise add the key value pair.
0194         warning('### Do not use the same key twice.');
0195         found = 1;
0196       end
0197     end
0198 
0199     if found == 0
0200       params = [params param(key, val)];
0201     end
0202 
0203   end
0204 
0205   pl         = init(VERSION);
0206   pl.params  = params;
0207 
0208 else
0209   error('### Unknown number of constructor arguments.');
0210 end
0211 
0212 %--------------------------------------------------------------------------
0213 % Retrieve a plist from the repository
0214 %
0215   function       pls = retrievePlistFromRepository(pl, VERSION, ALGONAME)
0216 
0217     dpl = getDefaultPlist('From Repository');
0218     pl  = combine(pl, dpl);
0219 
0220     % Get parameters
0221     conn = find(pl, 'conn');
0222     hostname = find(pl, 'hostname');
0223     database = find(pl, 'database');
0224     ids      = find(pl, 'id');
0225 
0226     % do we have a connection?
0227     closeConn = 0;
0228     if isempty(conn)
0229       closeConn = 1;
0230       % Connect to repository
0231       conn = mysql_connect(hostname, database);
0232     end
0233     if ~isa(conn, 'database')
0234       error('### connection failed.');
0235     end
0236     % Get each ID
0237     Nids = length(ids);
0238     pls  = [];
0239     for kk=1:Nids
0240 
0241       %---- This id
0242       id = ids(kk);
0243       disp(sprintf('  - retrieving ID %d', id));
0244 
0245       %---- check ID object type
0246       tt = mysql_getObjType(conn, id);
0247       %---- If this is a plist
0248       if strcmp(tt, mfilename)
0249         %---- call database constructor
0250         a = ltpda_obj_retrieve(conn, id);
0251         %---- Add to output array
0252         pls = [pls a];
0253       else
0254         warning('    !skipping ID %d, type %s', id, tt);
0255       end
0256 
0257     end
0258 
0259     % close connection
0260     if closeConn
0261       close(conn);
0262     end
0263   end
0264 
0265 %--------------------------------------------------------------------------
0266 % Default Parameter Lists
0267 %
0268   function out = getDefaultPlist(varargin)
0269 
0270     out = plist('hostname', 'localhost', 'database', 'ltpda', 'ID', []);
0271 
0272   end
0273 end
0274 % END

Generated on Mon 31-Mar-2008 13:54:54 by m2html © 2003