12 #include "PipelineApplication.h"
13 #include "ProgramLauncher.h"
14 #include "IException.h"
15 #include "Application.h"
16 #include "Preference.h"
38 p_procAppName = procAppName;
39 p_addedCubeatt =
false;
40 p_outputListNeedsModifiers =
false;
50 for (
int i = 0; i < (int)p_apps.size(); i++) {
71 if (p_apps.size() == 0)
return;
74 bool successfulPrepare =
false;
76 while (!successfulPrepare) {
78 successfulPrepare =
true;
79 bool foundFirst =
false;
82 bool mustElimBands =
false;
85 for (
unsigned int i = 0; i < p_virtualBands.size(); i++) {
86 mustElimBands |= !p_virtualBands[i].isEmpty();
90 vector<QString> tmpFiles;
95 for (
int i = 0; i < (int)p_apps.size() && successfulPrepare; i++) {
96 if (p_apps[i] == NULL)
continue;
97 if (mustElimBands && p_apps[i]->SupportsVirtualBands()) {
98 if (i != 0 && p_virtualBands.size() != 1) {
99 QString message =
"If multiple original inputs were set in the pipeline, the first application must support virtual bands.";
103 p_apps[i]->SetVirtualBands(p_virtualBands);
104 mustElimBands =
false;
109 if (p_addedCubeatt && i != (
int)p_apps.size() - 1) {
110 delete p_apps[p_apps.size() - 1];
111 p_apps.resize(p_apps.size() - 1);
112 p_appIdentifiers.resize(p_appIdentifiers.size() - 1);
113 p_apps[p_apps.size() - 1]->SetNext(NULL);
114 p_addedCubeatt =
false;
115 successfulPrepare =
false;
122 vector<QString> empty;
123 p_apps[i]->SetVirtualBands(empty);
128 p_apps[i]->BuildParamString();
131 vector<QString> theseTempFiles = p_apps[i]->TemporaryFiles();
132 for (
int tmpFile = 0; tmpFile < (int)theseTempFiles.size(); tmpFile++) {
134 if (theseTempFiles[tmpFile].contains(
"blank")) {
135 tmpFiles.push_back(theseTempFiles[tmpFile]);
139 if (!foundFirst && p_apps[i]->Enabled()) {
142 if (p_apps[i]->InputBranches().size() != OriginalBranches().size()) {
143 QString msg =
"The program [" + p_apps[i]->Name() +
"] can not be the first in the pipeline";
144 msg +=
" because it must be run multiple times with unspecified varying inputs";
152 string msg =
"No applications are enabled in the pipeline";
157 for (
int i = 0; successfulPrepare && i < (int)tmpFiles.size(); i++) {
158 for (
int j = i + 1; j < (int)tmpFiles.size(); j++) {
159 if (tmpFiles[i] == tmpFiles[j]) {
160 QString msg =
"There is a conflict with the temporary file naming. The temporary file [";
161 msg += tmpFiles[i] +
"] is created twice.";
168 if (successfulPrepare && mustElimBands) {
169 AddToPipeline(
"cubeatt",
"~PIPELINE_RESERVED_FOR_BANDS~");
170 Application(
"~PIPELINE_RESERVED_FOR_BANDS~").SetInputParameter(
"FROM",
true);
171 Application(
"~PIPELINE_RESERVED_FOR_BANDS~").SetOutputParameter(
"TO",
"final");
172 p_addedCubeatt =
true;
173 successfulPrepare =
false;
176 int lastApp = p_apps.size()-1;
177 if (p_apps[p_apps.size()-1] == NULL)
178 lastApp = p_apps.size() - 2;
180 if (p_apps[lastApp]->GetOutputs().size() == 0) {
181 string msg =
"There are no outputted files in the pipeline. At least one program must generate an output file.";
207 pipelineProg.
SetText(p_procAppName);
212 for (
int i = p_pausePosition; i < Size(); i++) {
215 if (p_apps[i] == NULL) {
227 const vector<QString> ¶ms =
Application(i).ParamString();
228 for (
int j = 0; j < (int)params.size(); j++) {
231 QString special(params[j].mid(0, 7));
234 if (special ==
">>LIST ") {
235 QString cmd = params[j].mid(7);
238 QString listFileName = listData.takeFirst();
241 while (!listData.isEmpty()) {
242 listFile.push_back(listData.takeFirst());
245 listFile.
write(listFileName);
258 cerr <<
"Continuing ......" << endl;
267 if (!KeepTemporaryFiles()) {
268 for (
int i = 0; i < Size(); i++) {
269 if (p_apps[i] == NULL)
continue;
271 vector<QString> tmpFiles =
Application(i).TemporaryFiles();
272 for (
int file = 0; file < (int)tmpFiles.size(); file++) {
273 QFile::remove(tmpFiles[file]);
280 p_pausePosition = -1;
293 SetInputFile(QString(inputParam));
309 p_originalInput.push_back(ui.
GetFileName(inputParam));
310 p_inputBranches.push_back(inputParam);
325 p_originalInput.push_back(inputFile.
original());
326 p_inputBranches.push_back(inputFile.
original());
327 p_virtualBands.push_back(
"");
340 SetInputListFile(QString(inputParam));
358 SetInputListFile(inputFileName);
375 while (!filelist.isEmpty()) {
376 filename = filelist.takeFirst();
377 p_originalInput.push_back(filename.
expanded());
378 p_inputBranches.push_back(inputFileName.
name() +
toString(branch));
379 p_virtualBands.push_back(
"");
380 p_finalOutput.push_back(filename.
name());
385 p_outputListNeedsModifiers =
true;
401 SetInputFile(QString(inputParam), QString(virtualBandsParam));
420 p_originalInput.push_back(ui.
GetAsString(inputParam));
421 p_inputBranches.push_back(inputParam);
423 if (!virtualBandsParam.isEmpty() && ui.
WasEntered(virtualBandsParam)) {
424 p_virtualBands.push_back(ui.
GetAsString(virtualBandsParam));
427 p_virtualBands.push_back(
"");
442 SetOutputFile(QString(outputParam));
457 p_finalOutput.clear();
460 p_finalOutput.push_back(ui.
GetAsString(outputParam));
474 p_finalOutput.clear();
475 p_finalOutput.push_back(outputFile.
expanded());
488 SetOutputListFile(QString(outputFileNameParam));
507 p_finalOutput.clear();
510 for (
unsigned int i = 0; i < p_originalInput.size(); i++) {
511 p_finalOutput.push_back(
FileName(p_originalInput[i]).name());
514 p_outputListNeedsModifiers =
true;
526 p_finalOutput.clear();
531 while (!filelist.isEmpty()) {
532 filename = filelist.takeFirst().expanded();
533 p_finalOutput.push_back(filename);
536 p_outputListNeedsModifiers =
false;
547 p_keepTemporary = keep;
560 QString pauseAppId =
"";
563 p_apps.push_back(pauseApp);
564 p_appIdentifiers.push_back(pauseAppId);
583 for (
unsigned int appIdentifier = 0; appIdentifier < p_appIdentifiers.size(); appIdentifier++) {
584 if (p_appIdentifiers[appIdentifier] == identifier) {
585 QString message =
"The application identifier [" + identifier +
"] is not unique. " +
586 "Please providing a unique identifier";
593 QString cubeAttId =
"";
594 if (p_addedCubeatt) {
595 cubeAtt = p_apps[p_apps.size()-1];
596 cubeAttId = p_appIdentifiers[p_appIdentifiers.size()-1];
597 p_apps.resize(p_apps.size() - 1);
598 p_appIdentifiers.resize(p_appIdentifiers.size() - 1);
599 p_apps[p_apps.size()-1]->
SetNext(NULL);
604 QString pauseAppId =
"";
606 for (
int iapp = 0; iapp < (int) p_apps.size(); iapp++)
607 if (p_apps[iapp] != NULL) appsSize++;
610 if (p_apps.size() == 0) {
614 if (p_apps[p_apps.size()-1] != NULL)
620 p_appIdentifiers.push_back(identifier);
624 p_apps[p_apps.size()-1]->SetNext(cubeAtt);
626 p_apps.push_back(cubeAtt);
627 p_appIdentifiers.push_back(cubeAttId);
645 for (
unsigned int appIdentifier = 0; appIdentifier < p_appIdentifiers.size(); appIdentifier++) {
646 if (p_appIdentifiers[appIdentifier] == appname) {
647 QString message =
"The application identifier [" + appname +
"] is not unique. Please use " +
648 "the other AddToPipeline method providing a unique identifier";
655 QString cubeAttId =
"";
656 if (p_addedCubeatt) {
657 cubeAtt = p_apps[p_apps.size()-1];
658 cubeAttId = p_appIdentifiers[p_appIdentifiers.size()-1];
659 p_apps.resize(p_apps.size() - 1);
660 p_appIdentifiers.resize(p_appIdentifiers.size() - 1);
661 p_apps[p_apps.size()-1]->
SetNext(NULL);
665 if (p_apps.size() == 0) {
669 if (p_apps[p_apps.size()-1] != NULL)
675 p_appIdentifiers.push_back(appname);
679 p_apps[p_apps.size()-1]->SetNext(cubeAtt);
681 p_apps.push_back(cubeAtt);
682 p_appIdentifiers.push_back(cubeAttId);
700 while (!found && index < Size()) {
701 if (p_appIdentifiers[index] == identifier) {
710 QString msg =
"Application identified by [" + identifier +
"] has not been added to the pipeline";
714 return *p_apps[index];
727 if (index > Size()) {
728 QString msg =
"Index [" + QString(index) +
"] out of bounds";
732 return *p_apps[index];
750 for (appIndex = 0; appIndex < (int)p_apps.size() &&
751 p_apps[appIndex]->Name() != appname; appIndex++) {
752 if (p_apps[appIndex] == NULL)
continue;
753 p_apps[appIndex]->Disable();
759 if (appIndex >= (
int)p_apps.size()) {
760 QString msg =
"Pipeline could not find application [" + appname +
"]";
779 int appIndex = p_apps.size() - 1;
780 for (appIndex = p_apps.size() - 1; appIndex >= 0 && p_apps[appIndex]->Name() != appname; appIndex --) {
781 if (p_apps[appIndex] == NULL)
continue;
782 p_apps[appIndex]->Disable();
786 QString msg =
"Pipeline could not find application [" + appname +
"]";
806 QString output = ((p_finalOutput.size() != 0) ? p_finalOutput[0] :
"");
808 if (p_apps.size() == 0)
return output;
810 if (p_finalOutput.size() > 1) {
811 if ((
unsigned int)branch >= p_finalOutput.size()) {
812 QString msg =
"Output not set for branch [" + QString(branch) +
"]";
816 if (!p_outputListNeedsModifiers) {
817 return p_finalOutput[branch];
820 output = p_finalOutput[branch];
826 if (last == NULL) last = p_apps[p_apps.size()-2];
829 if (output ==
"" || p_finalOutput.size() > 1) {
839 if (addModifiers && p_finalOutput.size() > 1)
847 if (p_finalOutput.size() <= 1) {
851 if (addModifiers && p_finalOutput.size() > 1)
857 else if (addModifiers) {
865 if (p_finalOutput.size() > 1) {
883 Pvl &pref = Preference::Preferences();
884 return pref.
findGroup(
"DataDirectory")[
"Temporary"];
897 for (
int i = 0; i < Size(); i++) {
898 if (p_apps[i] != NULL) p_apps[i]->Enable();
922 if (!pipeline.
Name().isEmpty()) {
923 os <<
"PIPELINE -------> " << pipeline.
Name() <<
" <------- PIPELINE" << endl;
926 for (
int i = 0; i < pipeline.
Size(); i++) {
929 for (
int j = 0; j < (int)params.size(); j++) {
930 QString special(params[j].mid(0, 7));
931 if (special ==
">>LIST ") {
932 QString cmd = params[j].mid(7);
935 QString file = listFileData.takeFirst();
936 os <<
"echo -e \"" << listFileData.join(
"\\n") <<
"\" > " << file << endl;
946 for (
int i = 0; i < pipeline.
Size(); i++) {
949 for (
int file = 0; file < (int)tmpFiles.size(); file++) {
950 if (!tmpFiles[file].contains(
"blank")) {
951 os <<
"rm " << tmpFiles[file] << endl;
958 if (!pipeline.
Name().isEmpty()) {
959 os <<
"PIPELINE -------> " << pipeline.
Name() <<
" <------- PIPELINE" << endl;