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.23 2008/02/24 10:15:56 hewitson 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.23 2008/02/24 10:15:56 hewitson Exp $
0032 %
0033 % HISTORY:     30-01-07 M Hewitson
0034 %                Creation
0035 %
0036 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0037 
0038 VERSION  = '$Id: plist.m,v 1.23 2008/02/24 10:15:56 hewitson 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()
0061     pl.name     = 'plist object';
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();
0076 
0077 elseif nargin == 1
0078 
0079   %%%%%%%%%%  p = param(plist)   %%%%%%%%%%
0080   if isa(varargin{1}, 'plist')
0081     pli = varargin{1};
0082 
0083     % Retrieve from repository?
0084     hostname = find(pli, 'hostname');
0085     if ~isempty(hostname)
0086       pl = retrievePlistFromRepository(pli);
0087     else
0088       pl = varargin{1};
0089     end
0090 
0091     % Store the input plist
0092     pl.plist = remove(pli, 'conn');
0093 
0094     %%%%%%%%%%   pl = plist(struct)   %%%%%%%%%%
0095   elseif isstruct(varargin{1})
0096     plstruct = varargin{1};
0097 
0098     pi = plstruct.params;
0099     params = [];
0100     for j=1:length(pi)
0101       if isstruct(pi(j))
0102         params = [params param(pi(j))];
0103       elseif isa(pi(j), 'param')
0104         params = [params pi(j)];
0105       else
0106         error ('### the parameter is not from class param.')
0107       end
0108     end
0109 
0110     pl         = init();
0111     pl.name    = plstruct.name;
0112     pl.created    = plstruct.created;
0113     if isstruct(pl.created)
0114       pl.created = time(pl.created);
0115     end
0116     pl.params  = params;
0117     pl.version = plstruct.version;
0118 
0119     %%%%%%%%%%% From file %%%%%%%%%%%%%%%%%%%%%%%%
0120   elseif ischar(varargin{1})
0121 
0122     filename = varargin{1};
0123     [path, name, ext, vers] = fileparts(filename);
0124     switch ext
0125       case '.mat'
0126         pl = load(filename);
0127         pl = pl.a;
0128       case '.xml'
0129         root_node = xmlread(filename);
0130         pl = ltpda_xmlread(root_node, 'plist');
0131       otherwise
0132         error('### Unknown file type.');
0133     end
0134     %%%%%%%%%%   pl = plist(param)   %%%%%%%%%%
0135   elseif isa(varargin{1}, 'param')
0136     pl        = init();
0137 
0138     %% Use only upper case characters for the 'key'
0139     param_list = varargin{1};
0140     for kk = 1:length(param_list)
0141       param_list(kk).key = upper(param_list(kk).key);
0142     end
0143 
0144     pl.params = param_list;
0145 
0146   else
0147     error ('### unknown arguments to construct a parameter list')
0148   end
0149 elseif nargin == 2
0150   %%%%%%%%%%%   From DATABASE   %%%%%%%%%%%
0151   if isa(varargin{1}, 'database')
0152 
0153     pl = retrieve(varargin{1}, varargin{2:end});
0154 
0155     %%%%%%%%%%   pl = plist('key1', val1)   %%%%%%%%%%
0156   else
0157     pl  = init();
0158     key = upper(varargin{1});
0159     val =       varargin{2};
0160 
0161     pl.params  = param(key, val);
0162   end
0163 
0164 elseif nargin > 2
0165 
0166   %%%%%%%%%%   pl = plist('key1, val1, 'key2', 'val2' , ...)   %%%%%%%%%%
0167   params = [];
0168   argin  = varargin;
0169   while length(argin) >= 2
0170     key = upper(argin{1});
0171     val =       argin{2};
0172     argin = argin(3:end);
0173 
0174     if ~ischar(key)
0175       error('### the key ''%s'' must be a character string', key)
0176     end
0177 
0178     found = 0;
0179     for ii = 1:length(params)
0180       if strcmpi(params(ii).key, key)
0181         % If the key exist in pl then do nothing, otherwise add the key value pair.
0182         warning('### Do not use the same key twice.');
0183         found = 1;
0184       end
0185     end
0186 
0187     if found == 0
0188       params = [params param(key, val)];
0189     end
0190 
0191   end
0192 
0193   pl         = init();
0194   pl.params  = params;
0195 
0196 else
0197   error('### Unknown number of constructor arguments.');
0198 end
0199 
0200 %--------------------------------------------------------------------------
0201 % Retrieve a plist from the repository
0202 %
0203   function       pls = retrievePlistFromRepository(pl, VERSION, ALGONAME)
0204     
0205     dpl = getDefaultPlist('From Repository');
0206     pl  = combine(pl, dpl);
0207 
0208     % Get parameters
0209     conn = find(pl, 'conn');
0210     hostname = find(pl, 'hostname');
0211     database = find(pl, 'database');
0212     ids      = find(pl, 'id');
0213 
0214     % do we have a connection?
0215     closeConn = 0;
0216     if isempty(conn)
0217       closeConn = 1;
0218       % Connect to repository
0219       conn = mysql_connect(hostname, database);
0220     end
0221     if ~isa(conn, 'database')
0222       error('### connection failed.');
0223     end
0224     % Get each ID
0225     Nids = length(ids);
0226     pls  = [];
0227     for kk=1:Nids
0228 
0229       %---- This id
0230       id = ids(kk);
0231       disp(sprintf('  - retrieving ID %d', id));
0232 
0233       %---- check ID object type
0234       tt = mysql_getObjType(conn, id);
0235       %---- If this is a plist
0236       if strcmp(tt, mfilename)
0237         %---- call database constructor
0238         a = ltpda_obj_retrieve(conn, id);
0239         %---- Add to output array
0240         pls = [pls a];
0241       else
0242         warning('    !skipping ID %d, type %s', id, tt);
0243       end
0244 
0245     end
0246 
0247     % close connection
0248     if closeConn
0249       close(conn);
0250     end
0251   end
0252 
0253 %--------------------------------------------------------------------------
0254 % Default Parameter Lists
0255 %
0256   function out = getDefaultPlist(varargin)
0257 
0258     out = plist('hostname', 'localhost', 'database', 'ltpda', 'ID', []);
0259 
0260   end
0261 end
0262 % END

Generated on Fri 07-Mar-2008 15:46:43 by m2html © 2003