]> git.donarmstrong.com Git - mothur.git/blob - engine.cpp
testing mpi
[mothur.git] / engine.cpp
1 /*
2  *  engine.cpp
3  *  
4  *
5  *  Created by Pat Schloss on 8/15/08.
6  *  Copyright 2008 Patrick D. Schloss. All rights reserved.
7  *
8  *  There's a TON of duplicated code between InteractEngine and BatchEngine
9  *  I couldn't figure out how to transition between ifstream (batch) and cin (interact)
10  *  Fix later, don't have time now.
11  *
12  */
13
14
15 #include "engine.hpp"
16
17 /***********************************************************************/
18 Engine::Engine(){
19         try {
20                 cFactory = CommandFactory::getInstance();
21                 mout = MothurOut::getInstance();
22         }
23         catch(exception& e) {
24                 mout->errorOut(e, "Engine", "Engine");
25                 exit(1);
26         }
27 }
28
29 /***********************************************************************/
30
31 InteractEngine::InteractEngine(string path){
32
33         globaldata = GlobalData::getInstance();
34         globaldata->argv = path;
35         
36 }
37
38 /***********************************************************************/
39
40 InteractEngine::~InteractEngine(){}
41
42 /***********************************************************************/
43 //This function allows the user to input commands one line at a time until they quit.
44 //If the command is garbage it does nothing.
45 bool InteractEngine::getInput(){
46         try {
47                 string input = "";
48                 string commandName = "";
49                 string options = "";
50                 int quitCommandCalled = 0;
51                 
52                 while(quitCommandCalled != 1){
53
54                         mout->mothurOutEndLine();
55                         
56                         input = getCommand();   
57                         #ifdef USE_MPI
58                                         int pid;
59                                         MPI_Comm_rank(MPI_COMM_WORLD, &pid); 
60                                         
61                                         if (pid == 0) {
62                         #endif
63                         mout->mothurOutEndLine();       
64                         
65                         #ifdef USE_MPI
66                                 }
67                         #endif
68                         
69                         if (mout->control_pressed) { input = "quit()"; }
70                         
71                         //allow user to omit the () on the quit command
72                         if (input == "quit") { input = "quit()"; }
73                         
74                         CommandOptionParser parser(input);
75                         commandName = parser.getCommandString();
76         
77                         options = parser.getOptionString();
78                         
79                         if (commandName != "") {
80                                 mout->executing = true;
81                                 
82                                 #ifdef USE_MPI
83                                         int pid;
84                                         MPI_Comm_rank(MPI_COMM_WORLD, &pid); 
85                                         
86                                         if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
87                                 #endif
88                                 //executes valid command
89                                 Command* command = cFactory->getCommand(commandName, options);
90                                 quitCommandCalled = command->execute();
91                                 mout->control_pressed = 0;
92                                 mout->executing = false;
93                                 
94                                 #ifdef USE_MPI
95                                         }
96                                 #endif
97                         }else {
98                                 mout->mothurOut("Your input contains errors. Please try again."); 
99                                 mout->mothurOutEndLine();
100                         }
101                 }       
102                 return 1;
103         }
104         catch(exception& e) {
105                 mout->errorOut(e, "InteractEngine", "getInput");
106                 exit(1);
107         }
108 }
109 /***********************************************************************/
110 string Engine::getCommand()  {
111         try {
112                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
113                         #ifdef USE_READLINE
114                                 char* nextCommand = NULL;
115                                 nextCommand = readline("mothur > ");
116                                 
117                                 if(nextCommand != NULL) {  add_history(nextCommand);  } 
118                                 else{ //^D causes null string and we want it to quit mothur
119                                         nextCommand = "quit"; 
120                                         cout << nextCommand << endl;
121                                 }       
122                                 
123                                 #ifdef USE_MPI
124                                         int pid;
125                                         MPI_Comm_rank(MPI_COMM_WORLD, &pid); 
126                                         
127                                         if (pid == 0) { //only one process should output to screen
128                                 #endif
129
130                                 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
131                                 
132                                 #ifdef USE_MPI
133                                         }
134                                 #endif
135                                 
136                                 return nextCommand;
137                         #else
138                                 string nextCommand = "";
139                                 #ifdef USE_MPI
140                                         int pid;
141                                         MPI_Comm_rank(MPI_COMM_WORLD, &pid); 
142                                         
143                                         if (pid == 0) { //only one process should output to screen
144                                 #endif
145
146                                 mout->mothurOut("mothur > ");
147                                 
148                                 #ifdef USE_MPI
149                                         }
150                                 #endif
151                                 
152                                 getline(cin, nextCommand);
153                                 
154                                 #ifdef USE_MPI
155                                         int pid;
156                                         MPI_Comm_rank(MPI_COMM_WORLD, &pid); 
157                                         
158                                         if (pid == 0) { //only one process should output to screen
159                                 #endif
160                                 
161                                 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
162                                 
163                                 #ifdef USE_MPI
164                                         }
165                                 #endif
166
167                                 return nextCommand;
168                         #endif
169                 #else
170                         string nextCommand = "";
171                                 #ifdef USE_MPI
172                                         int pid;
173                                         MPI_Comm_rank(MPI_COMM_WORLD, &pid); 
174                                         
175                                         if (pid == 0) { //only one process should output to screen
176                                 #endif
177
178                                 mout->mothurOut("mothur > ");
179                                 
180                                 #ifdef USE_MPI
181                                         }
182                                 #endif
183                                 
184                                 getline(cin, nextCommand);
185                                 
186                                 #ifdef USE_MPI
187                                         int pid;
188                                         MPI_Comm_rank(MPI_COMM_WORLD, &pid); 
189                                         
190                                         if (pid == 0) { //only one process should output to screen
191                                 #endif
192                                 
193                                 mout->mothurOutJustToLog(toString(nextCommand));
194                                 
195                                 #ifdef USE_MPI
196                                         }
197                                 #endif
198
199                                 return nextCommand;
200                 #endif
201                 
202                 mout->mothurOutEndLine();
203                                                 
204         }
205         catch(exception& e) {
206                 mout->errorOut(e, "Engine", "getCommand");
207                 exit(1);
208         }
209 }
210 /***********************************************************************/
211 //This function opens the batchfile to be used by BatchEngine::getInput.
212 BatchEngine::BatchEngine(string path, string batchFileName){
213         try {
214                 globaldata = GlobalData::getInstance();
215         
216                 openedBatch = openInputFile(batchFileName, inputBatchFile);
217                 globaldata->argv = path;
218                                 
219         }
220         catch(exception& e) {
221                 mout->errorOut(e, "BatchEngine", "BatchEngine");
222                 exit(1);
223         }
224 }
225
226 /***********************************************************************/
227
228 BatchEngine::~BatchEngine(){    }
229
230 /***********************************************************************/
231 //This Function allows the user to run a batchfile containing several commands on Dotur
232 bool BatchEngine::getInput(){
233         try {
234                 //check if this is a valid batchfile
235                 if (openedBatch == 1) {  
236                         mout->mothurOut("unable to open batchfile");  
237                         mout->mothurOutEndLine();
238                         return 1; 
239                 }
240         
241                 string input = "";
242                 string commandName = "";
243                 string options = "";
244                 
245                 //CommandFactory cFactory;
246                 int quitCommandCalled = 0;
247         
248                 while(quitCommandCalled == 0){
249         
250                         if (inputBatchFile.eof()) { input = "quit()"; }
251                         else { input = getline(inputBatchFile); }
252                         
253                         if (input[0] != '#') {
254                                 
255                                 #ifdef USE_MPI
256                                         int pid;
257                                         MPI_Comm_rank(MPI_COMM_WORLD, &pid); 
258                                         
259                                         if (pid == 0) { //only one process should output to screen
260                                 #endif
261
262                                 mout->mothurOutEndLine();
263                                 mout->mothurOut("mothur > " + input);
264                                 mout->mothurOutEndLine();
265                                 
266                                 #ifdef USE_MPI
267                                         }
268                                 #endif
269                                 
270                                 if (mout->control_pressed) { input = "quit()"; }
271                                 
272                                 //allow user to omit the () on the quit command
273                                 if (input == "quit") { input = "quit()"; }
274
275                                 CommandOptionParser parser(input);
276                                 commandName = parser.getCommandString();
277                                 options = parser.getOptionString();
278                                                                                 
279                                 if (commandName != "") {
280                                         mout->executing = true;
281                                         #ifdef USE_MPI
282                                                 int pid;
283                                                 MPI_Comm_rank(MPI_COMM_WORLD, &pid); 
284                                         
285                                                 if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
286                                         #endif
287                                         //executes valid command
288                                         Command* command = cFactory->getCommand(commandName, options);
289                                         quitCommandCalled = command->execute();
290                                         mout->control_pressed = 0;
291                                         mout->executing = false;
292                                 
293                                         #ifdef USE_MPI
294                                                 }
295                                         #endif
296                                 }else {         
297                                         mout->mothurOut("Invalid."); 
298                                         mout->mothurOutEndLine();
299                                 }
300                                 
301                         }
302                         gobble(inputBatchFile);
303                 }
304                 
305                 inputBatchFile.close();
306                 return 1;
307         }
308         catch(exception& e) {
309                 mout->errorOut(e, "BatchEngine", "getInput");
310                 exit(1);
311         }
312 }
313
314
315 /***********************************************************************/
316 /***********************************************************************/
317 //This function opens the batchfile to be used by BatchEngine::getInput.
318 ScriptEngine::ScriptEngine(string path, string commandString){
319         try {
320                 globaldata = GlobalData::getInstance();
321                 
322                 //remove quotes
323                 listOfCommands = commandString.substr(1, (commandString.length()-1));
324
325                 globaldata->argv = path;
326                                 
327         }
328         catch(exception& e) {
329                 mout->errorOut(e, "ScriptEngine", "ScriptEngine");
330                 exit(1);
331         }
332 }
333
334 /***********************************************************************/
335
336 ScriptEngine::~ScriptEngine(){  }
337
338 /***********************************************************************/
339 //This Function allows the user to run a batchfile containing several commands on mothur
340 bool ScriptEngine::getInput(){
341         try {
342                         
343                 string input = "";
344                 string commandName = "";
345                 string options = "";
346                 
347                 
348                 //CommandFactory cFactory;
349                 int quitCommandCalled = 0;
350         
351                 while(quitCommandCalled == 0){
352                 
353                         input = getNextCommand(listOfCommands); 
354                         
355                         if (input == "") { input = "quit()"; }
356                         
357                         #ifdef USE_MPI
358                                         int pid;
359                                         MPI_Comm_rank(MPI_COMM_WORLD, &pid); 
360                                         
361                                         if (pid == 0) {
362                         #endif
363
364                         mout->mothurOutEndLine();
365                         mout->mothurOut("mothur > " + input);
366                         mout->mothurOutEndLine();
367                         
368                         #ifdef USE_MPI
369                                         }
370                         #endif
371                         
372                         if (mout->control_pressed) { input = "quit()"; }
373                                 
374                         //allow user to omit the () on the quit command
375                         if (input == "quit") { input = "quit()"; }
376
377                         CommandOptionParser parser(input);
378                         commandName = parser.getCommandString();
379                         options = parser.getOptionString();
380                                                                                 
381                         if (commandName != "") {
382                                 mout->executing = true;
383                                 #ifdef USE_MPI
384                                         int pid;
385                                         MPI_Comm_rank(MPI_COMM_WORLD, &pid); 
386                                         
387                                         if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
388                                 #endif
389                                 //executes valid command
390                                 Command* command = cFactory->getCommand(commandName, options);
391                                 quitCommandCalled = command->execute();
392                                 mout->control_pressed = 0;
393                                 mout->executing = false;
394                                 
395                                 #ifdef USE_MPI
396                                         }
397                                 #endif
398                         }else {         
399                                 mout->mothurOut("Invalid."); 
400                                 mout->mothurOutEndLine();
401                         }
402                         
403                 }
404                 
405                 return 1;
406         }
407         catch(exception& e) {
408                 mout->errorOut(e, "ScriptEngine", "getInput");
409                 exit(1);
410         }
411 }
412 /***********************************************************************/
413 string ScriptEngine::getNextCommand(string& commandString) {
414         try {
415                 string nextcommand = "";
416                 int count = 0;
417                 
418                 //go through string until you reach ; or end
419                 while (count < commandString.length()) { 
420                         
421                         if (commandString[count] == ';') {  break;   }
422                         else {          nextcommand += commandString[count];    }
423                         
424                         count++;
425                 }
426                 
427                 //if you are not at the end
428                 if (count != commandString.length())  {   commandString = commandString.substr(count+1, commandString.length());  }
429                 else { commandString = ""; }
430                                 
431                 
432                 //get rid of spaces in between commands if any
433                 if (commandString.length() > 0) {
434                         while (commandString[0] == ' ') {  
435                                 commandString = commandString.substr(1,commandString.length());
436                                 if (commandString.length() == 0) {  break;  }
437                         }
438                 }
439                                         
440                 return nextcommand;
441         }
442         catch(exception& e) {
443                 mout->errorOut(e, "ScriptEngine", "getNextCommand");
444                 exit(1);
445         }
446 }
447 /***********************************************************************/