Ignore:
Timestamp:
11/13/12 08:25:29 (12 years ago)
Author:
jschierm
Message:

NEW: Preliminary version of organizer.py (plus Matlab cosmetic changes).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/m/classes/organizer.m

    r13646 r13946  
    1010%      org = organizer(varargin)
    1111%
    12 %
    1312%   Examples:
    1413%      org = organizer('repository','Models/','prefix','AGU2015','steps',0);  %build an empty organizer object with a given repository
     
    1615classdef organizer
    1716    properties (SetAccess=private)
    18                  % {{{
    19                  currentstep   =0;
    20          end
     17                % {{{
     18                currentstep   =0;
     19        end
    2120    properties (SetAccess=public)
    22                  repository    ='./';
    23                  prefix        ='model.';
    24                  trunkprefix   ='';
    25                  steps         =[];
    26                  requestedsteps=[0];
    27                  %}}}
    28          end
    29          methods
    30                  function org=organizer(varargin) % {{{
     21                repository    ='./';
     22                prefix        ='model.';
     23                trunkprefix   ='';
     24                steps         =[];
     25                requestedsteps=[0];
     26                %}}}
     27        end
     28        methods
     29                function org=organizer(varargin) % {{{
    3130
    32                          %process options
    33                          options=pairoptions(varargin{:});
     31                        %process options
     32                        options=pairoptions(varargin{:});
    3433
    35                          %Get prefix
    36                          prefix=getfieldvalue(options,'prefix','model.');
    37                          if ~ischar(prefix),                            error('prefix is not a string'); end
    38                          if ~strcmp(regexprep(prefix,'\s+',''),prefix), error('prefix should not have any white space'); end
    39                          org.prefix=prefix;
     34                        %Get prefix
     35                        prefix=getfieldvalue(options,'prefix','model.');
     36                        if ~ischar(prefix),                            error('prefix is not a string'); end
     37                        if ~strcmp(regexprep(prefix,'\s+',''),prefix), error('prefix should not have any white space'); end
     38                        org.prefix=prefix;
    4039
    41                          %Get repository
    42                          repository=getfieldvalue(options,'repository','./');
    43                          if ~ischar(repository),        error('repository is not a string'); end
    44                          if exist(repository,'dir')~=7, error(['Directory ' repository ' not found']), end
    45                          org.repository=repository;
     40                        %Get repository
     41                        repository=getfieldvalue(options,'repository','./');
     42                        if ~ischar(repository),        error('repository is not a string'); end
     43                        if exist(repository,'dir')~=7, error(['Directory ' repository ' not found']), end
     44                        org.repository=repository;
    4645
    47                          %Get repository
    48                          org.requestedsteps=getfieldvalue(options,'steps',0);
     46                        %Get steps
     47                        org.requestedsteps=getfieldvalue(options,'steps',0);
    4948
    50                          %Get trunk prefix (only if provided by user)
    51                          if exist(options,'trunkprefix'),
    52                                  trunkprefix=getfieldvalue(options,'trunkprefix','');
    53                                  if ~ischar(trunkprefix),                                 error('trunkprefix is not a string'); end
    54                                  if ~strcmp(regexprep(trunkprefix,'\s+',''),trunkprefix), error('trunkprefix should not have any white space'); end
    55                                  org.trunkprefix=trunkprefix;
    56                          end
    57                  end
    58                  %}}}
    59                  function disp(org) % {{{
    60                          disp(sprintf('   Repository: ''%s''',org.repository));
    61                          disp(sprintf('   Prefix:     ''%s''',org.prefix));
    62                          if isempty(org.steps)
    63                                  disp('   no step');
    64                          else
    65                                  for i=1:length(org.steps),
    66                                          disp(sprintf('   step #%2i: ''%s''',org.steps(i).id,org.steps(i).string));
    67                                  end
    68                          end
    69                  end
    70                  %}}}
    71                  function md=load(org,string),% {{{
     49                        %Get trunk prefix (only if provided by user)
     50                        if exist(options,'trunkprefix'),
     51                                trunkprefix=getfieldvalue(options,'trunkprefix','');
     52                                if ~ischar(trunkprefix),                                 error('trunkprefix is not a string'); end
     53                                if ~strcmp(regexprep(trunkprefix,'\s+',''),trunkprefix), error('trunkprefix should not have any white space'); end
     54                                org.trunkprefix=trunkprefix;
     55                        end
     56                end
     57                %}}}
     58                function disp(org) % {{{
     59                        disp(sprintf('   Repository: ''%s''',org.repository));
     60                        disp(sprintf('   Prefix:     ''%s''',org.prefix));
     61                        if isempty(org.steps)
     62                                disp('   no step');
     63                        else
     64                                for i=1:length(org.steps),
     65                                        disp(sprintf('   step #%2i: ''%s''',org.steps(i).id,org.steps(i).string));
     66                                end
     67                        end
     68                end
     69                %}}}
     70                function md=load(org,string),% {{{
    7271
    73                          %Get model path
    74                          if ~ischar(string), error('argument provided is not a string'); end
    75                          path=[org.repository '/' org.prefix string];
     72                        %Get model path
     73                        if ~ischar(string), error('argument provided is not a string'); end
     74                        path=[org.repository '/' org.prefix string];
    7675
    77                          %figure out if the model is there
    78                          if exist(path,'file'),
    79                                  struc=load(path,'-mat');
    80                                  name=char(fieldnames(struc));
    81                                  md=struc.(name);
    82                                  if nargout,
    83                                          varargout{1}=md;
    84                                  end
    85                          else
    86                                  error(['Could not find ' path ]);
    87                          end
    88                  end%}}}
    89                  function md=loadmodel(org,string),% {{{
     76                        %figure out if the model is there
     77                        if exist(path,'file'),
     78                                struc=load(path,'-mat');
     79                                name=char(fieldnames(struc));
     80                                md=struc.(name);
     81                                if nargout,
     82                                        varargout{1}=md;
     83                                end
     84                        else
     85                                error(['Could not find ' path ]);
     86                        end
     87                end%}}}
     88                function md=loadmodel(org,string),% {{{
    9089
    91                          %Get model path
    92                          if ~ischar(string), error('argument provided is not a string'); end
    93                          path=[org.repository '/' org.prefix string];
     90                        %Get model path
     91                        if ~ischar(string), error('argument provided is not a string'); end
     92                        path=[org.repository '/' org.prefix string];
    9493
    95                          %figure out if the model is there, otherwise, we have to use the default path supplied by user.
    96                          if exist(path,'file') | exist([path '.mat'],'file'),
    97                                  md=loadmodel(path);
    98                                  return;
    99                          end
     94                        %figure out if the model is there, otherwise, we have to use the default path supplied by user.
     95                        if exist(path,'file') | exist([path '.mat'],'file'),
     96                                md=loadmodel(path);
     97                                return;
     98                        end
    10099
    101                          %If we are here, the model has not been found. Try trunk prefix if provided
    102                          if ~isempty(org.trunkprefix),
    103                                  path2=[org.repository '/' org.trunkprefix string];
    104                                  if ~exist(path2,'file'),
    105                                          error(['Could neither find ' path ', nor ' path2]);
    106                                  else
    107                                          disp(['--> Branching ' org.prefix ' from trunk ' org.trunkprefix]);
    108                                          md=loadmodel(path2);
    109                                          return;
    110                                  end
    111                          else
    112                                  error(['Could not find ' path ]);
    113                          end
    114                  end%}}}
    115                  function bool=perform(org,string) % {{{
     100                        %If we are here, the model has not been found. Try trunk prefix if provided
     101                        if ~isempty(org.trunkprefix),
     102                                path2=[org.repository '/' org.trunkprefix string];
     103                                if ~exist(path2,'file'),
     104                                        error(['Could find neither ' path ', nor ' path2]);
     105                                else
     106                                        disp(['--> Branching ' org.prefix ' from trunk ' org.trunkprefix]);
     107                                        md=loadmodel(path2);
     108                                        return;
     109                                end
     110                        else
     111                                error(['Could not find ' path ]);
     112                        end
     113                end%}}}
     114                function bool=perform(org,string) % {{{
    116115
    117                          bool=false;
     116                        bool=false;
    118117
    119                          %Some checks
    120                          if ~ischar(string),                            error('Step provided should be a string'); end
    121                          if ~strcmp(regexprep(string,'\s+',''),string), error('Step provided should not have any white space'); end
    122                          if (org.currentstep>0 & ismember({string},{org.steps.string}))
    123                                  error(['Step ' string ' already present. Change name']);
    124                          end
     118                        %Some checks
     119                        if ~ischar(string),                            error('Step provided should be a string'); end
     120                        if ~strcmp(regexprep(string,'\s+',''),string), error('Step provided should not have any white space'); end
     121                        if (org.currentstep>0 & ismember({string},{org.steps.string}))
     122                                error(['Step ' string ' already present. Change name']);
     123                        end
    125124
    126                          %Add step
    127                          org.steps(end+1).id=length(org.steps)+1;
    128                          org.steps(end).string=string;
    129                          org.currentstep=org.currentstep+1;
     125                        %Add step
     126                        org.steps(end+1).id=length(org.steps)+1;
     127                        org.steps(end).string=string;
     128                        org.currentstep=org.currentstep+1;
    130129
    131                          %if requestedsteps = 0, print all steps in org
    132                          if any(org.requestedsteps==0),
    133                                  if org.currentstep==1,
    134                                          disp(sprintf('   prefix: %s',org.prefix));
    135                                  end
    136                                  disp(sprintf('   step #%2i : %s',org.steps(org.currentstep).id,org.steps(org.currentstep).string));
    137                          end
     130                        %if requestedsteps = 0, print all steps in org
     131                        if any(org.requestedsteps==0),
     132                                if org.currentstep==1,
     133                                        disp(sprintf('   prefix: %s',org.prefix));
     134                                end
     135                                disp(sprintf('   step #%2i : %s',org.steps(org.currentstep).id,org.steps(org.currentstep).string));
     136                        end
    138137
    139                          %Ok, now if currentstep is a member of steps, return true
    140                          if ismember(org.currentstep,org.requestedsteps),
    141                                  disp(sprintf('\n   step #%i : %s\n',org.steps(org.currentstep).id,org.steps(org.currentstep).string));
    142                                  bool=true;
    143                          end
     138                        %Ok, now if currentstep is a member of steps, return true
     139                        if ismember(org.currentstep,org.requestedsteps),
     140                                disp(sprintf('\n   step #%i : %s\n',org.steps(org.currentstep).id,org.steps(org.currentstep).string));
     141                                bool=true;
     142                        end
    144143
    145                          %assign org back to calling workspace
    146                          assignin('caller',inputname(1),org);
     144                        %assign org back to calling workspace
     145                        assignin('caller',inputname(1),org);
    147146
    148                  end%}}}
    149                  function savemodel(org,md) % {{{
     147                end%}}}
     148                function savemodel(org,md) % {{{
    150149
    151                          %check
    152                          if (org.currentstep==0), error('Cannot save model because organizer (org) is empty! Make sure you did not skip any perform call'); end
    153                          if (org.currentstep>length(org.steps)), error('Cannot save model because organizer (org) is not up to date!'); end
     150                        %check
     151                        if (org.currentstep==0), error('Cannot save model because organizer (org) is empty! Make sure you did not skip any perform call'); end
     152                        if (org.currentstep>length(org.steps)), error('Cannot save model because organizer (org) is not up to date!'); end
    154153
    155                          name=[org.repository '/' org.prefix org.steps(org.currentstep).string ];
    156                          disp(['saving model as: ' name]);
     154                        name=[org.repository '/' org.prefix org.steps(org.currentstep).string ];
     155                        disp(['saving model as: ' name]);
    157156
    158                          %check that md is a model
    159                          if ~isa(md,'model'), warning('third argument is not a model'); end
    160                          if (org.currentstep>length(org.steps)), error(['organizer error message: element with id ' num2str(org.currentstep) ' not found']); end
     157                        %check that md is a model
     158                        if ~isa(md,'model'), warning('second argument is not a model'); end
     159                        if (org.currentstep>length(org.steps)), error(['organizer error message: element with id ' num2str(org.currentstep) ' not found']); end
    161160
    162                          %save model
    163                          save(name,'md','-v7.3');
    164                  end%}}}
     161                        %save model
     162                        save(name,'md','-v7.3');
     163                end%}}}
    165164        end
    166165end
Note: See TracChangeset for help on using the changeset viewer.