5 * Created by Pat Schloss on 8/15/08.
6 * Copyright 2008 Patrick D. Schloss. All rights reserved.
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.
17 /***********************************************************************/
20 cFactory = CommandFactory::getInstance();
21 mout = MothurOut::getInstance();
24 mout->errorOut(e, "Engine", "Engine");
29 /***********************************************************************/
31 InteractEngine::InteractEngine(string path){
33 globaldata = GlobalData::getInstance();
35 string temppath = path.substr(0, (path.find_last_of('m')));
37 //this will happen if you set the path variable to contain mothur's exe location
40 string envPath = getenv("PATH");
42 //delimiting path char
44 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
50 //break apart path variable by ':'
52 splitAtChar(envPath, dirs, delim);
54 //get path related to mothur
55 string mothurPath = "";
56 for (int i = 0; i < dirs.size(); i++) {
57 //to lower so we can find it
58 string tempLower = "";
59 for (int j = 0; j < dirs[i].length(); j++) { tempLower += tolower(dirs[i][j]); }
61 //is this mothurs path?
62 if (tempLower.find("mothur") != -1) { mothurPath = dirs[i]; break; }
65 //add mothur so it looks like what argv would look like
66 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
67 mothurPath += "/mothur";
69 mothurPath += "\\mothur";
75 globaldata->argv = path;
78 /***********************************************************************/
80 InteractEngine::~InteractEngine(){}
82 /***********************************************************************/
83 //This function allows the user to input commands one line at a time until they quit.
84 //If the command is garbage it does nothing.
85 bool InteractEngine::getInput(){
88 string commandName = "";
90 int quitCommandCalled = 0;
92 while(quitCommandCalled != 1){
97 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
98 MPI_Comm_size(MPI_COMM_WORLD, &processors);
104 mout->mothurOutEndLine();
106 input = getCommand();
107 mout->mothurOutEndLine();
109 if (mout->control_pressed) { input = "quit()"; }
111 //allow user to omit the () on the quit command
112 if (input == "quit") { input = "quit()"; }
117 for(int i = 1; i < processors; i++) {
118 int length = input.length();
119 MPI_Send(&length, 1, MPI_INT, i, 2001, MPI_COMM_WORLD);
120 MPI_Send(&input[0], length, MPI_CHAR, i, 2001, MPI_COMM_WORLD);
125 MPI_Recv(&length, 1, MPI_INT, 0, 2001, MPI_COMM_WORLD, &status);
127 char* tempBuf = new char[length];
128 MPI_Recv(&tempBuf[0], length, MPI_CHAR, 0, 2001, MPI_COMM_WORLD, &status);
131 if (input.length() > length) { input = input.substr(0, length); }
138 CommandOptionParser parser(input);
139 commandName = parser.getCommandString();
141 options = parser.getOptionString();
143 if (commandName != "") {
144 mout->executing = true;
147 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
149 if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
150 //cout << pid << " is in execute " << commandName << endl;
152 //executes valid command
153 Command* command = cFactory->getCommand(commandName, options);
154 quitCommandCalled = command->execute();
155 mout->control_pressed = 0;
156 mout->executing = false;
162 mout->mothurOut("Invalid.");
163 mout->mothurOutEndLine();
168 catch(exception& e) {
169 mout->errorOut(e, "InteractEngine", "getInput");
173 /***********************************************************************/
174 string Engine::getCommand() {
177 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
179 char* nextCommand = NULL;
180 nextCommand = readline("mothur > ");
182 if(nextCommand != NULL) { add_history(nextCommand); }
183 else{ //^D causes null string and we want it to quit mothur
184 nextCommand = "quit";
185 mout->mothurOut(nextCommand);
188 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
191 string nextCommand = "";
192 mout->mothurOut("mothur > ");
193 getline(cin, nextCommand);
194 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
199 string nextCommand = "";
201 mout->mothurOut("mothur > ");
202 getline(cin, nextCommand);
203 mout->mothurOutJustToLog(toString(nextCommand));
210 catch(exception& e) {
211 mout->errorOut(e, "Engine", "getCommand");
215 /***********************************************************************/
216 //This function opens the batchfile to be used by BatchEngine::getInput.
217 BatchEngine::BatchEngine(string path, string batchFileName){
219 globaldata = GlobalData::getInstance();
221 openedBatch = openInputFile(batchFileName, inputBatchFile);
223 string temppath = path.substr(0, (path.find_last_of('m')));
225 //this will happen if you set the path variable to contain mothur's exe location
226 if (temppath == "") {
228 string envPath = getenv("PATH");
230 //delimiting path char
232 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
238 //break apart path variable by ':'
240 splitAtChar(envPath, dirs, delim);
242 //get path related to mothur
243 string mothurPath = "";
244 for (int i = 0; i < dirs.size(); i++) {
245 //to lower so we can find it
246 string tempLower = "";
247 for (int j = 0; j < dirs[i].length(); j++) { tempLower += tolower(dirs[i][j]); }
249 //is this mothurs path?
250 if (tempLower.find("mothur") != -1) { mothurPath = dirs[i]; break; }
253 //add mothur so it looks like what argv would look like
254 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
255 mothurPath += "/mothur";
257 mothurPath += "\\mothur";
263 globaldata->argv = path;
266 catch(exception& e) {
267 mout->errorOut(e, "BatchEngine", "BatchEngine");
272 /***********************************************************************/
274 BatchEngine::~BatchEngine(){ }
276 /***********************************************************************/
277 //This Function allows the user to run a batchfile containing several commands on Dotur
278 bool BatchEngine::getInput(){
280 //check if this is a valid batchfile
281 if (openedBatch == 1) {
282 mout->mothurOut("unable to open batchfile");
283 mout->mothurOutEndLine();
288 string commandName = "";
291 //CommandFactory cFactory;
292 int quitCommandCalled = 0;
294 while(quitCommandCalled == 0){
299 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
300 MPI_Comm_size(MPI_COMM_WORLD, &processors);
306 input = getNextCommand(inputBatchFile);
311 for(int i = 1; i < processors; i++) {
312 int length = input.length();
313 MPI_Send(&length, 1, MPI_INT, i, 2001, MPI_COMM_WORLD);
314 MPI_Send(&input[0], length, MPI_CHAR, i, 2001, MPI_COMM_WORLD);
319 MPI_Recv(&length, 1, MPI_INT, 0, 2001, MPI_COMM_WORLD, &status);
321 char* tempBuf = new char[length];
322 MPI_Recv(&tempBuf[0], length, MPI_CHAR, 0, 2001, MPI_COMM_WORLD, &status);
325 if (input.length() > length) { input = input.substr(0, length); }
334 if (input[0] != '#') {
336 mout->mothurOutEndLine();
337 mout->mothurOut("mothur > " + input);
338 mout->mothurOutEndLine();
340 if (mout->control_pressed) { input = "quit()"; }
342 //allow user to omit the () on the quit command
343 if (input == "quit") { input = "quit()"; }
345 CommandOptionParser parser(input);
346 commandName = parser.getCommandString();
347 options = parser.getOptionString();
349 if (commandName != "") {
350 mout->executing = true;
353 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
355 //cout << pid << " is here " << commandName << '\t' << count << endl;
356 if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
358 //executes valid command
359 Command* command = cFactory->getCommand(commandName, options);
360 quitCommandCalled = command->execute();
361 mout->control_pressed = 0;
362 mout->executing = false;
368 mout->mothurOut("Invalid.");
369 mout->mothurOutEndLine();
373 gobble(inputBatchFile);
376 inputBatchFile.close();
379 catch(exception& e) {
380 mout->errorOut(e, "BatchEngine", "getInput");
384 /***********************************************************************/
385 string BatchEngine::getNextCommand(ifstream& inputBatchFile) {
388 string nextcommand = "";
390 if (inputBatchFile.eof()) { nextcommand = "quit()"; }
391 else { nextcommand = getline(inputBatchFile); }
395 catch(exception& e) {
396 mout->errorOut(e, "BatchEngine", "getNextCommand");
401 /***********************************************************************/
402 /***********************************************************************/
403 //This function opens the batchfile to be used by BatchEngine::getInput.
404 ScriptEngine::ScriptEngine(string path, string commandString){
406 globaldata = GlobalData::getInstance();
409 listOfCommands = commandString.substr(1, (commandString.length()-1));
411 string temppath = path.substr(0, (path.find_last_of('m')));
413 //this will happen if you set the path variable to contain mothur's exe location
414 if (temppath == "") {
416 string envPath = getenv("PATH");
418 //delimiting path char
420 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
426 //break apart path variable by ':'
428 splitAtChar(envPath, dirs, delim);
430 //get path related to mothur
431 string mothurPath = "";
432 for (int i = 0; i < dirs.size(); i++) {
433 //to lower so we can find it
434 string tempLower = "";
435 for (int j = 0; j < dirs[i].length(); j++) { tempLower += tolower(dirs[i][j]); }
437 //is this mothurs path?
438 if (tempLower.find("mothur") != -1) { mothurPath = dirs[i]; break; }
441 //add mothur so it looks like what argv would look like
442 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
443 mothurPath += "/mothur";
445 mothurPath += "\\mothur";
451 globaldata->argv = path;
454 catch(exception& e) {
455 mout->errorOut(e, "ScriptEngine", "ScriptEngine");
460 /***********************************************************************/
462 ScriptEngine::~ScriptEngine(){ }
464 /***********************************************************************/
465 //This Function allows the user to run a batchfile containing several commands on mothur
466 bool ScriptEngine::getInput(){
470 string commandName = "";
474 //CommandFactory cFactory;
475 int quitCommandCalled = 0;
477 while(quitCommandCalled == 0){
482 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
483 MPI_Comm_size(MPI_COMM_WORLD, &processors);
489 input = getNextCommand(listOfCommands);
491 if (input == "") { input = "quit()"; }
493 mout->mothurOutEndLine();
494 mout->mothurOut("mothur > " + input);
495 mout->mothurOutEndLine();
499 for(int i = 1; i < processors; i++) {
500 int length = input.length();
501 MPI_Send(&length, 1, MPI_INT, i, 2001, MPI_COMM_WORLD);
502 MPI_Send(&input[0], length, MPI_CHAR, i, 2001, MPI_COMM_WORLD);
507 MPI_Recv(&length, 1, MPI_INT, 0, 2001, MPI_COMM_WORLD, &status);
509 char* tempBuf = new char[length];
510 MPI_Recv(&tempBuf[0], length, MPI_CHAR, 0, 2001, MPI_COMM_WORLD, &status);
513 if (input.length() > length) { input = input.substr(0, length); }
521 if (mout->control_pressed) { input = "quit()"; }
523 //allow user to omit the () on the quit command
524 if (input == "quit") { input = "quit()"; }
526 CommandOptionParser parser(input);
527 commandName = parser.getCommandString();
528 options = parser.getOptionString();
530 if (commandName != "") {
531 mout->executing = true;
533 int pid, numProcesses;
536 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
537 MPI_Comm_size(MPI_COMM_WORLD, &numProcesses);
539 //cout << pid << " is here " << commandName << endl;
540 if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
541 //cout << pid << " is in execute" << endl;
543 //executes valid command
544 Command* command = cFactory->getCommand(commandName, options);
545 quitCommandCalled = command->execute();
546 mout->control_pressed = 0;
547 mout->executing = false;
550 //cout << pid << " is done in execute" << endl;
554 mout->mothurOut("Invalid.");
555 mout->mothurOutEndLine();
563 catch(exception& e) {
564 mout->errorOut(e, "ScriptEngine", "getInput");
568 /***********************************************************************/
569 string ScriptEngine::getNextCommand(string& commandString) {
572 string nextcommand = "";
575 //go through string until you reach ; or end
576 while (count < commandString.length()) {
578 if (commandString[count] == ';') { break; }
579 else { nextcommand += commandString[count]; }
584 //if you are not at the end
585 if (count != commandString.length()) { commandString = commandString.substr(count+1, commandString.length()); }
586 else { commandString = ""; }
589 //get rid of spaces in between commands if any
590 if (commandString.length() > 0) {
591 while (commandString[0] == ' ') {
592 commandString = commandString.substr(1,commandString.length());
593 if (commandString.length() == 0) { break; }
599 catch(exception& e) {
600 mout->errorOut(e, "ScriptEngine", "getNextCommand");
604 /***********************************************************************/