]> git.donarmstrong.com Git - mothur.git/blob - engine.cpp
some bug fixes
[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
55                         mout->mothurOutEndLine();
56                         
57                         input = getCommand();   
58                         mout->mothurOutEndLine();       
59                 
60                         if (mout->control_pressed) { input = "quit()"; }
61                         
62                         //allow user to omit the () on the quit command
63                         if (input == "quit") { input = "quit()"; }
64                         
65                         CommandOptionParser parser(input);
66                         commandName = parser.getCommandString();
67         
68                         options = parser.getOptionString();
69                         
70                         if (commandName != "") {
71                                         mout->executing = true;
72                                         #ifdef USE_MPI
73                                                 int pid;
74                                                 MPI_Comm_rank(MPI_COMM_WORLD, &pid); 
75  
76 //cout << pid << " is here " << commandName << endl;
77                                                 if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
78                                         #endif
79                                         //executes valid command
80                                         Command* command = cFactory->getCommand(commandName, options);
81                                         quitCommandCalled = command->execute();
82                                         mout->control_pressed = 0;
83                                         mout->executing = false;
84                                                                                 
85                                         #ifdef USE_MPI
86                                                 }
87                                         #endif
88                                 }else {         
89                                         mout->mothurOut("Invalid."); 
90                                         mout->mothurOutEndLine();
91                                 }
92                 }       
93                 return 1;
94         }
95         catch(exception& e) {
96                 mout->errorOut(e, "InteractEngine", "getInput");
97                 exit(1);
98         }
99 }
100 /***********************************************************************/
101 string Engine::getCommand()  {
102         try {
103                 #ifdef USE_MPI
104                         MPI_Barrier(MPI_COMM_WORLD);
105                 #endif
106         
107                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
108                         #ifdef USE_READLINE
109                                 char* nextCommand = NULL;
110                                 nextCommand = readline("mothur > ");
111                                 
112                                 if(nextCommand != NULL) {  add_history(nextCommand);  } 
113                                 else{ //^D causes null string and we want it to quit mothur
114                                         nextCommand = "quit"; 
115                                         mout->mothurOut(nextCommand);
116                                 }       
117                                 
118                                 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
119                                 return nextCommand;
120                         #else
121                                 string nextCommand = "";
122                                 mout->mothurOut("mothur > ");
123                                 getline(cin, nextCommand);
124                                 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
125                                 
126                                 return nextCommand;
127                         #endif
128                 #else
129                                 string nextCommand = "";
130                                 
131                                 mout->mothurOut("mothur > ");
132                                 getline(cin, nextCommand);
133                                 mout->mothurOutJustToLog(toString(nextCommand));
134                                 
135                                 return nextCommand;
136                 #endif
137         
138                                                 
139         }
140         catch(exception& e) {
141                 mout->errorOut(e, "Engine", "getCommand");
142                 exit(1);
143         }
144 }
145 /***********************************************************************/
146 //This function opens the batchfile to be used by BatchEngine::getInput.
147 BatchEngine::BatchEngine(string path, string batchFileName){
148         try {
149                 globaldata = GlobalData::getInstance();
150         
151                 openedBatch = openInputFile(batchFileName, inputBatchFile);
152                 globaldata->argv = path;
153                                 
154         }
155         catch(exception& e) {
156                 mout->errorOut(e, "BatchEngine", "BatchEngine");
157                 exit(1);
158         }
159 }
160
161 /***********************************************************************/
162
163 BatchEngine::~BatchEngine(){    }
164
165 /***********************************************************************/
166 //This Function allows the user to run a batchfile containing several commands on Dotur
167 bool BatchEngine::getInput(){
168         try {
169                 //check if this is a valid batchfile
170                 if (openedBatch == 1) {  
171                         mout->mothurOut("unable to open batchfile");  
172                         mout->mothurOutEndLine();
173                         return 1; 
174                 }
175         
176                 string input = "";
177                 string commandName = "";
178                 string options = "";
179                 
180                 //CommandFactory cFactory;
181                 int quitCommandCalled = 0;
182             int count = 0;
183                 while(quitCommandCalled == 0){
184         
185                         input = getNextCommand(inputBatchFile);
186                         count++;
187                         
188                         if (input[0] != '#') {
189                                 
190                                 mout->mothurOutEndLine();
191                                 mout->mothurOut("mothur > " + input);
192                                 mout->mothurOutEndLine();
193                                                         
194                                 if (mout->control_pressed) { input = "quit()"; }
195                                 
196                                 //allow user to omit the () on the quit command
197                                 if (input == "quit") { input = "quit()"; }
198
199                                 CommandOptionParser parser(input);
200                                 commandName = parser.getCommandString();
201                                 options = parser.getOptionString();
202                                                                                 
203                                 if (commandName != "") {
204                                         mout->executing = true;
205                                         #ifdef USE_MPI
206                                                 int pid;
207                                                 MPI_Comm_rank(MPI_COMM_WORLD, &pid); 
208                                                 
209 //cout << pid << " is here " << commandName << '\t' << count << endl;
210                                                 if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
211                                         #endif
212                                         //executes valid command
213                                         Command* command = cFactory->getCommand(commandName, options);
214                                         quitCommandCalled = command->execute();
215                                         mout->control_pressed = 0;
216                                         mout->executing = false;
217                                                                                 
218                                         #ifdef USE_MPI
219                                                 }
220                                         #endif
221                                 }else {         
222                                         mout->mothurOut("Invalid."); 
223                                         mout->mothurOutEndLine();
224                                 }
225                                 
226                         }
227                         gobble(inputBatchFile);
228                 }
229                 
230                 inputBatchFile.close();
231                 return 1;
232         }
233         catch(exception& e) {
234                 mout->errorOut(e, "BatchEngine", "getInput");
235                 exit(1);
236         }
237 }
238 /***********************************************************************/
239 string BatchEngine::getNextCommand(ifstream& inputBatchFile) {
240         try {
241                 
242                 #ifdef USE_MPI
243                         int err = MPI_Barrier(MPI_COMM_WORLD);
244 //cout << "barrier = " << err << '\t' << MPI_SUCCESS << endl;
245                 #endif
246                 
247                 string nextcommand = "";
248                 
249                 if (inputBatchFile.eof()) { nextcommand = "quit()"; }
250                 else { nextcommand = getline(inputBatchFile); }
251                 
252                 return nextcommand;
253         }
254         catch(exception& e) {
255                 mout->errorOut(e, "BatchEngine", "getNextCommand");
256                 exit(1);
257         }
258 }
259
260 /***********************************************************************/
261 /***********************************************************************/
262 //This function opens the batchfile to be used by BatchEngine::getInput.
263 ScriptEngine::ScriptEngine(string path, string commandString){
264         try {
265                 globaldata = GlobalData::getInstance();
266                 
267                 //remove quotes
268                 listOfCommands = commandString.substr(1, (commandString.length()-1));
269
270                 globaldata->argv = path;
271                                 
272         }
273         catch(exception& e) {
274                 mout->errorOut(e, "ScriptEngine", "ScriptEngine");
275                 exit(1);
276         }
277 }
278
279 /***********************************************************************/
280
281 ScriptEngine::~ScriptEngine(){  }
282
283 /***********************************************************************/
284 //This Function allows the user to run a batchfile containing several commands on mothur
285 bool ScriptEngine::getInput(){
286         try {
287                         
288                 string input = "";
289                 string commandName = "";
290                 string options = "";
291                 
292                 
293                 //CommandFactory cFactory;
294                 int quitCommandCalled = 0;
295         
296                 while(quitCommandCalled == 0){
297                 
298                         input = getNextCommand(listOfCommands); 
299                         
300                         if (input == "") { input = "quit()"; }
301                         
302                         mout->mothurOutEndLine();
303                         mout->mothurOut("mothur > " + input);
304                         mout->mothurOutEndLine();
305                         
306                         if (mout->control_pressed) { input = "quit()"; }
307                                 
308                         //allow user to omit the () on the quit command
309                         if (input == "quit") { input = "quit()"; }
310
311                         CommandOptionParser parser(input);
312                         commandName = parser.getCommandString();
313                         options = parser.getOptionString();
314                                                                                 
315                         if (commandName != "") {
316                                         mout->executing = true;
317                                         #ifdef USE_MPI
318                                                 int pid, numProcesses;
319                                                 MPI_Status status; 
320                                                 
321                                                 MPI_Comm_rank(MPI_COMM_WORLD, &pid); 
322                                                 MPI_Comm_size(MPI_COMM_WORLD, &numProcesses); 
323                                         
324 //cout << pid << " is here " << commandName  << endl;
325                                                 if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
326                                                         //cout << pid << " is in execute" << endl;      
327                                         #endif
328                                         //executes valid command
329                                         Command* command = cFactory->getCommand(commandName, options);
330                                         quitCommandCalled = command->execute();
331                                         mout->control_pressed = 0;
332                                         mout->executing = false;
333                                                                         
334                                         #ifdef USE_MPI
335                                         //cout << pid << " is done in execute" << endl;
336                                                 }
337                                         #endif
338                                 }else {         
339                                         mout->mothurOut("Invalid."); 
340                                         mout->mothurOutEndLine();
341                                 }
342
343                         
344                 }
345                 
346                 return 1;
347         }
348         catch(exception& e) {
349                 mout->errorOut(e, "ScriptEngine", "getInput");
350                 exit(1);
351         }
352 }
353 /***********************************************************************/
354 string ScriptEngine::getNextCommand(string& commandString) {
355         try {
356                 
357                 #ifdef USE_MPI
358                         MPI_Barrier(MPI_COMM_WORLD);
359                 #endif
360                 
361                 string nextcommand = "";
362                 int count = 0;
363                 
364                 //go through string until you reach ; or end
365                 while (count < commandString.length()) { 
366                         
367                         if (commandString[count] == ';') {  break;   }
368                         else {          nextcommand += commandString[count];    }
369                         
370                         count++;
371                 }
372                 
373                 //if you are not at the end
374                 if (count != commandString.length())  {   commandString = commandString.substr(count+1, commandString.length());  }
375                 else { commandString = ""; }
376                                 
377                 
378                 //get rid of spaces in between commands if any
379                 if (commandString.length() > 0) {
380                         while (commandString[0] == ' ') {  
381                                 commandString = commandString.substr(1,commandString.length());
382                                 if (commandString.length() == 0) {  break;  }
383                         }
384                 }
385                 
386                 return nextcommand;
387         }
388         catch(exception& e) {
389                 mout->errorOut(e, "ScriptEngine", "getNextCommand");
390                 exit(1);
391         }
392 }
393 /***********************************************************************/