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