Mercurial > hg > ltpda
comparison m-toolbox/classes/+utils/@models/makeBuiltInModel.m @ 0:f0afece42f48
Import.
author | Daniele Nicolodi <nicolodi@science.unitn.it> |
---|---|
date | Wed, 23 Nov 2011 19:22:13 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:f0afece42f48 |
---|---|
1 % MAKEBUILTINMODEL prepares a new built-in model template | |
2 % | |
3 % DESCRIPTION: This utility creates a new built-in model file according to | |
4 % the standard template. It also creates a unit-test directory inheriting | |
5 % the standard built-in model tests. The tests are run at the end of the | |
6 % creation process. | |
7 % | |
8 % | |
9 % CALL: | |
10 % utils.models.makeBuiltInModel(extension_dir, class, name) | |
11 % | |
12 % INPUTS: | |
13 % extension_dir - the directory where the extension module resides | |
14 % class - the LTPDA user-object class that this model belongs | |
15 % to. | |
16 % name - the name of the new model | |
17 % | |
18 % If you are making a new model in an existing extension module, then a | |
19 % typical call would be: | |
20 % | |
21 % utils.models.makeBuiltInModel('path/to/extension', ... | |
22 % 'ao', ... | |
23 % 'myNewModel'); | |
24 % | |
25 % | |
26 % VERSION: $Id: makeBuiltInModel.m,v 1.4 2011/05/16 06:55:51 hewitson Exp $ | |
27 % | |
28 | |
29 function makeBuiltInModel(varargin) | |
30 | |
31 if nargin ~= 3 | |
32 help('utils.models.makeBuiltInModel') | |
33 error('Incorrect inputs'); | |
34 end | |
35 | |
36 extDir = varargin{1}; | |
37 mclass = varargin{2}; | |
38 mname = varargin{3}; | |
39 | |
40 % check mclass is one of the known LTPDA uo classes | |
41 if ~utils.helper.isSubclassOf(mclass, 'ltpda_uo') | |
42 error('The specified class must be an LTPDA user-object subclass (i.e. a subclass of ltpda_uo).'); | |
43 end | |
44 | |
45 % remove bad characters from the model name | |
46 mname = genvarname(mname); | |
47 | |
48 % read the template file | |
49 lines = readModelTemplateFile(mclass, mname); | |
50 | |
51 % write the lines back out to the destination file | |
52 fullmname = [mclass '_model_' mname]; | |
53 mdlfile = [fullmname '.m']; | |
54 dstDir = fullfile(extDir, 'models', mclass); | |
55 dstFile = fullfile(dstDir, mdlfile); | |
56 fprintf('+ Writing model file %s...\n', dstFile); | |
57 % Check if the destination file for the new model exists | |
58 if exist(dstFile, 'file') == 2 | |
59 r = input(sprintf('A file exists at the chosen location (%s). \nDo you want to overwrite it? (yes/no) ', dstFile), 's'); | |
60 if ~strcmpi(r, 'yes') | |
61 return; | |
62 end | |
63 end | |
64 [success,message,messageid] = mkdir(dstDir); | |
65 if ~success | |
66 error('Failed to create model directory %s [%s]', dstDir, message); | |
67 end | |
68 addpath(dstDir); | |
69 savepath; | |
70 writeFile(dstFile, lines); | |
71 | |
72 % read test-class template file | |
73 lines = readTestClassTemplateFile(mclass, mname); | |
74 | |
75 % write test class | |
76 testClass = ['@test_' fullmname]; | |
77 testName = ['test_' fullmname]; | |
78 testConstructor = [testName '.m']; | |
79 testClassPath = fullfile(extDir, 'tests', 'models', mclass); | |
80 testClassDir = fullfile(testClassPath, testClass); | |
81 [success,message,messageid] = mkdir(testClassDir); | |
82 if ~success | |
83 error('Failed to create unit-test directory %s [%s]', testClassDir, message); | |
84 end | |
85 addpath(testClassPath); | |
86 savepath; | |
87 | |
88 testClassFilePath = fullfile(testClassDir, testConstructor); | |
89 fprintf('+ Writing model test-class constructor file %s...\n', testClassFilePath); | |
90 % Check if the destination test class file for the new model exists | |
91 if exist(testClassFilePath, 'file') == 2 | |
92 r = input(sprintf('A file exists at the chosen location (%s). \nDo you want to overwrite it? (yes/no) ', testClassFilePath), 's'); | |
93 if ~strcmpi(r, 'yes') | |
94 return; | |
95 end | |
96 end | |
97 writeFile(testClassFilePath, lines); | |
98 | |
99 runner = ltpda_test_runner(); | |
100 runner.run_tests(testName) | |
101 | |
102 if all([runner.results.passed]) | |
103 fprintf('** successfully created model %s and test class %s\n', mname, testName); | |
104 fprintf('** All tests passed. You can now edit your new model file.\n'); | |
105 else | |
106 fprintf(2, 'Created model file %s\n', dstFile); | |
107 fprintf(2, 'Created model test class at %s\n', testClassFilePath); | |
108 fprintf(2, 'One or more tests failed. Please review the written files.\n'); | |
109 end | |
110 | |
111 edit(dstFile); | |
112 | |
113 end | |
114 | |
115 function writeFile(filename, lines) | |
116 fd = fopen(filename, 'w+'); | |
117 if fd < 0 | |
118 error('Could not open destination file for writing: %s', filename); | |
119 end | |
120 | |
121 for kk=1:numel(lines) | |
122 l = lines{kk}; | |
123 fprintf(fd, '%s\n', l); | |
124 end | |
125 | |
126 fclose(fd); | |
127 | |
128 fprintf(' - wrote model file %s\n', filename); | |
129 end | |
130 | |
131 function lines = readTestClassTemplateFile(mclass, mname) | |
132 | |
133 path = fileparts(which('utils.models.makeBuiltInModel')); | |
134 templateFile = fullfile(path, 'built_in_model_unittest_template.m'); | |
135 lines = readFile(templateFile, mclass, mname); | |
136 | |
137 end | |
138 | |
139 | |
140 function lines = readModelTemplateFile(mclass, mname) | |
141 | |
142 path = fileparts(which('utils.models.makeBuiltInModel')); | |
143 templateFile = fullfile(path, 'built_in_model_template.m'); | |
144 lines = readFile(templateFile, mclass, mname); | |
145 | |
146 end | |
147 | |
148 function lines = readFile(filename, mclass, mname) | |
149 fd = fopen(filename, 'r'); | |
150 if fd < 0 | |
151 error('Failed to read template file %s', filename); | |
152 end | |
153 | |
154 lines = {}; | |
155 while ~feof(fd) | |
156 l = makeSubstitutions(fgetl(fd), mclass, mname); | |
157 lines = [lines {l}]; | |
158 end | |
159 | |
160 % close file | |
161 fclose(fd); | |
162 end | |
163 | |
164 | |
165 function s = makeSubstitutions(s, mclass, mname) | |
166 | |
167 s = strrep(s, '<CLASS>', mclass); | |
168 s = strrep(s, '<NAME>', mname); | |
169 | |
170 | |
171 end | |
172 | |
173 % END | |
174 |