File: CommandClassCreateAlgorithm.cxx

package info (click to toggle)
connectome-workbench 1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 28,968 kB
  • ctags: 23,747
  • sloc: cpp: 260,127; ansic: 3,670; sh: 308; makefile: 146
file content (439 lines) | stat: -rw-r--r-- 16,186 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*LICENSE_START*/
/*
 *  Copyright (C) 2014  Washington University School of Medicine
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
/*LICENSE_END*/

#include <fstream>
#include <ostream>
#include <iostream>

#include "CaretAssertion.h"
#include "CommandClassCreateAlgorithm.h"
#include "FileInformation.h"
#include "ProgramParameters.h"
#include "TextFile.h"

using namespace caret;

/**
 * Constructor.
 */
CommandClassCreateAlgorithm::CommandClassCreateAlgorithm()
: CommandClassCreateBase("-class-create-algorithm",
                         "CREATE SOURCE CODE CLASS FILES (.h, .cxx) FOR ALGORITHM")
{
    
}

/**
 * Destructor.
 */
CommandClassCreateAlgorithm::~CommandClassCreateAlgorithm()
{
    
}

/**
 * @return The help information.
 */
AString 
CommandClassCreateAlgorithm::getHelpInformation(const AString& /*programName*/) 
{
    AString helpInfo = ("\n"
                        "Create Algorithm Class header (.h) and implementation (.cxx) files.\n"
                        "\n"
                        "Usage:  <algorithm-class-name>\n"
                        "        <command-line-switch>\n"
                        "        <short-description>\n"
                        "\n"
                        "    algorithm-class-name\n"
                        "        Required name of the algorithm class that MUST start with \"Algorithm\"\n"
                        "    \n"
                        "    command-line-switch\n"
                        "        Required command line switch for algorithm.\n"
                        "    \n"
                        "    short-description\n"
                        "        Required short description within double quotes.\n"
                        "    \n"
                        );
    return helpInfo; 
}

/**
 * Execute the operation.
 * 
 * @param parameters
 *   Parameters for the operation.
 * @throws CommandException
 *   If the command failed.
 * @throws ProgramParametersException
 *   If there is an error in the parameters.
 */
void 
CommandClassCreateAlgorithm::executeOperation(ProgramParameters& parameters) throw (CommandException,
                                                               ProgramParametersException)
{
    const AString algorithmClassName = parameters.nextString("Algorithm Class Name");
    const AString commandLineSwitch  = parameters.nextString("Command Line Switch");
    const AString shortDescription   = parameters.nextString("Short Description");
    if (parameters.hasNext()) {
        const AString param = parameters.nextString("Parameter");
        throw CommandException("Unexpected extra parameter: " + param);
    }
    
    AString errorMessage;
    if (algorithmClassName.isEmpty()) {
        throw CommandException("Algorithm Class Name is empty.");
    }
    else {
        if (algorithmClassName.startsWith("Algorithm") == false) {
            throw CommandException("Algorithm Class Name must start with \"Algorithm\".\n");
        }
        if (algorithmClassName == "Algorithm") {
            throw CommandException("\"Algorithm\" is not allowed for Algorithm Class Name");
        }
    }
    
    if (commandLineSwitch.isEmpty()) {
        throw CommandException("Command line switch is empty.");
    }
    else if (commandLineSwitch.startsWith("-") == false) {
        throw CommandException("Command line must begin with \"-\".");
    }
    
    if (shortDescription.isEmpty()) {
        throw CommandException("Short description is empty.");
    }
    const AString headerFileName = algorithmClassName + ".h";
    const AString implementationFileName = algorithmClassName + ".cxx";
    
    FileInformation headerInfo(headerFileName);
    if (headerInfo.exists()) {
        errorMessage += headerFileName + " exists and this command will not overwrite it.\n";
    }
    FileInformation impInfo(implementationFileName);
    if (impInfo.exists()) {
        errorMessage += implementationFileName + " exists and this command will not overwrite it.\n";
    }
    
    if (errorMessage.isEmpty() == false) {
        throw CommandException(errorMessage);
    }    
    
    AString ifndefName;
    AString ifdefNameStaticDeclarations;
    this->getIfDefNames(algorithmClassName,
                        ifndefName, 
                        ifdefNameStaticDeclarations);
    
    this->createHeaderFile(headerFileName,
                           algorithmClassName,
                           ifndefName);
    
    this->createImplementationFile(implementationFileName,
                                   algorithmClassName,
                                   commandLineSwitch,
                                   shortDescription);
}

