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();
34 globaldata->argv = path;
38 /***********************************************************************/
40 InteractEngine::~InteractEngine(){}
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(){
48 string commandName = "";
50 int quitCommandCalled = 0;
52 while(quitCommandCalled != 1){
57 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
58 MPI_Comm_size(MPI_COMM_WORLD, &processors);
64 mout->mothurOutEndLine();
67 mout->mothurOutEndLine();
69 if (mout->control_pressed) { input = "quit()"; }
71 //allow user to omit the () on the quit command
72 if (input == "quit") { input = "quit()"; }
77 for(int i = 1; i < processors; i++) {
78 int length = input.length();
79 MPI_Send(&length, 1, MPI_INT, i, 2001, MPI_COMM_WORLD);
80 MPI_Send(&input[0], length, MPI_CHAR, i, 2001, MPI_COMM_WORLD);
85 MPI_Recv(&length, 1, MPI_INT, 0, 2001, MPI_COMM_WORLD, &status);
87 char* tempBuf = new char[length];
88 MPI_Recv(&tempBuf[0], length, MPI_CHAR, 0, 2001, MPI_COMM_WORLD, &status);
91 if (input.length() > length) { input = input.substr(0, length); }
98 CommandOptionParser parser(input);
99 commandName = parser.getCommandString();
101 options = parser.getOptionString();
103 if (commandName != "") {
104 mout->executing = true;
107 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
109 if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
110 //cout << pid << " is in execute " << commandName << endl;
112 //executes valid command
113 Command* command = cFactory->getCommand(commandName, options);
114 quitCommandCalled = command->execute();
115 mout->control_pressed = 0;
116 mout->executing = false;
122 mout->mothurOut("Invalid.");
123 mout->mothurOutEndLine();
128 catch(exception& e) {
129 mout->errorOut(e, "InteractEngine", "getInput");
133 /***********************************************************************/
134 string Engine::getCommand() {
137 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
139 char* nextCommand = NULL;
140 nextCommand = readline("mothur > ");
142 if(nextCommand != NULL) { add_history(nextCommand); }
143 else{ //^D causes null string and we want it to quit mothur
144 nextCommand = "quit";
145 mout->mothurOut(nextCommand);
148 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
151 string nextCommand = "";
152 mout->mothurOut("mothur > ");
153 getline(cin, nextCommand);
154 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
159 string nextCommand = "";
161 mout->mothurOut("mothur > ");
162 getline(cin, nextCommand);
163 mout->mothurOutJustToLog(toString(nextCommand));
170 catch(exception& e) {
171 mout->errorOut(e, "Engine", "getCommand");
175 /***********************************************************************/
176 //This function opens the batchfile to be used by BatchEngine::getInput.
177 BatchEngine::BatchEngine(string path, string batchFileName){
179 globaldata = GlobalData::getInstance();
181 openedBatch = openInputFile(batchFileName, inputBatchFile);
182 globaldata->argv = path;
185 catch(exception& e) {
186 mout->errorOut(e, "BatchEngine", "BatchEngine");
191 /***********************************************************************/
193 BatchEngine::~BatchEngine(){ }
195 /***********************************************************************/
196 //This Function allows the user to run a batchfile containing several commands on Dotur
197 bool BatchEngine::getInput(){
199 //check if this is a valid batchfile
200 if (openedBatch == 1) {
201 mout->mothurOut("unable to open batchfile");
202 mout->mothurOutEndLine();
207 string commandName = "";
210 //CommandFactory cFactory;
211 int quitCommandCalled = 0;
213 while(quitCommandCalled == 0){
218 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
219 MPI_Comm_size(MPI_COMM_WORLD, &processors);
225 input = getNextCommand(inputBatchFile);
230 for(int i = 1; i < processors; i++) {
231 int length = input.length();
232 MPI_Send(&length, 1, MPI_INT, i, 2001, MPI_COMM_WORLD);
233 MPI_Send(&input[0], length, MPI_CHAR, i, 2001, MPI_COMM_WORLD);
238 MPI_Recv(&length, 1, MPI_INT, 0, 2001, MPI_COMM_WORLD, &status);
240 char* tempBuf = new char[length];
241 MPI_Recv(&tempBuf[0], length, MPI_CHAR, 0, 2001, MPI_COMM_WORLD, &status);
244 if (input.length() > length) { input = input.substr(0, length); }
253 if (input[0] != '#') {
255 mout->mothurOutEndLine();
256 mout->mothurOut("mothur > " + input);
257 mout->mothurOutEndLine();
259 if (mout->control_pressed) { input = "quit()"; }
261 //allow user to omit the () on the quit command
262 if (input == "quit") { input = "quit()"; }
264 CommandOptionParser parser(input);
265 commandName = parser.getCommandString();
266 options = parser.getOptionString();
268 if (commandName != "") {
269 mout->executing = true;
272 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
274 //cout << pid << " is here " << commandName << '\t' << count << endl;
275 if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
277 //executes valid command
278 Command* command = cFactory->getCommand(commandName, options);
279 quitCommandCalled = command->execute();
280 mout->control_pressed = 0;
281 mout->executing = false;
287 mout->mothurOut("Invalid.");
288 mout->mothurOutEndLine();
292 gobble(inputBatchFile);
295 inputBatchFile.close();
298 catch(exception& e) {
299 mout->errorOut(e, "BatchEngine", "getInput");
303 /***********************************************************************/
304 string BatchEngine::getNextCommand(ifstream& inputBatchFile) {
307 string nextcommand = "";
309 if (inputBatchFile.eof()) { nextcommand = "quit()"; }
310 else { nextcommand = getline(inputBatchFile); }
314 catch(exception& e) {
315 mout->errorOut(e, "BatchEngine", "getNextCommand");
320 /***********************************************************************/
321 /***********************************************************************/
322 //This function opens the batchfile to be used by BatchEngine::getInput.
323 ScriptEngine::ScriptEngine(string path, string commandString){
325 globaldata = GlobalData::getInstance();
328 listOfCommands = commandString.substr(1, (commandString.length()-1));
330 globaldata->argv = path;
333 catch(exception& e) {
334 mout->errorOut(e, "ScriptEngine", "ScriptEngine");
339 /***********************************************************************/
341 ScriptEngine::~ScriptEngine(){ }
343 /***********************************************************************/
344 //This Function allows the user to run a batchfile containing several commands on mothur
345 bool ScriptEngine::getInput(){
349 string commandName = "";
353 //CommandFactory cFactory;
354 int quitCommandCalled = 0;
356 while(quitCommandCalled == 0){
361 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
362 MPI_Comm_size(MPI_COMM_WORLD, &processors);
368 input = getNextCommand(listOfCommands);
370 if (input == "") { input = "quit()"; }
372 mout->mothurOutEndLine();
373 mout->mothurOut("mothur > " + input);
374 mout->mothurOutEndLine();
378 for(int i = 1; i < processors; i++) {
379 int length = input.length();
380 MPI_Send(&length, 1, MPI_INT, i, 2001, MPI_COMM_WORLD);
381 MPI_Send(&input[0], length, MPI_CHAR, i, 2001, MPI_COMM_WORLD);
386 MPI_Recv(&length, 1, MPI_INT, 0, 2001, MPI_COMM_WORLD, &status);
388 char* tempBuf = new char[length];
389 MPI_Recv(&tempBuf[0], length, MPI_CHAR, 0, 2001, MPI_COMM_WORLD, &status);
392 if (input.length() > length) { input = input.substr(0, length); }
400 if (mout->control_pressed) { input = "quit()"; }
402 //allow user to omit the () on the quit command
403 if (input == "quit") { input = "quit()"; }
405 CommandOptionParser parser(input);
406 commandName = parser.getCommandString();
407 options = parser.getOptionString();
409 if (commandName != "") {
410 mout->executing = true;
412 int pid, numProcesses;
415 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
416 MPI_Comm_size(MPI_COMM_WORLD, &numProcesses);
418 //cout << pid << " is here " << commandName << endl;
419 if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
420 //cout << pid << " is in execute" << endl;
422 //executes valid command
423 Command* command = cFactory->getCommand(commandName, options);
424 quitCommandCalled = command->execute();
425 mout->control_pressed = 0;
426 mout->executing = false;
429 //cout << pid << " is done in execute" << endl;
433 mout->mothurOut("Invalid.");
434 mout->mothurOutEndLine();
442 catch(exception& e) {
443 mout->errorOut(e, "ScriptEngine", "getInput");
447 /***********************************************************************/
448 string ScriptEngine::getNextCommand(string& commandString) {
451 string nextcommand = "";
454 //go through string until you reach ; or end
455 while (count < commandString.length()) {
457 if (commandString[count] == ';') { break; }
458 else { nextcommand += commandString[count]; }
463 //if you are not at the end
464 if (count != commandString.length()) { commandString = commandString.substr(count+1, commandString.length()); }
465 else { commandString = ""; }
468 //get rid of spaces in between commands if any
469 if (commandString.length() > 0) {
470 while (commandString[0] == ' ') {
471 commandString = commandString.substr(1,commandString.length());
472 if (commandString.length() == 0) { break; }
478 catch(exception& e) {
479 mout->errorOut(e, "ScriptEngine", "getNextCommand");
483 /***********************************************************************/