1 | %ORGANIZER class definition
|
---|
2 | %
|
---|
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 | %
|
---|
9 | % Usage:
|
---|
10 | % org = organizer(varargin)
|
---|
11 | %
|
---|
12 | % Examples:
|
---|
13 | % org = organizer('repository','Models/','prefix','AGU2015','steps',0); %build an empty organizer object with a given repository
|
---|
14 |
|
---|
15 | classdef organizer < handle
|
---|
16 | properties (SetAccess=private)
|
---|
17 | % {{{
|
---|
18 | currentstep =0;
|
---|
19 | end
|
---|
20 | properties (SetAccess=public)
|
---|
21 | repository ='./';
|
---|
22 | prefix ='model.';
|
---|
23 | trunkprefix ='';
|
---|
24 | steps =[];
|
---|
25 | requestedsteps=[0];
|
---|
26 | %}}}
|
---|
27 | end
|
---|
28 | methods
|
---|
29 | function org=organizer(varargin) % {{{
|
---|
30 |
|
---|
31 | %process options
|
---|
32 | options=pairoptions(varargin{:});
|
---|
33 |
|
---|
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;
|
---|
39 |
|
---|
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;
|
---|
45 |
|
---|
46 | %Get steps
|
---|
47 | org.requestedsteps=getfieldvalue(options,'steps',0);
|
---|
48 |
|
---|
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),% {{{
|
---|
71 |
|
---|
72 | %Get model path
|
---|
73 | if ~ischar(string), error('argument provided is not a string'); end
|
---|
74 | path=[org.repository '/' org.prefix string];
|
---|
75 |
|
---|
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),% {{{
|
---|
89 |
|
---|
90 | %Get model path
|
---|
91 | if ~ischar(string), error('argument provided is not a string'); end
|
---|
92 | path=[org.repository '/' org.prefix string];
|
---|
93 |
|
---|
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
|
---|
99 |
|
---|
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) % {{{
|
---|
115 |
|
---|
116 | bool=false;
|
---|
117 |
|
---|
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
|
---|
124 |
|
---|
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;
|
---|
129 |
|
---|
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
|
---|
137 |
|
---|
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
|
---|
143 |
|
---|
144 | end%}}}
|
---|
145 | function savemodel(org,md) % {{{
|
---|
146 |
|
---|
147 | %check
|
---|
148 | if (org.currentstep==0), error('Cannot save model because organizer (org) is empty! Make sure you did not skip any perform call'); end
|
---|
149 | if (org.currentstep>length(org.steps)), error('Cannot save model because organizer (org) is not up to date!'); end
|
---|
150 |
|
---|
151 | name=[org.repository '/' org.prefix org.steps(org.currentstep).string ];
|
---|
152 | disp(['saving model as: ' name]);
|
---|
153 |
|
---|
154 | %check that md is a model
|
---|
155 | if ~isa(md,'model'), warning('second argument is not a model'); end
|
---|
156 | if (org.currentstep>length(org.steps)), error(['organizer error message: element with id ' num2str(org.currentstep) ' not found']); end
|
---|
157 |
|
---|
158 | %save model
|
---|
159 | save(name,'md','-v7.3');
|
---|
160 | end%}}}
|
---|
161 | end
|
---|
162 | end
|
---|