/**
 * Create and write the header (.h) file.
 *     
 * @param outputFileName
 *    Name for file that is written.
 * @param algorithmClassName
 *    Name of algorithm type class.
 * @param ifndefName
 *    Name of "ifndef" value.
 */
void 
CommandClassCreateAlgorithm::createHeaderFile(const AString& outputFileName,
                                              const AString& algorithmClassName,
                                              const AString& ifndefName)
{
    AString t;

    t += ("#ifndef " + ifndefName + "\n");
    t += ("#define " + ifndefName + "\n");
    t += this->getCopyright();
    t += ("\n");
    
    t += ("#include \"AbstractAlgorithm.h\"\n");
    t += ("\n");
    t += ("namespace caret {\n");
    t += ("\n");
    t += ("    class " + algorithmClassName + " : public AbstractAlgorithm {\n");
    t += ("\n");
    t += ("    private:\n");
    t += ("        " + algorithmClassName + "(); \n");
    t += ("\n");
    t += ("    protected:\n");
    t += ("        static float getSubAlgorithmWeight();\n");
    t += ("\n");
    t += ("        static float getAlgorithmInternalWeight();\n");
    t += ("\n");
    t += ("    public:\n");
    t += ("        " + algorithmClassName + "(ProgressObject* myProgObj /*INSERT PARAMETERS HERE*/); \n");
    t += ("\n");
    t += ("        static OperationParameters* getParameters();\n");
    t += ("\n");
    t += ("        static void useParameters(OperationParameters* myParams, \n");
    t += ("                                  ProgressObject* myProgObj);\n");
    t += ("\n");
    t += ("        static AString getCommandSwitch();\n");
    t += ("\n");
    t += ("        static AString getShortDescription();\n");
    t += ("\n");
    
    t += ("    };\n");
    t += ("\n");
    t += ("    typedef TemplateAutoOperation<" + algorithmClassName + "> Auto" + algorithmClassName + ";\n");
    t += ("\n");
    t += ("} // namespace\n");
    t += ("\n");
    
    t += ("#endif  //" + ifndefName + "\n");
    t += ("\n");

    TextFile tf;
    tf.replaceText(t);
    
    try {
        tf.writeFile(outputFileName);
    }
    catch (const DataFileException& e) {
        throw CommandException(e);
    }
}

/**
 * Create and write the implementation (.cxx) file.
 *     
 * @param outputFileName
 *    Name for file that is written.
 * @param algorithmClassName
 *    Name of algorithm type class.
 * @param commandLineSwitch
 *    Command line switch for algorithm.
 * @param shortDescription
 *    Short description of algorithm.
 */
