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 | %
|
---|
13 | % Examples:
|
---|
14 | % org = organizer('repository','Models/','prefix','AGU2015','steps',0); %build an empty organizer object with a given repository
|
---|
15 |
|
---|
16 | classdef organizer
|
---|
17 | properties (SetAccess=private)
|
---|
18 | % {{{
|
---|
19 | currentstep =0;
|
---|
20 | end
|
---|
21 | 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) % {{{
|
---|
31 |
|
---|
32 | %process options
|
---|
33 | options=pairoptions(varargin{:});
|
---|
34 |
|
---|
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;
|
---|
40 |
|
---|
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;
|
---|
46 |
|
---|
47 | %Get repository
|
---|
48 | org.requestedsteps=getfieldvalue(options,'steps',0);
|
---|
49 |
|
---|
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=loadmodel(org,string),% {{{
|
---|
72 |
|
---|
73 | %Get model path
|
---|
74 | if ~ischar(string), error('argument provided is not a string'); end
|
---|
75 | path=[org.repository '/' org.prefix string];
|
---|
76 |
|
---|
77 | %figure out if the model is there, otherwise, we have to use the default path supplied by user.
|
---|
78 | if exist(path,'file'),
|
---|
79 | md=loadmodel(path);
|
---|
80 | return;
|
---|
81 | end
|
---|
82 |
|
---|
83 | %If we are here, the model has not been found. Try trunk prefix if provided
|
---|
84 | if ~isempty(org.trunkprefix),
|
---|
85 | path2=[org.repository '/' org.trunkprefix string];
|
---|
86 | if ~exist(path2,'file'),
|
---|
87 | error(['Could neither find ' path ', nor ' path2]);
|
---|
88 | else
|
---|
89 | disp(['--> Branching ' org.prefix ' from trunk ' org.trunkprefix]);
|
---|
90 | md=loadmodel(path2);
|
---|
91 | return;
|
---|
92 | end
|
---|
93 | else
|
---|
94 | error(['Could not find ' path ]);
|
---|
95 | end
|
---|
96 | end%}}}
|
---|
97 | function bool=perform(org,string) % {{{
|
---|
98 |
|
---|
99 | bool=false;
|
---|
100 |
|
---|
101 | %Some checks
|
---|
102 | if ~ischar(string), error('Step provided should be a string'); end
|
---|
103 | if ~strcmp(regexprep(string,'\s+',''),string), error('Step provided should not have any white space'); end
|
---|
104 | if (org.currentstep>0 & ismember({string},{org.steps.string}))
|
---|
105 | error(['Step ' string ' already present. Change name']);
|
---|
106 | end
|
---|
107 |
|
---|
108 | %Add step
|
---|
109 | org.steps(end+1).id=length(org.steps)+1;
|
---|
110 | org.steps(end).string=string;
|
---|
111 | org.currentstep=org.currentstep+1;
|
---|
112 |
|
---|
113 | %if requestedsteps = 0, print all steps in org
|
---|
114 | if any(org.requestedsteps==0),
|
---|
115 | if org.currentstep==1,
|
---|
116 | disp(sprintf(' prefix: %s',org.prefix));
|
---|
117 | end
|
---|
118 | disp(sprintf(' step #%2i : %s',org.steps(org.currentstep).id,org.steps(org.currentstep).string));
|
---|
119 | end
|
---|
120 |
|
---|
121 | %Ok, now if currentstep is a member of steps, return true
|
---|
122 | if ismember(org.currentstep,org.requestedsteps),
|
---|
123 | disp(sprintf('\n step #%i : %s\n',org.steps(org.currentstep).id,org.steps(org.currentstep).string));
|
---|
124 | bool=true;
|
---|
125 | end
|
---|
126 |
|
---|
127 | %assign org back to calling workspace
|
---|
128 | assignin('caller',inputname(1),org);
|
---|
129 |
|
---|
130 | end%}}}
|
---|
131 | function savemodel(org,md) % {{{
|
---|
132 |
|
---|
133 | %check
|
---|
134 | if (org.currentstep==0), error('Cannot save model because organizer (org) is empty! Make sure you did not skip any perform call'); end
|
---|
135 | if (org.currentstep>length(org.steps)), error('Cannot save model because organizer (org) is not up to date!'); end
|
---|
136 |
|
---|
137 | name=[org.repository '/' org.prefix org.steps(org.currentstep).string ];
|
---|
138 | disp(['saving model as: ' name]);
|
---|
139 |
|
---|
140 | %check that md is a model
|
---|
141 | if ~isa(md,'model'), error('savemodel error message: third argument is not a model'); end
|
---|
142 | if (org.currentstep>length(org.steps)), error(['organizer error message: element with id ' num2str(org.currentstep) ' not found']); end
|
---|
143 |
|
---|
144 | %save model
|
---|
145 | save(name,'md','-v7.3');
|
---|
146 | end%}}}
|
---|
147 | end
|
---|
148 | end
|
---|