00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00036 #include "bbtkExecuter.h"
00037 #include "bbtkMessageManager.h"
00038 #include "bbtkFactory.h"
00039 #include "bbtkUtilities.h"
00040
00041 #include <fstream>
00042
00043 #ifdef USE_WXWIDGETS
00044 #include <wx/textdlg.h>
00045 #endif
00046
00047 #include "bbtkWxBlackBox.h"
00048
00049 #include "bbtkConfigurationFile.h"
00050
00051 namespace bbtk
00052 {
00053
00054 Executer::Pointer Executer::New()
00055 {
00056 bbtkDebugMessage("object",9,"Executer::New()"<<std::endl);
00057 return MakePointer(new Executer());
00058 }
00059
00060
00061
00062 Executer::Executer()
00063 :
00064 mFactory(),
00065 mRootPackage(),
00066 mRootCBB(),
00067 mNoExecMode(false),
00068 mDialogMode(NoDialog),
00069 mNoErrorMode(false)
00070 {
00071 bbtkDebugMessage("object",2,"==> Executer()" <<std::endl);
00072 mFactory = Factory::New();
00073
00074
00075
00076
00077
00078 mFactory->SetExecuter(MakePointer(this,true));
00079 Reset();
00080 bbtkDebugMessage("object",2,"<== Executer()" <<std::endl);
00081 }
00082
00083
00084
00085 Executer::~Executer()
00086 {
00087 bbtkDebugMessage("object",2,"==> ~Executer()" <<std::endl);
00088 mOpenDefinition.clear();
00089 mOpenPackage.clear();
00090 mFactory->Reset();
00091 mFactory.reset();
00092 bbtkDebugMessage("object",2,"<== ~Executer()" <<std::endl);
00093 }
00094
00095
00096
00098 void Executer::LoadPackage(const std::string &name )
00099 {
00100 GetFactory()->LoadPackage(name);
00101 }
00102
00103
00104
00106 void Executer::UnLoadPackage(const std::string &name )
00107 {
00108 GetFactory()->UnLoadPackage(name);
00109 }
00110
00111
00112
00113 void Executer::Reset()
00114 {
00115 bbtkDebugMessage("kernel",9,"==> Executer::Reset()" <<std::endl);
00116
00117
00118
00119 mOpenDefinition.clear();
00120 mOpenPackage.clear();
00121
00122
00123
00124 GetFactory()->Reset();
00125 #if(USE_WXWIDGETS)
00126 Wx::ProcessPendingEvents();
00127 #endif
00128
00129
00130 Package::Pointer p =
00131 Package::New("user","internal","User defined black boxes","");
00132
00133 GetFactory()->InsertPackage(p);
00134
00135 mOpenPackage.push_back(p);
00136 mRootPackage = p;
00137
00138
00139 ComplexBlackBoxDescriptor::Pointer r =
00140 ComplexBlackBoxDescriptor::New("workspace");
00141
00142 r->SetFactory(GetFactory());
00143 r->AddToAuthor("bbtk");
00144 r->AddToDescription("User's workspace");
00145 mOpenDefinition.push_back(CBBDefinition(r,"user"));
00146
00147 p->Register(r);
00148 mRootCBB = r;
00149
00150
00151
00152 bbtkDebugMessage("kernel",9,"<== Executer::Reset()" <<std::endl);
00153 }
00154
00155
00156
00157
00159 void Executer::SetWorkspaceName( const std::string& n )
00160 {
00161 GetUserPackage()->ChangeDescriptorName( GetWorkspace()->GetTypeName(), n );
00162 }
00163
00164
00165
00166 void Executer::BeginPackage (const std::string &name)
00167 {
00168 bbtkDebugMessage("kernel",9,"==> Executer::BeginPackage(\""<<name<<"\")"
00169 <<std::endl);
00170 Package::Pointer p;
00171 try
00172 {
00173 p = GetFactory()->GetPackage(name);
00174 }
00175 catch (Exception e)
00176 {
00177 p = Package::New(name,"","","");
00178 GetFactory()->InsertPackage(p);
00179 }
00180 mOpenPackage.push_back(p);
00181
00182 bbtkDebugMessage("kernel",9,"<== Executer::BeginPackage(\""<<name<<"\")"
00183 <<std::endl);
00184 }
00185
00186
00187
00188 void Executer::EndPackage()
00189 {
00190 if (mOpenPackage.size()>1) mOpenPackage.pop_back();
00191 }
00192
00193
00194
00195 void Executer::Define (const std::string &name,
00196 const std::string &pack,
00197 const std::string &scriptfilename)
00198 {
00199 bbtkDebugMessage("kernel",9,"==> Executer::Define(\""<<name<<
00200 ","<<pack<<"\")"
00201 <<std::endl);
00202
00203 ComplexBlackBoxDescriptor::Pointer b
00204 = ComplexBlackBoxDescriptor::New(name);
00205 b->SetFactory(GetFactory());
00206 b->SetScriptFileName(scriptfilename);
00207 mOpenDefinition.push_back( CBBDefinition( b, pack ) );
00208
00209 bbtkDebugMessage("kernel",9,"<== Executer::Define(\""<<name<<
00210 ","<<pack<<"\")"
00211 <<std::endl);
00212 }
00213
00214
00215
00218 void Executer::SetCurrentFileName (const std::string &name )
00219 {
00220 mOpenDefinition.back().box->SetScriptFileName(name);
00221 }
00222
00223
00224
00225 void Executer::Clear()
00226 {
00227 bbtkDebugMessage("kernel",9,"==> Executer::Clear()" <<std::endl);
00228 GetCurrentDescriptor()->GetPrototype()->Clear();
00229 bbtkDebugMessage("kernel",9,"<== Executer::Clear()" <<std::endl);
00230 }
00231
00232
00233
00234 void Executer::EndDefine ()
00235 {
00236 bbtkDebugMessage("kernel",9,"==> Executer::EndDefine(\""
00237 <<GetCurrentDescriptor()->GetTypeName()<<"\")"
00238 <<std::endl);
00239
00240 Package::Pointer p;
00241 std::string pname(mOpenDefinition.back().package);
00242 if (pname.size()>0)
00243 {
00244 try
00245 {
00246 p = GetFactory()->GetPackage(pname);
00247 }
00248 catch (Exception e)
00249 {
00250 p = Package::New(pname,"","","");
00251 GetFactory()->InsertPackage(p);
00252 }
00253 }
00254 else
00255 {
00256 p = mOpenPackage.back().lock();
00257 }
00258
00259 p->Register(GetCurrentDescriptor());
00260
00261 bbtkDebugMessage("kernel",9,"<== Executer::EndDefine(\""
00262 <<GetCurrentDescriptor()->GetTypeName()<<"\")"
00263 <<std::endl);
00264 mOpenDefinition.pop_back();
00265 }
00266
00267
00268
00269 void Executer::Kind(const std::string& kind)
00270 {
00271 if (kind=="ADAPTOR")
00272 {
00273 GetCurrentDescriptor()->AddToCategory("adaptor");
00274 GetCurrentDescriptor()->SetKind(bbtk::BlackBoxDescriptor::ADAPTOR);
00275 }
00276 else if (kind=="DEFAULT_ADAPTOR")
00277 {
00278 GetCurrentDescriptor()->AddToCategory("adaptor");
00279 GetCurrentDescriptor()->SetKind(bbtk::BlackBoxDescriptor::DEFAULT_ADAPTOR);
00280 }
00281 if (kind=="GUI")
00282 {
00283 GetCurrentDescriptor()->AddToCategory("gui");
00284 GetCurrentDescriptor()->SetKind(bbtk::BlackBoxDescriptor::GUI);
00285 }
00286 else if (kind=="DEFAULT_GUI")
00287 {
00288 GetCurrentDescriptor()->AddToCategory("gui");
00289 GetCurrentDescriptor()->SetKind(bbtk::BlackBoxDescriptor::DEFAULT_GUI);
00290 }
00291 else
00292 {
00293 bbtkError("Unknown box kind : '"<<kind<<"'. "
00294 <<"Valid kinds are 'ADAPTOR','DEFAULT_ADAPTOR',"
00295 <<"'GUI','DEFAULT_GUI'");
00296 }
00297 }
00298
00299
00300
00301 void Executer::Create ( const std::string& nodeType,
00302 const std::string& nodeName)
00303 {
00304 GetCurrentDescriptor()->Add(nodeType,nodeName);
00305 }
00306
00307
00308
00309 void Executer::Destroy(const std::string &boxName)
00310 {
00311 GetCurrentDescriptor()->Remove(boxName,true);
00312 }
00313
00314
00315
00316 void Executer::Connect (const std::string &nodeFrom,
00317 const std::string &outputLabel,
00318 const std::string &nodeTo,
00319 const std::string &inputLabel)
00320 {
00321 GetCurrentDescriptor()->Connect(nodeFrom, outputLabel, nodeTo, inputLabel);
00322 }
00323
00324
00325
00326 void Executer::Execute (const std::string &nodeName)
00327 {
00328
00329 if (GetCurrentDescriptor()==GetWorkspace())
00330 {
00331 if (!mNoExecMode)
00332 {
00333 GetCurrentDescriptor()->GetPrototype()->bbGetBlackBox(nodeName)->bbExecute(true);
00334 }
00335 }
00336 else
00337 {
00338 GetCurrentDescriptor()->AddToExecutionList(nodeName) ;
00339 }
00340 }
00341
00342
00343
00344 void Executer::DefineInput ( const std::string &name,
00345 const std::string &box,
00346 const std::string &input,
00347 const std::string& help)
00348 {
00349
00350 if (GetCurrentDescriptor()==GetWorkspace())
00351 {
00352
00353
00354
00355
00356 if (mDialogMode == NoDialog)
00357 {
00358
00359 std::map<std::string,std::string>::iterator i;
00360 i = mInputs.find(name);
00361 if (i!=mInputs.end()) {
00362 Set(box,input,(*i).second);
00363 }
00364 }
00365
00366
00367 else if (mDialogMode == TextDialog)
00368 {
00369 std::cout << name << "=";
00370 std::string ans;
00371 std::cin >> ans;
00372 Set(box,input,ans);
00373 }
00374 #ifdef USE_WXWIDGETS
00375
00376
00377 else if (mDialogMode == GraphicalDialog)
00378 {
00379 std::string mess("Enter the value of '");
00380 mess += name;
00381 mess += "' (";
00382 mess += help;
00383 mess += ")";
00384 std::string title(name);
00385 title += " ?";
00386 std::string ans = wx2std ( wxGetTextFromUser( std2wx (mess), std2wx(title)));
00387 Set(box,input,ans);
00388 }
00389 #endif
00390 }
00391
00392 GetCurrentDescriptor()->DefineInput(name,box,input,help);
00393
00394 }
00395
00396
00397
00398 void Executer::DefineOutput ( const std::string &name,
00399 const std::string &box,
00400 const std::string &output,
00401 const std::string& help)
00402 {
00403 GetCurrentDescriptor()->DefineOutput(name,box,output,help);
00404 }
00405
00406
00407
00408 void Executer::Set (const std::string &box,
00409 const std::string &input,
00410 const std::string &value)
00411 {
00412 BlackBox::Pointer b = GetCurrentDescriptor()->GetPrototype()->bbGetBlackBox(box);
00413
00414
00415 if ( ( b->bbGetInputType(input) != typeid(bbtk::any<bbtk::thing>) )&&
00416 ( b->bbGetInputType(input) != typeid(std::string) ) )
00417 {
00418 BlackBox::Pointer a =
00419 GetFactory()->NewAdaptor(typeid(std::string),
00420 b->bbGetInputType(input),
00421 "tmp");
00422 if (!a)
00423 {
00424 bbtkError("No <"<<
00425 TypeName(b->bbGetInputType(input))
00426 <<"> to <std::string> found");
00427 }
00428 std::string v(value);
00429 a->bbSetInput("In",v);
00430 a->bbExecute();
00431 b->bbSetInput(input,a->bbGetOutput("Out"));
00432
00433 }
00434 else
00435 {
00436 std::string v(value);
00437 b->bbSetInput(input,v);
00438 }
00439 }
00440
00441
00442
00443 std::string Executer::Get(const std::string &box,
00444 const std::string &output)
00445 {
00446 BlackBox::Pointer b = GetCurrentDescriptor()->GetPrototype()->bbGetBlackBox(box);
00447
00448 if (b->bbGetOutputType(output) != typeid(std::string))
00449 {
00450 BlackBox::Pointer a =
00451 GetFactory()->NewAdaptor(
00452 b->bbGetOutputType(output),
00453 typeid(std::string),
00454 "tmp");
00455 if (!a)
00456 {
00457 bbtkError("No <"<<
00458 TypeName(b->bbGetOutputType(output))
00459 <<"> to <std::string> found");
00460 }
00461 b->bbExecute();
00462
00463 a->bbSetInput("In",b->bbGetOutput(output));
00464 a->bbExecute();
00465 std::string r = a->bbGetOutput("Out").unsafe_get<std::string>();
00466
00467
00468
00469
00470
00471
00472 return r;
00473 }
00474 else
00475 {
00476 b->bbExecute();
00477 return b->bbGetOutput(output).unsafe_get<std::string>();
00478
00479
00480
00481
00482 }
00483 }
00484
00485
00486
00487 void Executer::Author(const std::string &authorName)
00488 {
00489 GetCurrentDescriptor()->AddToAuthor(authorName,GetCurrentDescriptor()==GetWorkspace());
00490 }
00491
00492
00493
00494 void Executer::Category(const std::string &category)
00495 {
00496 GetCurrentDescriptor()->AddToCategory(category,GetCurrentDescriptor()==GetWorkspace());
00497 }
00498
00499
00500
00501 void Executer::Description(const std::string &d)
00502 {
00503 GetCurrentDescriptor()->AddToDescription(d,GetCurrentDescriptor()==GetWorkspace());
00504 }
00505
00506
00507
00508
00509
00511 void Executer::PrintHelpListBoxes()
00512 {
00513 bbtkMessage("help",1,"The black box descriptor \""
00514 <<GetCurrentDescriptor()->GetTypeName()<<"\" contains : "<<std::endl);
00515 GetCurrentDescriptor()->PrintBlackBoxes();
00516 }
00517
00518
00519
00520 std::string Executer::ShowGraph(const std::string &nameblackbox,
00521 const std::string &detailStr,
00522 const std::string &levelStr,
00523 const std::string &output_html,
00524 const std::string &custom_header,
00525 const std::string &custom_title,
00526 bool system_display )
00527 {
00528 int detail = atoi(detailStr.c_str());
00529 int level = atoi(levelStr.c_str());
00530 bool relative_link = true;
00531
00532 Package::Pointer p;
00533 try
00534 {
00535 p = GetFactory()->GetPackage(nameblackbox);
00536 }
00537 catch (Exception e)
00538 {
00539 p = GetUserPackage();
00540 }
00541
00542 std::string doc_path = bbtk::ConfigurationFile::GetInstance().Get_doc_path();
00543 doc_path += bbtk::ConfigurationFile::GetInstance().Get_file_separator();
00544 doc_path += "bbdoc";
00545 doc_path += bbtk::ConfigurationFile::GetInstance().Get_file_separator();
00546
00547 std::string pack_name(p->GetName());
00548 std::string pack_path = doc_path + pack_name;
00549
00550 if ( ! bbtk::Utilities::FileExists(pack_path) )
00551 {
00552 std::string command("mkdir \"" +pack_path+ "\"");
00553 system( command.c_str() );
00554 }
00555 std::string pack_index(pack_path);
00556 pack_index += bbtk::ConfigurationFile::GetInstance().Get_file_separator();
00557 pack_index += "index.html";
00558
00559
00560
00561 p->SetDocURL(pack_index);
00562 p->SetDocRelativeURL("index.html");
00563 p->CreateHtmlPage(pack_index,"bbtk","user package",custom_header,custom_title,detail,level,relative_link);
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576 return pack_index;
00577 }
00578
00579
00580
00582 std::string Executer::ShowGraphInstances(const std::string &nameblackbox, int detail, int level,
00583 bool system_display)
00584 {
00585
00586 BlackBox::Pointer blackbox;
00587 if (nameblackbox==".")
00588 {
00589 blackbox = GetCurrentDescriptor()->GetPrototype();
00590 }
00591 else
00592 {
00593 blackbox = GetCurrentDescriptor()->GetPrototype()->bbFindBlackBox(nameblackbox);
00594 }
00595
00596 std::string page;
00597
00598 if (blackbox)
00599 {
00600
00601 std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
00602 char c = default_doc_dir.c_str()[strlen(default_doc_dir.c_str())-1];
00603
00604 std::string directory = default_doc_dir;
00605 if (c != '/' && c !='\\') directory = directory + "/";
00606
00607 directory = directory + "temp_dir";
00608
00609 std::string filename(directory + "/" + "bbtk_graph_pipeline");
00610 std::string filename_html(filename+".html");
00611 std::string command0("mkdir \""+directory + "\"");
00612
00613 #if defined(_WIN32)
00614 std::string command2("start ");
00615 #else
00616 std::string command2("gnome-open ");
00617 #endif
00618
00619 command2=command2+filename_html;
00620 page = filename_html;
00621
00622 std::ofstream s;
00623 s.open(filename_html.c_str());
00624 if (s.good())
00625 {
00626 s << "<html><head><title>BBtk graph diagram</title><meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\"></head>\n";
00627 s << "<body bgcolor=\"#FFFFFF\" text=\"#000000\"> \n\n";
00628 if ( blackbox->bbGetName()=="workspacePrototype" )
00629 {
00630 s << "<center>Current workspace</center>";
00631 } else {
00632 s << "<center>" << blackbox->bbGetName()<< "</center>";
00633 }
00634 blackbox->bbInsertHTMLGraph( s, detail, level, true, directory, false );
00635 s << "</body></html>\n";
00636 }
00637 s.close();
00638
00639
00640 if (system_display) system( command2.c_str() );
00641 }
00642 else
00643 {
00644 bbtkMessage("help",1,"No black box: \""
00645 <<nameblackbox<<"\" " <<std::endl);
00646 }
00647 return page;
00648 }
00649
00650
00651
00652 void Executer::PrintHelpBlackBox(const std::string &nameblackbox,
00653 const std::string &detailStr,
00654 const std::string &levelStr)
00655 {
00656 bool found=false;
00657
00658 int detail = atoi(detailStr.c_str());
00659 int level = atoi(levelStr.c_str());
00660 BlackBox::Pointer blackbox;
00661 if (nameblackbox.compare(".")==0)
00662 {
00663 blackbox=GetCurrentDescriptor()->GetPrototype();
00664 }
00665 else
00666 {
00667 blackbox = GetCurrentDescriptor()->GetPrototype()->bbFindBlackBox(nameblackbox);
00668 }
00669
00670 if (blackbox)
00671 {
00672 found=true;
00673 blackbox->bbPrintHelp(blackbox,detail,level);
00674 }
00675
00676 if (!found)
00677 {
00678 bbtkError("box with name '" <<nameblackbox<<"' unknown");
00679 }
00680 }
00681
00682
00683
00685 void Executer::SetMessageLevel(const std::string &kind,
00686 int level)
00687 {
00688 bbtk::MessageManager::SetMessageLevel(kind,level);
00689 }
00690
00691
00692
00694 void Executer::HelpMessages()
00695 {
00696 bbtk::MessageManager::PrintInfo();
00697 }
00698
00699
00700
00702 void Executer::Print(const std::string &str)
00703 {
00704 if (GetNoExecMode() && (GetCurrentDescriptor()==GetWorkspace()) ) return;
00705 if (GetCurrentDescriptor()!=GetWorkspace()) return;
00706
00707 bbtkDebugMessage("kernel",9,"Executer::Print(\""<<str<<"\")"<<std::endl);
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717 std::vector<std::string> chains;
00718 std::string delimiters("$");
00719
00720
00721 std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
00722 bool is_text = true;
00723 if (lastPos>0) is_text = false;
00724
00725
00726 std::string::size_type pos = str.find_first_of(delimiters, lastPos);
00727
00728 while (std::string::npos != pos || std::string::npos != lastPos)
00729 {
00730 if (is_text)
00731 {
00732
00733 chains.push_back(str.substr(lastPos, pos - lastPos));
00734
00735
00736
00737 }
00738 else
00739 {
00740
00741
00742 std::string tok,box,output;
00743 tok = str.substr(lastPos, pos - lastPos);
00744 Utilities::SplitAroundFirstDot(tok,box,output);
00745 chains.push_back( Get(box,output) );
00746
00747
00748
00749 }
00750
00751 lastPos = str.find_first_not_of(delimiters, pos);
00752
00753 pos = str.find_first_of(delimiters, lastPos);
00754
00755 is_text = !is_text;
00756
00757 }
00758
00759
00760
00761 std::vector<std::string>::iterator i;
00762 for (i= chains.begin(); i!=chains.end(); ++i)
00763 {
00764 Utilities::SubsBackslashN(*i);
00765 bbtkMessage("output",1,*i);
00766 }
00767 bbtkMessage("output",1,std::endl);
00768 }
00769
00770
00771
00772 std::string Executer::GetObjectName() const
00773 {
00774 return std::string("Executer");
00775 }
00776
00777
00778
00779 std::string Executer::GetObjectInfo() const
00780 {
00781 std::stringstream i;
00782 return i.str();
00783 }
00784
00785
00786 size_t Executer::GetObjectSize() const
00787 {
00788 size_t s = Superclass::GetObjectSize();
00789 s += Executer::GetObjectInternalSize();
00790 return s;
00791 }
00792
00793
00794 size_t Executer::GetObjectInternalSize() const
00795 {
00796 size_t s = sizeof(Executer);
00797 return s;
00798 }
00799
00800
00801 size_t Executer::GetObjectRecursiveSize() const
00802 {
00803 size_t s = Superclass::GetObjectRecursiveSize();
00804 s += Executer::GetObjectInternalSize();
00805 s += mFactory->GetObjectRecursiveSize();
00806 return s;
00807 }
00808
00809 }