void 
CommandClassCreateAlgorithm::createImplementationFile(const AString& outputFileName,
                                                      const AString& algorithmClassName,
                                                      const AString& commandLineSwitch,
                                                      const AString& shortDescription)
{
    AString t;
    
    t += this->getCopyright();
    
    t += ("#include \"CaretAssert.h\"\n");
    t += ("#include \"CaretLogger.h\"\n");
    t += ("\n");
    t += ("#include \"" + algorithmClassName + ".h\"\n");
    t += ("#include \"AlgorithmException.h\"\n");
    t += ("\n");
    t += ("using namespace caret;\n");
    t += ("\n");
    t += ("/**\n");
    t += (" * \\class caret::" + algorithmClassName + " \n");
    t += (" * \\brief " + shortDescription + "\n");
    t += (" *\n");
    t += (" * <REPLACE-WITH-THOROUGH DESCRIPTION>\n");
    t += (" */\n");
    t += ("\n");
    
    
    t += ("/**\n");
    t += (" * @return Command line switch\n");
    t += (" */\n");
    t += ("AString\n");
    t += ("" + algorithmClassName + "::getCommandSwitch()\n");
    t += ("{\n");
    t += ("    return \"" + commandLineSwitch + "\";\n");
    t += ("}\n");
    t += ("\n");
    
    t += ("/**\n");
    t += (" * @return Short description of algorithm\n");
    t += (" */\n");
    t += ("AString\n");
    t += ("" + algorithmClassName + "::getShortDescription()\n");
    t += ("{\n");
    t += ("    return \"" + shortDescription.toUpper() + "\";\n");
    t += ("}\n");
    t += ("\n");
    
    t += ("/**\n");
    t += (" * @return Parameters for algorithm\n");
    t += (" */\n");
    t += ("OperationParameters*\n");
    t += ("" + algorithmClassName + "::getParameters()\n");
    t += ("{\n");
    t += ("    OperationParameters* ret = new OperationParameters();\n");
    t += ("    \n");
    t += ("    /*\n");
    t += ("     * Example parameters:\n");
    t += ("     *\n");
    t += ("     * ret->addSurfaceParameter(1, \"surface\", \"the surface to compute on\");\n");
    t += ("     *\n");
    t += ("     * ret->addMetricOutputParameter(2, \"metric\", \"the output metric\");\n");
    t += ("     *\n");
    t += ("     * OptionalParameter* columnSelect = ret->createOptionalParameter(3, \"-column\", \"select a single column\");\n");
    t += ("     *\n");
    t += ("     * columnSelect->addStringParameter(1, \"column\", \"the column number or name\")\n");
    t += ("     */\n");
    t += ("    AString helpText = (\"This is where you set the help text.  DO NOT add the info about what the command line format is\"\n");
    t += ("                        \"and do not give the command switch, short description, or the short descriptions of parameters.  Do not indent,\"\n");
    t += ("                        \"add newlines, or format the text in any way other than to separate paragraphs within the help text prose\");\n");
    t += ("\n");
    t += ("    ret->setHelpText(helpText);\n");
    t += ("    \n");
    t += ("    return ret;\n");
    t += ("}\n");
    t += ("\n");
    
    
    t += ("/**\n");
    t += (" * Use Parameters and perform algorithm\n");
    t += (" * @param myParams\n");
    t += (" *     Parameters for algorithm\n");
    t += (" * @param myProgObj\n");
    t += (" *     The progress object\n");
    t += (" * @throws\n");
    t += (" *     AlgorithmException if errors\n");
    t += (" */\n");
    t += ("void\n");
    t += ("" + algorithmClassName + "::useParameters(OperationParameters* myParams,\n");
    t += ("                                          ProgressObject* myProgObj)\n");
    t += ("{\n");
    t += ("    /*\n");
    t += ("     * Example parameter processing:\n");
    t += ("     *\n");
    t += ("     * Gets the surface with key 1\n");
    t += ("     * SurfaceFile* mySurf = myParams->getSurface(1);\n");
    t += ("     *\n");
    t += ("     * Gets the output metric with key 2\n");
    t += ("     * MetricFile* myMetricOut = myParams->getOutputMetric(2);\n");
    t += ("     *\n");
    t += ("     * Gets optional parameter with key 3\n");
    t += ("     * OptionalParameter* columnSelect = myParams->getOptionalParameter(3);\n");
    t += ("     * int columnNum = -1;\n");
    t += ("     * if (columnSelect->m_present) {\n");
    t += ("     *     columnNum = (int)myMetric->getMapIndexFromNameOrNumber(columnSelect->getString(1));\n");
    t += ("     *     if (columnNum < 0) {\n");
    t += ("     *          throw AlgorithmException(\"invalid column specified\");\n");
    t += ("     *     }\n");
    t += ("     * }\n");
    t += ("     */\n");
    t += ("    \n");
    t += ("    /*\n");
    t += ("     * Constructs and executes the algorithm \n");
    t += ("     */\n");
    t += ("    " + algorithmClassName + "(myProgObj /* INSERT PARAMTERS HERE */);\n");
    t += ("    \n");
    t += ("}\n");
    t += ("\n");
    
    t += ("/**\n");
    t += (" * Constructor\n");
    t += (" *\n");
    t += (" * Calling the constructor will execute the algorithm\n");
    t += (" *\n");
    t += (" * @param myProgObj\n");
    t += (" *     Parameters for algorithm\n");
    t += (" */\n");
    t += (algorithmClassName + "::" + algorithmClassName + "(ProgressObject* myProgObj /* INSERT PARAMTERS HERE - may get compilation error if no parameters added */)\n");
    t += ("   : AbstractAlgorithm(myProgObj)\n");
    t += ("{\n");
    t += ("    /*\n");
    t += ("     * Uncomment these if you use another algorithm inside here\n");
    t += ("     *\n");
    t += ("     * ProgressObject* subAlgProgress1 = NULL;\n");
    t += ("     * if (myProgObj != NULL) {\n");
    t += ("     *    subAlgProgress1 = myProgObj->addAlgorithm(AlgorithmInsertNameHere::getAlgorithmWeight());\n");
    t += ("     * }\n");
    t += ("     */\n");
    t += ("    \n");
    t += ("    /*\n");
    t += ("     * Sets the algorithm up to use the progress object, and will \n");
    t += ("     * finish the progress object automatically when the algorithm terminates\n");
    t += ("     */\n");
    t += ("    LevelProgress myProgress(myProgObj);\n");
    t += ("    \n");
    t += ("    /*\n");
    t += ("     * How you say you are halfway done with the INTERNAL work of the algorithm\n");
    t += ("     * will report finished automatically when this function ends (myProgress goes out \n");
    t += ("     of scope, destructor triggers finish\n");
    t += ("     */\n");
    t += ("    //myProgress.reportProgress(0.5f);\n");
    t += ("    \n");
    t += ("}\n");
    t += ("\n");
    
    t += ("/**\n");
    t += (" * @return Algorithm internal weight\n");
    t += (" */\n");
    t += ("float\n");
    t += ("" + algorithmClassName + "::getAlgorithmInternalWeight()\n");
    t += ("{\n");
    t += ("    /*\n");
    t += ("     * override this if needed, if the progress bar isn't smooth\n");
    t += ("     */\n");
    t += ("    return 1.0f;\n");
    t += ("}\n");
    t += ("\n");
    
    t += ("/**\n");
    t += (" * @return Algorithm sub-algorithm weight\n");
    t += (" */\n");
    t += ("float\n");
    t += ("" + algorithmClassName + "::getSubAlgorithmWeight()\n");
    t += ("{\n");
    t += ("    /*\n");
    t += ("     * If you use a subalgorithm\n");
    t += ("     */\n");
    t += ("    //return AlgorithmInsertNameHere::getAlgorithmWeight()\n");
    t += ("    return 0.0f;\n");
    t += ("}\n");
    t += ("\n");
    
    TextFile tf;
    tf.replaceText(t);
    
    try {
        tf.writeFile(outputFileName);
    }
    catch (const DataFileException& e) {
        throw CommandException(e);
    }
    
    std::cout << std::endl;
    std::cout << "Algorithm was created successfully." << std::endl;
    std::cout << std::endl;
    std::cout << "Add the class files to Algorithms/CMakeLists.txt:" << std::endl;
    std::cout << "   " << qPrintable(algorithmClassName) << ".h" << std::endl;
    std::cout << "   " << qPrintable(algorithmClassName) << ".cxx" << std::endl;
    std::cout << std::endl;
    std::cout << "Add the header file and algorithm to Commands/CommandOperationManager.cxx:" << std::endl;
    std::cout << "   #include \"" << qPrintable(algorithmClassName) << ".h\"" << std::endl;
    std::cout << "   this->commandOperations.push_back(new CommandParser(new Auto" << algorithmClassName << "()));" << std::endl;
    std::cout << std::endl;
}