[6989] | 1 | %ORGANIZER class definition
|
---|
| 2 | %
|
---|
[8171] | 3 | % Supported options:
|
---|
| 4 | % repository: directory where all models will be saved
|
---|
| 5 | % prefix: prefix for saved model names
|
---|
| 6 | % steps: requested steps
|
---|
| 7 | % trunkprefix:prefix of previous run with a different prefix. Used to branch.
|
---|
| 8 | %
|
---|
[6989] | 9 | % Usage:
|
---|
| 10 | % org = organizer(varargin)
|
---|
| 11 | %
|
---|
| 12 | % Examples:
|
---|
[8171] | 13 | % org = organizer('repository','Models/','prefix','AGU2015','steps',0); %build an empty organizer object with a given repository
|
---|
[6989] | 14 |
|
---|
[14228] | 15 | classdef organizer < handle
|
---|
[6989] | 16 | properties (SetAccess=private)
|
---|
[13946] | 17 | % {{{
|
---|
| 18 | currentstep =0;
|
---|
| 19 | end
|
---|
[6989] | 20 | properties (SetAccess=public)
|
---|
[13946] | 21 | repository ='./';
|
---|
| 22 | prefix ='model.';
|
---|
| 23 | trunkprefix ='';
|
---|
| 24 | steps =[];
|
---|
| 25 | requestedsteps=[0];
|
---|
| 26 | %}}}
|
---|
| 27 | end
|
---|
| 28 | methods
|
---|
| 29 | function org=organizer(varargin) % {{{
|
---|
[8171] | 30 |
|
---|
[13946] | 31 | %process options
|
---|
| 32 | options=pairoptions(varargin{:});
|
---|
[8171] | 33 |
|
---|
[13946] | 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;
|
---|
[8171] | 39 |
|
---|
[13946] | 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;
|
---|
[8171] | 45 |
|
---|
[13946] | 46 | %Get steps
|
---|
| 47 | org.requestedsteps=getfieldvalue(options,'steps',0);
|
---|
[8171] | 48 |
|
---|
[13946] | 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),% {{{
|
---|
[12648] | 71 |
|
---|
[13946] | 72 | %Get model path
|
---|
| 73 | if ~ischar(string), error('argument provided is not a string'); end
|
---|
| 74 | path=[org.repository '/' org.prefix string];
|
---|
[12648] | 75 |
|
---|
[13946] | 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),% {{{
|
---|
[6989] | 89 |
|
---|
[13946] | 90 | %Get model path
|
---|
| 91 | if ~ischar(string), error('argument provided is not a string'); end
|
---|
| 92 | path=[org.repository '/' org.prefix string];
|
---|
[6989] | 93 |
|
---|
[13946] | 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
|
---|
[6989] | 99 |
|
---|
[13946] | 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%}}}
|
---|
[14278] | 114 | function md=loaddata(org,string),% {{{
|
---|
| 115 |
|
---|
| 116 | %Get model path
|
---|
| 117 | if ~ischar(string), error('argument provided is not a string'); end
|
---|
| 118 | path=[org.repository '/' org.prefix string];
|
---|
| 119 |
|
---|
| 120 | %figure out if the data is there, otherwise, we have to use the default path supplied by user.
|
---|
| 121 | if exist(path,'file') | exist([path '.mat'],'file'),
|
---|
| 122 | evalin('caller',['load -mat ' path]);
|
---|
| 123 | return;
|
---|
| 124 | end
|
---|
| 125 |
|
---|
| 126 | %If we are here, the data has not been found. Try trunk prefix if provided
|
---|
| 127 | if ~isempty(org.trunkprefix),
|
---|
| 128 | path2=[org.repository '/' org.trunkprefix string];
|
---|
| 129 | if ~exist(path2,'file'),
|
---|
| 130 | error(['Could find neither ' path ', nor ' path2]);
|
---|
| 131 | else
|
---|
| 132 | disp(['--> Branching ' org.prefix ' from trunk ' org.trunkprefix]);
|
---|
| 133 | evalin('caller',['load -mat ' path2]);
|
---|
| 134 | return;
|
---|
| 135 | end
|
---|
| 136 | else
|
---|
| 137 | error(['Could not find ' path ]);
|
---|
| 138 | end
|
---|
| 139 | end%}}}
|
---|
[13946] | 140 | function bool=perform(org,string) % {{{
|
---|
[13646] | 141 |
|
---|
[13946] | 142 | bool=false;
|
---|
[6989] | 143 |
|
---|
[13946] | 144 | %Some checks
|
---|
| 145 | if ~ischar(string), error('Step provided should be a string'); end
|
---|
| 146 | if ~strcmp(regexprep(string,'\s+',''),string), error('Step provided should not have any white space'); end
|
---|
| 147 | if (org.currentstep>0 & ismember({string},{org.steps.string}))
|
---|
| 148 | error(['Step ' string ' already present. Change name']);
|
---|
| 149 | end
|
---|
[6989] | 150 |
|
---|
[13946] | 151 | %Add step
|
---|
| 152 | org.steps(end+1).id=length(org.steps)+1;
|
---|
| 153 | org.steps(end).string=string;
|
---|
| 154 | org.currentstep=org.currentstep+1;
|
---|
[6989] | 155 |
|
---|
[13946] | 156 | %if requestedsteps = 0, print all steps in org
|
---|
| 157 | if any(org.requestedsteps==0),
|
---|
| 158 | if org.currentstep==1,
|
---|
| 159 | disp(sprintf(' prefix: %s',org.prefix));
|
---|
| 160 | end
|
---|
| 161 | disp(sprintf(' step #%2i : %s',org.steps(org.currentstep).id,org.steps(org.currentstep).string));
|
---|
| 162 | end
|
---|
[6989] | 163 |
|
---|
[13946] | 164 | %Ok, now if currentstep is a member of steps, return true
|
---|
| 165 | if ismember(org.currentstep,org.requestedsteps),
|
---|
| 166 | disp(sprintf('\n step #%i : %s\n',org.steps(org.currentstep).id,org.steps(org.currentstep).string));
|
---|
| 167 | bool=true;
|
---|
| 168 | end
|
---|
[6989] | 169 |
|
---|
[13946] | 170 | end%}}}
|
---|
| 171 | function savemodel(org,md) % {{{
|
---|
[6989] | 172 |
|
---|
[13946] | 173 | %check
|
---|
| 174 | if (org.currentstep==0), error('Cannot save model because organizer (org) is empty! Make sure you did not skip any perform call'); end
|
---|
| 175 | if (org.currentstep>length(org.steps)), error('Cannot save model because organizer (org) is not up to date!'); end
|
---|
[8151] | 176 |
|
---|
[13946] | 177 | name=[org.repository '/' org.prefix org.steps(org.currentstep).string ];
|
---|
| 178 | disp(['saving model as: ' name]);
|
---|
[13646] | 179 |
|
---|
[13946] | 180 | %check that md is a model
|
---|
| 181 | if ~isa(md,'model'), warning('second argument is not a model'); end
|
---|
| 182 | if (org.currentstep>length(org.steps)), error(['organizer error message: element with id ' num2str(org.currentstep) ' not found']); end
|
---|
[6989] | 183 |
|
---|
[13946] | 184 | %save model
|
---|
| 185 | save(name,'md','-v7.3');
|
---|
| 186 | end%}}}
|
---|
[14278] | 187 | function savedata(org,varargin) % {{{
|
---|
| 188 |
|
---|
| 189 | %check
|
---|
| 190 | if (org.currentstep==0), error('Cannot save data because organizer (org) is empty! Make sure you did not skip any perform call'); end
|
---|
| 191 | if (org.currentstep>length(org.steps)), error('Cannot save data because organizer (org) is not up to date!'); end
|
---|
| 192 |
|
---|
| 193 | name=[org.repository '/' org.prefix org.steps(org.currentstep).string ];
|
---|
| 194 | disp(['saving data in: ' name]);
|
---|
| 195 |
|
---|
| 196 | %check that md is a model
|
---|
| 197 | if (org.currentstep>length(org.steps)), error(['organizer error message: element with id ' num2str(org.currentstep) ' not found']); end
|
---|
| 198 |
|
---|
| 199 | %list of variable names:
|
---|
| 200 | variables='';
|
---|
| 201 | for i=2:nargin,
|
---|
| 202 | variables=[variables ',' '''' inputname(i) ''''];
|
---|
| 203 | eval([inputname(i) '= varargin{' num2str(i-1) '};']);
|
---|
| 204 | end
|
---|
| 205 | eval(['save(''' name '''' variables ',''-v7.3'');']);
|
---|
| 206 | end%}}}
|
---|
[6989] | 207 | end
|
---|
| 208 | end
|
---|